<template>
    <div>
        <el-dialog
            :title="addOrEditDepartmentConfig.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="deptInfo" 
                    :rules="rulesClient" 
                    label-width="100px" 
                    class="demo-ruleForm">
                    <el-row  type="flex" align="middle">
                        <el-col :span="24">
                        <el-form-item label="部门名称" prop="name">
                            <el-input
                            v-model.trim="deptInfo.name"
                            placeholder="请输入" maxlength="20"
                            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="50"
                                v-model="deptInfo.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: {
            addOrEditDepartmentConfig: {
                type: Object,
                default: () => {
                    return {}
                },
            },
        },
        data() {
            return {
                deptInfo:{
                    name:'',//部门名称
                    remark:'',//部门描述
                    companyId:this.$store.state.userinfo.companyId,//机构id  平台管理默认为0
                },
                rulesClient: {
                    name: [
                        { required: true, message: '请输入部门名称', trigger: 'change',},
                    ],
                    remark: [
                        { required: false, message: '请输入部门描述', trigger: 'change',},
                    ],
                }
                
            };
        },
        async created() {
            // 当为编辑部门时才调用详情接口
            if(Object.keys(this.addOrEditDepartmentConfig.data).length>0){
                this.getDeptById()
            }
        },
        methods: {
            // 根据角色id获取角色详情
            getDeptById(){
                let data={
                    id:this.addOrEditDepartmentConfig.data.id
                }
                systemManageApi.getDeptById(data).then(res => {
                    if (!res.code) {
                        this.deptInfo = {
                            name:res.name,
                            remark:res.remark,
                            companyId:res.companyId
                        }
                    }
                })
            },
            handleSubmit(){
                if(!this.$clickThrottle()) { return }
                let _this=this;
                 this.$refs.ruleForm.validate((valid) => {
                    if(valid) {
                        if(Object.keys(this.addOrEditDepartmentConfig.data).length>0)
                        {   
                            _this.deptInfo.id = this.addOrEditDepartmentConfig.data.id
                            systemManageApi.updateDeptById(_this.deptInfo).then(res => {
                                this.$message({
                                    showClose: true,
                                    message: '编辑部门成功！',
                                    type: 'success'
                                });
                                this.$parent.getDeptList(1)
                                this.handleClose()
                            })
                        }
                        else
                        {
                            systemManageApi.addDept(_this.deptInfo).then(res => {
                                this.$message({
                                    showClose: true,
                                    message: '新增部门成功！',
                                    type: 'success'
                                });
                                this.$parent.getDeptList(1)
                                this.handleClose()
                            })
                        }
                    }

                 })
            },
            handleChangeSort(){

            },
            handleClose() {
                this.$emit('update:addOrEditDepartmentConfig', 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>