<template>
  <div class="admin-user">
    <el-form :inline="true">
      <!-- <el-form-item label="账号">
        <el-input v-model="queryParams.username" size="small" placeholder="请输入账号"></el-input>
      </el-form-item> -->
      <el-form-item v-if="$permission('/admin/admin')">
        <el-input v-model="queryParams.name" size="small" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="" size="small" @click="getList2">查询</el-button>
        <el-button type="primary" size="small" @click="handleAdd()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData" border header-row-class-name="custom-table-header">
      <el-table-column type="index" label="#" width="55" align="center"></el-table-column>
      <el-table-column prop="username" label="账号" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="name" label="用户名" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="性别" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ genderObj[scope.row.gender] }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="mobile" label="联系方式" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-dropdown split-button type="primary" size="small" @click="handleEdit(scope.row)">
            编辑
            <el-dropdown-menu slot="dropdown" class="text-align_center">
              <el-dropdown-item @click.native="handleReset(scope.row)">重置密码</el-dropdown-item>
              <el-dropdown-item class="color-red" @click.native="handleDelete(scope.row)" divided>删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination :current-page.sync="queryParams.current" :page-size="queryParams.size" :total="total" @current-change="handleCurrentChange" layout="total, prev, pager, next, jumper" background></el-pagination>
    </div>
    <el-dialog :title="title" :visible.sync="visible" width="700px" :before-close="close">
      <el-form v-if="visible" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" label-position="left">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="账号" prop="username">
              <el-input v-model="ruleForm.username" placeholder="请输入账号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input v-model="ruleForm.password" placeholder="请输入密码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入姓名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="ruleForm.gender" class="width_100" placeholder="请选择">
                <el-option v-for="(item, key) in genderObj" :label="item" :value="parseInt(key)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
      
        </el-row>
        <el-row :gutter="24">
              <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="mobile">
              <el-input v-model="ruleForm.mobile" placeholder="请输入联系方式"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="ruleForm.remark" placeholder="请输入备注" type="textarea" maxlength="100" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {
  },
  data() {
    return {
      visible: false,
      loading: false,
      btnLoading: false,
      total: 0,
      tableData: [],
      title: '',
      queryParams: {
        size: 10,
        current: 1,
        username: '',
        name: ''
      },
      genderObj: {
        0: '女',
        1: '男'
      },
      ruleForm: {},
      rules: {
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' },],
        username: [{ required: true, message: '请输入账号', trigger: 'blur' },],
        gender: [{ required: true, message: '请选择性别', trigger: 'change' },],
      },
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
     //根据条件查询
     getList2(){
        this.queryParams.current=1
        this.getList()
      },
    handleAdd() {
      this.title = '新增系统管理员'
      this.visible = true
      this.ruleForm = {
        gender: 0,
      }
    },
    handleEdit(row) {
      this.title = '编辑系统管理员'
      this.visible = true
      this.ruleForm = { ...row }
    },
    close() {
      this.visible = false
    },
    getList() {
      this.loading = true
      this.$api.getAdminUserList(this.queryParams).then(response => {
        this.tableData = response.data.records
        this.total = response.data.total
        this.loading = false
      }).catch(error => {
        this.loading = false
      })
    },
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true
          if (this.ruleForm.id) {
            this.$api.editAdminUser(this.ruleForm).then(response => {
              this.$message.success('编辑成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          } else {
            this.$api.addAdminUser(this.ruleForm).then(response => {
              this.$message.success('新增成功')
              this.btnLoading = false
              this.getList()
              this.close()
            }).catch(error => {
              this.btnLoading = false
            })
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    handleReset(row) {
      this.$confirm('确认将用户密码重置为123456吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$api.resetAdminUserPassword({ id: row.id }).then(response => {
          this.$message.success('重置成功')
          this.loading = false
          this.getList()
        }).catch(error => {
          this.loading = false
        })
      })
    },
    handleDelete(row) {
      this.$confirm('确认删除本条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$api.deleteAdminUser({ id: row.id }).then(response => {
          this.$message.success('删除成功')
          this.loading = false
          this.getList()
        }).catch(error => {
          this.loading = false
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.admin-user {

}
</style>