<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="70">
                    </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 v-if="task==2" prop="配种日期" label="配种日期" width="120px" align="center">
                    </el-table-column>
                    <el-table-column v-if="task==2" prop="预产日期" label="预产日期" width="120px" align="center">
                    </el-table-column>
                    <el-table-column v-if="task!=2" prop="产犊日期" label="产犊日期" width="120px" align="center">
                    </el-table-column>
                    <el-table-column v-if="task==1" prop="产犊类型" label="产犊类型" align="center">
                    </el-table-column>
                    <el-table-column v-if="task==1" prop="提示日期" label="提示日期" width="120px" align="center">
                    </el-table-column>
                    <el-table-column v-if="task==3" prop="投喂博威钙次数" label="投喂博威钙次数" width="120px" align="center">
                    </el-table-column>
                    <el-table-column v-if="task==3" prop="投喂博威钙时间" label="投喂博威钙时间" width="160px" 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;" label-width="120px">
                <el-form-item label="" label-width="0">
                    <el-input v-model="formData.cowID" disabled 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>
                <div style="margin:20px">
                    <div style="margin-bottom:10px">{{nowTaskJson.use}}</div>

                    <el-radio-group v-model="formData.isUse">
                        <el-radio label="1">是</el-radio>
                        <el-radio v-if="task==1" label="0">否</el-radio>
                    </el-radio-group>
                </div>
                <template v-if="task==1&&formData.isUse=='1'">
                    <el-form-item label="体温">
                        <el-select v-model="bodyTemp" placeholder="请选择围产期护理" style="width:230px">
                            <el-option v-for="item in temperatureList" :key="item" :label="item" :value="item">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="疾病名称">
                        <el-select v-model="name" placeholder="请选择围产期护理" style="width:230px">
                            <el-option v-for="item in allDiseaseInfo" :key="item['名称']" :label="item['名称']" :value="item['名称']">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="转入兽医院">
                        <el-switch v-model="formData.isCheck">
                        </el-switch>
                    </el-form-item>
                </template>

                <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', [
            'PostpartumCare'
        ], 'list').then((data) => {
            this.allDiseaseInfo = data
            if (data.length > 4) {
                this.name = data[3]['名称']
            } else if (data.length > 0) {
                this.name = data[0]['名称']
            }


        })

    },
    data() {
        return {
            saving: false,
            formData: {
                cowID: '',
                isUse: '1',
            },
            mateModes: [],
            methods: ['人工观察', '计步器揭发', '同期发情'],
            bulls: [],
            tableData: [],
            list: [],
            cowshed: '',
            task: 3,
            name: '',
            isCheck: false,
            bodyTemp: '38.5-39度',
            temperatureList: ['37度及以下', '37-37.5度', '37.5-38度', '38-38.5度', '38.5-39度', '39-39.5度', '39.5度-40度', '40-40.5度', '40.5-41度', '41度及以上'],
            allDiseaseInfo: [],
            taskList: [
            //     {
            //     name: '产前保健',
            //     type: 2,
            //     use: '请注射维生素E'
            // }
             {
                name: '投喂博威钙',
                type: 3,
                use: '请投喂博威钙'
            }
            // , {
            //     name: '产后保健',
            //     type: 1,
            //     use: '2-3胎次需要投服一粒博威钙，继续护理请点"是",结束护理请点"否"（注意：结束护理后，将不再提示护理操作）'
            // }
            ]
        }
    },
    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)
        }
    },
    methods: {
        getList(type) {
            this.getHaleInfo('wsVeterinary', 'getPCTask', [
                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 list = [{
                name: '产前保健',
                type: 'addInjectVitaInfo',
                params: [
                    this.formData['牛号'], this.formData['当前胎次'], this.userInfo.username
                ]
            }, {
                name: '投喂博威钙',
                type: 'addFeedCalInfo',
                params: [
                    this.formData['牛号'], Date.parse(new Date(this.formData['产犊日期'])), this.userInfo.username
                ]
            }, {
                name: '产后保健',
                type: 'addPCFinishInfo',
            }]

            if (this.task == 1) {
                if (this.formData.isUse == '0') {
                    this.badTreat()
                    return
                }
                else {
                    this.saveFeed()
                    return
                }
            }

            var jsonp = {}

            list.forEach(item => {
                if (item.name == this.nowTaskJson.name) {
                    jsonp = item
                }
            })

            this.getHaleInfo('wsVeterinary', jsonp.type, jsonp.params, '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) {
            var list = []
            Object.keys(row).forEach(item => {
                list.push({
                    key: item,
                    value: row[item]
                })
            })

            // console.log(list)

            this.tableData = list



        },
        rowClick(row) {
            row.cowID = row['牛号']
            row.isUse = '1'
            this.formData = { ...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;
    }
}
</style>
