<template xmlns:a-col="http://www.w3.org/1999/html">
    <div class="page-container">
        <div class="page-header">
            <a-row type="flex" justify="center" align="middle">
                <a-col :span="18">
                    <span class="page-title">{{ title }}</span>
                </a-col>
                <a-col :span="6">
                    <div class="flex-wrap page-tool">
                        <div class="page-tool-item"
                             v-for="(item, index) in barMenu"
                             :key="index"
                             v-if="item.display === 'v'">
                            <a-button type="primary" @click="buttonClick(item.id)">
                                {{ item.name }}
                            </a-button>
                        </div>
                    </div>
                </a-col>
            </a-row>
        </div>
        <div class="page-body">
            <a-form-model :model="form" :rules="rules" ref="form">
                <a-row :gutter="[50, 10]">
                    <a-col :span="item.span" v-for="(item, index) in fieldArr" :key="index">
                        <component :is="'mix' + item.formtype"
                                   :attr="item"
                                   v-if="item.display !== 'd'"
                                   v-model="form[item.alias]">
                        </component>
                    </a-col>
                </a-row>
            </a-form-model>
        </div>
    </div>
</template>

<script>
import bus from "../../../utils/bus"
import mixText from "../../components/mix-text"
import mixTextarea from "../../components/mix-textarea"
import mixBox from "../../components/mix-box"
import mixDatetime from "../../components/mix-datetime"
import mixFile from "../../components/mix-file"
import mixImage from "../../components/mix-image"
import mixLinkage from "../../components/mix-linkage"
import mixNumber from "../../components/mix-number"

export default {
    name: "create",
    components: {
        mixText,
        mixTextarea,
        mixBox,
        mixDatetime,
        mixFile,
        mixImage,
        mixLinkage,
        mixNumber
    },
    data() {
        return {
            templateId: "",
            title: "创建数据",
            form: {},
            rules: {},
            setting: {},
            barMenu: [],
            operation: [],
            fieldArr: [],
            table: null
        }
    },
    created() {
        let query = this.$route.query;
        this.templateId = query.template_id;
        if (!this.templateId) {
            this.$message.error("视图模板不存在，请到菜单管理关联视图模板");
        }
        this.getTemplateInfo();
    },
    //被 keep-alive 缓存的组件激活时调用。
    activated() {
        this.getTemplateInfo()
    },
    methods: {
        changeTabTitle(title){
            title = title.replace('表', '')
            title = '添加' + title
            this.$store.commit('changeTabTitle', title)
        },
        getTemplateInfo() {
            let param = {template_id: this.templateId}
            this.api.post('template_info', param).then(result => {
                this.barMenu = result.result.barMenu
                this.operation = result.result.operation
                this.setting = result.result.setting
                this.fieldArr = result.result.fieldsArr
                this.table = result.result.table
                this.changeTabTitle(this.table.name)
                this.getRule(this.fieldArr)

            })
        },
        getRule(fieldArr) {
            fieldArr.forEach(v => {
                this.$set(this.form, v.alias, v.value)
                let rules = [];
                if (!v.setting.is_allow_null) {
                    //不能为空
                    rules.push({
                        required: true,
                        message: "该选项必填",
                        trigger: "blur"
                    })
                }
                if (v.setting.reg){
                    rules.push({
                        required: true,
                        pattern: v.setting.reg,
                        message: v.setting.reg_tip
                    })
                }

                this.$set(this.rules, v.alias, rules)
            })
        },
        buttonClick(id) {
            if (id === 'sys_add') {
                this.create();
            } else if (id === 'sys_close') {
                this.back()
            } else {
                this.$message.error('不支持的操作');
            }
        },
        create() {
            this.$refs['form'].validate(valid => {
                if (valid){
                    let data = {...this.form}
                    data.template_id = this.templateId
                    this.api.post('data_create', data).then(result => {
                        this.$message.success('添加成功');
                        this.back()
                    })
                }
            })
        },
        back(){
            bus.$emit('refresh', this.table.uid)
            history.back();
        }

    }
}
</script>

<style scoped>

</style>
