<template>
  <el-row>
    <el-col>
      <el-card shadow="never">
        <el-col>
          <div class="flagBoxStyle">
            <div class="flagStyle" />
            <div>离线设备</div>
          </div>
        </el-col>
        <el-col>
          <search-area type="">
            <!--一般查询条件-->
            <search-item>
              <el-input
                v-model="listQuery.deviceCode"
                placeholder="设备编号"
                clearable
              />
            </search-item>
            <search-item>
              <el-select
                v-model="listQuery.deviceType"
                clearable
                placeholder="设备类型"
              >
                <el-option
                  v-for="item in deviceTypeList"
                  :key="item.value"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </search-item>
            <search-item>
              <el-button size="small" type="primary" class="search-btn" @search="fetchData">
                搜索
              </el-button>
            </search-item>
            <search-item>
              <el-button size="small" type="primary" class="search-btn" @click="batchExport">
                导出
              </el-button>
            </search-item>
          </search-area>
          <normal-table
            :data="list"
            :height="height"
            :query="listQuery"
            :total="tableOption.total"
            :options="tableOption.options"
            :tools-option="tableOption.toolsOption"
            :list-loading="listLoading"
            size="small"
            @change="changePage"
          >
            <template slot="columns">
              <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
                <template slot-scope="scope">
                  <span :class="column.class">{{ scope.row[column.value] }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="60" align="center">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="edit(scope.row)">
                    详情
                  </el-button>
                </template>
              </el-table-column>
            </template>
          </normal-table>
        </el-col>
      </el-card>
    </el-col>
    <detail-device-statics ref="editDevice" @watchChild="fetchData" />
  </el-row>
</template>

<script>
import { getDeviceType } from '@/api/device/device'
import { offlineList } from '@/api/data/dataStatics'
import { batchExportNoiseData } from '@/api/data/data'
import detailDeviceStatics from './detailDeviceStatics'
export default {
  name: 'ListdeviceOffline',
  components: { detailDeviceStatics },
  data() {
    return {
      height: 300,
      // 表格属性
      tableOption: {
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        },
        total: 0
      },
      // 是否需要序号列
      options: {
        needIndex: true
      },
      listQuery: {
        deviceCode: '',
        deviceType: '',
        offset: 1,
        limit: 20
      },
      list: [],
      listLoading: false,
      deviceTypeList: [],
      columns: [
        {
          text: '设备编号',
          value: 'deviceType',
          align: 'center'
        },
        {
          text: '设备类型',
          value: 'deviceTypeName',
          align: 'center'
        },
        {
          text: '井编号',
          value: 'wellCode',
          align: 'center'
        },
        {
          text: '最近记录上传时间',
          value: 'lastTime',
          align: 'center'
        },
        {
          text: '离线天数',
          value: 'offlineDays',
          align: 'center'
        }
      ] // 显示列
    }
  },
  created() {
    this.fetchDeviceType()
    this.fetchData()
  },
  methods: {
    // 获取设备类型
    fetchDeviceType() {
      getDeviceType().then(response => {
        this.deviceTypeList = response.data
      })
    },
    fetchData() {
      offlineList(this.listQuery).then(response => {
        this.tableOption.total = response.total
        this.list = response.data
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportNoiseData(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        const blob = new Blob([res.data])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = `离线设备数据.xlsx` // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放blob对象
      }).catch((res) => {
        loading.close()
      })
    },
    // 详情
    edit(row) {
      this.$refs.editDevice.initDialog('detail', row)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.flagBoxStyle {
  display: flex;
  margin-bottom: 20px;
}
.flagBoxStyle div:nth-child(2){
  line-height: 30px;
  font-weight: 600;
}
.flagStyle {
  width: 4px;
  height: 30px;
  margin-right: 6px;
  background: rgb(64 121 242);
}
.search-btn {
  margin-top: 4px;
}
</style>
