<template>
    <div class="personRoleManagement">
        <div class="personRoleBtn">
            <p class="titleSty">角色管理</p>
            <div class="periodSeach">
                <div class="exceed">
                    <span style="margin-top: 3px;">角色名称：</span>
                    <el-input v-model="roleName" 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="personRoleTableData">
                <el-table :data="personRoleTableData" border style="width: 100%" @row-click="rowClick">
                    <el-table-column label="操作" width="60" align="center">
                        <template #default="scope">
                            <el-radio v-model="personRoleradio" :label="scope.row"></el-radio>
                        </template>
                    </el-table-column>
                    <el-table-column label="角色名称" prop="roleName">
                    </el-table-column>
                    <el-table-column label="菜单列表" prop="roleName">
                        <template #default="scope">
                            <el-tag size="small" v-for="(it,index) in scope.row.menus.split(',')" :key="index">{{it}}</el-tag>
                        </template>
                    </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="windframFromsxx">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="角色名称" prop="roles">
                        <el-input v-model="ruleForm.roles" placeholder="请输入角色名称">
                        </el-input>
                    </el-form-item>
                    <el-form-item label="菜单列表" prop="menus">
                        <el-select v-model="ruleForm.menus" multiple collapse-tags placeholder="请选择菜单列表">
                            <el-option
                            v-for="item in menuNameOptions"
                            :key="item.menuId"
                            :label="item.menuName"
                            :value="item.menuId">
                            </el-option>
                        </el-select>
                    </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 {apiGetgetRoleList, apiGetgetMenuList, apiGetgetaddPersonRoleMsg, apiGetgetuploadPersonRoleMsg, apiGetgetdeletePersonRoleMsg} from '../../api/api'
export default {
    data() {
        return {
            title:'',
            personRoleTableData:[],
            personRoleradio: {},
            single: false,
            dialogVisible:false,
            roleName: '',
            typeWh: '',
            menuNameOptions: [],
            page:{
                pagesize: 10,
                currentPage: 1,
                total: 0
            },
            ruleForm: {
                roles: '',
                menus: []
            },
            rules: {
                roles: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' }
                ],
                menus: [
                    { required: true, message: '请选择菜单列表', trigger: 'change' }
                ]
            }
        }
    },
    mounted() {
        this.getPersonListFn()
        this.getMenusData()
    },
    methods:{
        // 获取菜单数据
        getMenusData() {
            let that = this
            let params = {
                pageNum: 1,
                pageSize: 20
            }
            apiGetgetMenuList(params).then(datas =>{
                if (datas && datas.rows) {
                    that.menuNameOptions = datas.rows
                }
            })
        },
        getSeachData() {
            this.page.currentPage = 1
            this.getPersonListFn('seach')
        },
        reset() {
            this.roleName = ''
            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
            debugger
            let params = {
                roleName: that.ruleForm.roles,
                mIds: that.ruleForm.menus.join(',')
            }
            if (!that.single) {
                apiGetgetaddPersonRoleMsg(params).then(datas =>{
                    that.changeshowData(datas)
                })
            } else {
                params.roleId = this.personRoleradio.roleId
                apiGetgetuploadPersonRoleMsg(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 = {
                roles: '',
                menus: []
            }
        },
        // 新增区域信息
        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 = {
                roles: this.personRoleradio.roleName,
                menus: this.personRoleradio.mIds.split(',').map(it =>{
                    return it*1
                })
            }
        },
        rowClick(row, column, event) {
            this.personRoleradio = row
        },
        // 删除区域信息
        handleDelete() {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let that = this
                let params = {
                    roleId: that.personRoleradio.roleId
                }
                apiGetgetdeletePersonRoleMsg(params).then(datas =>{
                    if (datas.msg === '操作成功') {
                        that.getPersonListFn()
                    }
                })
                if (that.personRoleTableData.length>0) {
                    that.personRoleradio = that.personRoleTableData[0]
                }
                that.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            })
        },
        //获取风电场列表数据
        getPersonListFn(type) {
            let that = this
            let params = {
                pageNum: that.page.currentPage,
                pageSize: that.page.pagesize
            }
            if (type) {
                params.roleName = this.roleName
            }
            apiGetgetRoleList(params).then(datas =>{
                if (datas && datas.rows) {
                    that.personRoleTableData = datas.rows
                    that.personRoleradio = datas.rows[0]
                    that.page.total = datas.total
                }
            })
        }
    }
}
</script>

<style lang="less">
.personRoleManagement{
    .personRoleBtn{
        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;
            }
        }
    }
    .personRoleTableData{
        padding: 10px 20px;
        .el-table{
        .el-table__body-wrapper{
            .el-table__empty-block{
            height: 550px !important;
            }
            .el-table__body{
                .el-tag{
                    margin-right: 10px;
                    margin-top: 3px;
                    span{
                        font-size: 12px !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;
                .windframFromsxx{
                    padding-right: 20px !important;
                    .el-select{
                        width: 100%;
                    }
                }
            }
        }
    }
    
}
</style>