<template>
  <div class="app-container">
    <!--筛选条件-->
    <el-dialog :visible.sync="dialogFormVisible" :title="title" append-to-body width="1100px">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-form-item class="selectForm-container-item" prop="startTime">
          <el-date-picker
            v-model="timeRange"
            type="datetimerange"
            range-separator="至"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="查询开始时间"
            end-placeholder="查询结束时间"/>
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form>
      <!--查询结果Table显示-->
      <div>
        <el-row class="table-title">
          <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
        </el-row>
        <el-table v-loading="listLoading" :data="list" class="table" height="300px" border>
          <el-table-column :index="indexMethod" label="序号" align="center" type="index" width="55" />
          <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" >
            <template slot-scope="scope">
              <span>{{ scope.row[column.value] }}</span>
            </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>
    </el-dialog>
  </div>
</template>

<script>
import { deviceLogDetail } from '@/api/system/strategy'

export default {
  name: 'DeviceHistory',
  data() {
    return {
      listQuery: {
        deviceId: '',
        startTime: '',
        endTime: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '设备名称',
          value: 'deviceName',
          align: 'center'
        },
        {
          text: '设备类型',
          value: 'deviceTypeName',
          align: 'center',
          width: 100
        },
        {
          text: '策略名称',
          value: 'strategyName',
          align: 'center'
        },
        {
          text: '生效日期',
          value: 'startDate',
          align: 'center',
          width: 100
        },
        {
          text: '失效日期',
          value: 'endDate',
          align: 'center',
          width: 100
        },
        {
          text: '优先级',
          value: 'priorityName',
          align: 'center',
          width: 70
        },
        {
          text: '策略内容',
          value: 'strategyContent',
          align: 'center',
          width: 200
        },
        {
          text: '操作人',
          value: 'operationUserName',
          align: 'center',
          width: 100
        },
        {
          text: '设置时间',
          value: 'ts',
          align: 'center',
          width: 160
        }
      ],
      title: '',
      total: 0, // 数据总数
      list: [], // 报警列表
      listLoading: true, // 列表加载
      timeRange: [], // 时间范围
      dialogFormVisible: false
    }
  },
  watch: {
    // 当时间范围变化时，填充listQuery时间
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.startTime = val[0]
        this.listQuery.endTime = val[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  created() {
    this.fetchData()// 获取数据
  },
  activated() {
    this.fetchData()
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogFormVisible, row = null) {
      this.dialogFormVisible = dialogFormVisible
      this.listQuery.deviceId = row.deviceId
      this.title = row.deviceName + '历史策略记录'
      this.fetchData()
    },
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      deviceLogDetail(this.listQuery).then(response => {
        console.log(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)
    },
    cancel() {
      this.dialogFormVisible = 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()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .app-container{
    margin-bottom:10px
  }
  .table{
    margin-bottom: 10px;
  }
  .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>
