<template>
    <div>
        <el-form :model="formData" :rules="rules" ref="form" label-width="120px" label-position="right" size="small"
                 style="max-width: 500px;">
            <el-form-item>
                <h2> 测试表BBB</h2>
            </el-form-item>

                                                                                                                    <el-form-item prop="nameAaa" label="名称1">
                <el-input v-model="formData.nameAaa" :disabled="'nameAaa'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="nameBbb" label="名称2">
                <el-input v-model="formData.nameBbb" :disabled="'nameBbb'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="nameCcc" label="名称3">
                <el-input v-model="formData.nameCcc" :disabled="'nameCcc'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="nameDdd" label="名称4">
                <el-input v-model="formData.nameDdd" :disabled="'nameDdd'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="onDelete" v-if="formData.id&&isModulesShow('del')" type="danger">删除</el-button>
                <el-button @click="changeEditDialogVisible(false)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'
import { mapState } from 'vuex'
import { format } from 'date-fns'
import zh from 'date-fns/locale/zh_cn'

export default {
    props:{
        formData:{
            type:Object,
            default:{
                                                                                                                                                                                                                                                                                                                                        }
        }
    },

    created() {

                                                                                                                                                                                                                                                                                                                                                            },
    data() {
        return {
            saving: false,
        rules: {
                                                                                                        nameAaa:
                [
                                            {required: true, message: '请输入 名称1', trigger: 'blur'},
                                                        ],
                                                                nameBbb:
                [
                                            {required: true, message: '请输入 名称2', trigger: 'blur'},
                                                        ],
                                                                nameCcc:
                [
                                            {required: true, message: '请输入 名称3', trigger: 'blur'},
                                                        ],
                                                                },
        modules:            'search,query,orders,excel,add,edit,del'.split(',').map(i => String(i))
     ,
                                                                                                                                    }
    },
    computed: {
            ...mapState(['userInfo']),
                subColumn(){
                if(this.formData.subColumn){
                   return  this.formData.subColumn;
                }else{
                    return '';
                }
        },
},
methods: {
    changeEditDialogVisible(value){
        this.$emit('changeEditDialogVisible',value)
    },
    isModulesShow(column) {
        var row = this.modules.find(i => i === column);
        return row ? true : false;
    },
    onSave() {
        this.$refs.form.validate((valid) => {
            if (valid) {
                this.logicalValidate();
            } else {
                return false;
    }
    });
    },
    logicalValidate() {
        var data = JSON.parse(JSON.stringify(this.formData));

        var logicalData = {
                                                                                                                                    }

        if (JSON.stringify(logicalData) == '{}') {
            this.submit();
        } else {
            this.$http.get({
                url: '/testBbb/getOne',
                data: logicalData
            }).then(res => {
                if (res.success) {

                var logicalFlag = true;

                if (res.data) {
                    if (this.formData.id) {
                        if (res.data.id != this.formData.id) {
                            logicalFlag = false;
                        }
                    } else {
                        logicalFlag = false;
                    }

                }

                if (logicalFlag) {
                    this.submit();
                } else {
                    this.$message.warning('逻辑关键字:'+''+'验证失败')
                }


            } else {
                this.$message.warning('逻辑关键字:'+''+'验证失败')
            }
        });
        }
    },
    submit() {
        var data = JSON.parse(JSON.stringify(this.formData));

                                                                                                                                
    this.$http.post({
        url: this.formData.id ? '/testBbb/update' : '/testBbb/save',
        data: data
    }).then(res => {
        if (res.success) {
            this.$message.success('成功');
            this.$emit('changeEdit')
        } else {
            this.$message.warning('失败')
        }
    });
    },
    onDelete() {
        this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
            return this.$http.post({
                url: '/testBbb/del',
                data: { id: this.formData.id }
            })
        }).then(() => {
            this.$message.success('删除成功');
        this.$emit('changeEdit')
    }).catch(action => {
            if (action === 'cancel') {
            this.$message.info('删除取消');
        } else {
            this.$message.error('删除失败');
        }
    })
    },
}
}
</script>
<style lang="less" scoped>
</style>
