<template>
  <div class="app-container pageTopForm">
    <el-form :model="queryParams" ref="queryRef" size="small" v-show="showSearch" label-width="auto">
      <el-col :span="6">
        <el-form-item label="告警关键字" prop="clientId" titl="告警关键字">
          <el-input
            v-model="queryParams.clientId"
            placeholder="请输入告警关键字"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="告警类型" prop="alarmType">
          <el-select
            v-model="queryParams.alarmType"
            placeholder="请选择告警类型"
            clearable>
            <el-option
              v-for="dict in dict.type.alarm_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item class="lastBtnSty">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-col>
    </el-form>
    <TableList :columns="columns" :config="config" :modelIdent="this.$options.name" :queryParams="queryParams" :tableList="tableList" @fnClick="callback" @fnRenderList="getList" @value-change="handleValueChange">
      <template #tempAlarmType="{ row, column }">
        <dict-tag :options="dict.type.alarm_type" :value="row.alarmType"/>
      </template>
      <template #tempPushFlag="{ row, column }">
        <dict-tag :options="dict.type.push_flag" :value="row.pushFlag"/>
      </template>
    </TableList>
  </div>
</template>

<script setup>
  import TableList from "@/components/table/index.vue"
  import {listAlarmLog} from "@/api/disRevenue/resource"
  export default {
    name: 'AlarmLog',
    components: {TableList},
    dicts: ['alarm_type','push_flag'],
    data() {
      return {
        loading: true,
        showSearch: true,
        tableList: [],
        queryParams: {
          total: 0,
          pageNum: 1,
          pageSize: 10
        },
        // 列显隐信息
        columns: {
          id: { label: `ID`,width: '50'},
          clientId: { label: `告警设备`, minWidth: '250', visible: true },
          mgmPublicIp: { label: `IP`,minWidth: '120',visible: true},
          alarmType: { label: `告警类型`,minWidth: '120', slotName: 'tempAlarmType', visible: true},
          alarmTime: { label: `告警时间`,minWidth: '160',visible: true},
          alarmContent: { label: `告警内容`,minWidth: '250', visible: true },
          businessName: { label: `业务名称`,minWidth: '150', visible: true },
          pushFlag: { label: `推送结果`,minWidth: '150', slotName: 'tempPushFlag', visible: true },
        },
        config: {
          tableButton: {
            top: [
              {content: '告警阈值配置', fnCode: 'add', type: 'primary', icon: 'el-icon-add', hasPermi: 'resource:alarmLog:add'},
              {content: '导出', fnCode: 'export', type: 'warning', icon: 'el-icon-download', hasPermi: 'resource:alarmLog:export'},
            ],
            line: [
              {content: '详情', fnCode: 'details', type: 'primary', icon: 'el-icon-view', hasPermi: 'resource:alarmLog:details'},
            ]
          }
        }
      }
    },
    created() {
      this.getList();
    },
    activated() {
      this.$nextTick(() => {
        this.getList();
      });
    },
    methods: {
      /** 查询列表 */
      getList() {
        this.loading = true;
        listAlarmLog(this.queryParams).then(response => {
          this.tableList = response.rows;
          this.queryParams.total = response.total;
          this.loading = false;
        })
      },
      // 处理子组件传递的新值
      handleValueChange(newValue) {
        // 父组件更新自身数据，实现同步
        this.showSearch = newValue;
        // console.log('父组件拿到新值:', newValue);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryRef");
        this.handleQuery();
      },

      callback(result, rowData, selectChange) {
        if (result && result.fnCode) {
          switch (result.fnCode) {
            case 'add':
              this.$router.push({
                path: '/resource/alarmLog/add',
                query: {type: 'alarmThreshold'}
              });
              break;
            case 'details':
              // this.$router.push({
              //   path:'/resource/alarmLog/details',
              //   query:{
              //     id: rowData.id
              //   }
              // });
              if (rowData && (rowData.alarmType === '1' || rowData.alarmType === '4' || rowData.alarmType === '5')) {
                this.$router.push({
                  path: '/resource/alarmLog/details',
                  query: {
                    id: rowData.clientId,
                    clientId: rowData.clientId,
                    readonly: true,
                    type: 'alarmLog'
                  }
                });
              }
              // else if (rowData && rowData.alarmType === '3') {
              //   this.$router.push({
              //     path: '/resource/alarmLog/details',
              //     query: {
              //       id: rowData.clientId,
              //       readonly: true,
              //       type: 'alarmLog'
              //     }
              //   });
              // }
              break;
            case 'export':
              let paramsList = Object.assign({}, this.queryParams,rowData);
              this.download("rocketmq/alarmLog/export", paramsList, `告警日志 _${new Date().getTime()}.xlsx`, null, 'json');
              break;
            default:
          }
        }
      }
    }
  }
</script>
<style scoped>
  ::v-deep .lastBtnSty .el-form-item__content{
    margin-left: 10px!important;
  }
</style>
