<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></span>
                </div>
                <div class="selectContent">
                    <el-select v-model="cowshed" placeholder="请选择牛舍" style="width:180px">
                        <el-option v-for="item in cowShedList" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>

                    <div style="width:180px">
                        <el-input v-model="filitDay" placeholder="请输入配后天数" style="wdith:180px">
                            <template slot="prepend">
                                <i class="el-icon-arrow-right"></i>
                            </template>
                        </el-input>
                    </div>
                    <el-button type="primary" plain @click="filitDayList=filitDay">筛选</el-button>

                </div>
                <div class="tableNum">
                    {{filitList.length}}头奶牛
                </div>
                <el-table class="handleTable" border :data="filitList" style="width: 100%" :height='(allHeight-184)+"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" >
                    </el-table-column> -->
                    <el-table-column prop="最近配种日期" label="最近配种日期" min-width="100" 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-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-input v-model="formData.cowID" prefix-icon="el-icon-search" style="width:300px" maxlength="8"></el-input>
                    <el-button type="primary" @click="getNowCow" style="width:90px;margin-left:12px">确认</el-button>
                </el-form-item>
                <div class="sub" v-if="tableData.length>0">
                    操作牛号：{{tableData[0].value}}
                </div>

                <el-form-item label="妊检方法">
                    <el-select v-model="formData.geCheckMethod" placeholder="请选择">
                        <el-option v-for="item in ['直肠检测法']" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="妊检结果">
                    <el-select v-model="formData.geCheckResult" placeholder="请选择">
                        <el-option v-for="item in ['初检已孕','初检未孕']" :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" max-width="450px" width='25%' 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
        // })

    },
    data() {
        return {
            saving: false,
            formData: {
                cowID: '',
                geCheckMethod: '直肠检测法',
                geCheckResult: '',
                isReCheck: false,
                operators: '',
            },
            mateModes: [],
            methods: ['人工观察', '计步器揭发', '同期发情'],
            bulls: [],
            tableData: [],
            list: [],
            cowshed: '',
            cowShedList: [],
            filitDay: 40,
            filitDayList: 40
        }
    },
    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


        },
        filitList() {
            var list = []
            if (this.cowshed) {
                this.list.forEach(item => {
                    if (item['当前牛舍'] == this.cowshed && item['配后天数'] > this.filitDayList) {
                        list.push(item)
                    }
                })
            }
            return list
        }
        // cowShedList() {
        //     var list = []
        //     this.list.forEach(item => {
        //         var jsonp = { ...item }
        //         if (list.indexOf(jsonp['当前牛舍'] === -1)) {
        //             list.push(jsonp['当前牛舍'])
        //         }
        //     })
        //     return list
        // }
    },
    watch: {
        'formData.reason'() {
            setTimeout(() => {
                this.formData.remark = this.remarks[0]
            }, 100)
        },
        'formData.cowID'() {
            if (this.formData.cowID.length != 8) {
                return
            }
            this.getNowCow()
        }
    },
    methods: {
        getList() {
            this.getHaleInfo('wsBreeding', 'getGeCheckDataTask', [this.$store.state.farmName, 1], 'list').then((data) => {
                console.log(data)
                this.list = data

                data.forEach(item => {
                    if (this.cowShedList.indexOf(item['当前牛舍']) === -1) {
                        this.cowShedList.push(item['当前牛舍'])
                    }
                })

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

            if (!this.formData.geCheckResult) {
                this.$message({
                    message: '请选择妊检结果',
                    type: 'warning'
                });
                return
            }

            this.formData.geCheckDate = Date.parse(new Date())
            this.formData.operators = this.userInfo.username

            this.getHaleInfo('wsBreeding', 'addGeCheckInfo', [this.formData], '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]
                    })
                })

                // 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['牛号'],
                geCheckMethod: '直肠检测法',
                geCheckResult: '',
                isReCheck: false,
                operators: ''
            }

            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;
    }
}

.selectContent {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 0;
}

.tableNum {
    font-size: 13px;
    font-weight: bold;
    color: rgba(102, 102, 102, 1);
    line-height: 18px;
    padding-bottom: 15px;
}

.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>
