<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> -->
          <el-select v-model="task" placeholder="请选择" size="mini" style="float:right">
            <el-option v-for="item in taskList" :key="item.type" :label="item.name" :value="item.type"></el-option>
          </el-select>
        </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>
        </div>
        <div class="tableNum">{{list.length}}头奶牛</div>
        <el-table class="handleTable" border :data="list" style="width: 100%" :height="(allHeight-220)+'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>
      </div>
    </el-aside>
    <el-main class="containMain">
      <div class="contentTitle">{{pageTitle}}登记</div>
      <el-form ref="form" :model="formData" label-width="120px" style="margin-top:20px;width:522px">
        <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>
        <el-form-item label="是否进行操作">
          <el-radio-group v-model="formData.isEdit" v-if='task!=4&&task!=6'>
            <el-radio label="0">是</el-radio>
          </el-radio-group>
           <el-radio-group v-model="formData.isEdit" v-else>
            <el-radio label="0">阴性</el-radio>
            <el-radio label="1">阳性</el-radio>
          </el-radio-group>
        </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="25%" max-width="450px" style=" margin-left: 10px;">
      <div class="asideInfo">
        <div class="contentTitle">牛只信息</div>
        <el-table class="handleTable" border :data="tableData" style="width: 100%" :height="(allHeight-120)+'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.getTask()
   this.getList('')

  },
  data() {
    return {
      saving: false,
      formData: {
        cowID: '',
        isEdit: '0'
      },
      mateModes: [],
      methods: ['人工观察', '计步器揭发', '同期发情'],
      bulls: [],
      tableData: [],
      list: [],
      editCowInfo: {},
      reasonList: [],
      taskList: [{
        name: '结核初检',
        type: 3
      }, {
        name: '初检结果',
        type: 4
      }, {
        name: '结核复检',
        type: 5
      }, {
        name: '复检结果',
        type: 6
      }],
      task: 3,
      taskNOList: [],
      taskNO: '',
      cowshedList: [],
      cowshed: ''
    }
  },
  computed: {
    ...mapState(['userInfo', 'allHeight']),
    pageTitle() {
      var title=''
      this.taskList.forEach(item=>{
        if(item.type==this.task){
          title=item.name
        }
      })
      return 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()
    },
    task() {
      this.getTask()
      this.list = []
      this.cowshed=''
       this.getList('')
    },
    cowshed() {
      if (this.cowshed) {
        this.getList(this.cowshed)
      }
    }
  },
  methods: {
    getList(cowshed) {
      this.getHaleInfo('wsVeterinary', 'getIQTaskDetail', [
        this.$store.state.farmName, this.task, '', cowshed
      ], 'list').then((data) => {
        console.log(data)
        if (!cowshed) {
          this.cowshedList = data
        }
        else {
          this.list = data
        }


      })
    },
    getTask() {
      this.getHaleInfo('wsVeterinary', 'getIQTask', [
        this.$store.state.farmName, this.task
      ], 'list').then((data) => {

        
        console.log(data)
        this.taskNOList = data


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

      

      this.getHaleInfo('wsVeterinary', 'updateIQInfo', [
         this.formData.cowID, this.editCowInfo['任务单编号'], this.task, (this.task==4||this.task==6)?(this.formData.isEdit=='0'?'阴性':'阳性'):'', '', 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 {
          this.$message({
            message: '录入成功',
            type: 'success'
          });
          this.formData.cowID = ''
          this.getList(this.cowshed)
        }
      })
    },
    notMatch() {

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

      if (!this.formData.person) {
        this.$message({
          message: '请选择配种员',
          type: 'warning'
        });
        return
      }
      this.getHaleInfo('wsBreeding', 'updateHeatingByNomate', [
        this.formData.cowID, this.formData.reason, this.formData.person
      ], '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(data) {

   


      var list = []
      data = this.JsonSort(['牛号', '任务单编号'], data)
      Object.keys(data).forEach(item => {
        list.push({
          key: item,
          value: data[item]
        })
      })

      this.editCowInfo = data


      this.tableData = list



    },
    rowClick(row) {
      this.formData = {
        cowID: row['牛号'],
        isEdit: '0'
      }

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

.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;
}

.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>
