<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 prop="牛舍编号" label="牛舍编号">
                <el-input v-model="formData.牛舍编号" :disabled="'牛舍编号'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <el-form-item prop="牧场编号" label="牧场编号">
                <template>
                    <el-select v-model="formData.牧场编号" clearable  placeholder="请选择" :disabled="'牧场编号'==subColumn">
                        <el-option
                                v-for="item in 牧场编号Options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                <el-form-item prop="牛舍名称" label="牛舍名称">
                <el-input v-model="formData.牛舍名称" :disabled="'牛舍名称'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <el-form-item prop="牛舍类别" label="牛舍类别">
                <template>
                    <el-select v-model="formData.牛舍类别" clearable  placeholder="请选择" :disabled="'牛舍类别'==subColumn">
                        <el-option
                                v-for="item in 牛舍类别Options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        <el-form-item prop="实际存放牛只分类" label="实际存放牛只分类">
                <template>
                    <el-select v-model="formData.实际存放牛只分类" multiple clearable placeholder="请选择" :disabled="'实际存放牛只分类'==subColumn">
                        <el-option
                                v-for="item in 实际存放牛只分类Options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="管理员" label="管理员">
                            <el-input v-model="formData.管理员" disabled></el-input>
                        </el-form-item>
                                                                                                                                        <el-form-item prop="建筑面积" label="建筑面积">
                <el-input v-model="formData.建筑面积" :disabled="'建筑面积'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="设计容量" label="设计容量">
                <el-input v-model="formData.设计容量" :disabled="'设计容量'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="颈枷数" label="颈枷数">
                <el-input v-model="formData.颈枷数" :disabled="'颈枷数'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="制度要求使用数颈枷" label="制度要求使用数_颈枷">
                <el-input v-model="formData.制度要求使用数颈枷" :disabled="'制度要求使用数颈枷'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="卧床数" label="卧床数">
                <el-input v-model="formData.卧床数" :disabled="'卧床数'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="制度要求使用数卧床" label="制度要求使用数_卧床">
                <el-input v-model="formData.制度要求使用数卧床" :disabled="'制度要求使用数卧床'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="核定容量" label="核定容量">
                <el-input v-model="formData.核定容量" :disabled="'核定容量'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                    <el-form-item prop="综合核定的合理承载数" label="综合核定的合理承载数">
                <el-input v-model="formData.综合核定的合理承载数" :disabled="'综合核定的合理承载数'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                <el-form-item prop="是否青年围产舍" label="是否青年围产舍">
                <el-switch
                        v-model="formData.是否青年围产舍"
                        :disabled="'是否青年围产舍'==subColumn">
                </el-switch>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                 <el-form-item prop="备注" label="备注">
                 <single-upload v-model="formData.备注" :disabled="'备注'==subColumn"></single-upload>
             </el-form-item>
                                                                                                                                                                                                                                                                                                                            <el-form-item prop="修改人" label="修改人">
                <el-input type="textarea" v-model="formData.修改人" :disabled="'修改人'==subColumn"></el-input>
            </el-form-item>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <el-form-item prop="修改时间" label="修改时间">
                            <el-input v-model="formData.修改时间" disabled></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" type="danger">删除</el-button>
                <el-button @click="$router.go(-1)">取消</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 {
    created() {

        if (this.$route.query.column) {
            this.subColumn = this.$route.query.column.split(',')[1];
            this.subValue = this.$route.query.column.split(',')[0];
        }

        if (this.$route.query.id) {
            this.$http.get({
                url: '/tableNiuseRongliang/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {

                                                                                                                                                                                                                                                                                                                                                                                                                                
                        if(res.data.实际存放牛只分类){
                            res.data.实际存放牛只分类 = res.data.实际存放牛只分类.split(',');
                        }else{
                            res.data.实际存放牛只分类 = [];
                        }

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        if(res.data.修改时间){
                            res.data.修改时间 = format(new Date(res.data.修改时间), 'YYYY/MM/DD HH:mm', { locale: zh });
                        }

                                                        
                this.formData = res.data;

                if (this.$route.query.column) {
                    this.formData[this.subColumn] = this.subValue;
                }
            }
        })
        }else {
            if (this.$route.query.column) {
                this.formData[this.subColumn] = this.subValue;
            }
        }

                                                                                                                                                                        this.$http.get({
                        url:'/commonQuery/all',
                        data: {
                            tableName: 'Table_牛只_牧场信息',
                            databasetype: 'SqlServer',
                            dataSourceCode: 'dataSourceSqlserver',
                        }
                    }).then(res => {
                        if (res.success) {

                    if (res.data.length > 0) {
                        res.data.forEach(item => {
                            this.牧场编号Options.push({label: item.牧场名称, value:item.牧场编号});
                    })
                    }
                }
                });
                                                                                                                                                                                this.$http.get({
                        url:'/commonQuery/all',
                        data: {
                            tableName: 'Table_牛只_参数_牛舍类型',
                            databasetype: 'SqlServer',
                            dataSourceCode: 'dataSourceSqlserver',
                        }
                    }).then(res => {
                        if (res.success) {

                    if (res.data.length > 0) {
                        res.data.forEach(item => {
                            this.牛舍类别Options.push({label: item.牛舍类型, value:item.牛舍类型});
                    })
                    }
                }
                });
                                                                                                                        this.$http.get({
                        url:'/commonQuery/all',
                        data: {
                            tableName: 'Table_牛只_参数_牛只类型',
                            databasetype: 'SqlServer',
                            dataSourceCode: 'dataSourceSqlserver',
                        }
                    }).then(res => {
                        if (res.success) {

                    if (res.data.length > 0) {
                        res.data.forEach(item => {
                            this.实际存放牛只分类Options.push({label: item.牛只类型, value:item.牛只类型});
                    })
                    }
                }
                });
                                                                                                                                        this.formData.管理员 = this.userInfo.departId;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                },
    data() {
        return {
            saving: false,
            formData: {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    管理员:'',
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            修改时间:format(new Date(), 'YYYY/MM/DD HH:mm', { locale: zh }),
                                    },
        rules: {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    },
                                                                                                牧场编号Options:[],
                                                                                                        牛舍类别Options:[],
                                                                                    实际存放牛只分类Options:[],
                                                                                                                                                                                                                                                                                                                                    subColumn: '',
                subValue: '',
    }
    },
    computed: {
            ...mapState(['userInfo']),
},
methods: {
    onSave() {
        this.$refs.form.validate((valid) => {
            if (valid) {
                this.logicalValidate();
            } else {
                return false;
    }
    });
    },
    logicalValidate() {
        var data = JSON.parse(JSON.stringify(this.formData));

        var logicalData = {
                                                        牛舍编号:data.牛舍编号,
                                                牧场编号:data.牧场编号,
                                                                                                                                                                                                                                                                                                                                                                            }

        if (JSON.stringify(logicalData) == '{}') {
            this.submit();
        } else {
            this.$http.get({
                url: '/tableNiuseRongliang/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));

                                                                                                                                                                                                                                                                                                                                                                                                                                if(data.修改时间){
                    data.修改时间 = Date.parse(new Date(data.修改时间));
                }

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