<template>
  <div class="deviceStatistics card">
    <div class="card-head">
      <div class="title">当前设备告警记录</div>
      <div class="search">
        <el-select class="select" placeholder="请选择事件类型" v-model="paging.warnType" style="width: 150px; margin-right: 20px" @change="handleChange">
          <el-option label="全部" value="" />
          <el-option v-for="(item, index) in warnTypeList" :key="index" :label="item" :value="index" />
        </el-select>
        <el-select class="select" placeholder="请选择处理状态" v-model="paging.status" style="width: 150px" @change="handleChange">
          <el-option label="全部" value="" />
          <el-option label="待处理" value="0" />
          <el-option label="已处理" value="1" />
        </el-select>
      </div>
    </div>

    <TableCustom :columns="columns" :tableData="tableData" :paging="paging" :changePage="changePage">
      <template #status="{ rows }">
        <el-tag :type="statusColor[rows.status]">{{ warningStatusEnum[rows.status] }}</el-tag>
      </template>
      <template #warnType="{ rows }">
        {{ warnTypeEnum[rows.warnType] }}
      </template>
      <template #operator="{ rows }">
        <el-button type="primary" size="small" link @click="toIncidentDispose(rows.id)"> 处理事件</el-button>
      </template>
    </TableCustom>
  </div>
</template>
<script setup lang="ts">
import TableCustom from "@/components/table-custom.vue";
import { warningRecord } from "@/api/index";
import { ref, reactive, watch } from "vue";
import { TDevice } from "@/api/index.d";
import { useRouter } from "vue-router";

const statusColor = ["danger", "success"];

enum warningStatusEnum {
  "待处理",
  "已处理",
}

enum warnTypeEnum {
  "SOS告警",
  "围栏告警",
  "破坏告警",
  "低电告警",
  "心率告警",
  "血氧告警",
  "体温告警",
}

const router = useRouter();
const warnTypeList = ["SOS告警", "围栏告警", "破坏告警", "低电告警", "心率告警", "血氧告警", "体温告警"];
const paging = reactive({
  page: 1,
  size: 5,
  total: 0,
  deviceId: undefined,
  status: undefined,
  warnType: undefined,
});
const tableData = ref<TDevice.IWarningRecordRes[]>([]);

const props = defineProps({
  deviceInfo: {
    type: Object,
    default: undefined,
  },
});

// 表格相关
let columns = [
  { type: "index", label: "序号", width: 55, align: "center" },
  { prop: "warnType", label: "事件类型" },
  { prop: "status", label: "处理状态" },
  { prop: "createTime", label: "触发时间" },
  { prop: "operator", label: "操作" },
];
const getData = async () => {
  const res = await warningRecord(paging);
  tableData.value = res.records;
  paging.total = res.total;
};

const handleChange = () => {
  paging.page = 1;
  getData();
};
const changePage = (val: number) => {
  paging.page = val;
  getData();
};

watch(
  () => props.deviceInfo,
  (newVal) => {
    if (newVal) {
      paging.deviceId = newVal.deviceId;
      getData && getData();
    }
  },
  { immediate: true } // 关键选项
);
const toIncidentDispose = (id: string) => {
  router.push({
    path: "/incidentDispose",
    query: { id },
  });
};
</script>
<style scoped lang="less">
.card {
  min-height: 345px;
  height: 100%;
  padding: 16px 10px;
  box-sizing: border-box;
  flex-shrink: 0;
  background: #ffffff;

  .card-head {
    color: #061451;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transform: translateX(-10px);

    .title {
      &::before {
        display: inline-block;
        margin-right: 10px;
        content: "";
        width: 2px;
        height: 14px;
        border-radius: 20px;
        background: #061451;
      }
    }

    .search {
      display: flex;
      align-items: center;
    }
  }
}
</style>
