<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.parentId !== 0">
                    <el-form-item label="上级节点" prop="parentId">
                        <treeselect
                                v-model="form.parentId"
                                :options="siteBusinessOptions"
                                :normalizer="normalizer"
                                placeholder="选择上级站点事项"
                        />
                    </el-form-item>
                </el-col>

            </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 {
                siteBusinessOptions: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "站点事项",
                // 是否显示弹出层
                open: false,
                toString:[
                    "isBusiness",
                    "status",
                ],
                // 表单校验
                rules: {
                }
            };
        },

        methods: {
            /** 编辑 */
            edit(row, siteBusinessOptions) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl =this.pageInfo.editUrl;;
                this.getData();
                this.siteBusinessOptions = siteBusinessOptions;
                this.title = "修改站点事项";
            },
            /** 新增 */
            add(row, siteBusinessOptions) {
                this.reset()
                if(row.id){
                    this.form.parentId=row.id
                }
                this.urls.currUrl = this.pageInfo.addUrl;
                this.getData();
                this.siteBusinessOptions = siteBusinessOptions;
                this.title = "新增站点事项";
            },
            /** 查看*/
            view(row, siteBusinessOptions) {
                this.query = { id: row.id };
                this.urls.currUrl =this.pageInfo.viewUrl;;
                this.getData();
                this.siteBusinessOptions = siteBusinessOptions;
                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.businessId,
                    label: node.businessName,
                    children: node.children,
                };
            },
            // 表单重置
            reset() {
                this.form = {
                    siteId : null,
                    siteName : null,
                    businessId : null,
                    businessName : null,
                    ancestors : "",
                    isBusiness : null,
                    parentId : null,
                    status : 1,
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
