<template>
  <div class="mod-role">
    <el-container>
      <!-- form表单 -->
      <el-header>
        <div class="rect rect-form">
          <el-form :inline="true" :model="form" @keyup.enter.native="getDataList()" size="mini" ref="form">
            <!-- <el-form-item style="width: 140px">
              <el-select placeholder="牧场" v-model="form.farmId" :disabled="farmId > 0" @change="farmChange">
                <el-option
                  v-for="item in farmList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item style="width: 140px">
              <el-select placeholder="猪舍" v-model="form.pigstyId">
                <el-option
                  v-for="item in pigstyList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="form.time"
                type="datetimerange"
                align="right"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['08:00:00', '16:00:00']"
                @change="timeChange"
                value-format="yyyy-MM-dd HH:mm:ss">
              </el-date-picker>
            </el-form-item>
            <!-- <el-form-item style="width: 140px">
              <el-select placeholder="报警类型" v-model="form.warningType">
                <el-option
                  v-for="item in thresholdTypes"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-button @click="getDataList()" icon="el-icon-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>
      <!-- role表格 -->
      <el-main>
        <div class="rect rect-table">
          <el-table
            :data="dataList"
            border
            stripe
            v-loading="dataListLoading"
            style="width: 100%;"
            size="mini"
            height="578"
            :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'}">
            <el-table-column
              prop="nowDate"
              header-align="center"
              align="center"
              label="当前日期">
            </el-table-column>
            <!-- <el-table-column
              prop="farmName"
              header-align="center"
              align="center"
              label="牧场">
            </el-table-column> -->
            <el-table-column
              prop="pigstyName"
              header-align="center"
              align="center"
              label="猪舍">
            </el-table-column>
            <el-table-column
              header-align="center"
              align="center"
              label="报警类型">
              <template slot-scope="scope">
                <span>饮用水指标异常</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="nowValue"
              header-align="center"
              align="center"
              label="当前饮用水成分">
            </el-table-column>
            <el-table-column
              prop="warningTime"
              header-align="center"
              align="center"
              label="报警时间">
            </el-table-column>
            <el-table-column
              header-align="center"
              align="center"
              label="操作">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="mini"
                  style="color: rgb(24,144,255)"
                  @click="verifyHandle(scope.row.id)">
                  处理确认
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper">
          </el-pagination>
          <!-- 弹窗, 新增 / 修改 -->
          <verify v-if="verifyVisible" ref="verify" @refreshDataList="getDataList" :thresholdTypes="thresholdTypes"></verify>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
  import verify from './verify'
  export default {
    data () {
      return {
        form: {
          // farmId: '',
          pigstyId: '',
          time: [],
          warningType: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        // farmId: undefined,
        // farmList: [], // 牧场List
        pigstyList: [], // 猪舍List
        // pigstySelectedList: [], // 根据牧场限制的猪舍SelectedList
        labelPosition: 'right',
        thresholdTypes: [
          {
            label: '高温',
            value: 1
          },
          {
            label: '低温',
            value: 2
          },
          {
            label: '空气污染',
            value: 3
          }
        ],
        verifyVisible: false, // 弹窗是否显示
        // pigsty: {
        //   farmId: '',
        //   pigstyId: ''
        // }
      }
    },
    activated () {
      this.getDataList()
    },
    components: {
      verify
    },
    methods: {
      // 获取数据列表
      getDataList () {
        // this.farmId = this.$cookie.get('farmId') === 'null'? undefined: parseInt(this.$cookie.get('farmId'))
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/management/warningInfo/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'farmId': parseInt(this.$cookie.get('formFarmId')),
            'pigstyId': this.form.pigstyId || undefined,
            'startTime': this.form.time[0] || undefined,
            'endTime': this.form.time[1] || undefined,
            'belongTo': 4,
            // 'warningType': this.form.warningType || undefined
          })
        }).then(async({data}) => {
          if (data && data.code === 0) {
            console.log(data);
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
          // // 获取所有牧场
          // this.farmList = []
          // let farmList = []
          // let farmResult = await this.$http({
          //   url: this.$http.adornUrl("/management/farm/findAll"),
          //   method: "get",
          //   params: this.$http.adornParams({})
          // })
          // farmResult.data.all && (farmList = farmResult.data.all)
          // farmList.forEach(farm => {
          //   let item = {
          //     label: farm.name,
          //     value: farm.id
          //   }
          //   this.farmList.push(item)
          // })
          // 栋舍List
          this.pigstyList = []
          let pigstyList = []
          let pigstyR = await this.$http({
            url: this.$http.adornUrl("/management/pigsty/findAll"),
            method: "get",
            params: this.$http.adornParams({})
          })
          pigstyR.data.all && (pigstyList = pigstyR.data.all)
          pigstyList.forEach(pigsty => {
            let item = {
              value: pigsty.id,
              label: pigsty.number,
              // farmId: pigsty.farmId
            }
            this.pigstyList.push(item)
          })
          // this.pigstySelectedList = this.pigstyList
          // this.form.farmId = (this.form.farmId || this.farmId) || undefined
          this.dataListLoading = false
        })
      },
      // 切换正常异常状态
      changeClick () {
        this.abnormalStatus = !this.abnormalStatus
        this.getDataList()
      },
      // // 根据牧场选择限制猪舍
      // farmChange (val) {
      //   this.pigstySelectedList = this.pigstyList.filter(item => item.farmId === val)
      //   if (this.pigsty.farmId === val) {
      //     return
      //   }
      //   this.form.pigstyId = ''
      // },
      // // 根据猪舍选择确定牧场
      // pigstyChange (val) {
      //   for (let pigsty of this.pigstyList) {
      //     if (pigsty.value === val) {
      //       this.form.farmId = pigsty.farmId
      //       this.pigsty.farmId = pigsty.farmId
      //       this.pigsty.pigstyId = val
      //       break
      //     }
      //   }
      //   this.farmChange(this.form.farmId)
      // },
      // 获取起始与结束时间
      timeChange (val) {
        let start = new Date(val[0])
        let end = new Date(val[1])
        let now = new Date()
        if (start > now || end > now) {
          this.form.time = []
          this.$message.error('请勿选择超出现在的时刻')
        }
      },
      // form表单清空，单元SelectedList 和 栏期SelectedList重置
      clearAll () {
        // this.form.farmId = this.$cookie.get('farmId') === 'null'? '': parseInt(this.$cookie.get('farmId'))
        this.form.pigstyId = ''
        this.form.time = []
        this.form.warningType = ''
        // this.pigstySelectedList = this.pigstyList
        // console.log(this.$cookie.get('farmId'));
        // console.log(this.form.id);
      },
      // 确认处理
      verifyHandle (id) {
        this.verifyVisible = true
        this.$nextTick(() => {
          this.$refs.verify.init(id)
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      }
    }
  }
</script>
<style scoped>
.rect {
  background-color: #fff;
  padding: 30px 15px;
  border-radius: 5px;
  border: 1px solid #e8e8e8;
}
.rect-form {
  padding-bottom: 10px;
}
.rect-table {
  margin-top: 20px;
}
.demo-table-expand {
  font-size: 0;
  margin-left: 40px;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 80%;
}
.el-table .height {
  background: rgba(254, 254, 254, 0.5);
}
/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>
