
<template>
  <el-card  v-loading="loading" gshadow="always" class="common-card-container common-card-container-no-title-bg">
    <!--    搜索栏-->
    <el-form :inline="true" class="select-container">
      <el-form-item label="设备类型">
        <el-select v-model="search.deviceType" placeholder="请选择" :loading="loading" loading-text="正在加载数据">
          <el-option :label="value" :value="key" v-for="(value,key) in $store.state.ems.deviceTypeOptions" :key="key+'deviceTypeOptions'"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="告警等级">
        <el-select v-model="search.alarmLevel" placeholder="请选择" :loading="loading" loading-text="正在加载数据">
          <el-option :label="value" :value="key" v-for="(value,key) in $store.state.ems.alarmLevelOptions" :key="key+'alarmLevelOptions'"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="时间选择">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          :picker-options="pickerOptions"
          :default-value="defaultDateRange"
          end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch" native-type="button">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="onReset" native-type="button">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="margin:30px 0;">
      <!--        二个选择按钮-->
      <el-row style="">
        <el-col :xs="24" :sm="24" :lg="24">
          <el-button-group class="ems-btns-group">
            <el-button v-for="(item,index) in btnList" :key="index+'dtdcBtns'" :class="{'activeBtn' : activeBtn === item.id}" @click="changeDataType(item.id)">{{item.name}}</el-button>
          </el-button-group>
        </el-col>
      </el-row>
      <!--表格-->
      <el-table
          class="common-table"
          :data="tableData"
          stripe
          max-height="500"
          style="width: 100%;margin-top:25px;">
          <el-table-column
            prop="name"
            width="200"
            label="设备名称">
          </el-table-column>
          <el-table-column
            label="告警等级"
          >
            <template slot-scope="scope">
              <span>{{$store.state.ems.alarmLevelOptions[scope.row.alarmLevel]}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="alarmContent"
            min-width="400"
            show-overflow-tooltip
            label="告警内容">
          </el-table-column>
          <el-table-column
            prop="alarmStartTime"
            label="告警发生时间">
          </el-table-column>
          <el-table-column
            prop="alarmEndTime"
            label="告警结束时间">
          </el-table-column>
          <el-table-column
            label="状态">
            <template slot-scope="scope">
              <span :class="['0','2'].includes(scope.row.status) ? 'warning-status' : ''">{{$store.state.ems.alarmStatusOptions[scope.row.status]}}</span>
            </template>
          </el-table-column>
        </el-table>
      <el-pagination
        v-show="tableData.length>0"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
        style="margin-top:15px;text-align: center"
      >
      </el-pagination>
    </div>
  </el-card>
</template>


<script>
import {getAlarmDetailList} from'@/api/ems/dzjk'
import getQuerySiteId from "@/mixins/ems/getQuerySiteId";
export default {
  name:'DzjkGzgj',
  mixins:[getQuerySiteId],
  data() {
    return {
      loading:false,
      btnList:[
        {name:'今日告警',id:'today'},
        {name:'历史告警',id:'yesterday'},
      ],
      pickerOptions:{
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      defaultDateRange:[],//默认展示的时间
      dateRange:[],//alarmStartTime,alarmEndTime
      activeBtn:'today',
      search:{deviceType:'',alarmLevel:''},
      deviceTypeOptions:[
        {name:'类型一',id:1},
        {name:'类型二',id:2},
        {name:'类型三',id:3},
      ],
      alarmLevelOptions:[
        {name:'等级一',id:1},
        {name:'等级二',id:2},
      ],
      // 表格、分页
      tableData:[],
      pageSize:10,//分页栏当前每个数据总数
      pageNum:1,//分页栏当前页数
      totalSize:0,//table表格数据总数
    }
  },
  methods:{
    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.$nextTick(()=>{
        this.getTableData()
      })
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.$nextTick(()=>{
        this.getTableData()
      })
    },
    // 搜索
    onSearch(){
      this.getData()
    },
    // 重置
    onReset(){
      this.search={deviceType:'',alarmLevel:''}
      this.dateRange=[]
      this.getData()
    },
    // 切换今日、历史告警
    changeDataType(id){
      if(id !== this.activeBtn){
        console.log('点击了不同的菜单，更新数据')
        this.activeBtn=id;
        this.getData()
      }
    },
    // 获取数据
    getData(){
      this.loading=true
      const {deviceType,alarmLevel} = this.search
      const {siteId,pageNum,pageSize,activeBtn} =this
      const [alarmStartTime='',alarmEndTime='']=this.dateRange
      const todayFlag = activeBtn === 'today'
      getAlarmDetailList({deviceType,alarmLevel,siteId,pageSize,pageNum,alarmStartTime,alarmEndTime,todayFlag}).then(response => {
        this.tableData=response?.data || [];
        this.totalSize = response?.total || 0
      }).finally(() => {this.loading=false})
    },
    init(){
      this.onReset()
    },
  },
  mounted(){
    const now = new Date();
    const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
    this.defaultDateRange = [lastMonth, now];
  }
}
</script>
