<template>
  <div class="app-container">
    <el-card shadow="never" class="mb16 filter-card">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
      <el-form-item label="管理员名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入管理员名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入登录账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </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-card>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['admin:companyUser:list']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" border size="small" style="width:100%">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="登录账号" align="center" prop="userName" min-width="100" />
      <el-table-column label="管理员名称" align="center" prop="nickName" min-width="100" />
      <el-table-column label="邮箱" align="center" prop="email" min-width="140" show-overflow-tooltip />
      <el-table-column label="手机号" align="center" prop="phonenumber" min-width="110" />
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="140" />
      <el-table-column label="状态" align="center" prop="status" min-width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '0'" type="success" size="mini">正常</el-tag>
          <el-tag v-else type="danger" size="mini">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-lock"
            @click="handleDisable(scope.row)"
            v-hasPermi="['admin:companyUser:edit']"
          >禁用</el-button>
          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-unlock"
            @click="handleEnable(scope.row)"
            v-hasPermi="['admin:companyUser:edit']"
          >启用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleView(scope.row)"
          >详情</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="管理员详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border size="small">
        <el-descriptions-item label="登录账号">{{ viewForm.userName }}</el-descriptions-item>
        <el-descriptions-item label="管理员名称">{{ viewForm.nickName }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ viewForm.email || '-' }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ viewForm.phonenumber || '-' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ viewForm.createTime }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag v-if="viewForm.status == '0'" type="success" size="mini">正常</el-tag>
          <el-tag v-else type="danger" size="mini">停用</el-tag>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listAdminUsers, getAdminUserInfo, disableAdminUser, enableAdminUser, exportAdminUser } from '@/api/admin/sysUser'

export default {
  name: 'SysUserAdmin',
  data() {
    return {
      loading: true,
      exportLoading: false,
      showSearch: true,
      total: 0,
      userList: [],
      statusOptions: [
        { value: '0', label: '正常' },
        { value: '1', label: '停用' }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        userName: null,
        status: null
      },
      viewOpen: false,
      viewForm: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listAdminUsers(this.queryParams).then(response => {
        this.userList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleView(row) {
      getAdminUserInfo(row.userId).then(response => {
        this.viewForm = response.data
        this.viewOpen = true
      })
    },
    handleDisable(row) {
      this.$confirm('确定禁用管理员 [' + row.nickName + '] 吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        disableAdminUser(row.userId).then(res => {
          if (res.code === 200) {
            this.$message.success('禁用成功')
            this.getList()
          } else {
            this.$message.error(res.msg || '禁用失败')
          }
        })
      })
    },
    handleEnable(row) {
      this.$confirm('确定启用管理员 [' + row.nickName + '] 吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(() => {
        enableAdminUser(row.userId).then(res => {
          if (res.code === 200) {
            this.$message.success('启用成功')
            this.getList()
          } else {
            this.$message.error(res.msg || '启用失败')
          }
        })
      })
    },
    handleExport() {
      this.exportLoading = true
      exportAdminUser(this.queryParams).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => { this.exportLoading = false })
    }
  }
}
</script>

<style scoped>
.mb8 { margin-bottom: 8px; }
.mb16 { margin-bottom: 16px; }
.filter-card { padding-bottom: 0; }
</style>
