<template>
  <div class="stock">
    <template v-if="!addOrUpdateVisible && !detailVisible && !historyVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="所属部门">
          <org-component v-model="dataForm.orgId"/>
        </el-form-item>
        <el-form-item label="设备维护人">
          <user-component v-model="dataForm.maintainerId"/>
        </el-form-item>
        <el-form-item label="设备型号">
          <el-select
            v-model="dataForm.model"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in optionsModel"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备编号">
          <el-input v-model="dataForm.code" placeholder="设备编号" clearable/>
        </el-form-item>
        <el-form-item label="最后上报时间">
          <el-date-picker
            v-model="dataForm.date"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
          <el-button v-if="isAuth('device:temp:save')" type="primary" @click="addOrUpdateHandle(0)">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column
          prop="code"
          header-align="center"
          align="center"
          min-width="120"
          label="设备编号">
        </el-table-column>
        <el-table-column
          prop="model"
          header-align="center"
          align="center"
          min-width="100"
          :formatter="formatOptionsModel"
          :show-overflow-tooltip="true"
          label="设备型号">
        </el-table-column>
        <el-table-column
          prop="orgStr"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="所属部门">
        </el-table-column>
        <el-table-column
          prop="maintainerStr"
          header-align="center"
          align="center"
          min-width="100"
          :show-overflow-tooltip="true"
          label="设备维护人">
        </el-table-column>
        <el-table-column
          prop="lastReportTime"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="最后一次上报时间">
        </el-table-column>
        <el-table-column
          prop="temperature"
          header-align="center"
          align="center"
          min-width="100"
          label="温度（℃）">
        </el-table-column>
        <el-table-column
          prop="humidity"
          header-align="center"
          align="center"
          min-width="100"
          label="湿度（%）">
        </el-table-column>
        <el-table-column
          prop="lastAlarmTime"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="最后一次报警时间">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          min-width="180"
          :show-overflow-tooltip="true"
          label="备注">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="200"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('device:temp:info')" type="text" size="small" @click="detailHandle(scope.row.id)">查看</el-button>
            <el-button v-if="isAuth('device:temp:edit')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
            <el-button v-if="isAuth('device:temp:list')" type="text" size="small" @click="historyHandle(scope.row)">历史数据</el-button>
            <el-button v-if="isAuth('device:temp:history')" style="color: red" type="text" size="small" @click="deleteHandle(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>
    </template>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" @onChose="onChose"></add-or-update>
    <detail v-if="detailVisible" ref="detail" @onChose="onChose"/>
    <history v-if="historyVisible" ref="history" @onChose="onChose"/>
  </div>
</template>

<script>
import { getDictList } from '@/api/dict'
import AddOrUpdate from './temp-add-or-update'
import Detail from './temp-detail'
import History from './temp-history'
import OrgComponent from '../common/org-component'
import UserComponent from '../common/user-component'
export default {
  name: 'temp',
  components: {
    Detail,
    History,
    AddOrUpdate,
    UserComponent,
    OrgComponent
  },
  data () {
    return {
      addOrUpdateVisible: false,
      detailVisible: false,
      historyVisible: false,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      optionsModel: []
    }
  },
  created () {
    this.getModelList()
    this.getDataList()
  },
  methods: {
    onChose () {
      this.addOrUpdateVisible = false
      this.detailVisible = false
      this.historyVisible = false
    },
    // 获取温湿度计设备型号列表
    getModelList () {
      getDictList({type: 'temp_model'}).then(({data}) => {
        if (data) {
          this.optionsModel = data
        }
      })
    },
    // 查询
    search () {
      this.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.addOrUpdateVisible = false
      this.$http({
        url: this.$http.adornUrl('/iot-platform/temp/list'),
        method: 'get',
        params: this.$http.adornParams({
          'current': this.pageIndex,
          'size': this.pageSize,
          'code': this.dataForm.code ? this.dataForm.code : null,
          'model': this.dataForm.model ? this.dataForm.model : null,
          'orgId': this.dataForm.orgId ? this.dataForm.orgId : null,
          'maintainerId': this.dataForm.maintainerId ? this.dataForm.maintainerId : null,
          'lastReportTimeStart': this.dataForm.date ? this.dataForm.date[0] : null,
          'lastReportTimeEnd': this.dataForm.date ? this.dataForm.date[1] : null
        })
      }).then(({data}) => {
        if (data && data.code === '200') {
          this.dataList = data.data.records
          this.totalPage = Number(data.data.total)
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id, disable) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, disable)
      })
    },
    detailHandle (id) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detail.init(id)
      })
    },
    historyHandle (row) {
      this.historyVisible = true
      this.$nextTick(() => {
        this.$refs.history.init(row.code)
      })
    },
    // 转换属性“设备型号”
    formatOptionsModel (row) {
      for (let i = 0; i < this.optionsModel.length; i++) {
        if (this.optionsModel[i].code === row.model) {
          return this.optionsModel[i].value
        }
      }
    },
    // 删除
    deleteHandle (id) {
      if (!id) return
      let ids = []
      ids.push(id)
      this.$confirm(`确定删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/iot-platform/temp/delete`),
          method: 'DELETE',
          data: ids
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>

</style>
