<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <user-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="pinanUserTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            type="primary"
            size="small"
            class="ele-btn-icon"
            @click="openEdit({})"
          >
            新增
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
          <el-button
            size="small"
            icon="el-icon-upload2"
            class="ele-btn-icon"
            @click="openImport"
          >
            导入
          </el-button>
        </template>
        <!-- 用户名列 -->
        <template v-slot:username="{ row }">
          <div>{{ row.username }}</div>
          <div>{{ row.userCode }}</div>
          <div>{{ row.mobile }}</div>
        </template>
        <template v-slot:team="{ row }">
          <div>{{ row.teamUser.username }}</div>
          <div>{{ row.teamUser.userCode }}</div>
        </template>
        <template v-slot:dep="{ row }">
          <div>{{ row.depUser.username }}</div>
          <div>{{ row.depUser.userCode }}</div>
        </template>
        <template v-slot:area="{ row }">
          <div>{{ row.areaUser.username }}</div>
          <div>{{ row.areaUser.userCode }}</div>
        </template>
        <!-- 状态列 -->
        <template v-slot:status="{ row }">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="row.status"
            @change="editStatus(row)"
          />
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此用户吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <user-edit :visible.sync="showEdit" :data="current" @done="reload" />
    <!-- 导入弹窗 -->
    <user-import :visible.sync="showImport" @done="reload" />
  </div>
</template>

<script>
  import UserImport from './components/user-import.vue';
  import UserSearch from './components/user-search.vue';
  import UserEdit from './components/user-edit.vue';
  import {
    pageUsers,
    removeUser,
    removeUsers,
    updateUserStatus
  } from '@/api/organization/user';

  export default {
    name: 'SystemUser',
    components: {
      UserEdit,
      UserImport,
      UserSearch
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 50,
            align: 'center',
            fixed: 'left'
          },
          {
            prop: 'username',
            label: '姓名工号',
            align: 'center',
            showOverflowTooltip: true,
            width: 180,
            slot: 'username',
            fixed: 'left'
          },
          {
            prop: 'team',
            label: '团队长',
            align: 'center',
            showOverflowTooltip: true,
            width: 180,
            slot: 'team'
          },
          {
            prop: 'dep',
            label: '部经理',
            align: 'center',
            showOverflowTooltip: true,
            width: 180,
            slot: 'dep'
          },
          {
            prop: 'area',
            label: '区经理',
            align: 'center',
            showOverflowTooltip: true,
            width: 180,
            slot: 'area'
          },
          {
            prop: 'centerNumber',
            label: '中心',
            align: 'center',
            width: 120,
            resizable: false,
            showOverflowTooltip: true
          },
          {
            prop: 'orgName',
            label: '归属机构',
            align: 'center',
            width: 80,
            resizable: false,
            showOverflowTooltip: true
          },
          {
            prop: 'cityName',
            label: '归属城市',
            align: 'center',
            width: 100,
            resizable: false,
            showOverflowTooltip: true
          },
          {
            prop: 'typeCn',
            label: '用户角色',
            align: 'center',
            width: 100,
            resizable: false,
            showOverflowTooltip: true
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            sortable: 'custom',
            width: 80,
            resizable: false,
            slot: 'status',
            showOverflowTooltip: true
          },
          {
            prop: 'updateTime',
            label: '更新时间',
            sortable: 'updateTime',
            showOverflowTooltip: true,
            width: 180,
            align: 'center',
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            minWidth: 150,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 是否显示导入弹窗
        showImport: false
      };
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return pageUsers({ ...where, ...order, page, limit });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 打开编辑弹窗 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 打开导入弹窗 */
      openImport() {
        this.showImport = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        removeUser(row.id)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的用户吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            removeUsers(this.selection.map((d) => d.id))
              .then((msg) => {
                loading.close();
                this.$message.success(msg);
                this.reload();
              })
              .catch((e) => {
                loading.close();
                this.$message.error(e.message);
              });
          })
          .catch(() => {});
      },
      /* 更改状态 */
      editStatus(row) {
        const loading = this.$loading({ lock: true });
        updateUserStatus(row.id, 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);
          });
      }
    }
  };
</script>
