<template>
    <div>
        <el-dialog
            :title="addOrEditDeptConfig.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-row v-if="Object.keys(this.addOrEditDeptConfig.data).length>0 && this.addOrEditDeptConfig.data.pid != 0"  type="flex" align="middle">
                        <el-col :span="24">
                        <el-form-item label="团队负责人" prop="remark">
                            <el-select v-model="deptInfo.directorIds" multiple placeholder="请选择" class="width100" :multiple-limit="5">
                                <el-option
                                v-for="item in usersOptions"
                                :key="item.id"
                                :label="item.showName"
                                :value="item.id">
                                </el-option>
                            </el-select>
                        </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: {
            addOrEditDeptConfig: {
                type: Object,
                default: () => {
                    return {}
                },
            },
        },
        data() {
            return {
                deptInfo:{
                    pid:this.addOrEditDeptConfig.data.id,//团队默认1
                    name:'',//团队名称
                    remark:'',//团队描述
                    companyId:this.$store.state.userinfo.companyId,//机构id  平台管理默认为0
                    directorId:[]
                },
                rulesClient: {
                    name: [
                        { required: true, message: '请输入团队名称', trigger: 'change',},
                    ],
                    remark: [
                        { required: false, message: '请输入团队描述', trigger: 'change',},
                    ],
                },
                usersOptions:[]
                
            };
        },
        async created() {
            // 当为编辑团队时才调用详情接口
            if(Object.keys(this.addOrEditDeptConfig.data).length>0 && this.addOrEditDeptConfig.data.pid != 0){
                this.getDeptById()
            }
            this.getUserList()
        },
        methods: {
            // 根据角色id获取角色详情
            getDeptById(){
                let data={
                    id:this.addOrEditDeptConfig.data.id
                }
                systemManageApi.getDeptById(data).then(res => {
                    if (!res.code) {
                        this.deptInfo = {
                            name:res.name,
                            remark:res.remark,
                            companyId:res.companyId,
                            directorIds:res.directorId
                        }
                    }
                })
            },
            getUserList(val){
                if(val!=undefined){this.queryData.current = val}
                systemManageApi.getUserList({current: 1,size: 500,deptId:this.addOrEditDeptConfig.data.id}).then(res => {
                    if (!res.code) {
                        this.usersOptions = res.records;
                    }
                })
            },
            handleSubmit(){
                if(!this.$clickThrottle()) { return }
                let _this=this;
                 this.$refs.ruleForm.validate((valid) => {
                    if(valid) {
                        if(Object.keys(this.addOrEditDeptConfig.data).length>0 && this.addOrEditDeptConfig.data.pid != 0)
                        {   
                            _this.deptInfo.id = this.addOrEditDeptConfig.data.id
                            if(this.deptInfo.directorIds.length == 0){_this.deptInfo.directorId ='[]'}
                            else{_this.deptInfo.directorId ='['+ this.deptInfo.directorIds.toString()+']'}
                            systemManageApi.updateDeptById(_this.deptInfo).then(res => {
                                this.$message({
                                    showClose: true,
                                    message: '编辑团队成功！',
                                    type: 'success'
                                });
                                this.$parent.getDeptList(1,this.addOrEditDeptConfig.data.pid)
                                this.handleClose()
                            })
                        }
                        else
                        {
                            systemManageApi.addDept(_this.deptInfo).then(res => {
                                this.$message({
                                    showClose: true,
                                    message: '新增团队成功！',
                                    type: 'success'
                                });
                                this.$parent.getDeptList(1,this.addOrEditDeptConfig.data.id)
                                this.handleClose()
                            })
                        }
                    }

                 })
            },
            handleChangeSort(){

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