<template>
  <ele-modal
    custom-class="ele-dialog-form long-dialog-form"
    :centered="true"
    :visible.sync="addAccountDialogFlag"
    :title="title"
    :close-on-click-modal="false"
    width="70%"
    :before-close="cancel"
  >
    <!--采购表单表单-->
    <purchase-form @handleSearch="handleSearch" :dataForm.sync="dataForm" :datasource.sync="datasource"
                   :dialogType="dialogType"></purchase-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"
                    :datasource.sync="datasource"
                    :dialogType="dialogType"></inventoryTable>
    <div slot="footer" class="footer">
      <el-button v-if="dialogType!=='view'" type="primary" @click="save">保存</el-button>
      <el-button v-if="dialogType!=='view'" type="primary" @click="save('sub')">提交</el-button>
      <el-button @click="cancel">返回</el-button>
    </div>

  </ele-modal>
</template>

<script>


import InventoryTable from "./inventoryTable.vue";
import purchaseForm from "./purchaseForm.vue";
import {
  getSearchMergeListByTypeAPI,
  infoAccountStatementAPI,
  saveAccountStatementAPI,
  submitAccountStatementApproveAPI,
  updateAccountStatementAPI
} from "@/api/saleManage/accountstatement";

export default {
  name: 'addAccountDialog',
  props: ['addAccountDialogFlag'],
  components: {
    InventoryTable,
    purchaseForm
  },
  data() {
    return {
      datasource: [],
      dataForm: {
        sourceType: 1,
        dateType: 1,
        dateValue: '',
        contactId: '',
        contactName: '',
        sourceName: '',
        sourceId: '',
        type: '',
        year: '',
        quarter: '',
        month: '',
        dateTimeRange: [],
        orderNo: '',
        orderId: '',
        id: '',
        startDate: '',
        endDate: '',
        repliedFiles: []
      },
      title: '',
      dialogType: ''
    }
  },
  computed: {},
  methods: {
    open(dialogType, row, type = '') {
      this.dialogType = dialogType
      this.title = dialogType == 'add' ? '新增' : dialogType == 'update' ? '修改' : '详情'
      this.dataForm.type = type
      if (this.dialogType !== 'add') {
        this.getInfo(row)
      }
    },
    //获取对账单详情
    async getInfo(row) {
      let data = await infoAccountStatementAPI(row.id)
      this.datasource = data.orderList
      this.dataForm = data
      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]
      }
    },
    //关闭弹窗
    cancel() {
      this.$emit('update:addAccountDialogFlag', false)
    },
    //查询获取订单数据
    async handleSearch(params) {

      let searchQuery = {
        endDate: params.endDate,
        sourceId: params.sourceId,
        sourceType: params.sourceType,
        startDate: params.startDate,
        type: 2,
      }
      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('暂无订单信息')
    },
    //保存
    async save(is) {
      if (!this.datasource.length) return this.$message.warning('暂无订单信息')
      let api = this.dialogType == 'add' ? saveAccountStatementAPI : updateAccountStatementAPI
      let params = {
        ...this.dataForm,
        orderList: this.datasource,
      }
      let data = await api(params)
      if (is == 'sub') {
        await submitAccountStatementApproveAPI(
          {
            businessId: data,
            type: this.dataForm.type
          }
        )
      }
      this.cancel()
      //刷新主页面
      this.$emit('done');
    },

  }
};
</script>
<style scoped lang="scss">
::v-deep.el-divider {
  margin: 10px;
  font-weight: bold;
}
</style>
