<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="关键词" prop="keyword">
        <el-input v-model="queryParams.keyword" placeholder="请输入关键词" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="员工姓名" align="center" prop="userName" width="100" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" width="120" />
      <el-table-column label="部门" align="center" prop="deptName" width="120" />
      <el-table-column label="角色" align="center" prop="roleName" width="120" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==='0'?'success':'danger'" size="small">{{ scope.row.status==='0'?'正常':'停用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)"
            v-hasPermi="['admin:sysUser:query']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:sysUser:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-key" @click="handleResetPwd(scope.row)"
            v-hasPermi="['admin:sysUser:resetPwd']">重置密码</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['admin:sysUser:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 编辑对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入员工姓名" :disabled="true" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApi, getApi, updateApi, delApi, resetPwdApi } from '@/api/sysUser'

export default {
  name: 'SysUser',
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      list: [],
      // 编辑对话框
      open: false,
      title: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: null
      },
      form: {},
      rules: {
        userName: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' }],
        phonenumber: [
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        email: [
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listApi(this.queryParams).then(res => {
        this.list = res.rows || (res.data && res.data.list) || []
        this.total = res.total || (res.data && res.data.total) || 0
        this.loading = false
      }).catch(() => { this.loading = false })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleDetail(row) {
      this.$message.info('详情功能开发中')
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.reset()
      getApi(row.id || row.userId).then(response => {
        const data = response.data || response
        this.form = {
          id: data.id || data.userId,
          userId: data.id || data.userId,
          userName: data.userName,
          phonenumber: data.phonenumber,
          email: data.email,
          status: data.status,
          remark: data.remark
        }
        this.open = true
        this.title = '编辑管理员'
      })
    },
    /** 重置密码操作 */
    handleResetPwd(row) {
      this.$prompt('请输入"' + (row.userName || row.name) + '"的新密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: '密码长度必须介于 5 和 20 之间',
        inputType: 'password'
      }).then(({ value }) => {
        const userId = row.id || row.userId
        resetPwdApi({ userId: userId, password: value }).then(() => {
          this.$modal.msgSuccess('密码重置成功')
        })
      }).catch(() => {})
    },
    /** 提交编辑 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateApi(this.form).then(() => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 取消编辑 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 重置表单 */
    reset() {
      this.form = {
        id: null,
        userId: null,
        userName: null,
        phonenumber: null,
        email: null,
        status: '0',
        remark: null
      }
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除?').then(() => {
        return delApi(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    }
  }
}
</script>
