<template>
    <!-- 弹出框表单 -->
    <el-drawer
            :title="title"
            :visible.sync="open"
            :direction="direction"
            :destroy-on-close="true"
            size="70%">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
              <Field label="区域名称" prop="areaName" v-model="form.areaName" placeholder="请输入区域名称"/>
                            <Field label="区域编码" prop="areaCode" v-model="form.areaCode" placeholder="请输入区域编码"/>
                            <Field label="背景图片" prop="bigPath"  placeholder="请输入背景图片地址">
                              <ImageUpload v-model="form.bigPath" prePath="/file/fileupload" />
                            </Field>

                            <Field label="标签" prop="tag" v-model="form.tag" type="textarea" placeholder="请输入标签"/>
                            <Field label="排序" prop="sort" v-model="form.sort" placeholder="请输入排序"/>
                            <Field label="备注" prop="remark" v-model="form.remark" placeholder="请输入备注"/>

            </el-row>
                <el-divider content-position="center">区域接入系统信息</el-divider>
                <br/>
                <el-row style="margin-left:10px"     :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddAccessSystem">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteAccessSystem">删除</el-button>
                    </el-col>
                </el-row>

                <el-table :data="accessSystemList" :row-class-name="rowAccessSystemIndex" @selection-change="handleAccessSystemSelectionChange" ref="accessSystem">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="序号" align="center" prop="index" width="50"/>
                            <el-table-column label="接入系统名称" prop="systemName">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.systemName" placeholder="请输入接入系统名称" />
                                </template>
                            </el-table-column>
                            <el-table-column label="接入系统编码" prop="systemCode">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.systemCode" placeholder="请输入接入系统编码" />
                                </template>
                            </el-table-column>
                            <el-table-column label="启用状态 " prop="enabled">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.enabled" placeholder="请输入启用状态 " />
                                </template>
                            </el-table-column>

                            <el-table-column label="排序" prop="sort">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.sort" placeholder="请输入排序" />
                                </template>
                            </el-table-column>
                            <el-table-column label="备注" prop="remark">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.remark" placeholder="请输入备注" />
                                </template>
                            </el-table-column>
                </el-table>

            <form-buttons @submit='submitForm' v-if="pageInfo.type!='view'" noCancelBtn />
        </el-form>

    </el-drawer>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    export default {
        name: "AccessDetail",
        mixins: [form],
        components: {

        },
        created() {
            this.changePath("access")
        },
        data() {
            return {
                // 子表选中数据
                checkedAccessSystem: [],
                // 区域接入系统表格数据
                accessSystemList: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "区域接入",
                // 是否显示弹出层
                open: false,
                direction:"rtl",
                toString:[
                ],
                toDate:[
                ],
                // 表单校验
                rules: {
                    areaName: [
                        {required: true,message: "请输入区域名称", trigger: "blur" },
                        {max: 64,message: "最多只能录入64个字符",trigger: "blur",},
                    ],
                    areaCode: [
                        {required: true,message: "请输入区域编码", trigger: "blur" },
                        {max: 64,message: "最多只能录入64个字符",trigger: "blur",},
                    ],
                    bigPath: [
                        {required: true,message: "请输入背景图片地址", trigger: "blur" },
                        {max: 128,message: "最多只能录入128个字符",trigger: "blur",},
                    ],
                    tag: [
                        {required: true,message: "请输入标签", trigger: "blur" },
                        {max: 255,message: "最多只能录入255个字符",trigger: "blur",},
                    ],
                    createTime: [
                        {required: true,message: "请选择创建时间" },
                    ],
                }
            };
        },

        methods: {
            /** 区域接入系统序号 */
            rowAccessSystemIndex({ row, rowIndex }) {
                row.index = rowIndex + 1;
            },
            /** 区域接入系统添加按钮操作 */
            handleAddAccessSystem() {
                let obj = {};
                obj.systemName = "";
                obj.systemCode = "";
                obj.enabled = "";
                obj.accessTime = "";
                obj.sort = "";
                obj.remark = "";
                obj.createUserId = "";
                obj.createTime = "";
                obj.updateTime = "";
                this.accessSystemList.push(obj);
            },
            /** 区域接入系统删除按钮操作 */
            handleDeleteAccessSystem() {
                if (this.checkedAccessSystem.length == 0) {
                    this.$alert("请先选择要删除的区域接入系统数据", "提示", { confirmButtonText: "确定", });
                } else {
                    this.accessSystemList.splice(this.checkedAccessSystem[0].index - 1, 1);
                }
            },
            /** 单选框选中数据 */
            handleAccessSystemSelectionChange(selection) {
                if (selection.length > 1) {
                    this.$refs.accessSystem.clearSelection();
                    this.$refs.accessSystem.toggleRowSelection(selection.pop());
                } else {
                    this.checkedAccessSystem = selection;
                }
            },
            // 渲染前置处理
            beforeRender(data) {
                if(data.entity.accessSystemList) {
                    this.accessSystemList = data.entity.accessSystemList;
                }
                return data
            },
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="access/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改区域接入";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "access/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增区域接入";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="access/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "区域接入详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },

            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            // 表单重置
            reset() {
                this.form = {
                    areaName : null,
                    areaCode : null,
                    bigPath : null,
                    tag : null,
                    sort : 0,
                    remark : null,
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
