<template>
    <div>
        <el-dialog
            :title="addOrEditRoleConfig.title"
            :visible="true"
            width="400px"
            append-to-body
            :close-on-click-modal="false"
            @close="handleClose"
        >
        <el-scrollbar class="bor">
            <div class="dialog-content">
                <el-form ref="ruleForm" 
                    :model="roleInfo" 
                    :rules="rulesClient" 
                    label-width="80px" 
                    class="demo-ruleForm">
                    <el-row  type="flex" align="middle">
                        <el-col :span="24">
                        <el-form-item label="角色名称" prop="name">
                            <el-input
                            v-model.trim="roleInfo.name"
                            placeholder="请输入"
                            clearable
                            ></el-input>
                        </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row  type="flex" align="middle">
                        <el-col :span="24">
                        <el-form-item label="角色描述" prop="remark">
                            <el-input
                                type="textarea"
                                :rows="3"
                                placeholder="请输入内容" maxlength="100"
                                v-model="roleInfo.remark">
                            </el-input>
                        </el-form-item>
                        </el-col>
                    </el-row>                        
                
                </el-form>
            </div>
        </el-scrollbar>
        <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose()">取消</el-button>
            <el-button type="primary" @click="handleSubmit()">确认</el-button>
        </span>
        </el-dialog>
        
    </div>
</template>
<script>
import systemManageApi from "@/services/systemManage";
    export default {
        components: {
           
        },
        props: {
            addOrEditRoleConfig: {
                type: Object,
                default: () => {
                    return {}
                },
            },
        },
        data() {
            return {
                roleInfo:{
                    name:'',//角色名称
                    type:2,//角色类型
                    remark:'',//角色描述
                    identifier:'',//权限标志
                    companyId:this.$store.state.userinfo.companyId,//机构id  平台管理默认为0
                },
                rulesClient: {
                    name: [
                        { required: true, message: '请输入角色名称', trigger: 'change',},
                    ],
                    type: [
                        { required: true, message: '请选择角色类型', trigger: 'change',},
                    ],
                }
                
            };
        },
        async created() {
            // 当为编辑角色时才调用详情接口
            if(Object.keys(this.addOrEditRoleConfig.data).length>0){
                this.getRoleById()
            }
        },
        methods: {
            // 根据角色id获取角色详情
            getRoleById(){
                let data={
                    id:this.addOrEditRoleConfig.data.id
                }
                systemManageApi.getRoleById(data).then(res => {
                    if (!res.code) {
                        this.roleInfo = {
                            name:res.name,
                            type:res.type,
                            remark:res.remark,
                            identifier:res.identifier,
                            companyId:res.companyId
                        }
                    }
                })
            },
            handleSubmit(){
                if(!this.$clickThrottle()) { return }
                let _this=this;
                 this.$refs.ruleForm.validate((valid) => {
                    if(valid) {
                        if(Object.keys(this.addOrEditRoleConfig.data).length>0)
                        {   
                            _this.roleInfo.id = this.addOrEditRoleConfig.data.id
                            systemManageApi.updateRoleById(_this.roleInfo).then(res => {
                                this.$message({
                                    showClose: true,
                                    message: '编辑角色成功！',
                                    type: 'success'
                                });
                                this.$parent.getRoleList(1)
                                this.handleClose()
                            })
                        }
                        else
                        {
                            systemManageApi.addRole(_this.roleInfo).then(res => {
                                this.$message({
                                    showClose: true,
                                    message: '新增角色成功！',
                                    type: 'success'
                                });
                                this.$parent.getRoleList(1)
                                this.handleClose()
                            })
                        }
                    }

                 })
            },
            handleChangeSort(){

            },
            handleClose() {
                this.$emit('update:addOrEditRoleConfig', null)
            },
        }
    };
</script>
<style lang="scss" scoped>
.dialog-content{
    padding: $size16;
    max-height:500px
}

::v-deep .el-dialog__header{
    border-bottom: 1px solid $border-color-lighter !important;
}

::v-deep  .el-dialog__footer{
    border-top: 1px solid $border-color-lighter !important;
}
</style>