<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="70%"
    v-if="visible"
  >
    <el-card shadow="never">
      <searchProduct @search="reload"></searchProduct>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="eomContactPageTable"
        height="calc(100vh - 480px)"
        class="dict-table"
      >
        <template v-slot:action="{ row }">
          <el-radio v-model="radio" @change="cellClick(row)" :label="row.id"><i></i></el-radio>
        </template>
      </ele-pro-table>
    </el-card>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submit">确定
      </el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
    <generate-order-dialog :generate-order-dialog-flag.sync="generateOrderDialogFlag" v-if="generateOrderDialogFlag"
                           ref="generateOrderDialogRef" @changeParent="getGoodsListData"></generate-order-dialog>
  </el-dialog>
</template>

<script>
import {generateContract, getTableList, isHasGeneratedContractPI} from '@/api/purchasingManage/inquiryManage';
import searchProduct from '@/views/purchasingManage/inquiryManage/components/searchIndex.vue';
import generateOrderDialog from "@/views/purchasingManage/purchaseOrder/components/generateOrderDialog.vue";

export default {
  components: {
    generateOrderDialog,
    searchProduct
  },

  data() {
    return {
      visible: false,
      generateOrderDialogFlag: false,
      currentIndex: null,
      radio: null,
      selection: [],
      current: {},
      sourceType:'',
      columns: [
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'action',
          slot: 'action',
          label: '选择',
          align: 'center',
          minWidth: 100
        },
        {
          prop: 'inquiryName',
          slot: 'inquiryName',
          label: '采购询价名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          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: 'winnerSupples',
          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(sourceType) {
      this.sourceType=sourceType
      this.visible = true;
    },

    /* 表格数据源 */
    datasource({page, limit, where, order}) {
      if(this.sourceType){
        where['sourceType']=this.sourceType
      }
      return getTableList({
        pageNum: page,
        size: limit,
        ...where,
        status: 2
      });
    },
// 单击获取id
    cellClick(row) {
      this.current = row;
      this.radio = row.id;
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({pageNum: 1, where: where});
    },
    //
    async submit() {
      console.log(111111);
      if (this.current?.winnerSuppleIds.split(',').length>1) {
        this.generateOrderDialogFlag = true;
        this.$nextTick(() => {
          this.$refs.generateOrderDialogRef.open(this.current);
        })
      } else {
        this.$emit('changeParent',this.current)
        this.handleClose();
      }
    },
    getGoodsListData(val){
      let params = {
        id:this.current.id,
        inquiryCode:this.current.inquiryCode,
        inquiryName:this.current.inquiryName,
        winnerSuppleIds:val.supplierId,
      }
     this.$emit('changeParent', params)
      this.handleClose();
    },
    async setContractInfo(data) {
      if (data) {
        data.contractVO.contractName = data.contractVO.partbName
        //获取优惠金额和总计的差价
        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)
        })
        let sourceId = this.selection.map(item => item.id).join(',')
        let sourceName = this.selection.map(item => item.inquiryName).join(',');
        let winnerSupples = this.selection.map(item => item.winnerSupples).join(',');
        this.$emit('changeParent', {data, sourceName, sourceId, winnerSupples});
        this.handleClose();
      }
    },

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


  }
};
</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>
