<template>
  <h-view id="prolist" class="public-style" title="产品线">
    <h-header :proportion="[5, 1, 1]" class="bar-custom">
      <div slot="left" class="h-header-btn">
        <img src="@/assets/userBind/arrow.png" @click="$routeGo()" >
        <span>产品线</span>
      </div>
    </h-header>
    <!-- 搜索 -->
    <div class="search has-header">
      <input
        v-model="searchInput"
        type="text"
        placeholder="请输入经销商/产品名称/产品型号"
      >
    </div>
    <scroll
      ref="scroll"
      :updateData="lists"
      :pullUp="true"
      class="scroll-content"
      @pullingUp="loadMore"
    >
      <div class="division-box">
        <div
          v-for="(item, index) in lists"
          :key="index"
          class="item-pic"
          @click="goDetailed(item.division)"
        >
          <img :src="item.url" alt="" >
          <p>{{ item.product_line }}</p>
        </div>
      </div>
    </scroll>
    <div v-if="lists.length === 0">
      <div class="display">
        <img src="@/assets/messageCenter/noMsg.png" alt="" >
      </div>
    </div>
  </h-view>
</template>
<script>
import noImg from '../../assets/productQuery/none.png'
export default {
  name: 'ProductList',
  data () {
    return {
      lists: [],
      pagenum: 1,
      factory_bp_id: '',
      searchInput: '',
    }
  },
  computed: {},
  watch: {
    searchInput () {
      let vm = this
      if (vm.timeout) {
        clearTimeout(vm.timeout)
      }
      // vm.timeout = setTimeout(() => {
      vm.search()
      // }, 1000)
    },
  },
  beforeRouteEnter (to, from, next) {
    next((vm) => {
      if (from.name === 'QueryHome' || from.name === 'TabQueryHome') {
        vm.factory_bp_id = vm.$route.params.factory_bp_id
        vm.pagenum = 1
        vm.searchInput = ''
        vm.proQuery()
      } else if (from.name === 'ProDetailed') {
        vm.proQuery()
      }
    })
  },
  methods: {
    // 根据产品名称查询列表
    proQuery () {
      let vm = this
      vm.pagenum = 1
      let url = $config.basePath + 'prd_product_division_list'
      let param = {
        //  division: vm.division,
        factory_bp_id: vm.factory_bp_id,
        searchInput: vm.searchInput,
        pagenum: vm.pagenum,
        pagesize: 10,
      }
      vm.hlsPopup.showLoading('数据加载中')
      vm.$post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        if (res.result === 'S') {
          vm.lists = res.lists
          if (vm.lists.length === 0) {
            vm.$refs.scroll.update(true)
          } else if (vm.lists.length > 0 && vm.lists.length < 10) {
            vm.lists.forEach((item) => {
              item['url'] = item.division_attachment_id
                ? process.env.filePath +
                  'attachment_id=' +
                  item.division_attachment_id +
                  '&access_token=' +
                  window.localStorage.access_token
                : noImg
            })
            vm.$refs.scroll.update(true)
          } else if (vm.lists.length === 10) {
            vm.lists.forEach((item) => {
              item['url'] = item.division_attachment_id
                ? process.env.filePath +
                  'attachment_id=' +
                  item.division_attachment_id +
                  '&access_token=' +
                  window.localStorage.access_token
                : noImg
            })
            vm.$refs.scroll.update(false)
          }
        } else {
          hlsPopup.showLongCenter(res.message)
        }
      })
    },

    loadMore () {
      let vm = this
      vm.pagenum = vm.pagenum + 1
      let url = $config.basePath + 'prd_product_division_list'
      let param = {
        factory_bp_id: vm.factory_bp_id,
        searchInput: vm.searchInput,
        pagenum: vm.pagenum,
        pagesize: 10,
      }
      vm.hlsPopup.showLoading('数据加载中')
      vm.$post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        let returnData = []
        if (res.result === 'S') {
          returnData = res.lists
          if (returnData.length === 0) {
            vm.$refs.scroll.update(true)
          } else if (returnData.length > 0 && returnData.length < 10) {
            returnData.forEach((item) => {
              item['url'] = item.division_attachment_id
                ? process.env.filePath +
                  'attachment_id=' +
                  item.division_attachment_id +
                  '&access_token=' +
                  window.localStorage.access_token
                : noImg
            })
            returnData.forEach((data, index, array) => {
              vm.lists.push(array[index])
            })
            vm.$refs.scroll.update(true)
          } else if (returnData.length === 10) {
            returnData.forEach((item) => {
              item['url'] = item.division_attachment_id
                ? process.env.filePath +
                  'attachment_id=' +
                  item.division_attachment_id +
                  '&access_token=' +
                  window.localStorage.access_token
                : noImg
            })
            returnData.forEach((data, index, array) => {
              vm.lists.push(array[index])
            })
            vm.$refs.scroll.update(false)
          }
        } else {
          hlsPopup.showLongCenter(res.message)
        }
      })
    },

    // 搜索
    search () {
      let vm = this
      vm.pagenum = 1
      let num = Math.random() * 10
      // vm.bpList = []
      let url = $config.basePath + 'prd_product_division_list'
      let param = {
        pagesize: 10,
        pagenum: 1,
        searchInput: vm.searchInput,
        factory_bp_id: vm.factory_bp_id,
      }
      vm.hlsPopup.showLoading('数据加载中')
      vm.hlsHttp.post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        vm.lists = res.lists
        vm.lists.forEach((item) => {
          item['url'] = item.division_attachment_id
            ? process.env.filePath +
              'attachment_id=' +
              item.division_attachment_id +
              '&access_token=' +
              window.localStorage.access_token
            : noImg
        })
        if (vm.lists.length >= 0 && vm.lists.length < 10) {
          vm.$refs.scroll.update(true)
        } else if (vm.lists.length === 10) {
          vm.$refs.scroll.update(false)
        }
      })
    },
    // val：产品id, status: 收藏状态
    goDetailed (val) {
      this.$router.push({
        name: 'EntityList',
        params: {
          division: val,
        },
      })
    },
  },
}
</script>
<style lang='less' scoped>
#prolist {
  background: rgba(239, 239, 239, 0.55);
  .scroll-content {
    //top: 50px;
  }
  .display {
    position: fixed;
    top: 0;
    img {
      width: 100%;
      height: 100%;
      vertical-align: middle;
      height: calc(100% - 30px);
      // margin-top: -70px;
    }
  }
  .search {
    background-color: #fff;
    padding: 8px 12px;
    position: absolute;
    width: 100%;
    z-index: 100;
    margin-bottom: 8px;

    input {
      padding-left: 12px;
      height: 36px;
      width: 100%;
      font-family: PingFangSC-Regular;
      font-size: 14px;
      color: #21254c;
      line-height: 36px;
      border-radius: 4px;
      background: url("../../assets/contractStart/search1.png") 320px no-repeat;
      background-size: 16px 16px;
      background-color: rgba(239, 239, 239, 0.55);
    }

    input::placeholder {
      font-family: PingFangSC-Regular;
      font-size: 14px;
      color: #888c8f;
      letter-spacing: 0;
    }

    input:focus {
      background: url("../../assets/contractStart/search2.png") 320px no-repeat;
      background-size: 16px 16px;
      background-color: rgba(239, 239, 239, 0.55);
      border: 2px solid #bcc6ff;
    }
  }
  .division-box {
    display: flex;
    width: 100vw;
    flex-wrap: wrap;
    align-content: flex-start;
  }
  .item-pic {
    margin: 2vw;
    flex-basis: 46vw;
    width: 46vw;
    height: 55vw;
    background: white;
    padding: 4vw;
    border-radius: 5px;
    img {
      width: 100%;
      height: 40vw;
    }
    p {
      line-height: 7vw;
      text-align: center;
      font-size: 14px;
    }
  }
}
</style>
