<template>
    <div class="attention-bus-wrap" id="attention-bus-wrap">
      <!--<div class="product-switch-item">-->
        <!--<span :class="activeType==='attention'?'mobile-switch-btn active':'mobile-switch-btn'" @click="ChangeTab('attention')">已关注</span>-->
        <!--<span :class="activeType==='isattention'?'mobile-switch-btn active':'mobile-switch-btn'" @click="ChangeTab('isattention')">未关注</span>-->
      <!--</div>-->
      <div class="search-content" style="padding-bottom: 0.25rem">
        <input type="text" placeholder="请输入您要搜索的物料名称" v-model="keyword" @keyup.13="searchSeek">
        <span @click="searchSeek" >
          <i class="iconfont icon-sousuo"></i>
          </span>
      </div>
      <div class="attention-bus-list" v-if="attentionList.length > 0">
        <ul>
          <li v-for="(item, index) in attentionList" v-bind:key="index">
            <div class="item">
              <div class="name">
                {{ item.nameCn | filterStr}}
              </div>
              <div class="attention-button" @click="attentionFn(item, index)">
                <i class="iconfont icon-shoucang1" :class="item.status ? 'icon-shoucang1' : 'icon-shoucang2'"></i>
                <p v-text="item.status === 1 ? '已关注' : '关注'">已关注</p>
              </div>
            </div>
          </li>
        </ul>
      </div>
      <div class="none-state" v-if="!isSearchSearchingMore && attentionList.length === 0">
        <img src="/images/mobile/@2x/search-empty.png">
        <p>暂无数据</p>
        <nuxt-link to="/">返回首页</nuxt-link>
      </div>
      <div class="fixedAlert" v-show="isShowAlert">
        关注物料名称，商机无限！
        <i class="iconfont icon-guanbi" @click="isShowAlert = false"></i>
      </div>
      <remind-box :title="collectResult" :timeoutCount="timeoutCount"></remind-box>
      <pull-up :fixId="'attention-bus-wrap'" :searchMore="isSearchSearchingMore" :allPage="allPage" :page="page" @pullUpAction="getMoreSearch"></pull-up>
    </div>
</template>
<script>
    import { RemindBox, PullUp } from '~components/mobile/common'
    export default {
      name: 'attention-Bus',
      layout: 'mobile',
      middleware: 'authenticated',
      fetch({store}) {
        return Promise.all([
          store.dispatch('product/getUserCollectCode', {keyword: '', count: 20, page: 1, type: 'yes', enUU: store.state.option.user.data.enterprise.uu})
        ])
      },
      data() {
        return {
          activeType: 'attention',
          keyword: '',
          collectResult: '',
          timeoutCount: 0,
          page: 1,
          attentionList: [],
          isShowAlert: true,
          isSearchSearchingMore: false
        }
      },
      watch: {
        'getkindConcernList.data': {
          handler: function (val) {
            if (this.isChange) {
              this.attentionList = val.content
              this.isChange = false
            } else {
              this.attentionList = [...this.attentionList, ...val.content]
            }
            this.isSearchSearchingMore = false
          },
          immediate: true
        }
      },
      computed: {
        allPage() {
          return Math.ceil(this.$store.state.product.component.collectCode.data.totalElements / 10)
        },
        getkindConcernList() {
          return this.$store.state.product.component.collectCode
        }
      },
      methods: {
        ChangeTab(a) {
          this.activeType = a
          this.attentionList = []
          this.page = 1
          this.keyword = ''
          this.getAttentionListArray(this.page)
        },
        searchSeek() {
          this.page = 0
          this.attentionList = []
          this.getMoreSearch()
        },
        attentionFn(kindConcern, index) {
          let param = {
            date: kindConcern.date,
            enUU: kindConcern.enUU,
            nameCn: kindConcern.nameCn,
            nameEn: kindConcern.nameEn,
            status: kindConcern.status
          }
          if (kindConcern.status === 1) {
            param.id = kindConcern.id
            this.$http.post('/produce/kindConcern/modify', param).then(res => {
              this.collectResult = '取消成功'
              this.timeoutCount++
              // kindConcern.status = 0
              this.$store.dispatch('product/UpdateCollectCode', { key: index, status: 0 })
              // this.attentionList.splice(index, 1)
              // if (this.attentionList.length <= 15 && this.page < this.allPage) {
              //   this.page++
              //   this.getAttentionListArray(this.page)
              // }
            })
          } else {
            this.$http.post('/produce/kindConcern/modify', param).then(res => {
              this.collectResult = '关注成功'
              this.timeoutCount++
              this.$store.dispatch('product/UpdateCollectCode', {key: index, status: 1})
              // this.attentionList.splice(index, 1)
              // if (this.attentionList.length <= 15 && this.page < this.allPage) {
              //   this.page++
              //   this.getAttentionListArray(this.page)
              // }
            })
          }
        },
        getAttentionListArray(page) {
          this.isSearchSearchingMore = true
          if (this.activeType === 'attention') {
            this.$store.dispatch('product/getUserCollectCode', {keyword: this.keyword, count: 20, page: page, enUU: this.$store.state.option.user.data.enterprise.uu})
          } else {
            this.$store.dispatch('product/getUserCollectCode', {keyword: this.keyword, count: 20, page: page, enUU: this.$store.state.option.user.data.enterprise.uu})
          }
        },
        getMoreSearch() {
          this.page++
          this.getAttentionListArray(this.page)
        }
      },
      components: {
        RemindBox,
        PullUp
      },
      filters: {
        filterStr(val) {
          if (val.length > 30) {
            return val.substr(0, 30) + '...'
          }
          return val
        }
      }
    }
</script>
<style scoped lang='scss'>
  .attention-bus-wrap {
    background: #f1f3f6;
    margin: 1.26rem 0 0.98rem 0;
    height: calc(100vh - 1.26rem - 0.98rem);
    overflow-y: auto;
    padding-bottom: 0.2rem;
    .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;
        }
      }
    }
    .search-content {
      text-align: center;
      padding: .25rem 0 0 0;
      input {
        width: 7.1rem;
        border: 1px solid #376ff3;
      }
      span {
        /*height: .46rem;*/
        /*line-height: .46rem;*/
      }
    }
    .attention-bus-list {
      border: 0.01rem solid #e3e5e8;
      border-radius: 4px;
      background: #fff;
      width: 7.1rem;
      margin: 0 auto;
      ul {
        li {
          display: table;
          border-bottom: 1px solid #d3d3d3;
          margin: 0 0.24rem;
          height: 1.1rem;
          &:nth-last-of-type(1) {
            border-bottom: 0;
          }
          div {
            display: table-cell;
            vertical-align: middle;
            position: relative;
            word-wrap: break-word;
            &.name {
              font-size: 0.28rem;
              color: #666;
              width: 5.48rem;
              overflow : hidden;
            }
            &.attention-button {
              width: 1.2rem;
              font-size: 0.24rem;
              color: #666;
              text-align: center;
              i{
                color: #008bf7;
                font-size: 0.5rem;
              }
              p{
                font-size: .26rem;
                color: #008bf7;
              }
            }
          }
        }
      }
    }
    .fixedAlert {
      position: fixed;
      z-index: 11;
      width: 4.91rem;
      height: 0.7rem;
      line-height:0.7rem;
      left: 50%;
      margin-left: -2.455rem;
      background: rgba(82, 147, 255, 0.9);
      border-radius: 0.7rem;
      font-size: 0.32rem;
      color: #fff;
      text-align: center;
      top: 2.6rem;
      i {
        font-size: 0.4rem;
        position: absolute;
        right: -0.15rem;
        top: -0.28rem;
        color: rgba(61, 61, 61, 0.76);
        &:after {
          position: absolute;
          top: -0.1rem;
          right: -0.1rem;
          left: -0.1rem;
          bottom: -0.1rem;
          content: ' ';
        }
      }
    }
    .none-state{
      text-align: center;
      padding:1.5rem 0;
      background: #fff;
      margin-top:.1rem;
      width:100%;
      img{
        margin:0 auto;
        width: 4.08rem;
        height: 2.62rem;
      }
      p {
        font-size: .32rem;
        color: #999;
        margin: 1.19rem 0 0 0;
      }
      a {
        display: block;
        font-size: .28rem;
        color: #fff;
        width: 1.88rem;
        height: .54rem;
        line-height: .54rem;
        background: #418bf6;
        margin: .7rem auto 0;
        border-radius: .05rem;
      }
    }
  }
</style>
