<template>
  <view class="order-list">
    <uni-segmented-control
      :current="controlCurrent"
      :values="controlItems"
      @clickItem="onClickItem"
      styleType="text"
      activeColor="#007AFF"
      style="border-bottom: 2rpx solid #dddddd"
    >
      >
    </uni-segmented-control>
    <scroll-list style="touch-action: none" ref="orderRef" :option="orderOption" @load="orderUp" @refresh="orderDown">
      <view v-for="(item, index) in list" :key="index" @click="go2('order-detail', { orderId: item.orderId })">
        <view class="list-border list-title-line">
          <view>
            <view class="list-title">{{ item.customerEnterpriseName }}</view>
            <view class="list-title-subLine">{{ item.orderId }}</view>
          </view>
          <view>
            <view class="list-title-Subtitle">{{ supplierOrderStatusMap[item.status] }}</view>
            <view class="list-title-subLine">{{ item.createTime }}</view>
          </view>
        </view>
        <view class="list-border list-info-line" v-for="(subItem, subIndex) in item.orderItems" :key="subIndex">
          <view class="list-info">
            <view>
              <image class="list-image" :src="subItem.productImg"></image>
            </view>
            <view class="list-info-content">
              <view class="list-info-title">{{ subItem.productName }}</view>
              <view class="list-info-text">
                {{ subItem.categoryName }}/{{ subItem.brandName }}/{{ item.gramWeight }}g/{{ subItem.length }}*{{ subItem.width }}/{{ subItem.pieceQuantity }}张
              </view>
            </view>
          </view>
        </view>
        <view class="list-border list-bottom-content">
          <view>
            <text class="list-bottom-content-text">交货时间:</text>
            <text class="list-bottom-content-subtext">{{ item.deliveryLeadtime }}</text>
          </view>
          <view>
            <text class="list-title-Subtitle subTitle-right">¥{{ item.totalOfferPrice }}</text>
          </view>
        </view>
        <!-- <uGap></uGap> -->
      </view>
    </scroll-list>
  </view>
</template>

<script>
import uGap from '@/components/u-gap/u-gap.vue'
import { go2 } from '@/utils/hook.js'
import { getSupplierOrderList } from '@/apis/orderApi.js'
import { supplierOrderStatusMap, supplierOrderStatusEnum } from '@/enums/index.js'
export default {
  components: {
    uGap
  },
  props: {
    refresh: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    refresh() {
      this.orderDown()
    }
  },
  data() {
    return {
      controlCurrent: 0,
      controlItems: ['全部', '待确认', '待发货', '已完成'],
      orderOption: {
        size: 10,
        auto: true,
        emptyText: '暂无订单信息~',
        background: '#F7F8FA',
        fontSize: '40rpx',
        emptyImage: '/static/imgs/client-detail/no-data-icon.png'
      },
      params: {
        asc: '',
        desc: '',
        customerEnterpriseId: this.$store.state.supplierInfo.id || null, // 客户企业ID默认为空，传值时网关获取当前用户关联企业id失效
        mallSupplierId: this.$store.state.supplierInfo.supplierId || null, // 供应商id
        // customerEnterpriseId: '651107734133018624',
        // mallSupplierId: '670334117090562048', // 供应商id
        status: '0' // 状态 0 全部 待发货/30106,待收货/30107,待借款/30207,已完成/30104
        // enterpriseIds: [] //企业ID集合网关获取
      },
      pagination: {
        pageNum: 0, // 初始会执行一次下拉加载
        pageSize: 10
      },
      list: [],
      supplierOrderStatusMap: Object.freeze(supplierOrderStatusMap)
    }
  },
  methods: {
    go2,
    // 获取订单列表
    getList() {
      return new Promise((resolve, reject) => {
        getSupplierOrderList({ ...this.params, ...this.pagination })
          .then((res) => {
            if (res) {
              if (this.pagination.pageNum == 1) {
                this.list = res.records
              } else {
                this.list = this.list.concat(res.records)
              }
              resolve({ list: this.list, total: res.total })
            } else {
              reject()
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    //订单分页
    orderUp(page) {
      this.pagination.pageNum++
      this.getList()
        .then(({ list, total }) => {
          this.$refs.orderRef.refreshSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.orderRef.loadFail()
        })
    },
    //订单分页
    orderDown() {
      this.pagination.pageNum = 1
      this.getList()
        .then(({ list, total }) => {
          this.$refs.orderRef.refreshSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.orderRef.refreshFail()
        })
    },
    //分段器点击事件
    onClickItem(value) {
      this.pagination.pageNum = 1
      switch (value.currentIndex) {
        case 0:
          this.params.status = 0
          break
        case 1:
          this.params.status = supplierOrderStatusEnum.WAIT_SUPPLIER_CONFIRM
          break
        case 2:
          this.params.status = supplierOrderStatusEnum.WAIT_DELIVERY
          break
        case 3:
          this.params.status = supplierOrderStatusEnum.FINISHED
          break
        default:
          break
      }
      this.getList()
    }
  }
}
</script>

<style lang="scss">
.order-list {
  margin-bottom: 0px;
  background: #ffffff;
  .list-border {
    border-bottom: 2rpx solid #dddddd;
  }
  .list-title-line {
    width: 750rpx;
    background: #ffffff;
    display: flex;
    justify-content: space-between;
    padding: 20rpx 32rpx;
    line-height: 58rpx;
    align-items: center;
    height: 120rpx;
  }
  .list-title-subLine {
    font-size: 26rpx;
    color: #888888;
    letter-spacing: 0;
    text-align: left;
    line-height: 32rpx;
    font-weight: 400;
  }
  .list-title {
    font-family: PingFangSC-Semibold;
    font-size: 30rpx;
    color: #000000;
    letter-spacing: 0;
    font-weight: 600;
  }
  .list-title-Subtitle {
    font-family: PingFangSC-Medium;
    font-size: 30rpx;
    color: #ff5368;
    letter-spacing: 0;
    text-align: right;
    font-weight: 500;
  }
  .subTitle-right {
    margin-right: 32rpx;
  }
  .list-info-line {
    padding-left: 32rpx;
    line-height: 70rpx;
    // width: 750rpx;
    // height: 88rpx;
    background: #ffffff;
  }
  .list-info-content {
    margin-left: 48rpx;
  }
  .list-info-title {
    font-family: PingFangSC-Medium;
    font-size: 30rpx;
    color: #333333;
    letter-spacing: 0;
    text-align: left;
    font-weight: 550;
  }
  .list-info-text {
    font-family: PingFangSC-Regular;
    font-size: 26rpx;
    color: #888888;
    letter-spacing: 0;
    text-align: left;
    line-height: 64rpx;
    font-weight: 400;
  }
  .list-image {
    width: 100rpx;
    height: 100rpx;
    margin-top: 20rpx;
  }
  .list-info {
    display: flex;
  }
  .list-bottom-content {
    // line-height: 70rpx;
    align-items: center;
    padding-left: 32rpx;
    width: 750rpx;
    height: 80rpx;
    background: #ffffff;
    display: flex;
    justify-content: space-between;
    .list-bottom-content-text {
      font-size: 26rpx;
      color: #888888;
      letter-spacing: 0;
      line-height: 32rpx;
      font-weight: 400;
    }
    .list-bottom-content-subtext {
      font-size: 26rpx;
      color: #333333;
      letter-spacing: 0;
      line-height: 32rpx;
      font-weight: 400;
    }
  }
}
</style>
