<template>
    <el-container style="flex-grow:1">
        <el-aside class="containAside" max-width="500px" width='26%' style=" margin-right: 10px;">
            <div class="asideInfo">
                <div class="contentTitle">{{pageTitle}}

                    <span>{{list.length}}头奶牛</span>
                </div>
                <el-table class="handleTable" border :data="list" style="width: 100%" :height="(allHeight-70)+'px'" size='small' @row-click='rowClick'>
                    <el-table-column prop="牛号" label="牛号" fixed="left" align="center">
                    </el-table-column>
                    <el-table-column prop="当前牛舍" label="牛舍" align="center">
                    </el-table-column>
                    <el-table-column prop="发情观察日期" label="发情日期" align="center" min-width="100">
                    </el-table-column>
                    <el-table-column prop="繁殖状态" label="繁殖状态" align="center">
                    </el-table-column>
                    <el-table-column prop="最近配种" label="最近配种" align="center">
                    </el-table-column>
                    <el-table-column prop="不建议配种原因" label="不建议配种原因" align="center">
                    </el-table-column>
                </el-table>
            </div>
        </el-aside>
        <el-main class="containMain" >
            <div class="contentTitle">配种信息登记</div>
            <el-form ref="form" :model="formData" label-width="80px" style="margin-top:20px;width:522px" >
                <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">
                    操作牛号：{{tableData[0].value}}
                </div>
                <el-form-item label="建议配种">
                    <el-radio-group v-model="formData.isMate">
                        <el-radio label="0">是</el-radio>
                        <el-radio label="1">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <template v-if="formData.isMate==0">
                    <el-form-item label="配种方式">
                        <el-select v-model="formData.mateMode" placeholder="请选择">
                            <el-option v-for="item in mateModes" :key="item['详细方式']" :label="item['详细方式']" :value="item['详细方式']">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="配种冻精">
                        <el-select v-model="formData.bull" placeholder="请选择">
                            <el-option v-for="item in bulls" :key="item['牛号']" :label="item['牛号']" :value="item['牛号']">
                            </el-option>
                        </el-select>

                        <div>
                            <el-popover placement="right" width="200" trigger="hover">
                                <div class="conetnt">
                                    <div class="item" v-for="(item,key) in bullMsg">
                                        <div class="item-title">{{key}}</div>
                                        <div class="text">{{item}}</div>
                                    </div>
                                </div>
                                <el-button type="text" slot="reference">查看此冻精详情</el-button>
                            </el-popover>
                        </div>
                    </el-form-item>
                </template>
                <template v-else>
                     <el-form-item label="未配原因">
                        <el-select v-model="formData.reason" placeholder="请选择">
                            <el-option v-for="item in reasonList" :key="item['名称']" :label="item['名称']" :value="item['名称']">
                            </el-option>
                        </el-select>

                    </el-form-item>
                </template>

                <el-form-item label="配种员">
                    <el-select v-model="formData.person" placeholder="请选择">
                        <el-option v-for="item in [userInfo.username]" :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='25%'  max-width="450px" style=" margin-left: 10px;">
            <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.getList()
        //配种方式
        this.getHaleInfo('wsCow', 'getUserDict', ['MateMode'], 'list').then((data) => {
            this.mateModes = data
            this.formData.mateMode = data[0]['详细方式']
        })
        //冻精列表
        this.getHaleInfo('wsCow', 'getAllBullByFarmName', [
            this.$store.state.farmName, true
        ], 'list').then((data) => {
            this.bulls = data
        })
         //未配原因
        this.getHaleInfo('wsBreeding', 'getUnmateReason', [], 'list').then((data) => {
            this.reasonList = data
            this.formData.reason = data[0]['详细方式']
        })

    },
    data() {
        return {
            saving: false,
            formData: {
                cowID: '',
                isMate: '0',
                mateMode: '',
                bull: '',
                person: '',
                reason:''
            },
            mateModes: [],
            methods: ['人工观察', '计步器揭发', '同期发情'],
            bulls: [],
            tableData: [],
            list: [],
            editCowInfo: {},
            reasonList:[]
        }
    },
    computed: {
        ...mapState(['userInfo', 'allHeight']),
        pageTitle() {
            return this.$route.meta.title
        },
        bullMsg() {
            var jsonp = {}
            if (this.formData.bull) {
                this.bulls.forEach(item => {
                    if (item['牛号'] == this.formData.bull) {
                        jsonp = item
                    }
                })
            }

            return jsonp


        }
    },
    watch: {
        'formData.reason'() {
            setTimeout(() => {
                this.formData.remark = this.remarks[0]
            }, 100)
        },
    },
    methods: {
        getList() {
            this.getHaleInfo('wsBreeding', 'getMateDataTask', [
                this.$store.state.farmName
            ], 'list').then((data) => {
                console.log(data)
                this.list = data

            })
        },
        onSave() {
            if (this.formData.cowID!=this.editCowInfo['牛号']) {
                this.$message({
                    message: '请录入牛只牛号',
                    type: 'warning'
                });
                return
            }

            if (this.formData.isMate == '1') {
                this.notMatch()
                return
            }


            if (!this.formData.bull) {
                this.$message({
                    message: '请选择冻精',
                    type: 'warning'
                });
                return
            }

            if (!this.formData.person) {
                this.$message({
                    message: '请选择配种员',
                    type: 'warning'
                });
                return
            }
            this.formData.operators = this.userInfo.username

            this.getHaleInfo('wsBreeding', 'addMateInfo', [{
                "cowID": this.formData.cowID,
                "mateDate": Date.parse(new Date()),
                "currFarm": this.$store.state.farmName,
                "currGroup": this.editCowInfo['当前牛舍'],
                "currCategory": this.editCowInfo['牛只类别'],
                "groCode": this.editCowInfo['泌乳状态'],
                "repCode": this.editCowInfo['繁殖状态'],
                "currLact": parseInt(this.editCowInfo['当前胎次']),
                "mateMode": this.formData.mateMode,
                "mateBull": this.formData.bull,
                "mateTimes": parseInt(this.editCowInfo['配次']) + 1,
                "semens": this.bullMsg['女儿数'],
                "operators": this.formData.person
            }], '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.$message({
                        message: '录入成功',
                        type: 'success'
                    });
                    this.formData.cowID = ''
                    this.getList()
                }
            })
        },
        notMatch() {

            if (!this.formData.reason) {
                this.$message({
                    message: '请选择未配原因',
                    type: 'warning'
                });
                return
            }

            if (!this.formData.person) {
                this.$message({
                    message: '请选择配种员',
                    type: 'warning'
                });
                return
            }
            this.getHaleInfo('wsBreeding', 'updateHeatingByNomate', [
                  this.formData.cowID, this.formData.reason, this.formData.person
            ], 'json').then((data) => {
                console.log(data)
                if (data.rtnCode == -1) {
                    this.$alert(data.rtnMessage, '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.formData.cowID = ''
                        }
                    });
                }
                else {
                    this.$message({
                        message: '录入成功',
                        type: 'success'
                    });
                    this.formData.cowID = ''
                    this.getList()
                }
            })
        },
        getNowCow() {

            this.getCowInfo(this.formData.cowID, 2).then((data) => {
                // console.log(data)
                var list = []
                Object.keys(data).forEach(item => {
                    list.push({
                        key: item,
                        value: data[item]
                    })
                })

                this.editCowInfo = data
                // console.log(list)

                this.tableData = list
                if (data['繁殖状态'] == '复检有胎' || data['繁殖状态'] == '初检已孕') {
                    this.$alert('当前牛号已经有胎，不允许录入配种信息，但目前允许选择冻精进行数据查看等。（可通过数据更正方式重置上次的妊检结果为未孕或无胎，再进行操作）', '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.formData.cowID = ''
                        }
                    });
                }
            }).catch(() => {
                this.formData.cowID = ''
            })


        },
        rowClick(row) {
            this.formData = {
                cowID: row['牛号'],
                isMate: '0',
                mateMode: this.mateModes['详细方式'],
                bull: '',
                person: '',
            }

            this.getNowCow()
        }
    }
}
</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;
    span {
        font-size: 13px;
        color: rgba(102, 102, 102, 1);
        line-height: 18px;
        margin-left: 10px;
    }
}

.containAside {
    padding: 0;

    display: flex;
    flex-direction: column;
    .asideInfo {
        flex-grow: 1;
        background: rgba(255, 255, 255, 1);
        border-radius: 4px;
        padding: 15px;
    }
}

.item {
    .item-title {
        font-size: 14px;
        font-weight: bold;

        color: rgba(0, 0, 0, 1);
        line-height: 20px;
        letter-spacing: 1px;
    }

    .text {
        font-size: 13px;
        color: rgba(0, 0, 0, 1);
        line-height: 18px;
        letter-spacing: 1px;
        word-wrap: break-word;
        padding: 10px 0 15px;
    }
}
</style>
