<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small"
                 style="max-width: 500px;">
            #foreach($field in $model.fields)
                #if(${field.showInForm})
                    #if(${field.formType}=="singleLineText")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <el-input v-model="formData.${field.modelName}"></el-input>
            </el-form-item>
                    #end
                    #if(${field.formType}=="textarea")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <el-input type="textarea" v-model="formData.${field.modelName}"></el-input>
            </el-form-item>
                    #end
                    #if(${field.formType}=="number")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <el-input type="number" v-model="formData.${field.modelName}"></el-input>
            </el-form-item>
                    #end
                    #if(${field.formType}=="date")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <template>
                    <div class="block">
                        <el-date-picker
                                v-model="formData.${field.modelName}"
                                type="datetime"
                                placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </template>
            </el-form-item>
                    #end
                    #if(${field.formType}=="select")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <template>
                    <el-select v-model="formData.${field.modelName}" placeholder="请选择">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
                    #end
                    #if(${field.formType}=="multiSelect")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <template>
                    <el-select v-model="formData.${field.modelName}" multiple placeholder="请选择">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
                    #end
                    #if(${field.formType}=="singleImage")
             <el-form-item prop="${field.modelName}" label="${field.remark}">
                 <single-upload v-model="formData.${field.modelName}"></single-upload>
             </el-form-item>
                    #end
                    #if(${field.formType}=="multiImage")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <multi-upload v-model="formData.${field.modelName}"></multi-upload>
            </el-form-item>
                    #end
                    #if(${field.formType}=="fileUpload")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <el-upload
                        class="upload-demo"
                        action="../assets/uploadFile"
                        :on-change="handleChange"
                        :file-list="fileList3">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件 ，且不超过500kb</div>
                </el-upload>
            </el-form-item>
                    #end
                    #if(${field.formType}=="tree")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <el-tree
                        :props="props"
                        :load="loadNode"
                        lazy
                        show-checkbox
                        @check-change="handleCheckChange">
                </el-tree>
            </el-form-item>
                    #end
                    #if(${field.formType}=="switch")
            <el-form-item prop="${field.modelName}" label="${field.remark}">
                <el-switch
                        v-model="formData.${field.modelName}"
                        active-color="#13ce66"
                        inactive-color="#ff4949">
                </el-switch>
            </el-form-item>
                    #end
                #end
            #end
            <el-form-item>
                <el-button @click="onSave" :loading="${esc.d}store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="${esc.d}router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
    import formValidator from '../formValidator'

    export default {
        created() {
            if (this.${esc.d}route.query.id) {
                this.${esc.d}http.get({
                    url: '/${display.uncapitalize($model.className)}/getOne',
                    data: {
                        id: this.${esc.d}route.query.id
                    }
                }).then(res => {
                    if (res.success) {
                        this.formData = res.data;
                    }
                })
            }
        },
        data() {
            return {
                saving: false,
                formData: {},
                rules: {
            #foreach($field in $model.fields)
                #if(${field.required})
                    ${field.modelName}:
                    [
                        {required: true, message: '请输入 $display.alt(${field.remark},${field.modelName})', trigger: 'blur'},
                        #if(${field.validate})
                        #if(${field.maxLength}&&${field.minLength})
                        { min: ${field.minLength}, max: ${field.maxLength}, message: '长度在 ${field.minLength} 到 ${field.maxLength} 个字符', trigger: 'blur' },
                        #end
                        #if(${field.min}&&${field.max})
                        { minValue: ${field.min}, maxValue: ${field.max}, message: '值在 ${field.min} 到 ${field.max} 之间', trigger: 'blur' },
                        #end
                        #if(${field.validatorType}=="phone")
                         formValidator.phone,
                        #end
                        #if(${field.validatorType}=="number")
                         formValidator.number,
                        #end
                        #if(${field.validatorType}=="url")
                         formValidator.url,
                        #end
                        #if(${field.validatorType}=="email")
                         formValidator.email,
                        #end
                        #if(${field.validatorType}=="idCard")
                         formValidator.idCard,
                        #end
                        #if(${field.validatorType}=="english")
                         formValidator.english,
                        #end
                        #end
                    ],
                #end
            #end
        },
        }
        },
        methods: {
            onSave() {
                this.${esc.d}refs.form.validate((valid) => {
                    if (valid) {
                        this.submit();
                    } else {
                        return false;
                    }
                });
            },
            submit() {
                var data = JSON.parse(JSON.stringify(this.formData));
                this.${esc.d}http.post({
                    url: this.formData.id ? '/${display.uncapitalize($model.className)}/update' : '/${display.uncapitalize($model.className)}/save',
                    data: data
                }).then(res => {
                    if (res.success) {
                        this.${esc.d}message.success('成功');
                        this.${esc.d}router.go(-1);
                    } else {
                        this.${esc.d}message.warning('失败')
                    }
                });
            }
        }
    }
</script>
<style lang="less" scoped>
</style>
