<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"></headerTitle>
    <inventoryTable ref="inventoryTableref" :priceObj="priceObj" :datasource.sync="datasource"
                    :dialogType="permissionType"
                    :taskDefinitionKey="taskDefinitionKey"></inventoryTable>
  </div>
</template>

<script>


import InventoryTable from "./inventoryTable.vue";
import purchaseForm from "./purchaseForm.vue";
import {getSearchMergeListByTypeAPI, infoAccountStatementAPI,} from "@/api/bpm/components/saleManage/saleorder";

export default {
  name: 'page',
  props: ['permissionType', 'businessId', 'taskDefinitionKey'],
  components: {
    InventoryTable,
    purchaseForm
  },
  data() {
    return {
      datasource: [],
      dataForm: {
        fromType: '',
        dateType: '',
        dateValue: '',
        contactId: '',
        contactName: '',
        type: '',
        year: '',
        quarter: '',
        month: '',
        dateTimeRange: [],
        orderNo: '',
        orderId: '',
        id: '',
        startDate: '',
        endDate: '',
        repliedFiles: [],
        BQYSKZE: undefined,
      },
      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 infoAccountStatementAPI(id)
      this.datasource = data?.mergeList || []
      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) {
      this.updateData()
      if (!await this.getFormValidate()) return
      this.datasource = await getSearchMergeListByTypeAPI(params)
    },
    getFormValidate() {
      return new Promise((resolve) => {
        this.$refs.saleFormRef.$refs.form.validate(validate => {
          resolve(validate)
        })
      })

    },
    getTableValidate() {
      return new Promise((resolve) => {
        this.$refs.inventoryTableref.$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)
        }
        if (!await this.getTableValidate()) {
          resolve(false)
        }

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

        }
        resolve(params)

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

</style>
