<template>
  <ele-modal
    custom-class="ele-dialog-form long-dialog-form"
    :centered="true"
    :visible.sync="generateContractsDialogFlag"
    title="生成合同"
    :close-on-click-modal="false"
    append-to-body
    width="80%"
    :before-close="cancel"
  >
    <inquiryTable
      :radio="form.winnerId"
      v-for="item in form.supplierList"
      :key="item.supplierId"
      style="margin-top: 15px"
      status="Detail"
      v-if="item.resultList.some(i=>i.isWinner)"
      :obj="item"
    >
      <template #generateContracts>
        <el-button @click="handleGenerate(item)" type="primary" plain>生成合同</el-button>
        <span v-if="item.isGeneratedContract"
              style="color: red;margin-left: 5px;font-size: 11px">*已生成过合同,请注意</span>
      </template>

    </inquiryTable>
    <div slot="footer" class="footer">
      <el-button @click="cancel">返回</el-button>
    </div>
  </ele-modal>

</template>
<script>
import inquiryTable from "@/views/purchasingManage/inquiryManage/components/inquiryTable.vue";
import {generateContract, getpurchaseinquiry} from "@/api/purchasingManage/inquiryManage";
import {contactDetail, contactTypeTree} from "@/api/saleManage/contact";

export default {
  name: "generateContractsDialog",
  components: {inquiryTable},
  props: {
    generateContractsDialogFlag: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      form: {
        supplierList: []
      },
      treeList: []
    };
  },
  methods: {
    async open(row) {
      await this.getInquiryData(row.id);
      await this.getTreeData();
    },
    async getInquiryData(id) {
      this.loading = true;
      const data = await getpurchaseinquiry(id);
      this.loading = false;
      if (data) {
        this.form = data;
      }
    },
    //获取合同分类
    async getTreeData() {
      try {
        this.treeLoading = true;
        const res = await contactTypeTree({ id: '20' });
        this.treeLoading = false;
        if (res?.code === '0') {
          this.treeList = res.data;

          return this.treeList;
        }
      } catch (error) {
      }
      this.treeLoading = false;
    },
    async handleGenerate(i) {
      let contractInfo = await generateContract({inquiryIds: [this.form.id], supplierId: i.supplierId});
      let contact = await contactDetail(contractInfo.contractVO.partbId);
      contractInfo.contractVO.contractName = contact.base.simpleName;
      await this.setContractInfo(contractInfo);

    },
    async setContractInfo(data) {
      if (data) {
        //获取优惠金额和总计的差价
        let diffPrice = Number(data.contractVO.totalPrice) - Number(data.contractVO.discountTotalPrice)
        data.productList.forEach((item) => {
          if (data.contractVO.discountTotalPrice === 0) {
            item.discountTotalPrice = 0
            item.discountSinglePrice = 0
            return
          }
          if (!data.contractVO.discountTotalPrice) {
            item.discountTotalPrice = item.totalPrice
            item.discountSinglePrice = item.singlePrice
            return
          }
          //获取详情每条的小计
          // 使用小计除以总价得出该条数据小计占比 在乘以差价或的该条数据应承担的差价 在小计-应承担差价获得折让后的小计
          item.discountTotalPrice = (Number(item.totalPrice) - Number(item.totalPrice) / Number(data.contractVO.totalPrice) * diffPrice).toFixed(2)
          //使用折让后的小计除以数量得到单价
          item.discountSinglePrice = (Number(item.discountTotalPrice) / Number(item.totalCount)).toFixed(2)
        })
        this.$emit('changeParent', {data});
        this.cancel();
      }
    },
    cancel() {
      this.$emit("update:generateContractsDialogFlag", false);
    },
  }
}
</script>

<style scoped lang="scss">

</style>
