<template>
  <div class="user-management-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            clearable
            placeholder="请输入部门名称"
            prefix-icon="el-icon-search"
            size="small"
            style="margin-bottom: 20px"
            @keyup.enter.native="getTreeselect" />
        </div>
        <div class="head-container">
          <el-tree
            ref="tree"
            :data="deptOptions"
            default-expand-all
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            :props="defaultProps"
            @node-click="handleNodeClick" />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <vab-query-form>
          <vab-query-form-left-panel :span="12">
            <el-button
              v-permissions="['system:user:add']"
              icon="el-icon-plus"
              type="primary"
              @click="handleEdit($event)">
              添加
            </el-button>
            <el-button
              v-permissions="['system:user:delete']"
              icon="el-icon-delete"
              type="danger"
              @click="handleDelete($event)">
              删除
            </el-button>
          </vab-query-form-left-panel>
          <vab-query-form-right-panel :span="12">
            <el-form :inline="true" :model="queryForm" @submit.native.prevent>
              <el-form-item>
                <el-input
                  v-model.trim="queryForm.keyWords"
                  clearable
                  placeholder="请输入用户名"
                  @keyup.enter.native="queryData" />
              </el-form-item>
              <el-form-item>
                <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
              </el-form-item>
            </el-form>
          </vab-query-form-right-panel>
        </vab-query-form>

        <el-table v-loading="listLoading" :data="list" :height="height" @selection-change="setSelectRows">
          <el-table-column align="center" show-overflow-tooltip type="selection" />
          <!--      <el-table-column align="center" label="序号" width="55">
            <template #default="{ $index }">
              {{ $index + 1 }}
            </template>
          </el-table-column>-->
          <el-table-column v-permissions="['SysAdmin']" align="center" label="id" prop="id" width="60" />
          <el-table-column align="center" label="用户名" prop="userName" show-overflow-tooltip />
          <el-table-column align="center" label="昵称" prop="nickName" show-overflow-tooltip />
          <el-table-column align="center" label="手机号" prop="phone" show-overflow-tooltip />
          <el-table-column align="center" label="邮箱" prop="email" show-overflow-tooltip />
          <el-table-column align="center" label="部门" prop="deptName" show-overflow-tooltip />
          <!--          <el-table-column align="center" label="角色" show-overflow-tooltip>-->
          <!--            <template #default="{ row }">-->
          <!--              <el-tag v-for="(item, index) in row.roles" :key="index">-->
          <!--                {{ item }}-->
          <!--              </el-tag>-->
          <!--            </template>-->
          <!--          </el-table-column>-->

          <el-table-column align="center" label="创建时间" prop="createdTime">
            <template #default="scope">
              <span>{{ parseTime(scope.row.createdTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="账号状态" prop="status">
            <template #default="scope">
              <span>{{ selectDictLabel(statusOptions, scope.row.status) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" show-overflow-tooltip width="120">
            <template #default="{ row }">
              <el-button v-permissions="['system:user:resetPwd']" type="text" @click="handleResetPwd(row)">
                重置
              </el-button>
              <el-button v-permissions="['system:user:edit']" type="text" @click="handleEdit(row)">编辑</el-button>
              <el-button v-permissions="['system:user:delete']" type="text" @click="handleDelete(row)">删除</el-button>
            </template>
          </el-table-column>
          <template #empty>
            <el-image class="vab-data-empty" :src="require('@/assets/empty_images/data_empty.png')" />
          </template>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNum"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange" />
      </el-col>
    </el-row>
    <edit ref="edit" @fetch-data="fetchData" />
    <rest-pwd ref="restPwd" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import deptApi from '@/api/system/dept'
  import userApi from '@/api/system/user'
  import Edit from './components/UserEdit'
  import RestPwd from './components/RestPwd'

  export default {
    name: 'User',
    components: { Edit, RestPwd },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          userName: '',
        },
        deptName: '',
        // 部门树选项
        deptOptions: undefined,
        defaultProps: {
          id: 'id',
          children: 'children',
          label: 'deptName',
        },
        // 状态数据字典
        statusOptions: [],
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight(1)
      },
    },
    created() {
      this.fetchData()
      this.getTreeselect()
      this.getDicts('sys_normal_disable').then((response) => {
        this.statusOptions = response.data.values || []
      })
    },
    methods: {
      /** 查询部门下拉树结构 */
      async getTreeselect() {
        const { data: data } = await deptApi.getTree({
          deptName: this.deptName,
        })
        this.deptOptions = data
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true
        return data[this.defaultProps.label].indexOf(value) !== -1
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryForm.deptId = data.id
        this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleResetPwd(row) {
        this.$refs['restPwd'].showEdit(row)
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await userApi.doDelete({ ids: [row.id] })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await userApi.doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, total },
        } = await userApi.getList(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
