<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.${treeParentCode} !== 0">
                    <el-form-item label="上级节点" prop="${treeParentCode}">
                        <treeselect
                                v-model="form.${treeParentCode}"
                                :options="${className}Options"
                                :normalizer="normalizer"
                                placeholder="选择上级${functionName}"
                        />
                    </el-form-item>
                </el-col>
                <#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>
                            <el-col span="12">
                                <el-form-item label="${comment}">
                                    <imageUpload v-model="form.${column.javaField}" prePath="/uploadimage"/>
                                </el-form-item>
                            </el-col>
                        <#elseif column.htmlType == 8>
                            <el-col span="12">
                                <el-form-item label="${comment}">
                                    <fileUpload v-model="form.${column.javaField}"  prePath="/file/fileupload" />
                                </el-form-item>
                            </el-col>
                        <#elseif column.htmlType == 9>
                            <el-col span="12">
                                <el-form-item label="${comment}">
                                    <editor v-model="form.${column.javaField}" :min-height="192"/>
                                </el-form-item>
                            </el-col>
                        <#else >
                            <Field label="${comment}" prop="${column.javaField}" v-model="form.${column.javaField}" placeholder="请输入${comment}"/>
                        </#if>
                    </#if>
                </#list>

            </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";
    <#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: {
            Treeselect,
            <#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 {
                ${className}Options: [],
                // 遮罩层
                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: {
            /** 编辑 */
            edit(row, ${className}Options) {
                this.reset()
                this.query = { ${pkColumn.javaField}: row.${pkColumn.javaField} };
                this.urls.currUrl =this.pageInfo.editUrl;;
                this.getData();
                this.${className}Options = ${className}Options;
                this.title = "修改${functionName}";
            },
            /** 新增 */
            add(row, ${className}Options) {
                this.reset()
                this.query = { ${pkColumn.javaField}: row.${pkColumn.javaField} };
                this.urls.currUrl = this.pageInfo.addUrl;
                this.getData();
                if(row.id){
                    this.form.parentId=row.id
                }
                this.${className}Options = ${className}Options;
                this.title = "新增${functionName}";
            },
            /** 查看*/
            view(row, ${className}Options) {
                this.query = { ${pkColumn.javaField}: row.${pkColumn.javaField} };
                this.urls.currUrl =this.pageInfo.viewUrl;;
                this.getData();
                this.${className}Options = ${className}Options;
                this.title = "${functionName}详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },
            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            /** 转换${functionName}数据结构 */
            normalizer(node) {
                if (node.children && !node.children.length) {
                    delete node.children;
                }
                return {
                    id: node.${treeCode},
                    label: node.${treeName},
                    children: node.children,
                };
            },
            // 表单重置
            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>
