<template>
    <!-- 弹出框表单 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
                <el-col :span="22" v-if="form.parentId !== 0">
                    <el-form-item label="上级节点" prop="parentId">
                        <treeselect
                                v-model="form.parentId"
                                :options="deptOptions"
                                :normalizer="normalizer"
                                placeholder="选择上级部门信息"
                        />
                    </el-form-item>
                </el-col>

                <Field :span="22" label="部门名称" prop="deptName" v-model="form.deptName" type="textarea" placeholder="请输入部门名称"/>
                <Field :span="22" label="部门状态" prop="deptStatus" v-model="form.deptStatus" type="select" :enumData="dict.deptStatus" placeholder="请选择部门状态"/>
                <Field :span="22" label="顺序" prop="orderNum" v-model="form.orderNum" placeholder="请输入顺序"/>
                <Field :span="22" label="备注" prop="remark" v-model="form.remark" type="textarea" placeholder="请输入备注"/>

            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    export default {
        mixins: [form],
        components: {
            Treeselect,
        },
        data() {
            return {
                deptOptions: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "部门信息",
                // 是否显示弹出层
                open: false,
                toString:[
                    "deptStatus",
                ],
                // 表单校验
                rules: {

                    deptName: [
                        {required: true,message: "请输入部门名称", trigger: "blur" },
                        {max: 256,message: "最多只能录入256个字符",trigger: "blur",},
                    ],
                    deptStatus: [
                        {required: true,message: "请输入部门状态", trigger: "blur" },
                    ],
                    orderNum: [
                        {required: true,message: "请输入顺序", trigger: "blur" },
                    ],
                }
            };
        },

        methods: {
            /** 编辑 */
            edit(row, deptOptions) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl =this.pageInfo.editUrl;;
                this.getData();
                this.deptOptions = deptOptions;
                this.title = "修改部门信息";
            },
            /** 新增 */
            add(row, deptOptions) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl = this.pageInfo.addUrl;
                this.getData();
                if(row.id){
                    this.form.parentId=row.id
                }
                this.deptOptions = deptOptions;
                this.title = "新增部门信息";
            },
            /** 查看*/
            view(row, deptOptions) {
                this.query = { id: row.id };
                this.urls.currUrl =this.pageInfo.viewUrl;;
                this.getData();
                this.deptOptions = deptOptions;
                this.title = "部门信息详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },
            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            /** 转换部门信息数据结构 */
            normalizer(node) {
                if (node.children && !node.children.length) {
                    delete node.children;
                }
                return {
                    id: node.id,
                    label: node.deptName,
                    children: node.children,
                };
            },
            // 表单重置
            reset() {
                this.form = {
                    parentId : null,
                    ancestors : "",
                    deptName : "",
                    deptStatus : 1,
                    orderNum : 0,
                    remark : "",
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
