<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="findUser" placeholder="请选择发病揭发人" style="width:230px">
            <el-option v-for="item in findUserList" :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="发病日期" width="160" align="center"></el-table-column>

          <el-table-column prop="疾病类型" label="疾病类型" width="140" align="center"></el-table-column>
          <el-table-column prop="疾病名称" label="疾病名称" width="160" 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="过抗检测" align="center" width="70"></el-table-column>
        </el-table>
      </div>
    </el-aside>
    <el-main class="containMain">
      <div class="contentTitle">
        {{pageTitle}}
        <el-button type="info" v-if="!isBatch" style="float:right" size="mini" @click="isBatch=true" plain>批量确诊</el-button>
        <el-button type="primary" v-else style="float:right" size="mini" @click="isBatch=false" icon="el-icon-check">批量确诊</el-button>
      </div>
      <el-form ref="form" :model="formData" label-width="80px" style="margin-top:20px;width:522px;">
        <el-form-item label label-width="0" v-if="!isBatch">
          <el-autocomplete v-model="formData.cowID" :fetch-suggestions="querySearchAsync" style="width:200px" clearable placeholder="请输入牛号" @select="chooseCow"></el-autocomplete>
        </el-form-item>
        <el-form-item label label-width="0" v-else>
          <el-autocomplete prefix-icon="el-icon-search" v-model="cowID" :fetch-suggestions="queryCow" placeholder="请输入批量牛号" style="width:300px" clearable @select="getNowCow"></el-autocomplete>
        </el-form-item>
        <div class="sub" v-if="tableData.length>0&&!isBatch">操作牛号：{{editCowInfo['牛号']}}</div>

        <el-card v-if="isBatch&&cowList.length>0" style="margin-bottom:20px">
          <div style="flex-wrap: wrap;display:flex;">
            <div class="chooseItem" v-for="(item,index) in cowList" style="margin-bottom:10px">
              <span>{{item['牛号']}}</span>
              <i class="el-icon-error" @click="delCow(index)"></i>
            </div>
          </div>
        </el-card>

        <el-form-item label="发病日期">
          <el-input v-model="formData.diseaseDate" disabled style="width:217px"></el-input>
        </el-form-item>
        <el-form-item label="疾病类型">
          <el-select v-model="formData.category" placeholder="请选择">
            <el-option v-for="item in categoryList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="疾病名称">
          <el-select v-model="formData.name" placeholder="请选择">
            <el-option v-for="item in nameList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="待淘汰">
          <el-switch v-model="formData.isChangeGroup" active-text="是" inactive-text="否"></el-switch>
        </el-form-item>
        <el-row>
          <el-table class="handleTable" border :data="drugList" style="width: 100%" size="mini">
            <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="医嘱" align="center"></el-table-column>
          </el-table>
        </el-row>
        <el-row style="margin:10px 0 20px;">
          <el-button type="primary" plain size="mini" @click="changeDrug">修改处方</el-button>
        </el-row>

        <el-form-item label="发病症状">
          <el-select v-model="formData.symptom" multiple placeholder="请选择">
            <el-option v-for="item in typeList" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="乳区状态" v-if="formData.name=='乳房炎'">
          <div class="selectVal">
            <div class="item" @click="formData.breastArea_FL=(formData.breastArea_FL+1)%5" :style="{backgroundColor:breastArea[formData.breastArea_FL].color}">{{breastArea[formData.breastArea_FL].lable}}</div>
            <div class="item" @click="formData.breastArea_FR=(formData.breastArea_FR+1)%5" :style="{backgroundColor:breastArea[formData.breastArea_FR].color}">{{breastArea[formData.breastArea_FR].lable}}</div>
            <div class="item" @click="formData.breastArea_BL=(formData.breastArea_BL+1)%5" :style="{backgroundColor:breastArea[formData.breastArea_BL].color}">{{breastArea[formData.breastArea_BL].lable}}</div>
            <div class="item" @click="formData.breastArea_BR=(formData.breastArea_BR+1)%5" :style="{backgroundColor:breastArea[formData.breastArea_BR].color}">{{breastArea[formData.breastArea_BR].lable}}</div>
          </div>
        </el-form-item>

        <el-form-item label="体温">
          <el-select v-model="formData.bodyTemp" placeholder="请选择">
            <el-option v-for="item in temperatureList" :key="item.val" :label="item.lable" :value="item.val"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="瘤胃蠕动">
          <el-select v-model="formData.rumen" placeholder="请选择">
            <el-option v-for="item in [{lable:'正常',val:1},{lable:'异常',val:0}]" :key="item.val" :label="item.lable" :value="item.val"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="心率">
          <el-select v-model="formData.heartRate" placeholder="请选择">
            <el-option v-for="item in [{lable:'正常',val:1},{lable:'异常',val:0}]" :key="item.val" :label="item.lable" :value="item.val"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="可视黏膜">
          <el-select v-model="formData.visibleMocus" placeholder="请选择">
            <el-option v-for="item in ['正常','异常']" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="精神状态">
          <el-select v-model="formData.mentalState" placeholder="请选择">
            <el-option v-for="item in ['正常','异常']" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="粪便">
          <el-select v-model="formData.defecationCase" placeholder="请选择">
            <el-option v-for="item in ['正常','异常']" :key="item" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>

        <div style="margin-top:80px">
          <el-button @click="onSave(true)" type="primary" style="width:98px;" size="mediu">确认保存</el-button>
          <el-button @click="onSave(false)" type="warning" 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-dialog title="治疗处方登记" :visible.sync="dialogVisible" width="50%">
      <center>
        <h3>{{formData.category}}————{{formData.name}}</h3>
      </center>
      <el-row type="flex" justify="center" style="margin-bottom:20px">
        <el-select v-model="prescription" placeholder="请选择预设处方" @change="changeuseDrugList">
          <el-option v-for="item in prescriptionList" :key="item.name" :label="item.name" :value="item.name"></el-option>
        </el-select>
      </el-row>
      <el-row>
        <el-table class="handleTable" border :data="useDrugList" style="width: 100%" size="mini">
          <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="医嘱" align="center"></el-table-column>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button @click="deleteRow(scope.row,scope.$index)" type="danger" size="mini" plain>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <el-row style="margin-top:20px">
        <el-button type="primary" size="mini" @click="innerVisible=true">添加药</el-button>
      </el-row>

      <el-dialog width="30%" title="添加药" :visible.sync="innerVisible" append-to-body>
        <el-tree v-if="!innerinnerVisible" :data="allDrugList" :props="{
          children: 'children',
          label: '药品名称'
        }" @node-click="handleNodeClick"></el-tree>

        <el-form v-if="innerinnerVisible" ref="form" :model="addForm" label-width="50px">
          <el-form-item label="药名">
            <el-input v-model="addForm['药品名称']" disabled></el-input>
          </el-form-item>
          <el-form-item label="用法">
            <el-select v-model="addForm['使用方法']" placeholder="请选择预设处方">
              <el-option v-for="item in  ['穿刺', '外敷', '口服', '灌注', '乳注', '包扎', '插胃管', '其他']" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="剂量">
            <el-input v-model="addForm['剂量']"></el-input>
          </el-form-item>
          <el-form-item label="医嘱">
            <el-input type="textarea" v-model="addForm['医嘱']"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="innerinnerVisible = false">取 消</el-button>
          <el-button type="primary" @click="sureAdd">确定添加</el-button>
        </span>
      </el-dialog>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureDrug">确 定</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('wsVeterinary', 'getDiseaseCategory', [], 'list', '疾病名称').then((data) => {
      this.diseaseList = data
      if (data.length > 0) {
        setTimeout(() => {
          this.formData.category = this.categoryList[0]
          this.formData.name = this.nameList[0]
        }, 500)
      }
    })

    //药品列表
    this.getHaleInfo('wsVeterinary', 'getAllMedicine', [], 'list', '药品编号').then((data) => {
      this.allDrugList = this.getChild('#', data)
    })
  },
  data() {
    return {
      cowID: '',
      cowList: [],
      saving: false,
      isBatch: false,
      formData: {
        "cowID": '',
        "diseaseDate": '',
        "currFarm": '',
        "currGroup": '',
        "currCategory": '',
        "groCode": '',
        "repCode": '',
        "currLact": '',
        "category": '',
        "name": '',
        "symptom": [],
        "bodyTemp": 2,
        "isChangeGroup": false,
        "heartRate": '',
        "breathing": '',
        "rumen": '',
        "visibleMocus": '',
        "mentalState": '',
        "defecationCase": '',
        "area_FL": 0,
        "area_FR": 0,
        "area_BL": 0,
        "area_BR": 0,
        "breastArea_FL": 0,
        "breastArea_FR": 0,
        "breastArea_BL": 0,
        "breastArea_BR": 0,
        "veterinary": '',
      },
      methods: ['人工观察', '计步器揭发', '同期发情'],
      tableData: [],
      diseaseList: [],
      editCowInfo: {},
      list: [],
      findUser: '',
      drugList: [],
      dialogVisible: false,
      prescriptionInfoList: [],
      prescription: '',
      useDrugList: [],
      allDrugList: [],
      innerVisible: false,
      innerinnerVisible: false,
      addForm: {
        '药品名称': '',
        '使用方法': '穿刺',
        '剂量': 1,
        '医嘱': ''
      },
      temperatureList: [{
        lable: '37度及以下',
        val: 0
      }, {
        lable: '37-37.5度',
        val: 1
      }, {
        lable: '37.5-38度',
        val: 2
      }, {
        lable: '38-38.5度',
        val: 3
      }, {
        lable: '38.5-39度',
        val: 4
      }, {
        lable: '39-39.5度',
        val: 5
      }, {
        lable: '39.5度-40度',
        val: 6
      }, {
        lable: '40-40.5度',
        val: 7
      }, {
        lable: '40.5-41度',
        val: 8
      }, {
        lable: '41度及以上',
        val: 9
      }],
      breastArea: [{
        lable: '正常',
        color: 'rgba(133,195,6,1)'
      }, {
        lable: '一级',
        color: 'rgba(221,201,4,1)'
      }, {
        lable: '二级',
        color: 'rgba(234,123,29,1)'
      }, {
        lable: '三级',
        color: 'rgba(208,73,44,1)'
      }, {
        lable: '盲乳',
        color: 'rgba(0,150,224,1)'
      }]
    }
  },
  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.findUser) {
          list.push(item)
        }
      })
      return list
    },
    findUserList() {
      var list = []
      this.list.forEach(item => {
        if (list.indexOf(item['发病揭发人']) == -1) {
          list.push(item['发病揭发人'])
        }
      })
      return list
    },
    nameList() {
      var list = []
      if (this.formData.category) {
        this.diseaseList.forEach(item => {
          if (list.indexOf(item['疾病名称']) == -1 && item['所属分类'] == this.formData.category) {
            list.push(item['疾病名称'])
          }
        })
      }
      return list
    },
    categoryList() {
      var list = []
      this.diseaseList.forEach(item => {
        if (list.indexOf(item['所属分类']) == -1) {
          list.push(item['所属分类'])
        }
      })
      return list
    },
    typeList() {
      var list = []
      if (this.formData.category && this.formData.name) {
        this.diseaseList.forEach(item => {
          if (item['疾病名称'] == this.formData.name && item['所属分类'] == this.formData.category) {
            list = item['主要症状'].split(';')
          }
        })
      }
      return list
    },
    prescriptionList() {
      var list = []
      this.prescriptionInfoList.forEach(item => {
        var flag = false
        for (var i = 0; i < list.length; i++) {
          if (list[i].name == item['处方名称']) {
            flag = true
            list[i].children.push(item)
            break
          }
        }
        if (!flag) {
          var json = {
            name: item['处方名称'],
            children: [item]
          }
          list.push(json)
        }
      })
      return list

    },
    allCowList() {
      var list = []
      this.list.forEach(item => {
        var jsonp = { ...item }
        jsonp.value = item['牛号']
        list.push(jsonp)
      })
      return list
    },
    chooseList() {
      var list = []
      this.cowList.forEach(item => {
        list.push(item['牛号'])
      })
      return list
    }
  },
  watch: {

    'formData.num'() {
      setTimeout(() => {
        this.formData.describe = this.describeList[0]
      }, 100)
    },
    'formData.category'() {
      setTimeout(() => {
        this.formData.name = this.nameList[0]
      }, 100)
    },
    dialogVisible() {
      if (this.dialogVisible) {
        this.getHaleInfo('wsCow', 'getDiseasePreInfo', [
          this.formData.name
        ], 'list', '疾病名称').then((data) => {
          this.prescriptionInfoList = data
        })
      }
    }
  },
  methods: {
    deleteRow(row, index) {
      this.useDrugList.splice(index, 1)
    },
    changeDrug() {
      this.useDrugList = [...this.drugList]
      this.dialogVisible = true
    },
    sureDrug() {
      this.drugList = [...this.useDrugList]
      this.dialogVisible = false
    },
    sureAdd() {
      this.useDrugList.push(this.addForm)
      this.innerinnerVisible = false
      this.innerVisible = false
    },
    getChild(id, allList) {

      var list = []
      allList.forEach(item => {
        if (id == "#") {

          if (item['药品编号'].length == 2) {
            var jsonp = item
            jsonp['剂量'] = 1
            jsonp['使用方法'] = '穿刺'
            jsonp.children = this.getChild(item['药品编号'], allList)
            list.push(jsonp)
          }
        }
        else {
          var name = item['药品编号'].slice(0, id.length)
          if (item['药品编号'].length - id.length == 2 && name == id) {
            var jsonp = item
            jsonp['剂量'] = 1
            jsonp['使用方法'] = '穿刺'
            jsonp.children = this.getChild(item['药品编号'], allList)
            list.push(jsonp)
          }
        }
      })
      return list
    },
    changeuseDrugList() {
      console.log(this.prescription)
      if (this.prescription) {
        this.prescriptionList.forEach(item => {
          if (item.name == this.prescription) {
            this.useDrugList = this.useDrugList.concat(item.children)
          }
        })
      }
      console.log(this.useDrugList)
    },
    getList(type) {
      this.getHaleInfo('wsVeterinary', 'getDiagnoseTask', [
        this.$store.state.farmName
      ], 'list', '', 'YYYY-MM-DD HH:mm:ss').then((data) => {
        this.list = data
        if (data.length > 0) {
          setTimeout(() => {
            this.findUser = this.findUserList[0]
          }, 500)
        }
        if (this.list.length == 0 && type == 1) {
          this.$alert('暂无需要处理的奶牛', '提示', {
            confirmButtonText: '确定',
            callback: action => {

            }
          });
        }

      })
    },
    onSave(isSick) {


      if (this.formData.cowID != this.editCowInfo['牛号'] && !this.isBatch) {
        this.$message({
          message: '请录入牛只牛号',
          type: 'warning'
        });
        return
      }

      if (this.isBatch && this.cowList.length <= 0) {
        this.$message({
          message: '请录入批量牛只牛号',
          type: 'warning'
        });
        return
      }

      if (isSick && this.formData.symptom.length == 0) {
        this.$message({
          message: '请选择发病症状',
          type: 'warning'
        });
        return
      }

      if (isSick && this.drugList.length == 0) {
        this.$message({
          message: '请选择处方',
          type: 'warning'
        });
        return
      }

      if (this.isBatch) {
        this.saveBatch(0, isSick)
        return
      }
      var formData = { ...this.formData }
      formData.symptom = formData.symptom.join(';')
      formData.area_FL = formData.breastArea_FL
      formData.area_FR = formData.breastArea_FR
      formData.area_BL = formData.breastArea_BL
      formData.area_BR = formData.breastArea_BR
      formData.diseaseDate = Date.parse(new Date(formData.diseaseDate)) + 28800000

      this.getHaleInfo('wsVeterinary', 'diseaseDiagnose', [
        formData, formData.category, formData.name, isSick
      ], '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 (isSick) {
            this.saveDrug(0)
          }
          else {
            this.$message({
              message: '保存成功',
              type: 'success'
            });
            this.getList()
            this.drugList = []
            this.formData.cowID = ''
          }

        }
      })
    },
    saveBatch(index, isSick) {
      if (index >= this.cowList.length) {
        this.$message({
          message: '保存成功',
          type: 'success'
        });
        this.drugList = []
        this.cowList = []
        return
      }
      var formData = { ...this.formData }
      formData.cowID = this.cowList[index]['牛号']
      formData.currGroup = this.cowList[index]['当前牛舍']
      formData.groCode = this.cowList[index]['当前状态']
      formData.repCode = this.cowList[index]['繁殖状态']
      formData.currLact = this.cowList[index]['当前胎次']
      formData.symptom = formData.symptom.join(';')
      formData.area_FL = formData.breastArea_FL
      formData.area_FR = formData.breastArea_FR
      formData.area_BL = formData.breastArea_BL
      formData.area_BR = formData.breastArea_BR
      formData.diseaseDate = Date.parse(new Date(formData.diseaseDate)) + 28800000
      this.getHaleInfo('wsVeterinary', 'diseaseDiagnose', [
        formData, formData.category, formData.name, isSick
      ], 'json').then((data) => {
        if (data.rtnCode == -1) {

          this.$alert(data.rtnMessage, '提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.formData.cowID = ''
            }
          });
        }
        else {
          if (isSick) {
            this.saveDrug(0, index)
          }
          else {
            this.saveBatch(index + 1, isSick)
          }

        }
      })
    },
    newGuid() {
      var guid = "";
      for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
          guid += "-";
      }
      return guid;
    },
    saveDrug(index, cowIndex) {
      if (index >= this.drugList.length) {
        if (this.isBatch) {
          if (cowIndex >= this.cowList.length - 1) {
            this.$message({
              message: '保存成功',
              type: 'success'
            });
            this.drugList = []
            this.cowList = []
          }
          else {
            this.saveBatch(cowIndex + 1, true)
          }

        }
        else {
          this.$message({
            message: '保存成功',
            type: 'success'
          });
          this.getList()
          this.drugList = []
          this.formData.cowID = ''

        }
        return
      }

      var prescriptionNO = this.getTime(Date.parse(new Date()), 'YYMMDD') + this.newGuid().replace(/-/g, '').toUpperCase().substr(0, 10)

      this.getHaleInfo('wsVeterinary', 'addPrescription', [
        {
          "prescriptionNO": prescriptionNO,
          "cowID": this.isBatch?this.cowList[cowIndex]['牛号']:this.formData.cowID,
          "prescriptionDate": Date.parse(new Date()),
          "currFarm": this.$store.state.farmName,
          "currGroup": this.isBatch?this.cowList[cowIndex]['当前牛舍']:this.editCowInfo['当前牛舍'],
          "bodyTemp": this.formData.bodyTemp,
          "heartRate": this.formData.heartRate,
          "breathing": this.formData.breathing,
          "rumen": this.formData.rumen,
          "visibleMocus": this.formData.visibleMocus,
          "mentalState": this.formData.mentalState,
          "defecationCase": this.formData.defecationCase,
          "area_FL": this.formData.breastArea_FL,
          "area_FR": this.formData.breastArea_FR,
          "area_BL": this.formData.breastArea_BL,
          "area_BR": this.formData.breastArea_BR,
          "drugNO": this.drugList[index]['药品编号'] || " ",
          "drugName": this.drugList[index]['药品名称'] || " ",
          "drugAmount": this.drugList[index]['剂量'] || " ",
          "treatMethod": this.drugList[index]['使用方法'] || " ",
          "doctorAdvice": this.editCowInfo['兽医'],
          "operators": this.$store.state.userInfo.username,
          "operatTime": Date.parse(new Date()),

        }, this.isBatch?this.cowList[cowIndex]['牛号']:this.editCowInfo['牛号'], this.formData.category, this.formData.name, prescriptionNO
      ], 'json').then((data) => {
        console.log(data)
        if (data.rtnCode == -1) {

          this.$alert(data.rtnMessage, '提示', {
            confirmButtonText: '确定',
            callback: action => {
              this.formData.cowID = ''
            }
          });
        }
        else {
          this.saveDrug(index + 1,cowIndex)
        }
      })

    },
    delCow(index) {
      this.cowList.splice(index, 1)
    },
    getNowCow() {

      this.getHaleInfo('wsCow', 'getCowExtendInfoByCowID', [
        this.cowID, 99
      ], 'json').then((data) => {
        // console.log(data)
        var list = []

        Object.keys(data).forEach(item => {
          list.push({
            key: item,
            value: data[item]
          })
        })

        if (this.chooseList.indexOf(data['牛号']) == -1) {
          this.cowList.push(data)
        }
        this.formData = {
          "cowID": '',
          "diseaseDate": this.getTime(Date.parse(new Date()), 'YYYY-MM-DD HH:mm:ss'),
          "currFarm": this.$store.state.farmName,
          "category": '乳房疾病',
          "name": '乳房炎',
          "symptom": [],
          "bodyTemp": 2,
          "isChangeGroup": false,
          "heartRate": 1,
          "breathing": 1,
          "rumen": 1,
          "visibleMocus": '正常',
          "mentalState": '正常',
          "defecationCase": '正常',
          "area_FL": 0,
          "area_FR": 0,
          "area_BL": 0,
          "area_BR": 0,
          "breastArea_FL": 0,
          "breastArea_FR": 0,
          "breastArea_BL": 0,
          "breastArea_BR": 0,
          "veterinary": this.$store.state.userInfo.username,
        }



        // console.log(list)

        this.cowID = ''

        this.tableData = list

      }).catch(() => {
        this.formData.cowID = ''
      })
    },
    rowClick(row) {
      this.isBatch=false
      this.editCowInfo = row
      this.formData = {
        "cowID": row['牛号'],
        "diseaseDate": row['发病日期'],
        "currFarm": this.$store.state.farmName,
        "currGroup": row['当前牛舍'],
        "currCategory": row['当前状态'],
        "groCode": row['泌乳状态'],
        "repCode": row['繁殖状态'],
        "currLact": row['当前胎次'],
        "category": row['疾病类型'],
        "name": row['疾病名称'],
        "symptom": [],
        "bodyTemp": 2,
        "isChangeGroup": false,
        "heartRate": 1,
        "breathing": 1,
        "rumen": 1,
        "visibleMocus": '正常',
        "mentalState": '正常',
        "defecationCase": '正常',
        "area_FL": 0,
        "area_FR": 0,
        "area_BL": 0,
        "area_BR": 0,
        "breastArea_FL": row['发病时左前区'],
        "breastArea_FR": row['发病时右前区'],
        "breastArea_BL": row['发病时左后区'],
        "breastArea_BR": row['发病时右后区'],
        "veterinary": this.$store.state.userInfo.username,
      }
      var list = []
      Object.keys(row).forEach(item => {
        list.push({
          key: item,
          value: row[item]
        })
      })
      this.tableData = list
    },
    handleNodeClick(data) {
      // console.log(data)
      if (data.children.length == 0) {
        data['使用方法'] = '穿刺'
        data['剂量'] = 1
        data['医嘱'] = ''
        this.addForm = data
        this.innerinnerVisible = true
      }
    },
    querySearchAsync(queryString, cb) {
      var restaurants = this.allCowList;
      var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;


      var timeout = setTimeout(() => {
        cb(results);
      }, 3000 * Math.random());
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    chooseCow(val) {
      this.rowClick(val)
    }
  }
}
</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;
}

.selectVal {
  width: 140px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  flex-wrap: wrap;

  .item {
    width: 60px;
    border-radius: 4px;
    height: 60px;
    text-align: center;
    font-size: 13px;
    font-weight: bold;
    color: rgba(255, 255, 255, 1);
    line-height: 60px;
    margin-bottom: 10px;
    cursor: pointer;
  }
}

.chooseItem {
  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;
  i {
    cursor: pointer;
  }
}
</style>
