<template>
  <div id="myInvoiceList">
    <div class="content" ref="wrapper">
      <van-pull-refresh class="list" ref="supplierList" v-model="isRefreshing" @refresh="onRefresh">
        <van-list
          v-model="isLoading"
          :finished="finished"
          finished-text="没有更多了"
          :error.sync="error"
          error-text="请求失败，点击重新加载"
          @load="onLoad">
          <div class="invoiceItem" @click="gotoDetail(item)"
            v-for="(item, index) in invoiceList"
            :key="index">
              <my-invoiec-list-item :item="item"/>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
// 引入接口
import { getNewMyInvoiceList } from '@/api/myInvoice'
import myInvoiecListItem from './myInvoiecListItem.vue'
import {getFormateDate} from '@/utils/common'
export default {
  name: 'MyInvoiceList',
  components: {
    myInvoiecListItem
  },
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      isRefreshing: false,
      isLoading: false,
      finished: false,
      error: false,
      logInfoShow: false,
      invoiceList: [],
      invoiceItem: {},
      // moreSearchInfo: {
      //   //接收搜索更多回传的数据
      //   supplier: { comPartyId: "" }, //供应商ID
      //   goods: { comGoodsId: "" }, //商品ID
      //   orderNo: "", //订单编号
      //   time: {
      //     //起始时间
      //     start: getFormateDate(new Date(), "yyyy-MM-dd"),
      //     end: getFormateDate(new Date(), "yyyy-MM-dd"),
      //   },
      // },
      moreSearchInfo: {
        supplier: [], // 供应商
        consignor: [], // 货主
        goods: '', // 商品
        client: [], // 客户
        orderNo: '',
        time: {
          start: getFormateDate(new Date(), 'yyyy-MM-dd'),
          end: getFormateDate(new Date(), 'yyyy-MM-dd')
        }
      },
      sphImage: require('@/assets/img/u48.png'),
      scrollPos: 0
    }
  },
  computed: {
    linkInfo () {
      return this.$store.state.linkInfo
    },
    routKey() {
      return this.$route.path
    },
    searchTime() {
      let time = this.time.start + '/' + this.time.end
      return time
    }
  },
  activated() {
    this.$refs.wrapper.scrollTop = this.scrollPos
    console.log('位置还原', this.scrollPos)
  },
  mounted() {
    // this.onLoad()
  },
  methods: {
    // 高级搜索
    httpAction(model) {
      this.moreSearchInfo = model
      this.onRefresh()
    },
    gotoDetail(model) {
      // console.log(model)
      let wrapperScrollTop = this.$refs.wrapper.scrollTop
      this.scrollPos = wrapperScrollTop
      console.log('点击时列表位置', this.scrollPos)
      this.$emit('gotoDetail', model)
    },
    onRefresh() {
      this.currentPage = 1
      this.isRefreshing = true
      this.getPostData()
    },
    onLoad() {
      this.isLoading = true
      this.getPostData()
    },
    getPostData() {
      let _this = this
      let data = {
        'clientID': [
          '3083'
        ],
        'endDate': this.moreSearchInfo.time.end,
        'goodsCode': '',
        'goodsDesc': '',
        'goodsId': '',
        'goodsName': '',
        'goodsSpec': '',
        'invoiceCode': '',
        'invoiceDate': '',
        'invoiceFullNo': '',
        'invoiceNo': '',
        'isTwoTicket': '',
        'manufacturer': '',
        'mdmClientCode': '',
        'mdmGoodsCode': '',
        'order': '',
        'orderId': '',
        'orderNo': '',
        'pageNum': this.currentPage,
        'pageSize': this.pageSize,
        'sort': '',
        'sourceDb': 'SHYY',
        'startDate': this.moreSearchInfo.time.start,
        // 'startDate': '2021-01-02',
        'supplierId': [
          '225'
        ]
      }
      let dict = {
        'list': this.setMoreSearchInfoAction(),
        // 'list': [data],
        'endDate': _this.moreSearchInfo.time.end,  // 结束时间
        'startDate': _this.moreSearchInfo.time.start, // 开始时间
        'pageNum': _this.currentPage,
        'pageSize': _this.pageSize
      }
      console.log(dict)
      // let dic = {
      //   "list": [
      //       {
      //           "businessType": "",
      //           "clientID": [
      //               "3004",
      //               "57847"
      //           ],
      //           "endDate": "2021-06-24",
      //           "startDate": "2021-06-24",
      //           "goodsCode": "",
      //           "goodsDesc": "",
      //           "goodsId": "",
      //           "goodsName": "",
      //           "goodsSpec": "",
      //           "isDelivered": "",
      //           "isInvoice": "",
      //           "isWeb": "",
      //           "manufacturer": "",
      //           "mdmClientCode": "",
      //           "order": "",
      //           "orderId": "",
      //           "orderNo": "",
      //           "orderStatusCode": "",
      //           "orderType": "SALE",
      //           "pageNum": 1,
      //           "pageSize": 10,
      //           "shippingAdress": "",
      //           "sort": "",
      //           "sourceDb": "SHYY",
      //           "sourceOrderNo": "",
      //           "sourceSystem": "",
      //           "supplierId": [
      //               "188",
      //               "204"
      //           ]
      //       },
      //       {
      //           "clientID": [
      //               "3168"
      //           ],
      //           "endDate": "2021-06-25",
      //           "goodsCode": "",
      //           "goodsDesc": "",
      //           "goodsId": "",
      //           "goodsName": "",
      //           "goodsSpec": "",
      //           "invoiceCode": "",
      //           "invoiceDate": "",
      //           "invoiceFullNo": "",
      //           "invoiceNo": "",
      //           "isTwoTicket": "",
      //           "manufacturer": "",
      //           "mdmClientCode": "",
      //           "mdmGoodsCode": "",
      //           "order": "",
      //           "orderId": "",
      //           "orderNo": "",
      //           "pageNum": 1,
      //           "pageSize": 10,
      //           "sort": "",
      //           "sourceDb": "SHYY",
      //           "startDate": "2021-06-25",
      //           "supplierId": [
      //               "305",
      //               "879",
      //               "301"
      //           ]
      //       }
      //   ],
      //   "endDate": "2021-06-24",
      //   "startDate": "2021-06-24",
      //   "pageNum": 1,
      //   "pageSize": 10
      // }
      getNewMyInvoiceList(dict).then(rt => {
        if (rt.data.code && rt.data.code === '00000') {
          if (_this.isRefreshing) {
            _this.invoiceList = rt.data.entity.invoiceList
          } else if (rt.data.entity.invoiceList || rt.data.entity.invoiceList.length > 0) {
            _this.invoiceList = _this.invoiceList.concat(rt.data.entity.invoiceList)
          }
          // 重置刷新提示
          _this.isRefreshing = false
          // 重置加载更多提示
          _this.isLoading = false
          if (rt.data.entity.invoiceList.length < 10) {
            _this.finished = true
          } else {
            // 加载成功后下一页自增1
            _this.currentPage++
            _this.finished = false
          }
        } else {
          _this.$toast(rt.msg)
          _this.isRefreshing = false
          _this.error = true
          _this.finished = false
        }
        // 加载状态结束
        _this.isLoading = false
      }).catch(e => {
        this.$toast('获取信息失败！')
        _this.isRefreshing = false
        _this.isLoading = false
        _this.error = true
        _this.finished = true
      })
    },
    // 高级搜索条件
    setMoreSearchInfoAction () {
      let _this = this
      const sourceDbList = []
      const dataList = []
      console.log(_this.moreSearchInfo)
      _this.moreSearchInfo.consignor.forEach(item => {
        if(sourceDbList.indexOf(item.ownerid) > -1) {
          return
        } else {
          sourceDbList.push(item.ownerid)
        }
        const model = {
          'businessType': '',
          'clientID': this.getClientValueArray(_this.moreSearchInfo.client, item.id), // 客户编码
          'endDate': _this.moreSearchInfo.time.end,  // 结束时间
          'startDate': _this.moreSearchInfo.time.start, // 开始时间
          'goodsCode': !_this.moreSearchInfo.goods.goodsOpcode ? '':_this.moreSearchInfo.goods.goodsOpcode, // 商品代码
          'goodsDesc': '', // 商品描述
          'goodsId': !_this.moreSearchInfo.goods.comGoodsId ? '':_this.moreSearchInfo.goods.comGoodsId, // 商品ID
          'goodsName': '', // 商品名称
          'goodsSpec': '', // 商品规格
          'manufacturer': '', // 生产企业
          'mdmClientCode': '', // 主数据客户编码
          'order': '',
          'orderId': '', // 订单ID
          'orderNo': !_this.moreSearchInfo.orderNo ? '':_this.moreSearchInfo.orderNo, // 订单号
          'orderType': 'SALE', // 订单类型 正常销售SALE销售退货SALE_RTN
          'pageNum': _this.currentPage,
          'pageSize': _this.pageSize,
          'shippingAdress': '', // 送货地址
          'sort': '',
          'sourceDb': !item.ownerid ? '':item.ownerid, // 数据来源
          'sourceOrderNo': '', // 来源订单号(网上订单号)
          'sourceSystem': '', // 订单来源 来源订单号和订单来源必须同时不为空
          'supplierId': this.getSupplierValueArray(_this.moreSearchInfo.supplier,item.id) // 供应商(部门)编码
        }
        dataList.push(model)
      })
      // console.log(dataList)
      return dataList
    },
    // 是否返回空数组
    getArray (value) {
      if (this.isJudgeStrNull(value)) {
        return [value]
      }
      return []
    },
    // 返回客户数据数组
    getClientValueArray (array,companynameId) {
      const vaueArray = []
      array.forEach(item => {
        if (item.companynameId === companynameId) {
          vaueArray.push(item.comPartyId)
        }
      })
      return vaueArray
    },
    // 返回供应商数据数组
    getSupplierValueArray (array,companynameId) {
      const vaueArray = []
      array.forEach(item => {
        if (item.companynameId === companynameId) {
          if (item.org === '205' || item.org === '307') {
            vaueArray.push('508')
          } else {
            vaueArray.push(item.org)
          }
        }
      })
      return vaueArray
    },
    /// 判断数值是否为空
    isJudgeStrNull(val) {
      if (val !== null && val !== undefined && val !== '') {
        return true
      }
      return false
    }
  }
}
</script>

<style lang="scss" scoped>
  #myInvoiceList {
    height: 100%;
    .content{
      height: 100%;
      overflow-x: hidden;
      overflow-y: auto;
      padding: 10PX;
      -webkit-overflow-scrolling: touch;
      .list {
        min-height: 100%;
      }
      .invoiceItem{
        // height: 145PX;
        background-color: white;
        margin-bottom: 10PX;
        border-radius: 10PX;
        font-size: 14PX;
        color: #333333;
      }
    }
  }
</style>
