<!-- 可开发票订单 -->
<template>
  <view class="order-box">
    <z-paging ref="paging" v-model="orderList" @query="queryList">
      <!-- 导航栏 -->
      <u-navbar
        title-color="#fff"
        :custom-back="customBack"
        :border-bottom="false"
        back-icon-color="#CCE8FF"
        :background="{ background: '#008CFF' }"
        title="可开票订单"
        slot="top"
      />
      <view class="order-box-list">
        <view class="order-box-list-item" v-for="(item, index) in orderList" :key="index" @click="radioClick(item)">
          <view class="left">
            <view class="left-radio small-radio">
              <radio value="true" :checked="currentIds.includes(item.id)" />
            </view>
            <view class="left-content">
              <view class="left-content-item money">¥{{ item.amount }}</view>
              <view class="left-content-item">{{ item.name }}</view>
              <view class="left-content-item time">{{ $u.timeFormat(new Date(item.payTime), 'yyyy-mm-dd hh:MM:ss') }}</view>
            </view>
          </view>
          <view class="right">{{ item.vehicleNo }}</view>
        </view>
      </view>
      <view class="order-box-bottom" slot="bottom">
        <view class="order-box-bottom-radio small-radio"> <radio value="true" :checked="checkedAll" @click="checkedChange" />本页全选 </view>
        <view class="order-box-bottom-next">
          <u-button type="primary" size="medium" :disabled="!currentIds.length" @click="nextStep">下一步</u-button>
        </view>
      </view>
    </z-paging>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      orderList: [],
      currentIds: [],
      currentList: [],
      checkedAll: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        invoType: 1
      }
    };
  },
  watch: {
    currentIds(val) {
      this.checkedAll = val.length === this.orderList.length;
    }
  },
  onLoad(options) {
    const { orderType } = options;
    if (orderType) {
      this.queryParams.invoType = orderType;
    }
  },
  methods: {
    /**
     * @description: 分页触发
     * @param {*} pageNo
     * @param {*} pageSize
     * @return {*}
     */
    queryList(pageNo, pageSize) {
      this.queryParams.pageNum = pageNo;
      this.queryParams.pageSize = pageSize;
      this.getList();
    },
    customBack() {
      this.$u.route({
        url: '/pages/invoiceModule/myInvoice/myInvoice'
      });
    },
    /**
     * @description: 获取列表
     * @return {*}
     */
    async getList() {
      const { code, data } = await this.$u.api.invoiceModuleApi.getInvoiceOrderListApi({ ...this.queryParams });
      if (code === 200) {
        this.$refs.paging.complete(data?.pageInfo?.rows || []);
      }
    },
    /**
     * @description: 单项条目点击
     * @param {*} item
     * @return {*}
     */
    radioClick(item) {
      if (this.currentIds.includes(item.id)) {
        const index = this.currentIds.indexOf(item.id);
        this.currentIds.splice(index, 1);
        this.currentList.splice(index, 1);
      } else {
        this.currentIds.push(item.id);
        this.currentList.push(item);
      }
    },
    /**
     * @description: 全选
     * @return {*}
     */
    checkedChange(item) {
      this.checkedAll = !this.checkedAll;
      this.currentIds = this.checkedAll ? this.orderList.map((item) => item.id) : [];
      this.currentList = this.checkedAll
        ? this.orderList.map((item) => {
            return { ...item };
          })
        : [];
    },
    /**
     * @description: 下一步
     * @return {*}
     */
    nextStep() {
      if (this.currentIds.length) {
        const storageObj = {
          total: this.countSum(this.currentList),
          ids: this.currentIds,
          invoType: this.queryParams.invoType
        };
        uni.setStorage({
          key: 'availableOrderKey',
          data: JSON.stringify(storageObj),
          success: () => {
            this.$u.route({
              url: '/pages/invoiceModule/addInvoice/addInvoice',
              params: {
                invoType: this.queryParams.invoType
              }
            });
          }
        });
      } else {
        this.$refs.uToast.show({
          title: '请先选择订单',
          type: 'warning'
        });
      }
    },
    /** 
     * @description: 计算累加精度丢失问题
     * @param {*} arr
     * @return {*}
     */
    countSum(arr) {
      if (!arr.length) return 0;
      arr = arr.map((v) => {
        if (v.amount && !Number.isNaN(Number(v.amount))) return Math.round((v.amount).toFixed(2) * 100);
        return 0;
      });
      const result = arr.reduce((prev, curr) => {
        return prev + curr;
      }, 0);
      return result / 100;
    }
  }
};
</script>

<style lang="scss" scoped>
@import './availableOrder.scss';
</style>