<template>
  <div class="hisolate">
    <el-container>
      <el-header>
        <div class="rect" style="padding-bottom: 10px">
          <el-form
              :inline="true"
              :model="searchForm"
              size="mini">
            <el-form-item style="width: 200px">
              <el-input placeholder="耳标" style="width: 200px" v-model="searchForm.earTag"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" @click="search">查 询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-circle-close" @click="clearAll">清 空</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main>
        <div class="rect" style="margin-top: 20px">
          <el-form inline size="mini">
            <el-form-item>
              <el-button icon="el-icon-plus" @click="visibleDialog = true">新 增</el-button>
            </el-form-item>
            <el-form-item>
              <el-button 
                icon="el-icon-delete" 
                @click="delAll" 
                type="danger"
                :disabled="selectList.length <= 0">删 除</el-button>
            </el-form-item>
          </el-form>
          <el-table
              id="select"
              height="530"
              :data="tableData"
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%"
              ref="table"
              stripe
              border
              :header-cell-style="{background:'rgb(245,245,245)',color:'#000',height: '45px',fontSize: '13px',fontWeight: 'normal',borderBottom: '1px solid #ccc'}"
              :cell-style="{color: '#888',fontSize: '13px'}"
              size="mini">
            <el-table-column
                type="selection"
                header-align="center"
                align="center"
                width="50">
            </el-table-column>
            <el-table-column
                prop="pigCode"
                header-align="center"
                align="center"
                label="猪只耳标">
            </el-table-column>
            <el-table-column
                prop="diseaseName"
                header-align="center"
                align="center"
                label="疾病名称">
            </el-table-column>
            <el-table-column
                prop="manName"
                header-align="center"
                align="center"
                label="操作人">
            </el-table-column>
            <el-table-column
                prop="createTime"
                header-align="center"
                align="center"
                label="创建时间">
            </el-table-column>
            <el-table-column label="温度">
              <template slot-scope="scope">
                <el-button
                    type="text"
                    size="medium"
                    @click="look(scope.row)"
                    style="color: rgb(24,144,255)">
                  查看温度曲线
                </el-button>
              </template>
            </el-table-column>
            <el-table-column label="温度">
              <template slot-scope="scope">
                <el-button
                    v-if="isAuth('sys:user:update')"
                    type="text"
                    size="medium"
                    @click="addOrUpdateHandle(scope.row)"
                    style="color: rgb(24,144,255)">
                  修改
                </el-button>
                <el-button
                    v-if="isAuth('sys:user:delete')"
                    type="text"
                    size="medium"
                    @click="deleteHandle(scope.row.id)"
                    style="color: rgb(24,144,255)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="sizeChange"
            @current-change="pageChange"
            :current-page="page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="limit"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper">
          </el-pagination>
        </div>
      </el-main>
      <el-dialog
          :title="'温度曲线'"
          :close-on-click-modal="false"
          :visible.sync="visible"
          width="650px">
        <div style="width: 100%; height: 600px">
          <chartTemp :eartag="eartag"></chartTemp>
        </div>
      </el-dialog>
      <el-dialog
          :title="!dataForm.id ? '新增' : '修改'"
          :close-on-click-modal="false"
          :visible.sync="visibleDialog"
          width="600px">
        <el-form
            :model="dataForm"
            :rules="dataRule"
            ref="dataForm"
            @keyup.enter.native="dataFormSubmit()"
            label-width="80px"
            size="mini"
            style="margin-left: 20px;width: 500px">
          <el-form-item label="猪只耳标" prop="pigId">
            <el-input
                type="text"
                placeholder="请输入耳标号"
                v-model="dataForm.pigCode"
                :disabled="dataForm.id? true: false">
            </el-input>
          </el-form-item>
          <el-form-item label="疾病名称" prop="diseaseName" >
            <el-select v-model="dataForm.diseaseName">
              <el-option v-for="item in illnesList" :key="item.id" :value="item.idseaseName" :label="item.idseaseName"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="操作人" prop="doctorId">
            <el-select
                @change="onChangeMan($event)"
                v-model="dataForm.doctorId">
              <el-option
                  v-for="item in manList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="mini" @click="onCancel">关闭</el-button>
            <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
          </span>
      </el-dialog>
    </el-container>
  </div>
</template>

<script>
import chartTemp from './chart/chartTemp'
import TableFooter from '../../../components/TableFooter'
import {mapActions, mapState} from 'vuex'
export default {
  name: 'hisolate',
  components: {
    chartTemp,
    TableFooter
  },
  data () {
    return {
      searchForm: {
        eartag: ''
      },
      page: 1,
      limit: 20,
      total: 0,
      dataListLoading: false,
      visible: false,
      tableData: [],
      selectList: [],
      visibleDialog: false,
      dataForm: {
        id: '',
        diseaseName: '',
        manId: '',
        manName: '',
        medicalRecordId: '',
        pigCode: ''
      },
      dataRule: {
        pigId: [
          { required: true, message: '猪只耳标不能为空', trigger: 'blur' }
        ],
        diseaseName: [
          { required: true, message: '疾病名称不能为空', trigger: 'blur' }
        ],
        doctorId: [
          { required: true, message: '操作人不能为空', trigger: 'blur' }
        ]
      },
      eartag: ''
    }
  },
  computed: {
    ...mapState(['manList', 'causeList', 'illnesList', 'symptomList', 'tmentplanList'])
  },
  methods: {
    ...mapActions(['getManList', 'getTmentplan', 'getSymptom', 'getCause', 'getIllnes']),
    search () {
      this.init()
    },
    clearAll () {
      this.searchForm.eartag = ''
    },
    init () {
      let params = {
        page: this.page,
        limit: this.limit,
        eartag: this.searchForm.earTag
      }
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/management/healthisolationrecord/list'),
        method: 'get',
        params: this.$http.adornParams(params)
      })
          .then(res => {
            if (res.data.code === 0) {
              this.tableData = res.data.page.list
              this.total = res.data.page.totalCount
            }
          }).finally(() => {
            this.dataListLoading = false
          })
    },
    // 修改size
    sizeChange (val) {
      this.limit = val
      this.init()
    },
    // 修改页数
    pageChange (val) {
      this.page = val
      this.init()
    },
    onChangeMan (id) {
      let man = {}
      man = this.manList.find(item => {
        return item.id === id
      })
      this.dataForm.manName = man.name
    },
    look (row) {
      this.visible = true
      this.eartag = row.pigCode
    },
    selectionChangeHandle (val) {
      this.selectList = val
      // val.forEach(item => {
      //   this.selectList.push(item.id)
      // })
    },
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id == '' ? undefined : this.dataForm.id,
            pigCode: this.dataForm.pigCode,
            diseaseName: this.dataForm.diseaseName,
            manId: this.dataForm.manId,
            manName: this.dataForm.manName,
            medicalRecordId: this.dataForm.medicalRecordId
          }
          this.$http({
            url: this.$http.adornUrl(`${!this.dataForm.id ? '/management/healthisolationrecord/save' : '/management/healthisolationrecord/update'}`),
            method: 'post',
            data: this.$http.adornData(params, true)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.visibleDialog = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.reset()
              this.init()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    addOrUpdateHandle (row) {
      this.visibleDialog = true
      this.dataForm = {
        id: row.id,
        diseaseName: row.diseaseName,
        manId: row.manId,
        manName: row.manName,
        medicalRecordId: row.medicalRecordId,
        pigCode: row.pigCode
      }
    },
    deleteHandle (id) {
      let ids = [id]
      let eartag = this.tableData.filter(item => item.id === id)[0].pigCode
      let that = this
      this.$confirm(`确定删除耳标号${eartag}的猪只的隔离管理?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.$http({
          url: that.$http.adornUrl('/management/healthisolationrecord/delete'),
          method: 'post',
          data: that.$http.adornData(ids, false)
        })
            .then(res => {
              if (res.data.code === 0) {
                this.$message.success('删除成功！')
                this.init()
              } else {
                this.$message.error(res.data.msg)
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    onCancel () {
      this.visibleDialog = false
      this.reset()
    },
    reset () {
      this.dataForm = {
        id: '',
        diseaseName: '',
        manId: '',
        manName: '',
        medicalRecordId: '',
        pigCode: ''
      }
    },
    // 批量删除
    delAll () {
      let ids = this.selectList.map(item => item.id)
      let eartags = this.selectList.map(item => item.pigCode)
      if (this.selectList.length > 0) {
        let that = this
        this.$confirm(`确定批量删除耳标号${eartags.join(',')}的猪只的隔离管理?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.$http({
            url: that.$http.adornUrl('/management/healthisolationrecord/delete'),
            method: 'post',
            data: that.$http.adornData(ids, false)
          }).then(res => {
            if (res.data.code === 0) {
              this.$message.success('删除成功！')
              this.init()
              this.selectList = []
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        this.$message.error('请选择要删除的数据！')
      }
    },
  },
  created () {
    this.getIllnes()
    this.getManList()
  },
  activated () {
    this.init()
  }
}
</script>

<style scoped>
.rect {
  background-color: #fff;
  padding: 30px 15px;
  border-radius: 5px;
  border: 1px solid #e8e8e8;
}
/deep/ .el-table--mini td, .el-table--mini th {
  padding: 3px 0 !important;
  height: 20px !important;
}
/deep/ .el-checkbox__input.is-checked+.el-checkbox__label {
  color: rgb(24,144,255);
  background-color: rgb(24,144,255);
  border-color: rgb(24,144,255);
}
/deep/.el-table .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: rgb(24,144,255);
  border-color: rgb(24,144,255);
}
/deep/.el-table .el-checkbox__inner:hover {
  border-color: rgb(24,144,255);
}
/deep/.el-table .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: rgb(24,144,255);
}
/deep/.el-table #select .cell .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: rgb(24,144,255);
  border-color: rgb(24,144,255);
}
</style>
