<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    top="5vh"
    :close-on-press-escape="false"
    append-to-body
    width="80%"
  >
    <el-card shadow="never">
      <ele-split-layout
        width="244px"
        allow-collapse
        :right-style="{ overflow: 'hidden' }"
      >
        <div class="ele-border-lighter split-layout-right-content">
          <DepartmentTree
            @handleNodeClick="handleNodeClick"
            :isFirstRefreshTable="false"
            ref="treeList"
          />
        </div>
        <!-- 表格 -->
        <template v-slot:content>
          <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.id"
                ><i></i
              ></el-radio>
            </template>
          </ele-pro-table>
        </template>
      </ele-split-layout>
    </el-card>

    <div class="btns">
      <el-button type="primary" size="small" @click="selected">选择</el-button>
      <el-button size="small" @click="handleClose">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getUserPage } from '@/api/system/organization';
  import DepartmentTree from '@/components/DepartmentTree';
  export default {
    components: {
      DepartmentTree
    },

    data() {
      return {
        currentIndex: 0,
        title:'选择负责人',
        visible: false,
        statusOptions: [
          { value: 1, label: '全职' },
          { value: 2, label: '兼职' },
          { value: 3, label: '实习' },
          { value: 4, label: '正式' },
          { value: 5, label: '试用' },
          { value: 6, label: '离职' }
        ],
        columns: [
          {
            action: 'action',
            slot: 'action',
            align: 'center',
            label: '选择'
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'name',
            label: '姓名',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'jobNumber',
            label: '工号',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'loginName',
            label: '用户账号',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'sex',
            label: '性别',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 80,
            formatter: (_row, _column, cellValue) => {
              return cellValue == 1 ? '男' : cellValue == 2 ? '女' : '';
            }
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            sortable: 'custom',
            width: 80,
            formatter: (_row, _column, cellValue) => {
              const dom = this.statusOptions.find((item) => {
                return item.value == cellValue;
              });
              return dom ? dom.label : '';
            }
          }
        ],

        radio: null
      };
    },

    watch: {},
    methods: {
      open(item, index = 0, title) {
        if (item) {
          this.radio = item.id;
        }
        this.currentIndex = index;
        if (title) {
          this.title = title;
          this.radio = '';

        }
       
        this.visible = true;
      },

      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getUserPage({
          pageNum: page,
          size: limit,
          ...where
        });
      },

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

      handleNodeClick(data, node) {
        this.curNodeData = data;
        this.reload({ groupId: data.id });
      },

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

      selected() {
        if (!this.current) {
          return this.$message.warning('请至少选择一项');
        }
        this.$emit('changeParent', this.current, this.currentIndex,this.title);
        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>
