<template>
  <ele-modal
    custom-class="ele-dialog-form long-dialog-form"
    :centered="true"
    :visible.sync="generateOrderDialogFlag"
    title="选择供应商"
    :close-on-click-modal="false"
    append-to-body
    width="70%"
    :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>
      </template>

    </inquiryTable>
    <div slot="footer" class="footer">
      <el-button @click="cancel">返回</el-button>
    </div>
    <addContractBookDialog
      ref="addContractBookDialogRef"
      :categoryTreeList="treeList"
    ></addContractBookDialog>
  </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";
import addContractBookDialog from "@/views/contractManage/contractBook/components/addDialog.vue";

export default {
  name: "generateContractsDialog",
  components: {addContractBookDialog, inquiryTable},
  props: {
    generateOrderDialogFlag: {
      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) {
      this.$emit('changeParent',i)
      this.cancel()
      // 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.$refs.addContractBookDialogRef.open('add', contractInfo, true, 'inquiryManage');
    },
    cancel() {
      this.$emit("update:generateOrderDialogFlag", false);
    },
  }
}
</script>

<style scoped lang="scss">

</style>
