<template>
  <div>
    <!-- 数据表格 -->
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      :toolbar="false"
      @row-click="rowClick"
      height="calc(100vh - 265px)"
      full-height="calc(100vh - 116px)"
      tool-class="ele-toolbar-form"
      cache-key="systemOrgUserTable"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar> </template>
      <!-- 角色列 -->
      <template v-slot:roles="{ row }">
        <el-tag
          v-for="item in row.roles"
          :key="item.roleId"
          type="primary"
          size="mini"
          :disable-transitions="true"
        >
          {{ item.roleName }}
        </el-tag>
      </template>
    </ele-pro-table>
    <!-- 编辑弹窗 -->
    <org-user-edit
      :data="current"
      :visible.sync="showEdit"
      :organization-list="organizationList"
      :organization-id="organizationId"
      @done="reload"
    />
  </div>
</template>

<script>
  import OrgUserEdit from './org-user-edit.vue';
  import {
    getUserPage,
    removePersonnel,
    unbindLoginName
  } from '@/api/system/organization';

  export default {
    components: { OrgUserEdit },
    props: {
      // 机构id
      organizationId: [Number, String],
      // 全部机构
      organizationList: Array,
      multipleSelect: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            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 : '';
            }
          },
          {
            prop: 'createTime',
            label: '创建时间',
            sortable: 'custom',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          }
        ],
        // 当前编辑数据
        current: null,

        selectedRecords: [],
        // 是否显示编辑弹窗
        showEdit: false,
        statusOptions: [
          { value: 1, label: '全职' },
          { value: 2, label: '兼职' },
          { value: 3, label: '实习' },
          { value: 4, label: '正式' },
          { value: 5, label: '试用' },
          { value: 6, label: '离职' }
        ]
      };
    },
    methods: {
      rowClick(row) {
        // let arr = this.selectedRecords;
        if (this.multipleSelect) {
          let index = this.selectedRecords.findIndex(
            (item) => item.id === row.id
          );
          if (index < 0) {
            console.log('push');
            this.selectedRecords.push(row);
          } else {
            console.log('remove');
            this.selectedRecords.splice(index, 1);
          }
        } else {
          this.selectedRecords = [row];
        }
        this.$emit('changeList', this.selectedRecords);
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getUserPage({
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          groupId: this.organizationId
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ pageNum: 1, where: where });
      },
      /* 显示编辑 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },

      // 解除绑定
      toUnBind(row) {
        const loading = this.$loading({ lock: true });
        unbindLoginName(row.id)
          .then((res) => {
            loading.close();
            this.$message.success('解绑成功');
            this.reload();
          })
          .catch((e) => {
            loading.close();
            // this.$message.error(e.message);
          });
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        removePersonnel([row.id])
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            // this.$message.error(e.message);
          });
      },
      /* 更改状态 */
      editStatus(row) {
        const loading = this.$loading({ lock: true });
        updateUserStatus(row.userId, row.status)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
          })
          .catch((e) => {
            loading.close();
            row.status = !row.status ? 1 : 0;
            // this.$message.error(e.message);
          });
      }
    },
    watch: {
      // 监听机构id变化
      organizationId() {
        this.reload();
      }
    }
  };
</script>
