<template>
    <a-modal :title="add?'添加部门':'修改部门'" v-model="visible" @ok="handleOk" :maskClosable="false" destroyOnClose>
        <a-form>
            <a-form-item label="部门名称">
                <a-input placeholder="请输入部门名称" v-model="modelData.simplename"></a-input>
            </a-form-item>
            <a-form-item label="部门全称">
                <a-input placeholder="请输入部门全称" v-model="modelData.fullname"></a-input>
            </a-form-item>
            <a-form-item label="排序">
                <a-input placeholder="请输入排序" v-model="modelData.num"></a-input>
            </a-form-item>
            <a-form-item label="备注">
                <a-input placeholder="请输入备注" v-model="modelData.tips"></a-input>
            </a-form-item>
            <a-form-item label="上级部门">
                <treecheck ref="tree1" @select="contentSelect1" :createFun="getList"></treecheck>
            </a-form-item>
        </a-form>
    </a-modal>
</template>
<script>
    import {getList, deptDetail, deptUpdate, deptAdd} from '@/api/system'
    import treecheck from '@/components/sysmanage/treeCheck'

    export default {
        name: 'roleModal',
        components: {
            treecheck
        },
        props: {},
        data() {
            return {
                getList,
                add: true, //弹窗标题
                visible: false, //弹窗是否显示
                modelData: {
                    //弹窗数据
                    simplename: "",
                    fullname: "",
                    tips: "",
                    num: 0,
                    pid: "",
                    id: ""
                }
            }
        },
        methods: {
            //tree1下拉框选中事件
            contentSelect1(data) {
                this.modelData.pid = data.id
            },
            showModal(type) {
                let that = this
                that.clearModal()
                if (type === 'add') {
                    that.add = true
                    that.visible = true
                } else {
                    that.add = false
                    that.visible = true
                    that.clearModal()
                    deptDetail(type).then(res => {
                        res = res.dept;
                        that.modelData.simplename = res.simplename
                        that.modelData.fullname = res.fullname
                        that.modelData.tips = res.tips
                        that.modelData.num = res.num
                        that.modelData.pid = res.pid
                        that.modelData.id = res.id
                        that.$refs.tree1.setValue(res.pid)
                    })
                }
            },
            handleOk() {
                let that = this
                if (that.modelData.id == '') {
                    deptAdd(that.modelData).then(() => {
                        that.visible = false
                        that.clearModal()
                        that.$emit('ok')
                    })
                } else {
                    deptUpdate(that.modelData).then(res => {
                        if (res.code == 400) {
                            this.$notification.error({message: res.message, description: '修改失败', duration: 4})
                        }
                        that.visible = false
                        that.clearModal()
                        that.$emit('ok')
                    })
                }
            },
            //清除弹窗信息
            clearModal() {
                this.modelData.simplename = ''
                this.modelData.fullname = ''
                this.modelData.tips = ''
                this.modelData.num = 0
                this.modelData.pid = ''
                this.modelData.pid = ''
                this.modelData.id = ''
            }
        }
    }
</script>

