<template>
  <div class="mod-role">
    <el-container>
      <!-- form表单 -->
      <el-header>
        <div class="rect rect-form">
          <el-form :inline="true" :model="form" size="mini" ref="form">
            <!-- <el-form-item style="width: 50px">
              <el-button type="success" :disabled="abnormalStatus===false" @click="changeClick">正常</el-button>
            </el-form-item>
            <el-form-item style="width: 100px">
              <el-button type="warning" :disabled="abnormalStatus===true" @click="changeClick">异常</el-button>
            </el-form-item> -->
            <el-form-item style="width: 140px">
              <el-select v-model="abnormalStatus">
                <el-option 
                  v-for="item in abnormalList" 
                  :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>
              <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-form inline size="mini">
            <el-form-item>
              <el-button 
                icon="el-icon-download" 
                type="danger" 
                @click="exportExcel()">
                导出Excel
              </el-button>
            </el-form-item>
          </el-form>
          <el-table
            :data="dataList"
            border
            stripe
            v-loading="dataListLoading"
            style="width: 100%;"
            size="mini"
            height="540"
            :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="date"
              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
              prop="drinkingWaterQuality"
              header-align="center"
              align="center"
              label="饮用水指标">
            </el-table-column>
            <el-table-column
              prop="collectTime"
              header-align="center"
              align="center"
              label="采集时间">
            </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>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        form: {
          // farmId: '',
          pigstyId: '',
          time: []
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        // farmId: undefined,
        // farmList: [], // 牧场List
        pigstyList: [], // 猪舍List
        // pigstySelectedList: [], // 根据牧场限制的猪舍SelectedList
        labelPosition: 'right',
        abnormalList: [
          {
            label: '正常',
            value: false
          },
          {
            label: '异常',
            value: true
          }
        ],
        abnormalStatus: false,
        // pigsty: {
        //   farmId: '',
        //   pigstyId: ''
        // }
      }
    },
    activated () {
      this.getDataList()
    },
    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/drinkingWater/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'abnormalStatus': this.abnormalStatus,
            'farmId': parseInt(this.$cookie.get('formFarmId')),
            'pigstyId': this.form.pigstyId || undefined,
            'startTime': this.form.time[0] || undefined,
            'endTime': this.form.time[1] || undefined
          })
        }).then(async({data}) => {
          if (data && data.code === 0) {
            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)
          })
          console.log(this.farmList);
          // 栋舍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)
      // },
      // 导出Excel
      exportExcel () {
        this.$http({
          url: this.$http.adornUrl('/management/drinkingWater/exportXls'),
          method: 'get',
          params: this.$http.adornParams({
            'abnormalStatus': this.abnormalStatus,
            'farmId': parseInt(this.$cookie.get('formFarmId')),
            'pigstyId': this.form.pigstyId || undefined,
            'startTime': this.form.time[0] || undefined,
            'endTime': this.form.time[1] || undefined
          }),
          responseType: 'blob'
        }).then(({data}) => {
          if (!data) {
            this.$message.error('文件下载失败')
            return
          }
          let url = window.URL.createObjectURL(new Blob([data], {type: 'application/vnd.ms-excel'}))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', '饮用水环境.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
          window.URL.revokeObjectURL(url)
        })
      },
      // 获取起始与结束时间
      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.abnormalStatus = false
        // this.pigstySelectedList = this.pigstyList
      },
      // 每页数
      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>
