<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>
                <div class="selectContent">
                    <el-select v-model="task" placeholder="请选择围产期护理" style="width:230px">
                        <el-option v-for="item in taskList" :key="item.type" :label="item.name" :value="item.type">
                        </el-option>
                    </el-select>

                    <el-select v-model="cowshed" placeholder="请选择牛舍" style="width:230px">
                        <el-option v-for="item in cowShedList" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                    <!-- <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" width="140">
                    </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-column prop="配种日期" label="配种日期" width="140" align="center">
                    </el-table-column>

                </el-table>
            </div>
        </el-aside>
        <el-main class="containMain">
            <div class="contentTitle">{{nowTaskJson.name}}信息登记</div>
            <el-form ref="form" :model="formData" style="margin-top:20px;width:522px;" label-width="80px">
                <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">
                    操作牛号：{{chooseCowInfo['牛号']}}
                </div>

                <el-form-item label="修蹄情况">
                    <div class="HoofList">
                        <div class="hoof-item" :class="{active:formData.nursing_FL==1}" @click="formData.nursing_FL=(formData.nursing_FL==1?0:1)">
                            {{formData.nursing_FL==1?'已修':'未修'}}
                        </div>
                        <div class="hoof-item" :class="{active:formData.nursing_BL==1}" @click="formData.nursing_BL=(formData.nursing_BL==1?0:1)">
                            {{formData.nursing_BL==1?'已修':'未修'}}

                        </div>
                        <div class="hoof-item" :class="{active:formData.nursing_FR==1}" @click="formData.nursing_FR=(formData.nursing_FR==1?0:1)">
                            {{formData.nursing_FR==1?'已修':'未修'}}

                        </div>
                        <div class="hoof-item" :class="{active:formData.nursing_BR==1}" @click="formData.nursing_BR=(formData.nursing_BR==1?0:1)">
                            {{formData.nursing_BR==1?'已修':'未修'}}

                        </div>
                    </div>

                </el-form-item>

                <el-form-item label="破行级别">
                    <el-select v-model="formData.level" placeholder="请选择围产期护理" style="width:230px">
                        <el-option v-for="item in levelList" :key="item" :label="item" :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="蹄病类型">
                    <el-select v-model="formData.type" placeholder="请选择围产期护理" style="width:230px">
                        <el-option v-for="item in typeList" :key="item['名称']" :label="item['名称']" :value="item['名称']">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="上次修蹄">
                    <el-input v-model="formData.nursingDate" disabled style="width:230px"></el-input>
                </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(1)


        //获取全部疾病
        this.getHaleInfo('wsCow', 'getUserDict', [
            'HoofTreatment'
        ], 'list').then((data) => {
            this.typeList = [{ 名称: '暂无蹄病' }]
            this.typeList = this.typeList.concat(data)
            this.formData.type = this.typeList[0]['名称']
        })

    },
    data() {
        return {
            saving: false,
            formData: {
                cowID: '',
                type: '',
                level: '0分：正常',
                nursingDate: '',
                nursing_FL: 1,
                nursing_BL: 1,
                nursing_FR: 1,
                nursing_BR: 1
            },
            mateModes: [],
            levelList: ['0分：正常', '1分：轻微破行', '2分：中度破行', '3分：破行', '4分：严重破行'],
            bulls: [],
            tableData: [],
            list: [],
            cowshed: '',
            task: 2,
            typeList: [],
            taskList: [{
                name: '干奶修蹄',
                type: 1,
                use: '请注射维生素E'
            }
                , {
                name: '泌乳牛普修',
                type: 2,
                use: '请投喂博威钙'
            }
                , {
                name: '青年牛修蹄',
                type: 3,
                use: '2-3胎次需要投服一粒博威钙，继续护理请点"是",结束护理请点"否"（注意：结束护理后，将不再提示护理操作）'
            }],
            chooseCowInfo:{}
        }
    },
    computed: {
        ...mapState(['userInfo', 'allHeight']),
        pageTitle() {
            return this.$route.meta.title
        },

        cowShedList() {
            var list = ['全部牛舍']
            this.list.forEach(item => {
                if (list.indexOf(item['当前牛舍']) == -1) {
                    list.push(item['当前牛舍'])
                }
            })
            return list
        },
        filitList() {
            var list = []
            if (this.cowshed) {
                if (this.cowshed == '全部牛舍') {
                    list = this.list
                }
                else {
                    this.list.forEach(item => {
                        if (item['当前牛舍'] == this.cowshed) {
                            list.push(item)
                        }
                    })
                }
            }
            return list
        },
        nowTaskJson() {
            var jsonp = {}
            if (this.task) {
                this.taskList.forEach(item => {
                    if (this.task == item.type) {
                        jsonp = item
                    }
                })
            }

            return jsonp
        }
    },
    watch: {
        task() {
            this.cowshed = ''
            this.formData.cowID = ''
            this.getList(1)
        },
        'formData.cowID'() {
            if (this.formData.cowID.length != 8) {
                return
            }
            this.getNowCow()
        }
    },
    methods: {
        getList(type) {
            this.getHaleInfo('wsVeterinary', 'getHoofNursingTask', [
                this.$store.state.farmName, this.task
            ], 'list').then((data) => {
                this.list = data

                if (this.list.length == 0 && type == 1) {
                    this.$alert('暂无需要处理的奶牛', '提示', {
                        confirmButtonText: '确定',
                        callback: action => {

                        }
                    });
                }

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


            var jsonp = {}



            this.getHaleInfo('wsVeterinary', 'addHoofNursing', [
                {
                    cowID: this.formData.cowID,
                    nursingDate: Date.parse(new Date(this.formData.nursingDate)),
                    nursing_FL: this.formData.nursing_FL,
                    nursing_BL:this.formData.nursing_BL,
                    nursing_FR: this.formData.nursing_FR,
                    nursing_BR: this.formData.nursing_BR,
                    level: this.formData.level,
                    type: this.formData.type,
                    recheckDate: Date.parse(new Date()),//复查日期F
                },
                this.chooseCowInfo['配种日期'] ? Date.parse(new Date(this.chooseCowInfo['配种日期'])) : Date.parse(new Date())

            ], '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()
                }
            })
        },
        saveFeed() {
            var name = this.name.split('-')
            this.getHaleInfo('wsVeterinary', 'addPCInfo', [
                {
                    cowID: this.formData['牛号'],
                    careDate: Date.parse(new Date()),
                    calvingDate: Date.parse(new Date(this.formData['产犊日期'])),
                    bodyTemp: this.temperatureList.indexOf(this.bodyTemp),
                    diseaseDate: Date.parse(new Date(this.formData['提示日期'])),
                    diseaseCategory: name[0],
                    diseaseName: name[1],
                    isToHospital: this.isCheck
                }
            ], '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()
                }
            })
        },
        badTreat() {
            this.getHaleInfo('wsVeterinary', 'addPCFinishInfo', [
                this.formData['牛号'], Date.parse(new Date(this.formData['产犊日期'])), '产犊', this.formData['产犊类型'], this.userInfo.username
            ], '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(row) {
            this.getHaleInfo('wsCow', 'getCowExtendInfoByCowID', [this.formData.cowID, 202], 'json').then((data) => {
                // console.log(data)
                this.chooseCowInfo=data
                var list = []
                Object.keys(data).forEach(item => {
                    list.push({
                        key: item,
                        value: data[item]
                    })
                })

                this.formData.nursingDate = 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['牛号']
            this.getNowCow(row)
        }
    }
}
</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;
    }
}

.HoofList {
    display: flex;
    width: 130px;
    flex-wrap: wrap;
    justify-content: space-between;
    .hoof-item {
        width: 60px;
        height: 60px;
        border-radius: 4px;
        font-size: 13px;
        font-weight: bold;
        color: rgba(255, 255, 255, 1);
        line-height: 18px;
        text-align: center;
        line-height: 60px;
        background: rgb(0, 85, 128);
        margin-bottom: 10px;
        cursor: pointer;

        &.active {
            background: rgba(133, 195, 6, 1);
        }
    }
}
</style>
