<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" width="140"></el-table-column>
          <el-table-column prop="疾病类型" label="疾病类型" align="center" width="100"></el-table-column>
          <el-table-column prop="疾病名称" label="疾病名称" align="center"></el-table-column>
          <el-table-column prop="发病揭发人" label="发病揭发人" align="center" width="100"></el-table-column>
          <el-table-column prop="过抗检测" label="过抗检测" align="center" width="100"></el-table-column>
        </el-table>
      </div>
    </el-aside>
    <el-main class="containMain">
      <div class="contentTitle">过抗采样信息登记</div>
      <el-form ref="form" :model="formData" style="margin-top:20px;width:522px;" label-width="120px">
        <el-form-item label label-width="0">
          <el-autocomplete v-model="formData.cowID" style="width:300px" prefix-icon="el-icon-search" :fetch-suggestions="querySearchAsync" placeholder="请输入内容" @select="handleSelect"></el-autocomplete>
          <!-- <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-form-item>
        <div class="sub" v-if="tableData.length>0">操作牛号：{{tableData[0].value}}</div>
        <div style="margin:20px">
          <div style="margin-bottom:10px">请挤奶采样</div>

          <el-radio-group v-model="formData.isUse">
            <el-radio label="1">确认</el-radio>
            <!-- <el-radio label="0">取消</el-radio> -->
          </el-radio-group>
        </div>

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

  },
  data() {
    return {
      formData: {
        cowID: '',
        isUse: '1',
      },
      tableData: [],
      list: [],
      timeout: '',
      editCow: {}
    }
  },
  computed: {
    ...mapState(['userInfo', 'allHeight']),
    pageTitle() {
      return this.$route.meta.title
    },
    restaurants() {
      var list = []
      this.list.forEach(item => {
        list.push({
          value: item['牛号']
        })
      })
      return list
    }
  },
  watch: {
  },
  methods: {
    querySearchAsync(queryString, cb) {
      var restaurants = this.restaurants;
      var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        cb(results);
      }, 3000 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    handleSelect(item) {
      this.list.forEach(todo => {
        if (todo['牛号'] == item.value) {
          this.getNowCow(todo)
        }
      })
    },
    getList(type) {
      this.getHaleInfo('wsVeterinary', 'getSampData', [
        this.$store.state.farmName
      ], 'list', '', 'YYYY-MM-DD HH:mm:ss').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
      }


      if (this.formData.cowID != this.tableData[0].value) {
        this.$message({
          message: '该牛只号不需要采样',
          type: 'warning'
        });
        return
      }

      console.log(Date.parse(new Date(this.editCow['发病日期'])))

      this.getHaleInfo('wsVeterinary', 'diseaseSamp', [
        this.formData.cowID, Date.parse(new Date(this.editCow['发病日期'])), this.editCow['疾病类型'], this.editCow['疾病名称']
      ], '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()
        }
      })
    },
    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 data = this.JsonSort(['牛号', '当前牛舍', '疾病类型', '疾病名称'], row)
      var list = []
      Object.keys(data).forEach(item => {
        list.push({
          key: item,
          value: data[item]
        })
      })

      // console.log(list)
      this.editCow = data

      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>
