<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-checkbox v-model="batch" style="float:right">批量</el-checkbox>
        </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-224)+'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" width="140"></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">断奶信息登记</div>
      <el-form ref="form" :model="formData" style="margin-top:20px;width:522px;" label-width="80px">
        <el-form-item label label-width="0">
          <!-- <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 v-model="formData.cowID" prefix-icon="el-icon-search" style="width:300px" :fetch-suggestions="querySearchAsync" placeholder="请输入内容" @select="handleSelect"></el-autocomplete>
        </el-form-item>
        <div class="sub" v-if="tableData.length>0">操作牛号：{{chooseCowInfo['牛号']}}</div>

        <div style="margin-top:80px">
          <el-button @click="onSave" type="primary" style="width:98px;" size="mediu">断奶</el-button>
          <el-button @click="later" type="warning" 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-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-dialog :title="formInline.cowID+'推迟断奶'" :visible.sync="handleClose" width="30%">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="推迟断奶原因">
          <el-select v-model="formInline.reason" placeholder="请选择">
            <el-option v-for="item in reasons" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose = false">取 消</el-button>
        <el-button type="primary" @click="sureLater">确 定</el-button>
      </span>
    </el-dialog>
  </el-container>
</template>
<script>
import formValidator from '../formValidator'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  created() {
    //任务列表
    this.getList(1)


    //获取全部疾病
    // this.getHaleInfo('wsCow', 'getUserDict', [
    //     'HoofTreatment'
    // ], 'list').then((data) => {
    //     this.typeList = [{ 名称: '暂无蹄病' }]
    //     this.typeList = this.typeList.concat(data)
    //     this.formData.type = this.typeList[0]['名称']
    // })

  },
  data() {
    return {
      handleClose: false,
      batch: false,
      saving: false,
      formData: {
        cowID: '',

      },
      mateModes: [],
      levelList: ['0分：正常', '1分：轻微破行', '2分：中度破行', '3分：破行', '4分：严重破行'],
      bulls: [],
      tableData: [],
      list: [],
      cowshed: '',
      task: 2,
      formInline: {
        cowID: '',
        reason: '发育不良'
      },
      reasons: ['发育不良', '病牛', '体质弱小', '采食量未达到1.5kG'],
      typeList: [],
      taskList: [{
        name: '干奶修蹄',
        type: 1,
        use: '请注射维生素E'
      }
        , {
        name: '泌乳牛普修',
        type: 2,
        use: '请投喂博威钙'
      }
        , {
        name: '青年牛修蹄',
        type: 3,
        use: '2-3胎次需要投服一粒博威钙，继续护理请点"是",结束护理请点"否"（注意：结束护理后，将不再提示护理操作）'
      }],
      chooseCowInfo: {}
    }
  },
  computed: {
    ...mapState(['userInfo', 'allHeight']),
    pageTitle() {
      return this.$route.meta.title
    },
    cowShedList() {
      var list = ['全部牛舍']
      this.list.forEach(item => {
        if (list.indexOf(item['当前牛舍']) == -1) {
          list.push(item['当前牛舍'])
        }
      })
      return list
    },
    filitList() {
      var list = []
      if (this.cowshed) {
        if (this.cowshed == '全部牛舍') {
          list = this.list
        }
        else {
          this.list.forEach(item => {
            if (item['当前牛舍'] == this.cowshed) {
              list.push(item)
            }
          })
        }
      }
      return list
    },
    nowTaskJson() {
      var jsonp = {}
      if (this.task) {
        this.taskList.forEach(item => {
          if (this.task == item.type) {
            jsonp = item
          }
        })
      }

      return jsonp
    },
    cowIDList() {
      var list = []
      this.list.forEach(item => {
        list.push({
          value: item['牛号']
        })
      })
      return list
    },
  },
  watch: {
    task() {
      this.cowshed = ''
      this.formData.cowID = ''
      this.getList(1)
    },
    // 'formData.cowID'() {
    //   if (this.formData.cowID.length != 8) {
    //     return
    //   }
    //   this.getNowCow()
    // }
  },
  methods: {
    handleSelect(choose) {
      var data = {}
      this.list.forEach(item => {
        if (item['牛号'] == choose.value) {
          data = item
        }
      })
      this.getNowCow(data)
    },
    querySearchAsync(queryString, cb) {
      var restaurants = this.cowIDList;
      var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;


      setTimeout(() => {
        cb(results);
      }, 3000 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    getList(type) {
      this.getHaleInfo('wsCalving', 'getWeaningTask', [
        this.$store.state.farmName
      ], 'list').then((data) => {
        this.list = data
        if (this.list.length == 0 && type == 1) {
          this.$alert('暂无需要处理的奶牛', '提示', {
            confirmButtonText: '确定',
            callback: action => {

            }
          });
        }


      })
    },
    later() {
      if (!this.formData.cowID) {
        this.$message({
          message: '请录入牛只牛号',
          type: 'warning'
        });
        return
      }
      this.formInline.cowID = JSON.parse(JSON.stringify(this.formData.cowID))
      this.handleClose = true
    },
    onSave() {
      if (!this.formData.cowID) {
        this.$message({
          message: '请录入牛只牛号',
          type: 'warning'
        });
        return
      }


      var jsonp = {}

      this.getHaleInfo('wsCalving', 'addWeaningInfo', [
        this.formData.cowID, 0, '', '', this.userInfo.username
      ], 'json').then((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()
        }
      })
    },
    getNowCow(row) {
      this.chooseCowInfo = row
      var list = []
      Object.keys(row).forEach(item => {
        list.push({
          key: item,
          value: row[item]
        })
      })

      this.tableData = list



    },
    rowClick(row) {
      this.formData.cowID = row['牛号']
      this.getNowCow(row)
    },
    sureLater() {
      this.getHaleInfo('wsCalving', 'addWeaningInfo', [
        this.formInline.cowID, 0, '', this.formInline.reason, this.userInfo.username
      ], 'json').then((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.formInline.cowID = ''
          this.handleClose = false
          this.getList()
        }
      })
    }
  }
}
</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;
  }
}

.HoofList {
  display: flex;
  width: 130px;
  flex-wrap: wrap;
  justify-content: space-between;
  .hoof-item {
    width: 60px;
    height: 60px;
    border-radius: 4px;
    font-size: 13px;
    font-weight: bold;
    color: rgba(255, 255, 255, 1);
    line-height: 18px;
    text-align: center;
    line-height: 60px;
    background: rgb(0, 85, 128);
    margin-bottom: 10px;
    cursor: pointer;

    &.active {
      background: rgba(133, 195, 6, 1);
    }
  }
}
</style>
