<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: 140px">
              <el-select placeholder="牧场" v-model="form.farmId" :disabled="farmId > 0">
                <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.belongTo">
                <el-option
                  v-for="item in belongTo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="width: 200px">
              <el-input v-model="form.thresholdType" placeholder="参数名称"  style="width: 200px"></el-input>
            </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-plus" 
                @click="addOrUpdateHandle()">
                新 增
              </el-button>
            </el-form-item>
            <el-form-item>
              <el-button 
                icon="el-icon-delete" 
                type="danger" 
                @click="deleteHandle()" 
                :disabled="dataListSelections.length <= 0">
                删 除
              </el-button>
            </el-form-item>
            <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"
            @selection-change="selectionChangeHandle"
            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
              type="selection"
              header-align="center"
              align="center"
              width="50">
            </el-table-column>
            <!-- <el-table-column
              prop="farmName"
              header-align="center"
              align="center"
              label="牧场名称">
              <template slot-scope="scope">
                <span>{{thresholdTypes[scope.row.thresholdType - 1].label}}</span>
              </template>
            </el-table-column> -->
            <el-table-column
              header-align="center"
              align="center"
              label="所属范围">
              <template slot-scope="scope">
                <span>{{belongTo[scope.row.belongTo - 1].label}}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="thresholdType"
              header-align="center"
              align="center"
              label="参数名称">
            </el-table-column>
            <el-table-column
              prop="thresholdValue"
              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="addOrUpdateHandle(scope.row.id)">
                  修改
                </el-button>
                <el-button
                  type="text"
                  size="mini"
                  @click="deleteHandle(scope.row.id)"
                  style="color: rgb(24,144,255)">
                  删除
                </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>
          <!-- 弹窗, 新增 / 修改 -->
          <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" :belongTo="belongTo"></add-or-update>
        </div>
      </el-main>
      
      
    </el-container>
  </div>
</template>

<script>
  import AddOrUpdate from './ealarm-add-or-update'
  export default {
    data () {
      return {
        form: {
          belongTo: '',
          thresholdType: '',
          // farmId: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        // farmId: undefined,
        // farmList: [], // 牧场List
        labelPosition: 'right',
        // thresholdTypes: [
        //   {
        //     label: '室内高温',
        //     value: 1
        //   },
        //   {
        //     label: '室内低温',
        //     value: 2
        //   },
        //   {
        //     label: '室内空气污染',
        //     value: 3
        //   },
        //   {
        //     label: '饮用水污染',
        //     value: 4
        //   },
        //   {
        //     label: '室外高温',
        //     value: 5
        //   },
        //   {
        //     label: '室外低温',
        //     value: 6
        //   },
        //   {
        //     label: '室外空气污染',
        //     value: 7
        //   },
        //   {
        //     label: '体温高温',
        //     value: 8
        //   },
        //   {
        //     label: '体温低温',
        //     value: 9
        //   },
        //   {
        //     label: '耳标电量过低',
        //     value: 10
        //   }
        // ],
        belongTo: [
          {
            label: '室内',
            value: 1
          },
          {
            label: '室外',
            value: 2
          },
          {
            label: '耳标数据',
            value: 3
          },
          {
            label: '饮用水',
            value: 4
          }
        ],
      }
    },
    components: {
      AddOrUpdate
    },
    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/thresholdManagement/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'thresholdType': this.form.thresholdType || undefined,
            'farmId': parseInt(this.$cookie.get('formFarmId')),
            'belongTo': this.form.belongTo || undefined
          })
        }).then(async({data}) => {
          // 获取所有牧场
          // 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
          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.form.farmId = (this.form.farmId || this.farmId) || undefined
          this.dataListLoading = false
        })
      },
      // form表单清空，单元SelectedList 和 栏期SelectedList重置
      clearAll () {
        this.form.thresholdType = ''
        this.form.belongTo = ''
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 导出Excel
      exportExcel () {
        // this.$http({
        //   url: this.$http.adornUrl('/management/thresholdManagement/exportXls'),
        //   method: 'get',
        //   params: this.$http.adornParams({
        //     'thresholdType': this.form.thresholdType || undefined,
        //     'farmId': parseInt(this.$cookie.get('formFarmId'))
        //   })
        // }).then()
        // window.open(`http://192.168.1.57:88/api/management/thresholdManagement/exportXls?thresholdType=${this.form.thresholdType}&farmId=${parseInt(this.$cookie.get('formFarmId'))}`);
        this.$http({
          url: this.$http.adornUrl('/management/thresholdManagement/exportXls'),
          method: 'get',
          params: this.$http.adornParams({
            'thresholdType': this.form.thresholdType || undefined,
            'farmId': parseInt(this.$cookie.get('formFarmId'))
          }),
          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)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定${id ? '删除' : '批量删除'}报警设置?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/management/thresholdManagement/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.getDataList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</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>
