<template>
  <div>
    <el-dialog
      title="选择项目"
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="main">
        <ele-pro-table
          ref="table"
          :initLoad="false"
          :columns="columns"
          :current.sync="current"
          highlight-current-row
          :datasource="datasource"
          tool-class="ele-toolbar-form"
          cache-key="systemOrgUserTable"
          @row-click="chooseRow"
        >
          <template v-slot:action="{ row }">
            <el-radio class="radio" v-model="radio" :label="row.id"
              ><i></i
            ></el-radio>
          </template>
        </ele-pro-table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleMine">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { getProjectsList } from '@/api/mes';
  import { getByCode } from '@/api/system/dictionary-data';
  export default {
    data() {
      return {
        reviewStatusEnum: [
          { value: 0, label: '未提交' },
          { value: 1, label: '审核中' },
          { value: 2, label: '已审核' },
          { value: 3, label: '审核未通过' }
        ],
        dictList: [],
        dataSource: {},
        dialogVisible: false,
        pages: {
          pageNum: 1,
          size: 10
        },
        total: 0,
        tableData: [],
        current: {},
        radio: null,
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'type',
            label: '项目类型',
            showOverflowTooltip: true,
            minWidth: 150,
            align: 'center',
            slot: 'type'
          },
          {
            prop: 'code',
            label: '项目编码',
            showOverflowTooltip: true,
            minWidth: 160,
            slot: 'code',
            align: 'center'
          },
          {
            prop: 'name',
            label: '项目名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },

          {
            prop: 'responsibleDeptName',
            label: '负责部门',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'responsibleUserName',
            label: '项目经理',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'teamName',
            label: '项目团队',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'cycle',
            label: '项目周期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'budget',
            label: '项目预算',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return cellValue
                ? cellValue +
                    this.getDictV('pro_projects_budget_unit', _row.unit)
                : '';
            }
          },
          {
            prop: 'planStartDate',
            label: '计划开始日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'planEndDate',
            label: '计划完成日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'realStartTime',
            label: '实际开始日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'realEndTime',
            label: '实际完成日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'cost',
            label: '花费(元)',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'status',
            label: '项目状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            slot: 'status'
          },
          {
            prop: 'processStatus',
            label: '审核状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.reviewStatusEnum[_row.processStatus].label;
            }
          },
          {
            prop: 'isOverTime',
            label: '是否超时',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            slot: 'isOverTime'
          },
          {
            prop: 'isOverbudget',
            label: '是否超预算',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            slot: 'isOverbudget'
          },
          {
            prop: 'speedPercent',
            label: '项目进度',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150,
            slot: 'speedPercent'
          },
          {
            columnKey: 'action',
            slot: 'action',
            align: 'center',
            fixed: 'right',
            width: 50
          }
        ]
      };
    },
    methods: {
      async handleMine() {
        if (this.current == null) {
          this.$message.warning('请先选择项目！');
          return;
        }
        this.$emit('success', this.current);
        this.dialogVisible = false;
      },
      getDictV(code, val) {
        if (!this.dictList[code]) return '';
        return this.dictList[code].find((item) => item.value == val)?.label;
      },
      async getDictList(code) {
        let { data: res } = await getByCode(code);
        this.dictList[code] = res.map((item) => {
          let values = Object.keys(item);
          return {
            value: values[0],
            label: item[values[0]]
          };
        });
      },
      async datasource({ page, limit, where, parent }) {
        await this.getDictList('pro_projects_status');
        await this.getDictList('pro_projects_type');
        await this.getDictList('pro_projects_budget_unit');
        return getProjectsList({
          pageNum: page,
          size: limit,
          parentId: parent?.id ?? '0',
          ...where
        });
      },
      async open() {
        this.current = {};
        this.radio = null;
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs.table.reload({
            pageNum: 1,
            size: 10,
            parentId: '0',
            where: {}
          });
        });
      },
      chooseRow(row) {
        this.current = row;
        this.radio = row.id;
      },
      handleClose() {
        this.dialogVisible = false;
      }
    }
  };
</script>
