<template>
    <div class="container">
        <div class="marginBottom1">
          <el-row>
            <el-col :span="12">
              <el-input class="queryStrName" v-model="queryStr" @input="getuserInfos" placeholder="请输入"><el-button slot="append" icon="el-icon-search" @click="getuserInfos(queryStr)"></el-button></el-input>
            </el-col>
            <el-col :span="12" style="text-align:right">
              <el-button icon="el-icon-refresh" @click="refreshData">刷新</el-button>
              <el-button type="primary" @click="addUser">新增用户</el-button>
            </el-col>
          </el-row>
            
        </div>
      <el-table :data="tableData" style="width: 100%;border-radius:6px;" stripe>
        <el-table-column type="index" label="序号" align="center"> </el-table-column>
        <el-table-column prop="username" label="账号" align="center"> </el-table-column>
        <el-table-column prop="nickname" label="昵称" align="center"> </el-table-column>
        <el-table-column prop="phoneNo" label="手机号" align="center"> </el-table-column>
        <!-- <el-table-column prop="email" label="邮箱" align="center"> </el-table-column> -->
        <el-table-column prop="status" label="状态" align="center">
            <template slot-scope="scope">
              <div style="display:flex;align-items:center;justify-content:center"><div class="spanCircle" :style="{background:scope.row.status?'#2bc644':'#e49334'}"></div><div>{{ scope.row.status?'正常':'封禁' }}</div></div> 
            </template>
        </el-table-column>
        <el-table-column prop="lastLoginAt" label="最后登录时刻" align="center"> </el-table-column>
        <el-table-column  label="操作" align="center"> 
            <template slot-scope="scope">
                <el-button type="text" @click="editItem(scope.row)">编辑</el-button>
                <el-button type="text" style="color:#2e64fa;" @click="resetPassword(scope.row)">重置密码</el-button>
                <el-button type="text" @click="deleteItem(scope.row)" style="color:#f56c6c;">删除</el-button>
            </template>
        </el-table-column>
      </el-table>
      <div class="tablePageBox iflexJustRight" v-show="tableData.length > 0">
        <el-pagination
          @current-change="changePage"
          background
          layout="prev, pager, next"
          :page-size="pageData.pageSize"
          :total="pageData.total"
        >
        </el-pagination>
      </div>
      <el-dialog :title="form.title" :visible.sync="dialogVisible" width="480px">
        <el-form :model="form" ref="form" label-width="80px">
          
          <el-form-item label="昵称" prop="nickname" :rules="[{ required: true, message: '请输入昵称', trigger: 'blur' }]">
            <el-input v-model="form.nickname" size="small"></el-input>
          </el-form-item>
          <el-form-item label="账号" prop="username" :rules="[{ required: true, message: '请输入账号', trigger: 'blur' },{ min: 5, message: '输入至少五位字符', trigger: 'blur' }]">
            <el-input v-model="form.username"  size="small"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="passwordOfNewUser" v-if="!form.edit">
            <el-input v-model="form.passwordOfNewUser"  size="small"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phoneNo" :rules="[{ required: true, message: '请输入手机号', trigger: 'blur' },{ pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }]">
            <el-input v-model="form.phoneNo"  size="small"></el-input>
          </el-form-item>
          <!-- <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" size="small"></el-input>
          </el-form-item> -->
  
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="form.gender">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="2">女</el-radio>
              <el-radio :label="0">未知</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0" :disabled="!form.edit">封禁</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="角色" prop="roleIds"  :rules="[{ required: true, message: '请选择角色', trigger: 'change' }]">
            <el-select :popper-append-to-body="false" v-model="form.roleIds" placeholder="请选择角色" multiple clearable size="small" style="width:360px;">
                <el-option v-for="item in optionData" :label="item.title" :value="item.id" :key="item.id"/>
            </el-select>
          </el-form-item>
        </el-form>
  
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </div>
      </el-dialog>
       <!-- 重置密码 -->
       <el-dialog title="重置密码" width="450px" :visible.sync="editPassWordData.dialogShow">
        <el-form :model="editPassWordData" label-width="90px" :rules="editPassWordDataRules" ref="editPassWordData">
          <el-form-item label="重置方式:">
            <el-radio-group v-model="radiouseDisabled" @input="checkPassword">
              <el-radio :label="1">使用新密码</el-radio>
              <el-radio :label="0">重置为默认密码</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item :label="radiouseDisabled ? '新密码:' : '默认密码:'" prop="randomPassword" ref="defaultPass1">
            <el-input v-model="editPassWordData.randomPassword" :disabled="checkPass" v-show="radiouseDisabled" />
            <p style="font-size: 12px;color:#909399;text-align:left;" v-show="!radiouseDisabled">默认密码：@123456</p>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="editPassWordData.dialogShow = false">取 消</el-button>
          <el-button size="small" style="background:#2e64fa;color:#fff" @click="confirmPassWord">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        tableData: [],
        pageData:{
            pageNum:1,
            pageSize:10,
            total:0,
            current:1,
        },
        dialogVisible: false,
        form: {
            title:"",
            // email:"",
            nickname:"",
            username: '',
            passwordOfNewUser:"",
            phoneNo: '',
            edit:false,
            gender:0,
            status: 1,  // 默认状态
            roleIds:[]
          },
          optionData:[],
          queryStr:"",
          editPassWordData: {
            dialogShow: false,
            id: "",
            username: "",
            randomPassword: "",
          },
          userId:"",
          checkPass: false,
          radiouseDisabled: 1,
          editPassWordDataRules: {
            randomPassword: [
              { required: true, message: '请输入新密码', trigger: 'blur' },
            ],
          },
      };
    },
    mounted(){
        this.getuserInfos();
        this.getRolesList();
    },
    methods:{
        getuserInfos(){
            let {pageNum,pageSize} = this.pageData;
            this.$api.user.getuserInfos({pageNum,pageSize,phoneNo:this.queryStr}).then(res=>{
                if(res.code==200){
                    this.tableData = res.data.records;
                    this.pageData.total = Number(res.data.total);
                    this.pageData.pageSize = Number(res.data.size);
                }
            })
        },
        getRolesList(){
            this.$api.user.getRolesList().then(res=>{
                if(res.code==200){
                    this.optionData = res.data;
                }
            })
        },
        refreshData(){
          this.pageData={
            pageNum:1,
            pageSize:10,
            total:0,
            current:1,
          }
          this.getuserInfos()
        },
        changePage(e) {
            this.pageData.pageNum = e;
            this.getuserInfos();
        },
        addUser(){
            this.form={
                title:"新增用户",
                edit:false,
                // email:"",
                nickname:"",
                username: '',
                passwordOfNewUser:"",
                phoneNo: '',
                gender:0,
                status: 1,  // 默认状态
                roleIds:[]
            }
            this.dialogVisible=true;
            this.$refs.form.resetFields();
        },
        editItem(rowData){
            this.form={
                title:"编辑用户",
                id:rowData.id,
                edit:true,
                // email:rowData.email,
                nickname:rowData.nickname,
                username: rowData.username,
                passwordOfNewUser:rowData.passwordOfNewUser,
                phoneNo: rowData.phoneNo,
                gender:rowData.gender,
                status: rowData.status, // 默认状态
                roleIds:rowData.roleIds
            }
            this.dialogVisible=true;
            if(this.$refs.form!==undefined){
                this.$refs.form.resetFields();
            }
        },
        deleteItem(rowData){
          this.$confirm('您确定删除用户吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            customClass:'page_dialog',
          }).then(() => {
            this.$api.user.deleteUsers({ids:[rowData.id]}).then(res=>{
                if(res.code==200){
                    this.getuserInfos();
                    this.$message.success(res.msg)
                }
            }).catch(err=>console.log(err))
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });          
          });
        },
        closeDialog() {
            this.dialogVisible = false;
            this.resetForm();
        },
        resetForm() {
            this.form={
                title:"",
                edit:false,
                email:"",
                nickname:"",
                username: '',
                passwordOfNewUser:"",
                phoneNo: '',
                gender:0,
                status: 1 
            }
        },
        submitForm(){
            if(this.form.edit){
                this.$api.user.editUsers(this.form).then(res=>{
                    if(res.code==200){
                        this.dialogVisible=false;
                        this.getuserInfos();
                        this.$message.success(res.msg)
                    }
                }).catch(err=>console.log(err))
            }else{
                this.$api.user.addUsers(this.form).then(res=>{
                    if(res.code==200){
                        this.dialogVisible=false;
                        this.getuserInfos();
                        this.$message.success(res.msg)
                    }
                }).catch(err=>console.log(err))
            }
        },
        resetPassword(rowData) {
          this.userId = rowData.id;
          this.editPassWordData.randomPassword = "";
          this.editPassWordData.dialogShow = true;
        },
        checkPassword() {
          this.checkPass = !this.checkPass;
          let error = this.$refs.defaultPass1.$el.querySelector(".el-form-item__error");
          this.editPassWordData.randomPassword = this.radiouseDisabled ? this.editPassWordData.randomPassword : '';
          if (this.checkPass) {
            this.editPassWordDataRules.randomPassword[0].required = false;
            this.editPassWordDataRules.randomPassword[0].message = "";
            if (error) {
              error.style.display = "none";
            }

          } else {
            this.editPassWordDataRules.randomPassword[0].required = true;
            this.editPassWordDataRules.randomPassword[0].message = "请输入新密码";
            if (error) {
              error.style.display = "block";
            }
          }
        },
        confirmPassWord() {
          let param = {
            userId: this.userId,
          };
          param["randomPassword"] = this.editPassWordData.randomPassword;
          this.$api.user.editPassWord(param).then((res) => {
            if (res.code == 200) {
              this.$message.success("重置成功");
            }

          }).catch((err) => {
            console.log(err)
          })
          this.editPassWordData.dialogShow = false;
        },
    }
  };
  </script>
  
  <style scoped lang="scss">
  :deep() .el-table tr th.el-table__cell{
    background: #F0F2F5;
  }
  .container{
    background:#fff;
    padding: 20px;
    border-radius: 10px;
    .marginBottom1{
      margin-bottom: 20px;
      button{
        font-size: 14px;
      }
    }
  }
  .queryStrName{
    width: 228px;
    :deep() .el-input-group__append .el-button.el-button--default{
      width: 72px !important;
    }
  }
  :deep() .el-table__body-wrapper table tbody tr td{
    height: 52px !important;
    padding: 0;
  }
  .tablePageBox{
    margin-top: 20px;
  }
  .spanCircle{
    width: 6px;
    height: 6px;
    border-radius: 50%;
    display: inline-block;
    margin-right: 3px;
  }

  </style>
  