<template>
  <div class="seek">
    <div class="com-mobile-header mobile-center-header">
      <a @click="goLastPage"><i class="iconfont icon-fanhui"></i></a>
      <p>{{seekTitle}}</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="seek-title com-switch-item" v-if="userType == 'saler' && seekType == 'wait'">
        <span class="mobile-switch-btn" :class="{'active': activeType=='all'}" @click="switchActiveType('all')">公司商机</span>
        <span class="mobile-switch-btn" :class="{'active': activeType=='self'}" @click="switchActiveType('self')">我的商机</span>
      </div>
      <div class="product-switch-item" v-if="userType == 'buyer' && seekType !== 'done'">
        <span :class="overdue==='attention'?'mobile-switch-btn active':'mobile-switch-btn'" @click="ChangeTab('attention')">未截止</span>
        <span :class="overdue==='isattention'?'mobile-switch-btn active':'mobile-switch-btn'" @click="ChangeTab('isattention')">已截止</span>
      </div>
      <div class="seek-title com-switch-item" v-if="userType == 'saler' && seekType == 'done'">
        <span class="mobile-switch-btn" :class="{'active': activeType=='all'}" @click="switchActiveType('all')">公司报价</span>
        <span class="mobile-switch-btn" :class="{'active': activeType=='self'}" @click="switchActiveType('self')">我的报价</span>
      </div>
      <div class="search-content">
        <input type="text" v-model="seekKeyword" :placeholder="userType == 'buyer' ? '品牌/型号' : '品牌/物料名称/型号/规格/公司'" @keyup.13="onSearch">
        <span @click="onSearch"><i class="iconfont icon-sousuo"></i></span>
      </div>
      <seek-list :keyword="remindKeyword" :isSearch="isSearch" :userType="userType" :seekType="seekType" :purchaseManList="purchaseManListData"></seek-list>
      <pull-up :fixId="'mobileFixContent'" :searchMore="fetching" :allPage="allPage" :page="page" @pullUpAction="onPullUpAction"></pull-up>
    </div>
  </div>
</template>
<script>
  import SeekList from '~components/mobile/applyPurchase/SeekList.vue'
  import { PullUp, EmptyStatus } from '~components/mobile/common'
  export default {
    layout: 'mobileNoHeader',
    middleware: 'authenticated',
    data () {
      return {
        seekKeyword: '',
        purchaseManListData: [],
        page: 1,
        count: 10,
        isChange: false,
        isSearch: false,
        remindKeyword: '',
        activeType: 'all',
        overdue: 'attention'
      }
    },
    props: ['userType'],
    components: {
      SeekList,
      PullUp,
      EmptyStatus
    },
    watch: {
      'purchase.data': {
        handler: function (val) {
          let list = this.baseUtils.deepCopy(val.content || [])
          if (this.seekType === 'done' && this.userType === 'saler') {
            list.map(val => {
              val.quotation = {
                replies: val.replies,
                vendName: val.vendName,
                leadtime: val.leadtime,
                user: val.user
              }
            })
          }
          if (this.isChange) {
            this.purchaseManListData = list
            this.isChange = false
          } else {
            this.purchaseManListData = this.purchaseManListData.concat(list)
          }
        },
        immediate: true
      }
    },
    computed: {
      seekType () {
        return this.$route.query.seekType
      },
      purchase () {
        return this.$store.state.applyPurchase.purchaseManList.purchaseManList
      },
      fetching () {
        return this.purchase.fetching
      },
      allPage () {
        return Math.floor(this.purchase.data.totalElements / this.purchase.data.size) + Math.floor(this.purchase.data.totalElements % this.purchase.data.size > 0 ? 1 : 0)
      },
      seekTitle () {
        if (this.seekType === 'wait') {
          if (this.userType === 'saler') {
            return '商机管理'
          } else {
            return '待报价'
          }
        } else if (this.seekType === 'done') {
          return '已报价'
        }
        return '已报价'
      }
    },
    mounted () {
//      获取链接
      this.$nextTick(() => {
        this.myActiveType()
      })
    },
    methods: {
      ChangeTab(a) {
        this.overdue = a
        this.page = 1
        this.remindKeyword = this.seekKeyword = ''
        this.isChange = true
        this.reloadData()
      },
      onSearch: function () {
        this.isSearch = true
        this.remindKeyword = this.seekKeyword
        this.page = 1
        this.isChange = true
        this.reloadData()
      },
      reloadData: function () {
        let overdue = this.overdue === 'attention' ? '0' : '1'
        this.$emit('reloadAction', this.page, this.count, this.seekKeyword, this.seekType, this.activeType, overdue)
      },
      onPullUpAction: function () {
        this.page++
        this.reloadData()
      },
      switchActiveType: function (type) {
        this.activeType = type
        this.isSearch = false
        this.remindKeyword = this.seekKeyword = ''
        this.page = 1
        this.isChange = true
        this.reloadData()
      },
      myActiveType: function () {
        if (this.$store.state.option.messageType) {
          this.activeType = 'self'
        } else {
          this.activeType = 'all'
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .search-content {
    text-align: center;
    padding: .25rem 0 0 0;
    input {
      border: 1px solid #376ff3;
      width: 7.2rem;
      margin: 0 0 0 -.02rem;
    }
    span {
      /*height: .46rem;*/
      /*line-height: .46rem;*/
    }
  }
  $seekTitleLine: .72rem;
  .product-switch-item {
    text-align: center;
    background: #fff;
    border-bottom: 1px solid #d8d8d8;
    box-shadow: 0 1px 3px #ddd;
    .mobile-switch-btn {
      color: #333;
      display: inline-block;
      height: .72rem;
      line-height: .72rem;
      font-size: .28rem;
      width: 1.4rem;
      &:first-child {
        margin-right: 1.78rem;
      }
      &.active {
        color: #3f84f6;
        border-bottom: .04rem solid #3f84f6;
      }
    }
  }
  .seek-title {
    height: $seekTitleLine;
    line-height: $seekTitleLine;
    .mobile-switch-btn {
     /* height: $seekTitleLine;
      line-height: $seekTitleLine;*/
      font-size: .28rem;
      /*&.active {
        border-bottom-width: .07rem;
      }*/
    }
  }
</style>
