<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="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"></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-column prop="预产日期" label="预产日期" width="140" align="center"></el-table-column>
          <el-table-column prop="当前胎次" label="胎次" align="center" width="70"></el-table-column>
        </el-table>
      </div>
    </el-aside>
    <el-main class="containMain">
      <div class="contentTitle">{{pageTitle}}</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" clearable></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="chooseCowshed" placeholder="请选择">
            <el-option v-for="item in chooseCowshedList" :key="item['牛舍编号']" :label="item['牛舍编号']" :value="item['牛舍编号']"></el-option>
          </el-select>
        </el-form-item>

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

        <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%'>
      <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('wsCalving', 'getCalvEase', [], 'list').then((data) => {
      this.difficultyList = data
      if (data.length > 0) {
        this.formData.difficulty = data[0]['名称']
        this.formData.describe = this.describeList[0]
      }
    })

    this.getHaleInfo('wsCow', 'getAllGroupByFarmName', [
      this.$store.state.farmName
    ], 'list', '牛舍编号').then((data) => {
      var list = []
      data.forEach(item => {
        if (item["牛舍类别"] == "围产牛舍") {
          list.push(item)
        }
      })
      this.chooseCowshedList = list
      if (list.length > 0) {
        this.chooseCowshed = list[0]['牛舍名称']
      }

    })




  },
  data() {
    return {
      saving: false,
      formData: {
        cowID: '',
        difficulty: '',
        method: '人工观察',
        num: '1',
        describe: '',
        type: '正产',
        isSeeTires: true,
      },
      methods: ['人工观察', '计步器揭发', '同期发情'],
      tableData: [],
      difficultyList: [],
      editCowInfo: {},
      list: [],
      cowShed: '',
      chooseCowshedList: [],
      chooseCowshed: '',
      chooseList: []
    }
  },
  computed: {
    ...mapState(['userInfo', 'allHeight']),
    describeList() {
      var list = []
      switch (this.formData.num) {
        case "1":
          list = ['单母胎', '单公胎']
          break;
        case "2":
          list = ['双母胎', '双公胎', '龙凤胎']
          break;
        case "3":
          list = ['三母胎', '三公胎', '两公一母', '两母一公']
          break;
      }
      return list
    },
    pageTitle() {
      return this.$route.meta.title
    },
    filitList() {
      var list = []
      this.list.forEach(item => {
        if (item['当前牛舍'] == this.cowShed) {
          list.push(item)
        }
      })
      return list
    },
    cowShedList() {
      var list = []
      this.list.forEach(item => {
        if (list.indexOf(item['当前牛舍']) == -1) {
          list.push(item['当前牛舍'])
        }
      })
      return list
    }
  },
  watch: {
    'formData.cowID'() {
      if (this.formData.cowID.length != 8) {
        return
      }
      this.getNowCow()
    },
    'formData.num'() {
      setTimeout(() => {
        this.formData.describe = this.describeList[0]
      }, 100)
    }
  },
  methods: {
    getList(type) {
      this.getHaleInfo('wsCalving', 'getPerinatalTask', [
        this.$store.state.farmName
      ], 'list').then((data) => {
        this.list = data
        if (data.length > 0) {
          setTimeout(() => {
            this.cowShed = this.cowShedList[0]
          }, 500)
        }
        if (this.list.length == 0 && type == 1) {
          this.$alert('暂无需要处理的奶牛', '提示', {
            confirmButtonText: '确定',
            callback: action => {

            }
          });
        }

      })
    },
    onSave() {
      if (this.chooseList.length == 0) {
        this.$message({
          message: '请录入转舍牛只牛号',
          type: 'warning'
        });
        return
      }


      this.getHaleInfo('wsCow', 'bulkChangeGroup', [
        this.chooseList, this.chooseCowshed, '围产调整', 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.chooseList = []
              this.formData.cowID = ''
            }
          });


        }
      })
    },
    getNowCow() {

      if (this.formData.cowID.length != 8) {
        return
      }
      var flag = false

      this.list.forEach(item => {
        if (item['牛号'] == this.formData.cowID) {
          if (this.chooseList.indexOf(item['牛号']) == -1) {
            this.chooseList.push(item['牛号'])
          }


          var list = []
          console.log(item)
          Object.keys(item).forEach(todo => {
            list.push({
              key: todo,
              value: item[todo]
            })
          })

          // console.log(list)

          this.tableData = list
          flag = true
        }
      })

      if (!flag) {
        this.$message({
          message: '牛号不在围产列表中',
          type: 'warning'
        });
        this.formData.cowID = ''
      }


    },
    rowClick(row) {
      this.formData.cowID = row['牛号']
      this.getNowCow()
    },
    delCow(index) {
      this.chooseList.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;
  }
}
.tableNum {
  font-size: 13px;
  font-weight: bold;
  color: rgba(102, 102, 102, 1);
  line-height: 18px;
  padding-bottom: 15px;
}
.selectContent {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0;
}

.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;
  margin-bottom: 10px;
  i {
    cursor: pointer;
  }
}
</style>
