<template>
  <div class="apply_purchase_mobile" @click="showSimilarBrandList = false">
    <div class="com-mobile-header mobile-center-header">
      <a @click.stop="goLastPage"><i class="iconfont icon-fanhui"></i></a>
      <p>发布求购</p>
      <p class="en-name"><img :src="`/images/mobile/center/${user.data.enterprise.uu ? 'en' : 'self'}.png`" alt="">{{currentEnName}}</p>
    </div>
    <div class="mobile-fix-content mobile-centerfix-content" id="mobileFixContent">
      <div class="search-content search-content2" style="position: relative">
        <input type="text" placeholder="请输入您要询价求购的型号" @input="onBrandChange" v-model="keyword" @keyup.13="searchComplist">
        <span @click.stop="searchComplist" >
          <i class="iconfont icon-sousuo"></i>
        </span>
        <ul class="similar brand-similar-list" v-show="showSimilarBrandList && keyword">
          <li v-for="sBrand in similarBrand" @click.stop="setBrand(sBrand.code)">{{sBrand.code}}</li>
        </ul>
      </div>
      <publish-seek v-if="showPublishBox" :dataObj="componentSeekObj" @remindAction="onRemind" :isNumberReg="true"></publish-seek>
      <template v-else>
        <ul v-if="compList && compList.length">
          <li v-for="comp in compList">
            <div class="listInfo">
              <p>品牌：<span v-text="comp.brand || '-'"></span></p>
              <p>物料名称：<span v-text="comp.kind || '-'"></span></p>
              <p>型号：<span v-text="comp.cmpCode || '-'"></span></p>
              <p>规格：<span v-text="comp.spec || '-'"></span></p>
              <p>单位：<span v-text="comp.unit || 'PCS'"></span></p>
            </div>
            <a class="sa-pub" @click.stop="compInquiry(comp, $event)">立即询价</a>
          </li>
        </ul>
        <empty-status :type="'collect'" :showLink="true" :text="'抱歉，没有相关信息'" v-else></empty-status>
      </template>
    </div>
    <remind-box :title="remindText" :timeoutCount="timeoutCount"></remind-box>
    <pull-up :fixId="'mobileFixContent'" :searchMore="fetching" :allPage="allPage" :page="page" @pullUpAction="onPullUpAction"></pull-up>
  </div>
</template>
<script>
  import { RemindBox, PullUp, EmptyStatus } from '~components/mobile/common'
  import {PublishSeek} from '~components/mobile/applyPurchase'
  export default {
    middleware: 'authenticated',
    layout: 'mobileNoHeader',
    fetch ({ store }) {
      return Promise.all([
        store.dispatch('product/loadPublishList', { count: 10, page: 1, keyword: null })
      ])
    },
    data () {
      return {
        keyword: '',
        remindText: '',
        timeoutCount: 0,
        page: 1,
        count: 10,
        isChange: false,
        compList: [],
        componentSeekObj: {
          code: '',
          brand: '',
          spec: '',
          prodTitle: ''
        },
        showPublishBox: true,
        showSimilarBrandList: false,
        similarBrand: []
      }
    },
    watch: {
      'compCollectList.data': {
        handler: function (val) {
          if (this.isChange) {
            this.compList = val.content
            this.isChange = false
          } else {
            this.compList = [...this.compList, ...val.content]
          }
        },
        immediate: true
      }
    },
    computed: {
      compCollectList () {
        return this.$store.state.product.common.publishMobile
      },
      fetching () {
        return this.compCollectList.fetching
      },
      allPage () {
        return Math.floor(this.compCollectList.data.totalElements / this.compCollectList.data.size) + Math.floor(this.compCollectList.data.totalElements % this.compCollectList.data.size > 0 ? 1 : 0)
      }
    },
    components: {
      RemindBox,
      PullUp,
      EmptyStatus,
      PublishSeek
    },
    methods: {
      onBrandChange: function () {
        this.keyword = this.keyword.trim()
        if ((/[^\x00-\xff]/g).test(this.keyword)) {
          let chineseIndex = -1
          for (let i = 0; i < this.keyword.length; i++) {
            if ((/[^\x00-\xff]/g).test(this.keyword.charAt(i)) && !(/[\u4e00-\u9fa5]/).test(this.keyword.charAt(i))) {
              chineseIndex = i
              break
            }
          }
          if (chineseIndex > -1) {
            this.keyword = this.keyword.substring(0, chineseIndex)
          }
        } else if (this.keyword && this.baseUtils.getRealLen(this.keyword) > 50) {
          this.keyword = this.baseUtils.cutOutString(this.keyword, 50)
        } else {
          this.getSimilarBrand()
        }
      },
      getSimilarBrand: function () {
        if (this.keyword) {
          this.$http.get('/search/similarKeywords', {params: {keyword: this.keyword, type: 'component'}})
            .then(response => {
              this.similarBrand = response.data.result
              this.showSimilarBrandList = response.data.result.length > 0
            })
        } else {
          this.showSimilarBrandList = false
        }
      },
      setBrand: function (brand) {
        this.keyword = brand
        this.showSimilarBrandList = false
        this.searchComplist()
      },
      searchComplist () {
        if (this.keyword !== '') {
          this.showSimilarBrandList = false
          this.reloadList()
          this.showPublishBox = false
          this.page = 1
          this.isChange = true
        } else {
          this.remindText = '请输入您要询价求购型号'
          this.timeoutCount++
        }
      },
      onRemind: function (str) {
        this.remindText = str
        this.timeoutCount++
      },
      reloadList: function () {
        this.$store.dispatch('product/loadPublishList', { page: this.page, count: this.count, keyword: this.keyword })
      },
      onPullUpAction: function () {
        this.page++
        this.reloadList()
      },
      compInquiry: function (item, e) {
        if (e) {
          e.stopPropagation()
        }
        this.componentSeekObj.code = item.cmpCode ? item.cmpCode : ''
        this.componentSeekObj.brand = item.brand ? item.brand : ''
        this.componentSeekObj.spec = item.spec ? item.spec : ''
        this.componentSeekObj.prodTitle = item.kind ? item.kind : ''
        this.componentSeekObj = JSON.parse(JSON.stringify(this.componentSeekObj))
        this.showPublishBox = true
      },
      goUrl: function (url) {
        this.$router.push(url)
      }
    }
  }
</script>
<style lang="scss" scoped>
  .mobile-fix-content {
    .similar {
      position: absolute;
      width: 6.5rem;
      max-height: 2.5rem;
      overflow-y: auto;
      z-index: 12;
      border: 1px solid #7e7e7e;
      border-radius: .05rem;
      top: .7rem;
      background: #fff;
      left: 0.1rem;
      right: 0;
      margin: 0 auto;
      li {
        height: .5rem;
        line-height: .5rem;
        font-size: .26rem;
        color: #999;
        padding-left: .19rem;
        text-align: left;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        &:focus, &:active, &:hover {
          background: #999;
          color: #fff;
        }
      }
    }
    .search-content {
      text-align: center;
      input {
        border: 1px solid #376ff3;
      }
      span{
        top:0;
      }
    }
    ul {
      margin: .2rem;
      li {
        border-radius: .05rem;
        margin: .25rem 0 0 0;
        position: relative;
        background: #fff;
        .listInfo {
          padding: .25rem .1rem .25rem .23rem;
          p {
            color: #666;
            font-size: .3rem;
            line-height: .45rem;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            span {
              color: #333;
            }
          }
        }
        .sa-pub {
          position: absolute;
          bottom: .2rem;
          right:0;
          display: block;
          width: 1.7rem;
          height: .47rem;
          line-height: .47rem;
          text-align: center;
          font-size: .26rem;
          color: #fff;
          background: #008bf7;
          margin-right:.2rem;
          border-radius: .05rem;
        }
      }
    }
  }
</style>
