<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%"
  >
    <search-quotation @search="reload"></search-quotation>
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      row-key="id"
      height="calc(100vh - 450px)"
      default-expand-all
      :tree-props="{children: 'sonPurchasePlanList', hasChildren: 'hasChildren'}"
      class="dict-table"
    >
<!--       @cell-click="cellClick"-->
      <!-- 表头工具栏 -->
      <template v-slot:action="{ row }">
        <el-radio v-if="(!row.isCut||(row.parentId&&row.parentId!=0))" class="radio" v-model="radio" @change="cellClick(row)" :label="row.planCode"><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,
  isHasGeneratedContractPI,
  isHasGeneratedInquiryAPI
} from '@/api/purchasingManage/purchasePlanManage';
import searchQuotation from './searchQuotation.vue';

export default {
  components: {
    searchQuotation
  },
  props: {
    generatedType: {
      type: String,
      default: ''
    },
  },

  data() {
    return {
      visible: false,
      currentIndex: null,
      list: [],
      columns: [
        {
          action: 'action',
          slot: 'action',
          align: 'center',
          label: '选择',
          fixed: 'left',
          width: 50,
        },
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'planCode',
          slot: 'planCode',
          label: '采购计划编码',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'planName',
          slot: 'planName',
          label: '采购计划名称',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'requirementCode',
          label: '采购需求编码',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'requireDeptName',
          label: '需求部门',
          align: 'center',
          slot: 'requireDeptName',
          showOverflowTooltip: true,
          minWidth: 200
        },
        {
          prop: 'requireUserName',
          label: '需求人',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },
        {
          prop: 'responsibleName',
          label: '负责人',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 120
        },
        {
          prop: 'detailCount',
          label: '明细条数',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },

        {
          prop: 'finishDate',
          label: '完成日期',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 140
        },
        {
          prop: 'createTime',
          label: '创建时间',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 180
        },
        {
          prop: 'status',
          label: '状态',
          align: 'center',
          slot: 'status',
          showOverflowTooltip: true,
          minWidth: 120
        },
        {
          prop: 'remark',
          label: '备注',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 170
        }
      ],

      radio: null
    };
  },
  watch: {},
  methods: {
    open(planCode) {
      //数据回显
      if (planCode) {
        this.radio = planCode;
        if (this.list.length > 0) {
          this.current = this.list.find(
            (item) => item.planCode == this.radio
          );
        }
      }
      this.visible = true;
    },
    /* 表格数据源 */
    datasource({page, limit, where, order}) {
      return getTableList({
        pageNum: page,
        size: limit,
        ...where
      }).then((res) => {
        this.current = res.list.find((item) => item.planCode == 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.planCode});
    },

    // 单击获取id
    cellClick(row) {
      this.current = row;
      this.radio = row.planCode;
    },
    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('请至少选择一条数据');
      }
      this.$emit('changeInquiryManageList', this.current);
      switch (this.generatedType) {
        case 'inquiry':
          //是否生成过核价单
          let res = await isHasGeneratedInquiryAPI(this.current.id)
          if (res) this.$message.warning('该计划已生成过核价单，请注意。')
          break;
        case 'contract':
          //是否生成过合同
          let res1 = await isHasGeneratedContractPI(this.current.id)
          if (res1) this.$message.warning('该计划已生成过合同，请注意。')
          break;
      }

      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;
}
::v-deep .el-table__row--level-1 {
  background: #e2f1ff61;
}

::v-deep .el-table__row--level-0 {
  background: rgb(187 216 243);
}
</style>
