<template>
  <div>
    <purchase-form @handleSearch="handleSearch"
                   ref="saleFormRef"
                   :taskDefinitionKey="taskDefinitionKey"
                   :dataForm.sync="dataForm"
                   :datasource.sync="datasource"
                   :dialogType="permissionType"></purchase-form>
    <headerTitle title="对账明细" style="margin-top: 30px">
      <template v-slot>
        <el-row style="font-weight: 700; color: red">
            <span>订单总金额：</span>
            <span>{{ dataForm.orderTotalAmount || 0 }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>已对账金额：</span>
            <span>{{ dataForm.amountCompletePrice || 0 }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>未对账金额：</span>
            <span>{{ dataForm.amountUnCompletePrice || 0 }}</span>
            <el-divider direction="vertical"></el-divider>
            <span>本次对账总金额：</span>
            <span>{{ dataForm.amountTotalPrice || 0 }}</span>
          </el-row>
      </template>
    </headerTitle>
    <!-- <recorpayTableList
      ref="recorpayListRef"
      :dataForm="dataForm"
      :recorpayList.sync="recorpayList"
      :dialogType="permissionType"
    ></recorpayTableList> -->
    <inventoryTable ref="inventoryTableref" type="purchase" :priceObj="priceObj" :dataForm="dataForm" :datasource.sync="datasource" :dialogType="permissionType" :taskDefinitionKey="taskDefinitionKey"></inventoryTable>
  </div>
</template>

<script>


import InventoryTable from "@/views/bpm/handleTask/components/saleOrder/accountstatement/inventoryTable.vue";
import purchaseForm from "./purchaseForm.vue";
import {getSearchMergeListByTypeAPI, infoAccountStatementAPI, accountstatementInfoAPI} from "@/api/bpm/components/saleManage/saleorder";
import recorpayTableList from '@/views/bpm/handleTask/components/saleOrder/accountstatement/recorpayTableList.vue';

export default {
  name: 'page',
  props: ['permissionType', 'businessId', 'taskDefinitionKey'],
  components: {
    InventoryTable,
    purchaseForm,
    recorpayTableList
  },
  data() {
    return {
      datasource: [],
      dataForm: {
        sourceType: '',
        dateType: '',
        queryDimension: '',
        dateValue: '',
        contactId: '',
        contactName: '',
        sourceName: '',
        sourceId: '',
        type: '',
        year: '',
        quarter: '',
        month: '',
        dateTimeRange: [],
        orderNo: '',
        orderId: '',
        id: '',
        startDate: '',
        endDate: '',
        repliedFiles: [],
        BQYSKZE: undefined,
      },
      recorpayList:[],
      title: '',
      quarterList: {
        '一季度': ['-01-01 00:00:00', '-03-31 12:59:59'],
        '二季度': ['-04-01 00:00:00', '-06-30 12:59:59'],
        '三季度': ['-07-01 00:00:00', '-09-30 12:59:59'],
        '四季度': ['-10-01 00:00:00', '-12-31 12:59:59'],
      },
      startTime: '-01-01 00:00:00',
      endTime: '12:59:59',
    }
  },
  computed: {
    priceObj() {
      // return this.datasource.reduce((obj, row) => {
      //   //已开票合计金额
      //   obj.amountInvoicedPrice = ((obj.amountInvoicedPrice * 1) + (row.invoicedPrice * 1)).toFixed(2)
      //   //未开票合计金额
      //   obj.amountNotInvoicedPrice = ((obj.amountNotInvoicedPrice * 1) + (row.notInvoicedPrice * 1)).toFixed(2)
      //   //应付款合计金额
      //   obj.amountNotPaidPrice = ((obj.amountNotPaidPrice * 1) + (row.notPaidPrice * 1)).toFixed(2)
      //   //已付款合计金额
      //   obj.amountPaidPrice = ((obj.amountPaidPrice * 1) + (row.paidPrice * 1)).toFixed(2)
      //   //已收货总金额
      //   obj.amountTotalPrice = ((obj.amountTotalPrice * 1) + (row.totalPrice * 1)).toFixed(2)
      //   return obj
      // }, {
      //   amountInvoicedPrice: 0,
      //   amountNotInvoicedPrice: 0,
      //   amountNotPaidPrice: 0,
      //   amountPaidPrice: 0,
      //   amountTotalPrice: 0,
      // })
    }
  },
  watch: {},
  mounted() {
    this.getInfo(this.businessId)
  },
  methods: {

    //获取对账单详情
    async getInfo(id) {
      let data = await accountstatementInfoAPI(id)
      this.datasource = data?.orderList || []
      // this.recorpayList=data?.recorpayList||[]
      data.orderTotalAmount = data.orderList.reduce((pre, cur) => pre + Math.round(+cur.orderAmount * 100), 0) / 100;
      data.amountTotalPrice = data.orderList.reduce((pre, cur) => pre + Math.round(+cur.statementAmount * 100), 0) / 100;
      data.amountCompletePrice = data.orderList.reduce((pre, cur) => pre + Math.round(+cur.statementedAmount * 100), 0) / 100;
      data.amountUnCompletePrice = data.orderList.reduce((pre, cur) => pre + Math.round(+cur.unStatementAmount * 100), 0) / 100;
      this.dataForm = data
      // this.priceObj.amountNotInvoicedPrice = data.amountNotInvoicedPrice
      switch (this.dataForm.dateType) {
        case 1:
          this.dataForm.year = this.dataForm.dateValue
          break;
        case 2:
          //2023年-四季度
          let data = this.dataForm.dateValue.split('年-')
          this.dataForm.year = data[0]
          this.dataForm.quarter = data[1]
          break
        case 3:
          this.dataForm.month = this.dataForm.dateValue
          break
        default:
          this.dataForm.dateValue = ''
          this.dataForm.dateTimeRange = [this.dataForm.startDate, this.dataForm.endDate]
      }
    },
    updateData() {
      switch (this.dataForm.dateType) {
        case 1:
          this.dataForm.dateValue = this.dataForm.year
          this.dataForm.startDate = this.dataForm.year + this.startTime
          this.dataForm.endDate = this.dataForm.year + '-12-31 12:59:59'
          break;
        case 2:
          this.dataForm.dateValue = this.dataForm.year + '年-' + this.dataForm.quarter
          this.dataForm.startDate = this.dataForm.year + this.quarterList[this.dataForm.quarter][0]
          this.dataForm.endDate = this.dataForm.year + this.quarterList[this.dataForm.quarter][1]
          break
        case 3:
          this.dataForm.dateValue = this.dataForm.month
          this.dataForm.startDate = this.dataForm.month + '-01 00:00:00'
          let data = this.dataForm.month.split('-')
          let days = new Date(data[0], data[1], 0).getDate()
          this.dataForm.endDate = this.dataForm.month + '-' + days + ' 12:59:59'
          break
        default:
          this.dataForm.dateValue = ''
          this.dataForm.startDate = this.dataForm.dateTimeRange[0] + ' 00:00:00'
          this.dataForm.endDate = this.dataForm.dateTimeRange[1] + ' 12:59:59'
      }
    },
    //查询获取订单数据
    async handleSearch(params) {
      let searchQuery = {
        endDate: params.endDate,
        sourceId: params.sourceId,
        sourceType: params.sourceType,
        startDate: params.startDate,
        type: 2,
      }
      this.updateData()
      if (!await this.getFormValidate()) return
      let data = await getSearchMergeListByTypeAPI(searchQuery)
      this.datasource = data.orderList || []
      this.dataForm = {
        ...this.dataForm,
        amountPayablePrice: data.amountPayablePrice,
          amountReceivablePrice: data.amountReceivablePrice,
          amountTotalPrice: data.amountTotalPrice,
          amountPayablePass: data.amountPayablePass,
          amountReceivablePass: data.amountReceivablePass,

      }
      if (!this.datasource.length) this.$message.warning('暂无订单信息')
    },
    getFormValidate() {
      return new Promise((resolve) => {
        this.$refs.saleFormRef.$refs.form.validate(validate => {
          resolve(validate)
        })
      })

    },
    //
    getTableValue() {
      return new Promise(async (resolve) => {
        if (!this.datasource.length) {
          this.$message.warning('暂无订单信息')
          resolve(false)
        }
        if (!await this.getFormValidate()) {
          resolve(false)
        }

        let params = {
          ...this.dataForm,
          orderList: this.datasource,
          ...this.priceObj,

        }
        resolve(params)

      })
    },
  }
};
</script>
<style scoped lang="scss">

</style>
