<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <div class="search-left">
        <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
          <el-row>
            <el-form-item>
              <el-input v-model.trim="listQuery.devcode" placeholder="设备编号" clearable/>
            </el-form-item>
            <el-form-item>
              <el-select v-model="listQuery.status" placeholder="下发状态" filterable clearable>
                <el-option v-for="item in statusList" :key="item.name" :label="item.name" :value="item.name"/>
              </el-select>
            </el-form-item>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            <el-button class="filter-item" type="warning" icon="el-icon-delete" @click="clearInput">重置</el-button>
          </el-row>
        </el-form>
      </div>
    </div>
    <!--查询结果Table显示-->
    <div>
      <el-row class="table-title">
        <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
        <el-col :span="12" :offset="6" class="edit_btns">
          <el-button class="edit_btn" size="small" @click="sendAll">全部设备下发</el-button>
          <el-button class="edit_btn" size="small" @click="send">设备下发</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" :row-style="{height: '47px'}" class="table" border @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column :index="indexMethod" label="#" align="center" type="index" />
        <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">
            <div v-if="column.value === 'channel1'">
              <span v-html="allNum(scope.row.elecMinAlarm1,scope.row.elecMaxAlarm1,scope.row.voltMinAlarm1,scope.row.voltMaxAlarm1,scope.row.channelOpenTime1,scope.row.channelCloseTime1)"/>
            </div>
            <div v-else-if="column.value === 'channel2'">
              <span v-html="allNum(scope.row.elecMinAlarm2,scope.row.elecMaxAlarm2,scope.row.voltMinAlarm2,scope.row.voltMaxAlarm2,scope.row.channelOpenTime2,scope.row.channelCloseTime2)"/>
            </div>
            <div v-else-if="column.value === 'channel3'">
              <span v-html="allNum(scope.row.elecMinAlarm3,scope.row.elecMaxAlarm3,scope.row.voltMinAlarm3,scope.row.voltMaxAlarm3,scope.row.channelOpenTime3,scope.row.channelCloseTime3)"/>
            </div>
            <div v-else-if="column.value === 'channel4'">
              <span v-html="allNum(scope.row.elecMinAlarm4,scope.row.elecMaxAlarm4,scope.row.voltMinAlarm4,scope.row.voltMaxAlarm4,scope.row.channelOpenTime4,scope.row.channelCloseTime4)"/>
            </div>
            <div v-else>
              <span>{{ scope.row[column.value] }}</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        v-show="total>listQuery.limit"
        :current-page="listQuery.offset"
        :page-sizes="[20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { deviceConfigLog, toDevice, toDeviceAll } from '@/api/system/base'

export default {
  name: 'ListLog',
  data() {
    return {
      listQuery: {
        devcode: '', // 设备编号/设备名称
        status: '',
        offset: 1,
        limit: 20,
        sort: 'devcode',
        order: 'asc'
      }, // 筛选条件
      columns: [
        {
          text: '设备编号',
          value: 'devcode',
          align: 'center',
          width: 150
        },
        {
          text: '电信设备编号',
          value: 'teleDeviceId',
          align: 'center'
        },
        {
          text: '采集开始时间',
          value: 'collectStartTime',
          align: 'center',
          width: 150
        },
        {
          text: '采集间隔',
          value: 'collectCycle',
          align: 'center',
          width: 80
        },
        {
          text: '采集次数',
          value: 'collectCnt',
          align: 'center',
          width: 80
        },
        {
          text: '通道1',
          value: 'channel1',
          align: 'center'
        }, {
          text: '通道2',
          value: 'channel2',
          align: 'center'
        }, {
          text: '通道3',
          value: 'channel3',
          align: 'center'
        }, {
          text: '通道4',
          value: 'channel4',
          align: 'center'
        },
        {
          text: '下发时间',
          value: 'ts',
          align: 'center',
          width: 150
        },
        {
          text: '下发状态',
          value: 'status',
          align: 'center',
          width: 90
        }
      ],
      total: 0, // 数据总数
      list: [], // 报警列表
      listLoading: true, // 列表加载
      multipleSelection: [], // 多选选中项
      dialogFormVisible: false,
      alarmStatusList: [{ name: '正在报警', value: '1' }, { name: '已取消', value: '2' }], // 报警状态列表
      statusList: [{ name: '未下发' }, { name: '已下发' }],
      alarmTypeList: [],
      timeRange: [] // 时间范围
    }
  },
  created() {
    this.fetchData()// 获取数据
  },
  activated() {
    this.fetchData()
  },
  methods: {
    allNum(num1, num2, num3, num4, num5, num6) {
      var str = num1
      if (num1.length !== 0) str += ('A/' + num2)
      else str += ('/' + num2)
      if (num2.length !== 0) str += ('A/' + num3)
      else str += ('/' + num3)
      if (num3.length !== 0) str += ('V/' + num4)
      else str += ('/' + num4)
      if (num4.length !== 0) str += ('V/' + num5 + '/' + num6)
      else str += ('/' + num5 + '/' + num6)
      return str
      // return num1 + 'A/' + num2 + 'A/' + num3 + 'V/' + num4 + 'V/' + num5 + '/' + num6
    },
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      deviceConfigLog(this.listQuery).then(response => {
        if (response.code === 200) {
          this.list = response.data.rows
          this.total = parseInt(response.data.total)
        } else {
          this.$message.error(response.message)
        }
        this.listLoading = false
      })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 设备下发
    send() {
      if (this.checkSelection()) {
        const devIds = []
        this.multipleSelection.forEach(function(value, index) {
          devIds.push(value.devcode)
        })
        this.$confirm(
          '确定要下发参数到所选设备吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          toDevice(devIds).then(response => {
            if (response.code === 200) {
              this.$message.success('信息发送成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 设备下发
    sendAll() {
      this.$confirm(
        '确定要下发参数到全部设备吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        toDeviceAll(this.listQuery).then(response => {
          if (response.code === 200) {
            this.$message.success('信息发送成功')
            this.fetchData()
          }
        })
      })
    },
    showDetail(row) {
      this.dialogFormVisible = true
      this.$refs.alarmdetail.initDialog(this.dialogFormVisible, row)
    },
    clearInput() {
      this.listQuery = {
        name: '', // 姓名
        sex: '', // 性别
        nation: '', // 民族
        collReason: '', // 采集原因
        cardType: '', // 证件类型
        idCardNo: '', // 证件号码
        alarmReason: '', // 报警原因
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }
      this.timeRange = []
      this.fetchData(false)
    },
    // 序号计算
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .app-container{
    margin-bottom:20px
  }
  .table{
    margin-bottom: 20px;
  }
  .small-row-class{
    height: 15px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
<style rel="stylesheet/scss" lang="scss">
  .small-row-class td{
    padding:2px 0px;
  }
</style>
