<template>
  <div>
    <sale-form @handleSearch="handleSearch"
               ref="saleFormRef"
               :taskDefinitionKey="taskDefinitionKey"
               :dataForm.sync="dataForm"
               :datasource.sync="datasource"
               :dialogType="permissionType"></sale-form>
    <headerTitle title="对账明细" style="margin-top: 30px">
      <template v-slot>
        <el-row style="font-weight: 700;color: red">
          <el-divider direction="vertical"></el-divider>
          <span>总金额：</span>
          <span>{{ dataForm.amountTotalPrice || 0 }}</span>
          <el-divider direction="vertical"></el-divider>
          <span>应收金额：</span>
          <span>{{ dataForm.amountReceivablePrice || 0 }}</span>
          <el-divider direction="vertical"></el-divider>
          <span>应付金额：</span>
          <span>{{ dataForm.amountPayablePrice || 0 }}</span>


        </el-row>
      </template>
    </headerTitle>
    <inventoryTable ref="inventoryTableref" :priceObj="priceObj" :dataForm="dataForm" :datasource.sync="datasource"
                    :dialogType="permissionType"
                    :taskDefinitionKey="taskDefinitionKey"></inventoryTable>
  </div>
</template>

<script>


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

export default {
  name: 'page',
  props: ['permissionType', 'businessId', 'taskDefinitionKey'],
  components: {
    InventoryTable,
    saleForm
  },
  data() {
    return {
      datasource: [],
      dataForm: {
        sourceType: '',
        dateType: '',
        dateValue: '',
        contactId: '',
        contactName: '',
        sourceName: '',
        sourceId: '',
        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.amountReceivablePrice = ((obj.amountReceivablePrice * 1) + (row.receivablePrice * 1)).toFixed(2)
      //   //已收款合计金额
      //   obj.amountReceivedPrice = ((obj.amountReceivedPrice * 1) + (row.receivedPrice * 1)).toFixed(2)
      //   //合计总金额(单价*数量)
      //   obj.amountTotalPrice = ((obj.amountTotalPrice * 1) + (row.totalPrice * 1)).toFixed(2)
      //   return obj
      // }, {
      //   amountInvoicedPrice: 0,
      //   amountNotInvoicedPrice: 0,
      //   amountReceivablePrice: 0,
      //   amountReceivedPrice: 0,
      //   amountTotalPrice: 0,
      //   previousPeriodEndDebt: 0,
      // })
    }
  },
  watch: {},
  mounted() {
    this.getInfo(this.businessId)
  },
  methods: {

    //获取对账单详情
    async getInfo(id) {
      let data = await infoAccountStatementAPI(id)
      this.datasource = data?.orderList || []
      this.dataForm = data
      //this.priceObj.previousPeriodEndDebt = data.previousPeriodEndDebt
      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: 1,
      }
      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,

      }
      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">
::v-deep.el-divider {
  margin: 10px;
  font-weight: bold;
}
</style>
