<template>
    <el-container style="flex-grow:1">
        <el-main class="containMain">
            <div class="contentTitle">{{pageTitle}}</div>
            <el-form ref="form" :model="formData" label-width="80px" style="margin-top:20px;">
                <el-form-item label="" label-width="0">
                     <el-autocomplete prefix-icon="el-icon-search" v-model="formData.cowID" :fetch-suggestions="queryCow" placeholder="请输入牛号" style="width:300px" clearable @select="getNowCow"></el-autocomplete>
       </el-form-item>
                <div class="sub" v-if="tableData.length>0">
                    操作牛号：{{editCowInfo['牛号']}}
                </div>
                <el-form-item label="产犊类型">
                    <el-input v-model="formData.type" disabled style="width:217px"></el-input>
                </el-form-item>
                <el-form-item label="产犊数">
                    <el-select v-model="formData.num" placeholder="请选择">
                        <el-option v-for="item in ['1','2','3']" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否见胎">
                    <el-switch v-model="formData.isSeeTires" active-text="是" inactive-text="否">
                    </el-switch>

                </el-form-item>
                <el-form-item label="发情症状">
                    <el-select v-model="formData.difficulty" placeholder="请选择">
                        <el-option v-for="item in difficultyList" :key="item['名称']" :label="item['详细方式']" :value="item['名称']">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="产犊描述">
                    <el-select v-model="formData.describe" placeholder="请选择">
                        <el-option v-for="item in describeList" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>

                <div style="margin-top:80px">
                    <el-button @click="onSave" type="primary" style="width:98px;" size="mediu">确认保存</el-button>

                </div>
            </el-form>
        </el-main>
        <el-aside class="containAside" width="450px">
            <div class="asideInfo">
                <div class="contentTitle">牛只信息</div>
                <el-table class="handleTable" border :data="tableData" style="width: 100%" :height="(allHeight-70)+'px'" size='small'>
                    <el-table-column prop="key" label="参数">
                    </el-table-column>
                    <el-table-column prop="value" label="当前牛只信信息">
                    </el-table-column>
                </el-table>
            </div>
        </el-aside>
    </el-container>

</template>
<script>
import formValidator from '../formValidator'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
    created() {
        //产犊子难易度
        this.getHaleInfo('wsCalving', 'getCalvEase', [], 'list').then((data) => {
            this.difficultyList = data
            if (data.length > 0) {
                this.formData.difficulty = data[0]['名称']
                this.formData.describe = this.describeList[0]
            }
        })




    },
    data() {
        return {
            saving: false,
            formData: {
                cowID: '',
                difficulty: '',
                method: '人工观察',
                num: '1',
                describe: '',
                type: '正产',
                isSeeTires: true,
            },
            methods: ['人工观察', '计步器揭发', '同期发情'],
            tableData: [],
            difficultyList: [],
            editCowInfo: {}
        }
    },
    computed: {
        ...mapState(['userInfo', 'allHeight']),
        describeList() {
            var list = []
            switch (this.formData.num) {
                case "1":
                    list = ['单母胎', '单公胎']
                    break;
                case "2":
                    list = ['双母胎', '双公胎', '龙凤胎']
                    break;
                case "3":
                    list = ['三母胎', '三公胎', '两公一母', '两母一公']
                    break;
            }
            return list
        },
        pageTitle() {
            return this.$route.meta.title
        },
    },
    watch: {
        // 'formData.cowID'() {
        //     if (this.formData.cowID.length != 8) {
        //         return
        //     }
        //     this.getNowCow()
        // },
        'formData.num'() {
            setTimeout(() => {
                this.formData.describe = this.describeList[0]
            }, 100)
        }
    },
    methods: {
        onSave() {
            if (!this.formData.cowID) {
                this.$message({
                    message: '请录入牛只牛号',
                    type: 'warning'
                });
                return
            }

            this.getHaleInfo('wsCalving', 'addCalvingInfo', [
                this.formData.cowID, this.formData.type, true, '', '', this.formData.num, this.formData.isSeeTires, '', '', '', '', this.formData.describe, this.userInfo.username, this.userInfo.username, this.formData.difficulty, ''
            ], 'json').then((data) => {
                console.log(data)
                if (data.rtnCode == -1) {
                    // this.$message({
                    //     message: data.rtnMessage,
                    //     type: 'warning'
                    // });
                    this.$alert(data.rtnMessage, '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.formData.cowID = ''
                        }
                    });
                }
                else {


                    this.$confirm('产犊信息录入成功，是否立即进入犊牛信息录入', '提示', {
                        confirmButtonText: '是',
                        cancelButtonText: '否',
                        type: 'success'
                    }).then(() => {
                        this.$router.push({
                            name: "handleCalf",
                            query: {
                                cowID: this.editCowInfo['牛号']
                            }
                        })
                    }).catch(() => {
                        this.formData.cowID = ''
                    });
                }
            })
        },
        getNowCow() {

            this.getHaleInfo('wsCow', 'getCowExtendInfoByCowID', [
                this.formData.cowID, 3
            ], 'json').then((data) => {
                this.editCowInfo = data
                // console.log(data)
                var list = []
                Object.keys(data).forEach(item => {
                    list.push({
                        key: item,
                        value: data[item]
                    })
                })

                // console.log(list)

                this.tableData = list

                if (data['繁殖状态'] != '复检有胎' && data['繁殖状态'] != '初检已孕') {
                    this.$alert('当前母牛无胎，不允许进行登记', '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.formData.cowID = ''
                        }
                    });
                }
                else if (data['牛只类别'] == '成母牛' && parseInt(data['在胎天数']) < 150) {
                    this.$alert('当前奶牛是成母牛，且在胎天数小于150天，不允许登记产犊（青年牛210天内允许报早产）！', '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.formData.cowID = ''
                        }
                    });
                }

                if (parseInt(data['在胎天数']) > 262) {
                    this.formData.type == '正产'
                }
                else {
                    this.formData.type == '早产'
                }
            }).catch(() => {
                this.formData.cowID = ''
            })
        }
    }
}
</script>
<style lang="less" scoped>
.containMain {
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    padding: 15px;
    // margin-right: 10px;
    .contentTitle {
        border-bottom: 1px solid #f2f4f5;
    }
    // min-width: 450px;

    .sub {
        font-size: 14px;
        font-weight: 600;
        color: rgba(102, 102, 102, 1);
        line-height: 20px;
        margin-bottom: 40px;
        padding-left: 20px;
    }
}

.contentTitle {
    font-size: 16px;
    font-weight: bold;
    color: rgba(0, 0, 0, 1);
    line-height: 22px;
    letter-spacing: 1px;
    padding-bottom: 14px;
}

.containAside {
    padding: 0;
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    .asideInfo {
        flex-grow: 1;
        background: rgba(255, 255, 255, 1);
        border-radius: 4px;
        padding: 15px;
    }
}
</style>
