<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="补登记数量" width="120" 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" placeholder="请输入新生犊牛牛号" 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">母牛号：{{editCowInfo['母牛号']}}</div>

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

        <el-form-item label="出生重">
          <el-input v-model="formData.weight" placeholder="请输入新生犊牛牛号" style="width:217px"></el-input>
        </el-form-item>

        <el-form-item label="是否活犊">
          <el-switch v-model="formData.isAlive" active-text="是" inactive-text="否"></el-switch>
        </el-form-item>

        <el-form-item label="是否母犊">
          <el-switch v-model="formData.isFemale" active-text="是" inactive-text="否"></el-switch>
        </el-form-item>

        <el-form-item label="是否留养">
          <el-switch v-model="formData.isStay" active-text="是" inactive-text="否"></el-switch>
        </el-form-item>

        <el-form-item label="产犊描述">
          <el-select v-model="formData.healthy" placeholder="请选择">
            <el-option v-for="item in healthyList" :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() {
    //配种列表
    if (this.$route.query.cowID) {
      this.getList(this.$route.query.cowID)
    }
    else {
      this.getList()
    }

    //健康情况
    this.getHaleInfo('wsCalving', 'getCalvHealthDesc', [], 'list').then((data) => {
      this.healthyList = data
    })
    //出生牛舍
    this.getHaleInfo('wsCow', 'getAllGroupByFarmName', [
      this.$store.state.farmName
    ], 'list').then((data) => {
      data.forEach(item => {
        if (item['牛舍类别'] == '哺乳犊牛舍') {
          this.cowshedList.push(item)
        }
      })
      if (this.cowshedList.length > 0) {
        this.formData.cowshed = this.cowshedList[0]['牛舍编号']
      }


    })
    //  //未配原因
    // this.getHaleInfo('wsBreeding', 'getUnmateReason', [], 'list').then((data) => {
    //     this.reasonList = data
    //     this.formData.reason = data[0]['详细方式']
    // })

  },
  data() {
    return {
      saving: false,
      formData: {
        cowID: '',
        mCowID: '',
        healthy: '健康',
        cowshed: '',
        isAlive: false,
        isFemale: false,
        isStay: false,
        weight: 0
      },
      healthyList: [],
      methods: ['人工观察', '计步器揭发', '同期发情'],
      bulls: [],
      tableData: [],
      list: [],
      editCowInfo: {},
      reasonList: [],
      cowshedList: []
    }
  },
  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)
    },
    'formData.cowID'() {
      if (this.formData.cowID.length != 8) {
        return
      }
      this.getNowCow()
    }
  },
  methods: {
    getList(cowID) {
      this.getHaleInfo('wsCalving', 'getRegTask', [
        this.$store.state.farmName
      ], 'list', '母牛号').then((data) => {
        this.list = data
        if (cowID) {
          data.forEach(item => {
            if (cowID == item['母牛号']) {
              this.rowClick(item)
            }
          })

        }

      })
    },
    onSave() {
      if (!this.formData.mCowID) {
        this.$message({
          message: '请选择母牛',
          type: 'warning'
        });
        return
      }

      if (parseInt(this.formData.weight) > 100) {
        this.$message({
          message: '新生犊牛的出生重错误',
          type: 'warning'
        });
        return
      }
      if (this.formData.isStay && !this.formData.isAlive) {
        this.$message({
          message: '死犊不能留养',
          type: 'warning'
        });
        return
      }
      if (!this.formData.isStay && this.formData.cowID) {
        this.$message({
          message: '不留养牛只不需要录入牛号',
          type: 'warning'
        });
        return
      }

      if(this.formData.isStay && !this.formData.weight){
         this.$message({
          message: '留养牛只需要录入重量',
          type: 'warning'
        });
        return
      }

      if (this.formData.isStay && !this.formData.cowID) {
        this.$message({
          message: '请输入牛号',
          type: 'warning'
        });
        return
      }




      this.getHaleInfo('wsCalving', 'addCalvesInfo', [
        this.formData.mCowID, Date.parse(new Date(this.editCowInfo['产犊日期'])), this.formData.cowID ? this.formData.cowID : '', this.$store.state.farmNO, this.formData.cowshed, this.formData.weight, '', this.formData.isAlive, this.formData.isFemale, this.formData.isStay, '1', this.formData.healthy, '', this.userInfo.username

      ], '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 {
          if (parseInt(this.editCowInfo['补登记数量']) != 1) {
            this.$alert('成功录入一只犊牛信息,该母牛还剩' + (parseInt(this.editCowInfo['补登记数量']) - 1) + '只犊牛需要登记', '提示', {
              confirmButtonText: '确定',
              type: 'success',
              callback: action => {
                this.formData.cowID = ''
                this.getList(this.editCowInfo['母牛号'])
              }
            });

          }
          else {
            this.$message({
              message: '录入成功',
              type: 'success'
            });
            this.formData.cowID = ''
            this.editCowInfo = {}
            this.tableData = []
            this.getList()
            this.formData.isAlive = false
            this.formData.isStay = false
          }

        }
      })
    },
    getNowCow() {
      this.getCowInfo(this.formData.cowID, 1, true).then((data) => {
        this.formData.isAlive = true
        this.formData.isStay = true
      }).catch(() => {
        this.formData.cowID = ''
      })



    },
    getFemale() {
      switch (this.editCowInfo['产犊描述']) {
        case '单母胎': case '双母胎': case '三母胎':
          this.formData.isFemale = true
          break;
        case '单公胎': case '双公胎': case '三公胎':
          this.formData.isFemale = false
          break;
        case '龙凤胎':
          if (this.editCowInfo['补登记数量'] == '1') {
            this.formData.isFemale = true
          }
          else {
            this.formData.isFemale = false
          }
          break;

        case '两公一母':
          if (this.editCowInfo['补登记数量'] == '1') {
            this.formData.isFemale = true
          }
          else {
            this.formData.isFemale = false
          }
          break;
        case '两母一公':
          if (this.editCowInfo['补登记数量'] == '3') {
            this.formData.isFemale = false
          }
          else {
            this.formData.isFemale = true
          }
          break;
      }
    },
    rowClick(row) {
      this.formData.mCowID = row['母牛号']
      this.editCowInfo = row

      var list = []
      Object.keys(row).forEach(item => {
        list.push({
          key: item,
          value: row[item]
        })
      })
      this.tableData = list
      this.getFemale()


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