<template>
    <div class="personManagement">
        <div class="personBtn">
            <p class="titleSty">用户管理</p>
            <div class="periodSeach">
                <div class="exceed">
                    <span style="margin-top: 3px;">登录名称：</span>
                    <el-input v-model="loginWh" placeholder="请输入">
                    </el-input>
                </div>
                <el-button type="primary" style="margin-left: 10px;" @click="getSeachData">搜索</el-button>
                <el-button style="margin-left: 10px;" @click="reset">重置</el-button>
            </div>
            <div style="display:flex;justify-content: space-between;padding: 10px;">
                <el-row :gutter="10" class="regionalBtn">
                    <el-col :span="1.5">
                        <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        size="mini"
                        @click="handleAdd"
                        >新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="success"
                        plain
                        icon="Edit"
                        size="mini"
                        @click="handleUpdate"
                        >修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        size="mini"
                        @click="handleDelete"
                        >删除</el-button>
                    </el-col>
                </el-row>
            </div>
            <div class="personTableData">
                <el-table :data="personTableData" border style="width: 100%" @row-click="rowClick">
                    <el-table-column label="操作" width="60" align="center">
                        <template #default="scope">
                            <el-radio v-model="personradio" :label="scope.row"></el-radio>
                        </template>
                    </el-table-column>
                    <el-table-column label="用户名称" prop="userName">
                    </el-table-column>
                    <el-table-column label="角色名称" prop="roleName">
                    </el-table-column>
                    <el-table-column label="登录名称" prop="loginName">
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createTime">
                    </el-table-column>
                </el-table>
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="page.currentPage"
                    :page-size="page.pagesize"
                    layout="total, prev, pager, next, jumper"
                    :total="page.total">
                </el-pagination>
            </div>
        </div>
        <el-dialog :title="title" v-model="dialogVisible" width="600px" :close-on-click-modal="false" @close="closeDialog">
            <div class="windframFrom">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="用户名称" prop="person">
                        <el-input v-model="ruleForm.person" placeholder="请输入用户名称">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="角色名称" prop="role">
                        <el-select v-model="ruleForm.role" placeholder="请选择角色名称">
                            <el-option
                            v-for="item in rolesOption"
                            :key="item.roleId"
                            :label="item.roleName"
                            :value="item.roleId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="登录名称" prop="loginName">
                        <el-input v-model="ruleForm.loginName" placeholder="请输入登录名称">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="登录密码" prop="password" v-if="!single">
                        <el-input v-model="ruleForm.password" placeholder="请输入登录密码">
                        </el-input>
                    </el-form-item>
                </el-form>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="resetForm" v-if="!single">重 置</el-button>
                    <el-button @click="dialogVisible = false" v-else>取 消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import {apiGetgetPersonList, apiGetgetaddPersonMsg, apiGetgetuploadPersonMsg, apiGetgetdeletePersonMsg, apiGetgetRoleList} from '../../api/api'
export default {
    data() {
        return {
            title:'',
            personTableData:[],
            personradio: {},
            single: false,
            dialogVisible:false,
            loginWh: '',
            typeWh: '',
            rolesOption: [],
            page:{
                pagesize: 10,
                currentPage: 1,
                total: 0
            },
            ruleForm: {
                person: '',
                role: '',
                loginName: '',
                password: ''
            },
            rules: {
                person: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' }
                ],
                role: [
                    { required: true, message: '请输入角色名称', trigger: 'change' }
                ],
                loginName: [
                    { required: true, message: '请输入登录名称', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入登录密码', trigger: 'blur' }
                ]
            }
        }
    },
    mounted() {
        this.getPersonListFn()
        this.getAllRolesData()
    },
    methods:{
        //获取所有角色
        getAllRolesData() {
            let params = {
                pageNum: 1,
                pageSize: 20
            }
            apiGetgetRoleList(params).then(datas =>{
                if (datas && datas.rows) {
                    this.rolesOption = datas.rows
                }
            })
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getPersonListFn('seach')
        },
        reset() {
            this.loginWh = ''
            this.page = {
                pagesize: 10,
                currentPage: 1,
                total: 0
            }
            this.getPersonListFn()
        },
        handleSizeChange(val){
            this.page.pagesize = val
            this.getPersonListFn('seach')
        },
        handleCurrentChange(val){
            this.page.currentPage =val
            this.getPersonListFn('seach')
        },
        handleChange(file, fileList) {
            this.ruleForm.fileList = fileList.slice(-3);
        },
        closeDialog() {
            this.$refs['ruleForm'].clearValidate()
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let that = this
                    that.addOrUpdatewindframDate()
                }
            });
        },
        // 新增/修改风电场配置
        addOrUpdatewindframDate() {
            let that = this
            let params = {
                loginName: that.ruleForm.loginName,
                roleId: that.ruleForm.role,
                password: that.ruleForm.password,
                userName: that.ruleForm.person
            }
            if (!that.single) {
                apiGetgetaddPersonMsg(params).then(datas =>{
                    that.changeshowData(datas)
                })
            } else {
                delete params.password
                delete params.loginName
                params.userId = that.personradio.userId
                apiGetgetuploadPersonMsg(params).then(datas =>{
                    that.changeshowData(datas)
                })
            }
        },
        changeshowData(datas) {
            if (datas.msg === '操作成功') {
                this.dialogVisible = false
                if (!this.single) {
                    this.$message({
                        message: '用户新增成功',
                        type: 'success'
                    });
                } else {
                    this.$message({
                        message: '用户修改成功',
                        type: 'success'
                    });
                }
                this.single = false
                this.reset()
            }
        },
        resetForm() {
            this.ruleForm = {
                person: '',
                role: '',
                loginName: '',
                password: ''
            }
        },
        // 新增区域信息
        handleAdd() {
            this.dialogVisible = true
            this.single = false
            this.title = '新增用户'
            this.$nextTick(() =>{
                this.resetForm()
                this.$refs['ruleForm'].clearValidate()
            })
        },
        // 修改区域信息
        handleUpdate() {
            this.dialogVisible = true
            this.single = true
            this.title = '修改用户'
            this.ruleForm = {
                person: this.personradio.userName,
                role: this.personradio.roleId,
                loginName: this.personradio.loginName,
                password: ''
            }
        },
        rowClick(row, column, event) {
            this.personradio = row
        },
        // 删除区域信息
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let params = {
                    userId: that.personradio.userId
                }
                apiGetgetdeletePersonMsg(params).then(datas =>{
                    if (datas.msg === '操作成功') {
                        that.getPersonListFn()
                    }
                })
                if (that.personTableData.length>0) {
                    that.personradio = that.personTableData[0]
                }
                that.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            })
        },
        //获取风电场列表数据
        getPersonListFn(type) {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize
            }
            if (type) {
                params.loginName = this.loginWh
            }
            apiGetgetPersonList(params).then(datas =>{
                if (datas && datas.rows) {
                    that.personTableData = datas.rows
                    that.personradio = datas.rows[0]
                    that.page.total = datas.total
                }
            })
        }
    }
}
</script>

<style lang="less">
.personManagement{
    .personBtn{
        background: #fff;
        padding: 10px 20px 0 20px;
        .titleSty{
            border-left: 4px solid #2996da;
            padding-left: 5px;
            font-size: 14px;
            margin-left:20px;
        }
        .periodSeach{
            display: flex;
            margin:20px;
                .exceed{
                    display: flex;
                    margin-right:20px;
                    .el-input{
                        width: 200px;
                        margin-right:10px;
                        .el-input__inner{
                            height:30px;
                        }
                        .el-input__suffix{
                            .el-select__caret{
                                line-height:30px;
                            }
                        }
                    }
                }
        }
        span{
            font-size:14px;
        }
        .regionalBtn{
            padding-left: 10px;
        }

        .exceed{
            display: flex;
            margin-right:20px;
            .exTime{
                width: 240px;
                .el-select__tags{
                    left: 1px;
                    background:#fff;
                }
            }
        }
        .el-select{
            width: 160px;
            margin-right:10px;
            .el-input__inner{
                height:30px;
            }
            .el-input__suffix{
                .el-select__caret{
                    line-height:30px;
                }
            }
        }
        .el-button{
            height: 30px;
            // width:100px;
            padding: 0 30px ;
            // padding-top: 8px;
            span{
                margin:0;
            }
        }
    }
    .personTableData{
        padding: 10px 20px;
        .el-table{
        .el-table__body-wrapper{
            .el-table__empty-block{
            height: 550px !important;
            }
        }
        .el-input__inner{
            height: 30px !important;
        }
        .el-radio__label{
            display: none;
        }
        }
        .el-pagination{
            margin-top: 20px;
            text-align: end;
            position: relative;
        }
    }
    .el-overlay{
            .el-dialog{
                .el-dialog__body{
                    padding: 30px 60px 30px 20px !important;
                    .windframFrom{
                        padding-right: 20px !important;
                        .el-select{
                            width: 100%;
                        }
                    }
                }
            }
        }
}
</style>