<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="16">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="0">
          <el-form-item label="" prop="objId" style="margin-right:10px">
            <el-select v-model="queryParams.objId" placeholder="请选择业务类型" style="width: 260px" clearable filterable remote reserve-keyword :remote-method="remoteMethod">
              <el-option
                  v-for="dict in objList"
                  :key="dict.objId"
                  :label="dict.objName"
                  :value="dict.objId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="objMetricsId" style="margin-right:10px">
            <el-input
                v-model="queryParams.objMetricsId"
                placeholder="请输入告警指标"
                clearable
                @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="" prop="alarmLevel" style="margin-right:10px">
            <el-select v-model="queryParams.alarmLevel" placeholder="请选择告警级别" clearable>
              <el-option
                  v-for="dict in alarm_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="" style="width: 308px;margin-right:10px">
            <el-date-picker
                v-model="daterangeAlarmTime"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="recordList" border>
      <el-table-column label="告警ID" align="left" prop="alarmId" width="80"/>
      <el-table-column label="告警对象" align="left" prop="bizobj.objName"/>
      <el-table-column label="告警指标" align="center" prop="bizObjMetrics.metricsName"/>
      <el-table-column label="告警级别" align="center" prop="alarmLevel">
        <template #default="scope">
          <dict-tag :options="alarm_level" :value="scope.row.alarmLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="告警值" align="center" prop="alarmValue" :formatter="rounding"/>
      <el-table-column label="告警时间" align="center" prop="alarmTime" width="220">
        <template #default="scope">
          <span>{{ parseTime(scope.row.alarmTime, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </div>
</template>

<script setup name="Record">
import {listRecord} from "@/api/alarm/record";
import {listObj} from "@/api/obj/obj.js"
const {proxy} = getCurrentInstance();
const {alarm_level} = proxy.useDict('alarm_level');

const recordList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const daterangeAlarmTime = ref([]);
const objList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    objId: null,
    objMetricsId: null,
    alarmLevel: null,
    alarmTime: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

function remoteMethod(keyword){
  listObj({ pageNum: 1,pageSize: 5,objName: keyword}).then(res=>{
    objList.value = res.rows
  })
}

/** 查询告警记录列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeAlarmTime && '' != daterangeAlarmTime) {
    queryParams.value.params["beginAlarmTime"] = daterangeAlarmTime.value[0];
    queryParams.value.params["endAlarmTime"] = daterangeAlarmTime.value[1];
  }
  listRecord(queryParams.value).then(response => {
    recordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeAlarmTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 保留2位小数*/
function rounding(row, column) {
  return parseFloat(row[column.property]).toFixed(2)
}

getList();
</script>
