<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
    v-if="visible"
    width="70%"
  >
    <search-quotation @search="reload"> </search-quotation>
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      row-key="id"
      height="calc(100vh - 350px)"
      class="dict-table"
      @cell-click="cellClick"
    >
      <!-- 表头工具栏 -->
      <template v-slot:action="{ row }">
        <el-radio class="radio" v-model="radio" :label="row.requirementCode"
          ><i></i
        ></el-radio>
      </template>
      <!-- 状态 -->
      <template v-slot:status="{ row }">
        {{ getStatus(row.status) }}
      </template>
    </ele-pro-table>

    <div slot="footer">
      <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, isHasGeneratedPlanAPI} from '@/api/purchasingManage/purchaseNeedManage';
  import searchQuotation from '@/views/purchasingManage/purchaseNeedManage/components/searchQuotation.vue';
import {isHasGeneratedContractPI, isHasGeneratedInquiryAPI} from "@/api/purchasingManage/purchasePlanManage";
  export default {
    components: {
      searchQuotation
    },
    props:{
      generatedType:{
        type:String,
        default:''
      }
    },
    data() {
      return {
        visible: false,
        currentIndex: null,
        list: [],
        columns: [
          {
            action: 'action',
            slot: 'action',
            align: 'center',
            label: '选择'
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'requirementName',
            slot: 'requirementName',
            label: '需求单名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'productNames',
            label: '产品名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'sourceTypeName',
            label: '需求类型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'requireDeptName',
            label: '需求部门',
            align: 'center',
            slot: 'requireDeptName',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'requireUserName',
            label: '需求人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },

          {
            prop: 'requirementCode',
            label: '需求编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200
          },
          {
            prop: 'finishDate',
            label: '完成日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            slot: 'status',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'remark',
            label: '备注',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 170
          }
        ],
        sourceType:'',
        radio: null
      };
    },
    watch: {},
    methods: {
      open(requirementCode,sourceType) {
        this.sourceType=sourceType
        //数据回显
        if (requirementCode) {
          this.radio = requirementCode;
          if (this.list.length > 0) {
            this.current = this.list.find(
              (item) => item.requirementCode == this.radio
            );
          }
        }
        this.visible = true;
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        if(this.sourceType){
        where['sourceType']=this.sourceType
      }
        return getTableList({
          pageNum: page,
          size: limit,
          ...where,
          status:2
        }).then((res) => {
          this.current = res.list.find(
            (item) => item.requirementCode == this.radio
          );
          this.list = res.list;

          return res;
        });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ pageNum: 1, where: where });
      },

      handleNodeClick(data, node) {
        this.curNodeData = data;
        this.reload({ categoryLevelId: data.requirementCode });
      },

      // 单击获取id
      cellClick(row) {
        this.current = row;
        this.radio = row.requirementCode;
      },
      handleClose() {
        this.visible = false;
        this.current = null;
        this.radio = '';
      },
      //获取状态
      getStatus(status) {
        return status == 0
          ? '未提交'
          : status == 1
          ? '审核中'
          : status == 2
          ? '审核通过'
          : status == 3
          ? '审核不通过'
          : '';
      },
     async selected() {
        if (!this.current) {
          return this.$message.warning('请至少选择一条数据');
        }

       switch (this.generatedType) {
         case 'plan':
           //需求是否生成过计划
           let res =  await isHasGeneratedPlanAPI(this.current.id)
           if(res) this.$message.warning('该需求已经生成过计划，请注意')
           break;

       }
        this.$emit('changeParent', this.current);
        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>
