<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>
                <#list columns as column>
                    <#if column.isPrimaryKey != 1 && !column.isSuperColumn(column.javaField)&& column.isEdit==1>
                        <#assign javaField=column.javaField />
                        <#assign comment=column.subComment(column.columnComment) />
                        <#if column.htmlType == 1 && !column.columnType?contains("tinyint")>
                            <Field label="${comment}" prop="${column.javaField}" v-model="form.${column.javaField}" placeholder="请输入${comment}"/>
                        <#elseif column.htmlType == 2>
                            <Field label="${comment}" prop="${column.javaField}" v-model="form.${column.javaField}" type="textarea" placeholder="请输入${comment}"/>
                        <#elseif column.htmlType == 3 ||column.columnType?contains("tinyint")>
                            <Field label="${comment}" prop="${column.javaField}" v-model="form.${column.javaField}" type="select" :enumData="dict.${column.javaField}" placeholder="请选择${comment}"/>
                        <#elseif column.htmlType == 4>
                            <Field label="${comment}" prop="${column.javaField}" v-model="form.${column.javaField}" type="checkbox" :enumData="dict.${column.javaField}" />
                        <#elseif column.htmlType == 5>
                            <Field label="${comment}" prop="${column.javaField}" v-model="form.${column.javaField}" type="radio" :enumData="dict.${column.javaField}" />
                        <#elseif column.htmlType == 6>
                            <Field label="${comment}" prop="${column.javaField}" v-model="form.${column.javaField}" type="date" />
                        <#elseif column.htmlType == 7>
                            <Field label="${comment}"><imageUpload v-model="form.${column.javaField}" prePath="/file/preview"/></Field>
                        <#elseif column.htmlType == 8>
                            <Field label="${comment}"><fileUpload v-model="form.${column.javaField}"  prePath="/file/fileupload"/></Field>
                        <#elseif column.htmlType == 9>
                            <Field label="${comment}"><editor v-model="form.${column.javaField}" :min-height="256"/></Field>
                        <#else >
                            <Field label="${comment}" prop="${column.javaField}" v-model="form.${column.javaField}" placeholder="请输入${comment}"/>
                        </#if>
                    </#if>
                </#list>

            </el-row>
            <#if table.tplCategory=="sub">
                <el-divider content-position="center">${subTable.functionName}信息</el-divider>
                <br/>
                <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd${subClassName}">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete${subClassName}">删除</el-button>
                    </el-col>
                </el-row>

                <el-table :data="${subclassName}List" :row-class-name="row${subClassName}Index" @selection-change="handle${subClassName}SelectionChange" ref="${subclassName}">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="序号" align="center" prop="index" width="50"/>
                    <#list subTable.columns as column >
                        <#assign javaField=column.javaField />
                        <#assign comment=column.subComment(column.columnComment) />
                        <#if column.isPrimaryKey==1 || javaField == subTableFkclassName>
                        <#elseif "" != javaField>
                            <el-table-column label="${comment}" prop="${javaField}">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.${javaField}" placeholder="请输入${comment}" />
                                </template>
                            </el-table-column>
                        </#if>
                    </#list>
                </el-table>
            </#if>

            <#if table.tplCategory=="subone">
                <el-divider content-position="center">${subTable.functionName}信息</el-divider>
                <br/>
                <el-row>
                    <#list subTable.columns as column >
                        <#if column.isPrimaryKey != 1 && !column.isSuperColumn(column.javaField)&& column.isEdit==1>
                            <#assign javaField=column.javaField />
                            <#assign comment=column.subComment(column.columnComment) />
                            <#if column.htmlType == 1 && !column.columnType?contains("tinyint")>
                                <Field label="${comment}" prop="${column.javaField}" v-model="form.${subclassName}Entity.${column.javaField}" placeholder="请输入${comment}"/>
                            <#elseif column.htmlType == 2>
                                <Field label="${comment}" prop="${column.javaField}" v-model="form.${subclassName}Entity.${column.javaField}" type="textarea" placeholder="请输入${comment}"/>
                            <#elseif column.htmlType == 3 ||column.columnType?contains("tinyint")>
                                <Field label="${comment}" prop="${column.javaField}" v-model="form.${subclassName}Entity.${column.javaField}" type="select" :enumData="dict.${column.javaField}" placeholder="请选择${comment}"/>
                            <#elseif column.htmlType == 4>
                                <Field label="${comment}" prop="${column.javaField}" v-model="form.${subclassName}Entity.${column.javaField}" type="checkbox" :enumData="dict.${column.javaField}" />
                            <#elseif column.htmlType == 5>
                                <Field label="${comment}" prop="${column.javaField}" v-model="form.${subclassName}Entity.${column.javaField}" type="radio" :enumData="dict.${column.javaField}" />
                            <#elseif column.htmlType == 6>
                                <Field label="${comment}" prop="${column.javaField}" v-model="form.${subclassName}Entity.${column.javaField}" type="date" />
                            <#elseif column.htmlType == 7>
                                <Field label="${comment}"><imageUpload v-model="form.${subclassName}Entity.${column.javaField}" prePath="/file/preview"/></Field>
                            <#elseif column.htmlType == 8>
                                <Field label="${comment}"><fileUpload v-model="form.${subclassName}Entity.${column.javaField}"  prePath="/file/fileupload"/></Field>
                            <#elseif column.htmlType == 9>
                                <Field label="${comment}"><editor v-model="form.${subclassName}Entity.${column.javaField}" :min-height="256"/></Field>
                            <#else >
                                <Field label="${comment}" prop="${column.javaField}" v-model="form.${subclassName}Entity.${column.javaField}" placeholder="请输入${comment}"/>
                            </#if>
                        </#if>
                    </#list>
                </el-row>
            </#if>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" v-if="pageInfo.type !== 'view'" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    import dialogShow from "./dialogshow";
    <#list columns as column>
    <#if column.isInsert==1 && !column.isSuperColumn(column.javaField) && column.isPrimaryKey!=1 && column.htmlType == 7>
    import ImageUpload from '@/components/ImageUpload';
    <#break>
    </#if>
    </#list>
    <#list columns as column>
    <#if column.isInsert==1 && !column.isSuperColumn(column.javaField) && column.isPrimaryKey!=1 && column.htmlType == 8>
    import FileUpload from '@/components/FileUpload';
    <#break>
    </#if>
    </#list>
    <#list columns as column>
    <#if column.isInsert==1 && !column.isSuperColumn(column.javaField) && column.isPrimaryKey!=1 && column.htmlType == 9>
    import Editor from '@/components/Editor';
    <#break>
    </#if>
    </#list>
    export default {
        mixins: [form],
        components: {
            dialogShow ,
            <#list columns as column>
            <#if column.isInsert==1 && !column.isSuperColumn(column.javaField) && column.isPrimaryKey!=1 && column.htmlType == 7>
            ImageUpload,
            <#break>
            </#if>
            </#list>
            <#list columns as column>
            <#if column.isInsert==1 && !column.isSuperColumn(column.javaField) && column.isPrimaryKey!=1 && column.htmlType == 8>
            FileUpload,
            <#break>
            </#if>
            </#list>
            <#list columns as column>
            <#if column.isInsert==1 && !column.isSuperColumn(column.javaField) && column.isPrimaryKey!=1 && column.htmlType == 9>
            Editor,
            <#break>
            </#if>
            </#list>
        },
        data() {
            return {
                <#if table.tplCategory=="sub">
                // 子表选中数据
                checked${subClassName}: [],
                // ${subTable.functionName}表格数据
                ${subclassName}List: [],
                </#if>
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "${functionName}",
                // 是否显示弹出层
                open: false,
                toString:[
                    <#list columns as column>
                    <#if column.columnType?contains("tinyint")>
                    "${column.javaField}",
                    </#if>
                    </#list>
                ],
                // 表单校验
                rules: {
                    <#list columns as column>
                    <#assign comment=column.subComment(column.columnComment) />
                    <#if column.javaType == "String" && column.isRequired==1>
                    ${column.javaField}: [
                        {<#if column.isRequired==1>required: true,message: "请输入${comment}",</#if> trigger: "blur" },
                        <#assign max=column.subBetween(column.columnType) />
                        {max: ${max},message: "最多只能录入${max}个字符",trigger: "blur",},
                    ],
                    </#if>
                    <#if column.javaType == "Integer" && column.isRequired==1>
                    ${column.javaField}: [
                        {<#if column.isRequired==1>required: true,message: "请输入${comment}",</#if> trigger: "blur" },
                    ],
                    </#if>
                    <#if column.javaType == "BigDecimal" && column.isRequired==1>
                    ${column.javaField}: [
                        {<#if column.isRequired==1>required: true,message: "请输入${comment}",</#if>trigger: "blur" },
                    ],
                    </#if>
                    <#if column.javaType == "Date" &&column.htmlType ==6 && column.isRequired==1>
                    ${column.javaField}: [
                        {<#if column.isRequired==1>required: true,message: "请选择${comment}"</#if> },
                    ],
                    </#if>
                    </#list>
                }
            };
        },

        methods: {
            <#if table.tplCategory=="sub">
            /** ${subTable.functionName}序号 */
            row${subClassName}Index({ row, rowIndex }) {
                row.index = rowIndex + 1;
            },
            /** ${subTable.functionName}添加按钮操作 */
            handleAdd${subClassName}() {
                let obj = {};
                <#list subTable.columns as column>
                <#if column.isPrimaryKey==1 || column.javaField == subTableFkclassName>
                <#elseif "" != javaField>
                obj.${column.javaField} = "";
                </#if>
                </#list>
                this.${subclassName}List.push(obj);
            },
            /** ${subTable.functionName}删除按钮操作 */
            handleDelete${subClassName}() {
                if (this.checked${subClassName}.length == 0) {
                    this.$alert("请先选择要删除的${subTable.functionName}数据", "提示", { confirmButtonText: "确定", });
                } else {
                    this.${subclassName}List.splice(this.checked${subClassName}[0].index - 1, 1);
                }
            },
            /** 单选框选中数据 */
            handle${subClassName}SelectionChange(selection) {
                if (selection.length > 1) {
                    this.$refs.${subclassName}.clearSelection();
                    this.$refs.${subclassName}.toggleRowSelection(selection.pop());
                } else {
                    this.checked${subClassName} = selection;
                }
            },
            // 渲染前置处理
            beforeRender(data) {
                if(data.entity.${subclassName}List) {
                    this.${subclassName}List = data.entity.${subclassName}List;
                }
                return data
            },
            <#elseif table.tplCategory=="subone" >
            </#if>
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { ${pkColumn.javaField}: row.${pkColumn.javaField} };
                this.urls.currUrl ="${RequestMapping}/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改${functionName}";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "${RequestMapping}/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增${functionName}";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { ${pkColumn.javaField}: row.${pkColumn.javaField} };
                this.urls.currUrl ="${RequestMapping}/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "${functionName}详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },

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

            // 表单重置
            reset() {
                this.form = {
                    <#list columns as column>
                    <#if !column.isSuperColumn(column.javaField)>
                    <#if column.javaType=="Long" && column.defaultValue??&& column.defaultValue!="">
                    <#assign Default>${column.defaultValue}</#assign>
                    <#elseif column.javaType=="Integer" && column.defaultValue??&& column.defaultValue!="">
                    <#assign Default>${column.defaultValue}</#assign>
                    <#elseif column.javaType=="String" && column.defaultValue??>
                    <#assign Default>"${column.defaultValue}"</#assign>
                    <#elseif column.javaType=="BigDecimal" && column.defaultValue??&& column.defaultValue!="">
                    <#assign Default>${column.defaultValue}</#assign>
                    <#else>
                    <#assign Default>null</#assign>
                    </#if>
                    ${column.javaField} : ${Default},
                    </#if>
                    </#list>
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
