<template>
  <div id="myOrderList">
    <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="orderItem"
            v-for="(item, index) in orderList"
            :key="index">
            <order-list-item @orderItemAction="orderItemAction" :item="item"/>
            <!-- <div class="itemInfo">
              <div class="orderNum">
                <van-image fit="contain" width="45" height="45" :src="sphImage"/>
                <div class="num">订单号：34678649876</div>
                <div class="dian"></div>
                <div class="numType"><span>已开单</span></div>
              </div>
              <div class="divider"></div>
              <div class="orderInfo">
                <div class="company">
                  <span style="width: 83%">上药控股有限公司</span>
                  <span style="margin-right: 5px;">共8件</span>
                  <van-icon style="margin: auto 0;" color="#1989fa" name="arrow" />
                </div>
                <div class="info">
                  <div class="headImage">
                    <van-image width="55" height="55"
                      src="https://img01.yzcdn.cn/vant/cat.jpeg"/>
                  </div>
                  <div class="details">
                    <div class="titleInfo">
                      [132521]阿西匹林/0.25g*s/上海市浦东新区金科路长泰广场负一楼
                    </div>
                    <div class="priceInfo">
                      <div class="price">￥<span style="color:red;">10.5</span></div>
                      <div class="numBer">X 8</div>
                    </div>
                  </div>
                </div>
                <div class="orderTime">
                  <van-icon size="20" style="margin: auto 0; width: 10%;" name="clock-o" />
                  <span style="width: 50%;">2020-11-10 18:09:33</span>
                  <div class="price">总计：￥<span style="color:red;">10.5</span></div>
                </div>
                <van-divider :style="{ color: '#999999', borderColor: '#999999', margin: '15px 0 10px 0'}"/>
                <div class="btnInfo" style="text-align: right;">
                  <van-button plain size="small" type="info">产看发票</van-button>
                  <van-button plain size="small" type="info">查看物流</van-button>
                  <van-button plain size="small" type="info">清货单</van-button>
                </div>
              </div>
            </div> -->
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>

<script>
// 引入接口
import { getMyOrderList, getMoreMyOrderList } from '@/api/myOrder'
import orderListItem from './orderListItem.vue'
import {getFormateDate} from '@/utils/common'
import { valid } from 'semver'
export default {
  name: 'MyOrderList',
  components: {
    orderListItem
  },
  data() {
    return {
      currentPage: 1,
      pageSize: 10,
      isRefreshing: false,
      isLoading: false,
      finished: false,
      error: false,
      logInfoShow: false,
      scrollPos: 0,
      orderList: [],
      orderItem: {},
      sphImage: require('@/assets/img/u48.png')
      // moreSearchInfo: {
        
      // }
    }
  },
  props: {
    activeTab: '',
    orderTypeCode: {
      type: String,
      default: ''
    },
    moreSearchInfo: {
      type: Object,
      default: () => {
        return {
          supplier: {}, // 供应商
          consignor: [], // 货主
          goods: '', // 商品
          client: '', // 客户
          orderNo: '',
          time: {
            start: getFormateDate(new Date(), 'yyyy-MM-dd'),
            end: getFormateDate(new Date(), 'yyyy-MM-dd')
          }
        }
      }
    }
  },
  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()
    let _this = this
    this.$EventBus.$on('myOrderMoreSearch', (model) => {
      _this.moreSearchInfo = model
      _this.onRefresh()
    })
  },
  watch: {
    moreSearchModel(val) {
      this.moreSearchInfo = val
    }
  },
  methods: {
    // 单个发票点击事件
    orderItemAction(model) {
      let wrapperScrollTop = this.$refs.wrapper.scrollTop
      this.scrollPos = wrapperScrollTop
      console.log('点击时列表位置', this.scrollPos)
      this.$emit('orderItemAction', model)
    },
    onRefresh() {
      this.currentPage = 1
      this.isRefreshing = true
      this.getPostData()
    },
    onLoad() {
      this.isLoading = true
      this.getPostData()
    },
    getPostData() {
      let _this = this
      let data = {
        'businessType': '',
        'clientID': [
          '3168'
        ], // 客户编码
        'endDate': _this.moreSearchInfo.time.end,  // 结束时间
        'startDate': _this.moreSearchInfo.time.start, // 开始时间
        'goodsCode': '', // 商品代码
        'goodsDesc': '', // 商品描述
        'goodsId': '', // 商品ID
        'goodsName': '', // 商品名称
        'goodsSpec': '', // 商品规格
        'isDelivered': '', // 是否配送 TRUE是/FALSE
        'isInvoice': '', // 是否开票 TURE 是 FALSE 否
        'isWeb': '', // 是否网单 TURE 是FALSE 否
        'manufacturer': '', // 生产企业
        'mdmClientCode': '', // 主数据客户编码
        'order': '',
        'orderId': '', // 订单ID
        'orderNo': !_this.moreSearchInfo.orderNo ? '':_this.moreSearchInfo.orderNo, // 订单号
        // 订单状态ERP 100 已开单110 已取消180 已完（销退）WMS200 待出库210 拣配中220 已出库TMS300 调度中310 运输中400 已签收410 签收执420 拒收
        'orderStatusCode': _this.orderTypeCode,
        'orderType': 'SALE', // 订单类型 正常销售SALE销售退货SALE_RTN
        'pageNum': _this.currentPage,
        'pageSize': _this.pageSize,
        'shippingAdress': '', // 送货地址
        'sort': '',
        'sourceDb': 'SHYY', // 数据来源
        'sourceOrderNo': '', // 来源订单号(网上订单号)
        'sourceSystem': '', // 订单来源 来源订单号和订单来源必须同时不为空
        'supplierId': [
        ] // 供应商(部门)编码
      }
      let dict = {
        'list': this.setMoreSearchInfoAction(),
        // 'list': [data],
        'endDate': _this.moreSearchInfo.time.end,  // 结束时间
        'startDate': _this.moreSearchInfo.time.start, // 开始时间
        'pageNum': _this.currentPage,
        'pageSize': _this.pageSize
      }
      getMoreMyOrderList(dict).then(rt => {
        if (rt.code === '1') {
          if (_this.isRefreshing) {
            _this.orderList = rt.data
          } else if (rt.data || rt.data.length > 0) {
            _this.orderList = _this.orderList.concat(rt.data)
          }
          // 重置刷新提示
          _this.isRefreshing = false
          // 重置加载更多提示
          _this.isLoading = false
          if (rt.data.length < 10) {
            _this.finished = true
          } else {
            // 加载成功后下一页自增1
            _this.currentPage++
            _this.finished = false
          }
        } else {
          _this.$toast(rt.message)
          _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': '', // 商品规格
          'isDelivered': '', // 是否配送 TRUE是/FALSE
          'isInvoice': '', // 是否开票 TURE 是 FALSE 否
          'isWeb': '', // 是否网单 TURE 是FALSE 否
          'manufacturer': '', // 生产企业
          'mdmClientCode': '', // 主数据客户编码
          'order': '',
          'orderId': '', // 订单ID
          'orderNo': !_this.moreSearchInfo.orderNo ? '':_this.moreSearchInfo.orderNo, // 订单号
          // 订单状态ERP 100 已开单110 已取消180 已完（销退）WMS200 待出库210 拣配中220 已出库TMS300 调度中310 运输中400 已签收410 签收执420 拒收
          'orderStatusCode': _this.orderTypeCode,
          '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>
  #myOrderList {
    height: 100%;
    .content{
      height: 100%;
      overflow-x: hidden;
      overflow-y: auto;
      // overflow: auto;
      -webkit-overflow-scrolling: touch;
      padding: 10PX;
      .list {
        // height: 100%;
        min-height: 100%;
      }
      .orderItem{
        // height: 250PX;
        background-color: white;
        margin-bottom: 10PX;
        border-radius: 6PX;
        font-size: 14px;
        color: #333333;
        padding: 0PX 10PX 10PX 10PX;
      }
    }
  }
</style>
