<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="24" v-if="form.pid !== 0">
                    <el-form-item label="上级节点" prop="pid">
                        <treeselect
                                v-model="form.pid"
                                :options="areaOptions"
                                :normalizer="normalizer"
                                placeholder="选择上级区域"
                        />
                    </el-form-item>
                </el-col>
                            <Field label="区域名称" prop="name" v-model="form.name" placeholder="请输入区域名称"/>
                            <Field label="一体化的ID号" prop="iid" v-model="form.iid" placeholder="请输入一体化的ID号"/>
                            <Field label="一体化的父id" prop="pid" v-model="form.pid" placeholder="请输入一体化的父id"/>
                            <Field label="是否有下级区域" prop="haveSonArea" v-model="form.haveSonArea" placeholder="请输入是否有下级区域"/>
                            <Field label="是否有下级部门" prop="haveSonDept" v-model="form.haveSonDept" placeholder="请输入是否有下级部门"/>
                            <Field label="是否获取部门" prop="haveGetDept" v-model="form.haveGetDept" placeholder="请输入是否获取部门"/>
                            <Field label="是否获取事项列表" prop="haveGetMatterList" v-model="form.haveGetMatterList" placeholder="请输入是否获取事项列表"/>
                            <Field label="区域编码" prop="areaCode" v-model="form.areaCode" placeholder="请输入区域编码"/>
                            <Field label="区域等级" prop="areaLevel" v-model="form.areaLevel" type="select" :enumData="dict.areaLevel" placeholder="请选择区域等级"/>
                            <Field label="名称简称" prop="shortName" v-model="form.shortName" placeholder="请输入名称简称"/>
                            <Field label="访问地址" prop="domain" v-model="form.domain" type="textarea" placeholder="请输入访问地址"/>
                            <Field label="区域状态 " prop="status" v-model="form.status" type="select" :enumData="dict.status" 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/form";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    export default {
        mixins: [form],
        components: {
            Treeselect,
        },
        data() {
            return {
                areaOptions: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "区域",
                // 是否显示弹出层
                open: false,
                toString:[
                    "areaLevel",
                    "status",
                ],
                // 表单校验
                rules: {
                }
            };
        },

        methods: {
            /** 编辑 */
            edit(row, areaOptions) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl =this.pageInfo.editUrl;;
                this.getData();
                this.areaOptions = areaOptions;
                this.title = "修改区域";
            },
            /** 新增 */
            add(row, areaOptions) {
                this.reset()
                if(row.id){
                    this.form.parentId=row.id
                }
                this.urls.currUrl = this.pageInfo.addUrl;
                this.getData();
                this.areaOptions = areaOptions;
                this.title = "新增区域";
            },
            /** 查看*/
            view(row, areaOptions) {
                this.query = { id: row.id };
                this.urls.currUrl =this.pageInfo.viewUrl;;
                this.getData();
                this.areaOptions = areaOptions;
                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.iid,
                    label: node.name,
                    children: node.children,
                };
            },
            // 表单重置
            reset() {
                this.form = {
                    ancestors : "",
                    name : null,
                    iid : null,
                    pid : null,
                    haveSonArea : null,
                    haveSonDept : null,
                    haveGetDept : null,
                    haveGetMatterList : null,
                    areaCode : null,
                    areaLevel : 1,
                    shortName : null,
                    domain : null,
                    status : 1,
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
