<template>
    <div class="app-container">
        <el-table :data="tableData" style="width: 100%" header-row-class-name="">
            <el-table-column prop="loginName" label="用户名"></el-table-column>
            <el-table-column prop="userName" label="姓名"></el-table-column>
            <el-table-column prop="phone" label="联系电话"></el-table-column>
            
            <el-table-column prop="roleId" label="用户级别"></el-table-column>
            <el-table-column prop="operateTime" label="操作时间"></el-table-column>
            <el-table-column prop="enIdText" label="操作人"></el-table-column>
            <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="resetPassDialog(scope.row.id)">重置密码</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            layout="total,  prev, pager, next, sizes, jumper"
            :total="total">
        </el-pagination>
        <el-dialog title="重置密码" :visible.sync="dialogResetPass" width="500px">
        <el-form :model="formPass" :rules="rulesPass" ref="passForm">
            <el-form-item label="用户名：" :label-width="formLabelWidth">
            <span>{{ formPass.account }}</span>
            </el-form-item>
            <el-form-item label="密码：" :label-width="formLabelWidth" prop="password">
            <el-input type="password" v-model="formPass.password" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" size="mini" @click="submitForm('passForm')">保存</el-button>
            <el-button type="info" size="mini" @click="resetForm('passForm'),dialogResetPass = false">取消</el-button>
        </div>
        </el-dialog>
        
    </div>
</template>
<script>
import {
    getUserList,resetPass
} from "@/api/user"
export default {
    name: "userManage",
    data() {
        return {
            currentPage: 1,                      //当前页
            pageSize: 10,                        //每页条数
            total: 0,                            //总条数
            dialogResetPass: false,              //重置密码的Dialog
            formLabelWidth: '130px',             //form的lable的宽带
            formPass: {                          //重置密码的form
                id: '',
                password: '',
                version: '',
                account: '',
            },
            rulesPass: {                         //重置密码的校验规则
                password: [
                    {required: true, message: '请输入新密码', trigger: 'blur'},
                    {min: 8, max: 12, message: '长度在 8 到 12 个字符', trigger: 'blur'}
                ],
            },
            tableData: [],                      //表格数据
        };
    },
    mounted() {
        this.initData()
    },
    methods: {
        //搜索查询
        search() {
            this.currentPage = 1
            this.initData()
        },
        //获取用户列表
        initData() {
            const params = {
                pageNo: this.currentPage,
                pageSize: this.pageSize
            }
            getUserList(params).then(res => {
                this.tableData = res.data.list
                this.total = res.data.total
            })
        },
        resetPassDialog(id){
            // this.dialogResetPass = true
            this.$confirm('此操作将重置改用户密码, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                resetPass({userId:id}).then(res => {
                    this.$message({
                        message: '重置成功！',
                        type: 'success'
                    });
                    this.initData()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        //重置密码保存
        resetPassword() {
            let params = JSON.parse(JSON.stringify(this.formPass)) 
            params.password = this.$md5(params.password)
            resetPass(params).then(res => {
                this.$message({
                    message: '密码重置成功！',
                    type: 'success'
                });
                this.dialogResetPass = false
            })
        },
        //form提交检验
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.resetPassword()

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //form重置
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        //每页条数变化
        handleSizeChange(val) {
            this.pageSize = val
            this.initData()
        },
        //当前页变化
        handleCurrentChange(val) {
            this.currentPage = val
            this.initData()
        }

    }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.input-with-select {
    float: left;
    top: 10px;
}

.el-input {
    width: 230px;
}
</style>
