<template>
  <el-dialog
    title="选择项目"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    top="5vh"
    v-if="visible"
    :close-on-press-escape="false"
    append-to-body
    width="70%"
  >
    <el-card shadow="never">
      <index-search @search="reload"/>

      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        row-key="id"
        height="calc(100vh - 500px)"
        class="dict-table"
        @cell-click="cellClick"
      >
        <!-- 表头工具栏 -->
        <template v-slot:action="{ row }">
          <el-radio class="radio" v-model="radio" :label="row.id"
            ><i></i
          ></el-radio>
        </template>
        <template v-slot:status="{ row }">
          {{ getDictValue('项目状态', row.status) }}
        </template>
        <template v-slot:type="{ row }">
          {{ getDictValue('项目类型', row.type) }}
        </template>
      </ele-pro-table>
    </el-card>

    <div class="btns" 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 { projectsPageAPI } from '@/api/pro';
import dictMixins from '@/mixins/dictMixins';
import indexSearch from "./project-search.vue";
export default {
  components: {indexSearch},
  mixins: [dictMixins],
  props: {
    contactData: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      visible: false,
      columns: [
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          action: 'action',
          slot: 'action',
          align: 'center',
          label: '选择'
        },

        {
          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.getDictValue('预算单位', _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'
        }
      ],

      radio: null
    };
  },
  created() {
    this.requestDict('项目状态');
    this.requestDict('项目类型');
    this.requestDict('预算单位');
  },
  methods: {
    open(item) {
      if (item && item.id) {
        this.radio = item.id;
      }
      this.visible = true;
    },

    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      if (this.contactData.id) {
        where['contactId'] = this.contactData.id;
      }
      let params = {
        pageNum: page,
        size: limit,
        ...where,
        parentId: 0,
        processStatus: ['2']
      };
      return projectsPageAPI(params);
    },

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

    // 单击获取id
    cellClick(row) {
      this.current = row;
      console.log(row);
      this.radio = row.id;
    },
    handleClose() {
      this.visible = false;
      this.current = null;
      this.radio = '';
    },

    async selected() {
      if (!this.current) {
        return this.$message.warning('请至少选择一条数据');
      }

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