<template>
  <el-dialog
    title="选择核价单"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    top="5vh"
    :close-on-press-escape="false"
    append-to-body
    width="80%"
  >
    <el-card shadow="never">
      <searchProduct @search="reload"></searchProduct>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="eomContactPageTable"
        height="calc(100vh - 350px)"
        class="dict-table"
      >
 
      </ele-pro-table>
    </el-card>

    <div class="btns">
      <el-button type="primary" size="small" @click="selected"
        >生成合同</el-button
      >
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getTableList,
    generateContract
  } from '@/api/purchasingManage/inquiryManage';
  import searchProduct from '@/views/purchasingManage/inquiryManage/components/searchQuotation.vue';
  export default {
    components: {
      searchProduct
    },

    data() {
      return {
        visible: false,
        currentIndex: null,
        selection: [],
        columns: [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'inquiryCode',
            slot: 'inquiryCode',
            label: '采购询价编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'planCode',
            slot: 'planCode',
            label: '采购计划编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'supplierNames',
            label: '供应商名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'winnerName',
            label: '中标供应商',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 180
          },
          {
            prop: 'remark',
            label: '备注',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 170
          }
        ]
      };
    },

    watch: {},
    methods: {
      open() {
        this.visible = true;
      },

      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getTableList({
          pageNum: page,
          size: limit,
          ...where,
          status:2
        });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ pageNum: 1, where: where });
      },
      //新增合同
      async addGenerateContract() {
        let { verify, text, ids } = this.verify();
        if (!verify) {
          this.$message.warning(text);
          return;
        }
        let data = await generateContract(ids);
        return data;
      },
      // 合同验证
      verify() {
        let verify = true;
        let text = '';
        let ids = [];
        let winnerIds = this.selection
          .filter((item) => item.winnerId)
          .map((item) => item.winnerId);
        this.selection.forEach((item) => {
          this.selection.forEach((val) => {
            if (item.winnerId != val.winnerId) {
              verify = false;
              text = '中标供应商不一致';
            }
          });
        });
        if (winnerIds.length != this.selection.length) {
          verify = false;
          text = '核价单中标供应商为空';
        }
        if (verify) {
          ids = this.selection.map((item) => item.id);
        }
        return { verify, text, ids };
      },

      handleClose() {
        this.visible = false;
        this.current = null;
      },

      async selected() {
        if (this.selection.length == 0) {
          return this.$message.warning('请至少选择一条数据');
        }
        let data = await this.addGenerateContract();

        if (data) {
          data.contractVO.contractName=data.contractVO.partbName
          this.$emit('changeParent', {data,sourceCode:this.selection.map(item=>item.inquiryCode).toString()});
          this.handleClose();
        }
      }
    }
  };
</script>

<style lang="scss" scoped>
  .tree_col {
    border: 1px solid #eee;
    padding: 10px 0;
    box-sizing: border-box;
    height: 500px;
    overflow: auto;
  }

  .table_col {
    padding-left: 10px;

    ::v-deep .el-table th.el-table__cell {
      background: #f2f2f2;
    }
  }

  .pagination {
    text-align: right;
    padding: 10px 0;
  }

  .btns {
    text-align: center;
    padding: 10px 0;
  }

  .topsearch {
    margin-bottom: 15px;
  }
</style>
