<template>
    <el-container style="flex-grow:1">
        <el-main class="containMain">
            <div class="contentTitle">{{pageTitle}}
                <el-select v-model="transform" size="mini" placeholder="请选择转舍类别" style="float:right">
                    <el-option v-for="item in ['自定义转牛','整舍转牛']" :key="item" :label="item" :value="item">
                    </el-option>
                </el-select>

            </div>
            <el-form ref="form" :model="formData" label-width="80px" style="margin-top:20px;">
                <el-form-item label="" label-width="0" v-if="transform=='自定义转牛'">
                    <!-- <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-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>

                <el-card v-if="formData.list.length>0" style="margin-bottom:20px">
                    <div style="flex-wrap: wrap;display:flex;">
                        <div class="item" v-for="(item,index) in formData.list" style="margin-bottom:10px">
                            <span> {{item}}</span>
                            <i class="el-icon-error" @click="delCow(index)"></i>
                        </div>
                    </div>
                </el-card>

                <el-form-item label="转出牛舍" v-if="transform=='整舍转牛'">
                    <el-select v-model="formData.outCowshed" placeholder="请选择">
                        <el-option v-for="item in cowShedList" :key="item['牛舍编号']" :label="item['牛舍编号']" :value="item['牛舍编号']">
                        </el-option>
                    </el-select>
                    <div class="num">
                        奶牛数：{{outCowshedInfo['当前存栏数']}}
                    </div>
                </el-form-item>

                <el-form-item label="转入牛舍">
                    <el-select v-model="formData.inCowshed" placeholder="请选择">
                        <el-option v-for="item in cowShedList" :key="item['牛舍编号']" :label="item['牛舍编号']" :value="item['牛舍编号']">
                        </el-option>
                    </el-select>

                    <div class="num" v-if="transform=='整舍转牛'">
                        奶牛数：{{inCowshedInfo['当前存栏数']}}
                    </div>
                </el-form-item>
                <el-form-item label="转舍原因">
                    <el-select v-model="formData.reason" placeholder="请选择">
                        <el-option v-for="item in reasons" v-if="reasons.length>0" :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.getCowshed()

    },
    data() {
        return {
            saving: false,
            formData: {
                "cowID": '',
                'inCowshed': '',
                'outCowshed': '',
                'reason': '',
                'list': []
            },
            reasons: ['按产后天数分群', '按月龄分群', '按在胎天数分群', '病牛出院', '病牛转舍', '哺乳转断奶', '产后调整', '产后牛转群', '初产转高产', '传染病隔离', '待产', '待淘牛集中', '犊牛入舍', '断奶转育成', '干奶修蹄转群', '干奶转群', '高产牛分栏', '过抗转舍', '合栏饲养', '混群调整', '牛群普查调整', '围产调整', '新发其他疾病转群', '新发乳房炎转群', '新发蹄病转群', '育成牛按体高分群', '整舍调整', '转参配牛', '转场', '转低产牛', '转高产1', '转高产2', '转禁配牛', '转流产牛', '转有胎牛', '转早产牛'],
            tableData: [],
            transform: '自定义转牛',
            cowShedList: []
        }
    },
    computed: {
        ...mapState(['userInfo', 'allHeight']),
        pageTitle() {
            return this.$route.meta.title
        },
        inCowshedInfo() {
            var jsonp = {}
            if (this.formData.inCowshed) {
                this.cowShedList.forEach(item => {
                    if (item['牛舍编号'] == this.formData.inCowshed) {
                        jsonp = item
                    }
                })

            }

            return jsonp
        },
        outCowshedInfo() {
            var jsonp = {}
            if (this.formData.outCowshed) {
                this.cowShedList.forEach(item => {
                    if (item['牛舍编号'] == this.formData.outCowshed) {
                        jsonp = item
                    }
                })

            }

            return jsonp
        },
    },
    watch: {
        'formData.reason'() {
            setTimeout(() => {
                this.formData.remark = this.remarks[0]
            }, 100)
        },
        // 'formData.cowID'() {
        //     if (this.formData.cowID.length != 8) {
        //         return
        //     }
        //     this.getNowCow()
        // }
    },
    methods: {
        getCowshed() {
            this.getHaleInfo('wsCow', 'getAllGroupByFarmName', [
                this.$store.state.farmName
            ], 'list').then((data) => {
                this.cowShedList = data
            })
        },
        onSave() {
            if (this.transform == '自定义转牛' && this.formData.list.length == 0) {
                this.$message({
                    message: '请录入转舍牛只',
                    type: 'warning'
                });
                return
            }
            if (this.transform == '整舍转牛' && !this.formData.outCowshed) {
                this.$message({
                    message: '请录选择转出牛舍',
                    type: 'warning'
                });
                return
            }
            if (!this.formData.inCowshed) {
                this.$message({
                    message: '请录选择转入牛舍',
                    type: 'warning'
                });
                return
            }

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

            if (this.transform == '整舍转牛') {
                this.saveAll()
                return
            }



            this.getHaleInfo('wsCow', 'bulkChangeGroup', [
                this.formData.list, this.formData.inCowshed, this.formData.reason, this.userInfo.username
            ], 'json').then((data) => {
                console.log(data)
                if (data.rtnCode == -1) {
                    this.$alert(data.rtnMessage, '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                            this.formData.cowID = ''
                        }
                    });
                }
                else {
                    if (this.transform == '自定义转牛') {
                        this.$alert('成功将' + data.rtnCode + '头牛进行转舍操作<br/>未成功转舍的牛只存在以下可能：<br/>1、牛号未在系统登录；<br/>2、牛只已经离场；<br/>3、当前牛舍与目标牛舍一致；<br/>4、牛只不在当前牧场', '提示', {
                            confirmButtonText: '确定',
                            dangerouslyUseHTMLString: true,
                            callback: action => {

                            }
                        });
                    }
                    else {
                        this.$message({
                            message: '录入成功',
                            type: 'success'
                        });
                    }


                }
            })
        },
        saveAll() {
            this.getHaleInfo('wsCow', 'bulkChangeGroupByWholeGroup', [
                this.$store.state.farmName, this.formData.outCowshed, this.formData.inCowshed, this.formData.reason, 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.$alert('成功将' + data.rtnCode + '头牛进行转舍操作<br/>未成功转舍的牛只存在以下可能：<br/>1、牛号未在系统登录；<br/>2、牛只已经离场；<br/>3、当前牛舍与目标牛舍一致；<br/>4、牛只不在当前牧场', '提示', {
                        confirmButtonText: '确定',
                        dangerouslyUseHTMLString: true,
                        callback: action => {
                            this.getCowshed()
                        }
                    });
                }
            })
        },
        getNowCow() {

            this.getCowInfo(this.formData.cowID, 99).then((data) => {
                this.formData.cowID = ''
                if (data['离场日期']) {
                    this.$alert('当前牛只已经离场，不能转舍。', '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                        }
                    });
                    return
                }

                var list = []
                Object.keys(data).forEach(item => {
                    list.push({
                        key: item,
                        value: data[item]
                    })
                })
                this.tableData = list
                if(this.formData.list.indexOf(data['牛号'])==-1){
this.formData.list.push(data['牛号'])
                }
                
            }).catch(() => {
                this.formData.cowID = ''
            })
        },
        delCow(index) {
            this.formData.list.splice(index, 1)
        }
    }
}
</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;
    }
}

.num {
    display: inline-block;
    width: 138px;
    height: 40px;
    background: rgba(225, 228, 230, 1);
    border-radius: 4px;
    font-size: 14px;
    font-weight: bold;
    color: rgba(0, 0, 0, 1);
    line-height: 40px;
    text-align: center;
    margin-left: 20px;
}

.item {
    width: 120px;
    height: 30px;
    background: rgba(236, 245, 255, 1);
    border-radius: 2px;
    border: 1px solid rgba(0, 150, 224, 1);
    font-size: 14px;
    font-weight: bold;
    color: rgba(0, 150, 224, 1);
    line-height: 30px;
    letter-spacing: 1px;
    padding: 0 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-right: 10px;
    i {
        cursor: pointer;
    }
}
</style>
