<template>
  <div class="p-2">
    <div class="custom-tabs">
      <el-radio-group v-model="activeTab" size="large" @change="getList">
        <el-radio-button label="事件推送记录" value="0" />
        <el-radio-button label="事件推送失败记录" value="1" />
      </el-radio-group>
    </div>
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="监控点名称" prop="pointName">
              <el-input v-model="queryParams.pointName" placeholder="请输入监控点名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="事件名称" prop="eventName">
              <el-input v-model="queryParams.eventName" placeholder="请输入事件名称" clearable @keyup.enter="handleQuery" />
            </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-card>
      </div>
    </transition>
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5" v-if="activeTab === '1'">
            <el-button type="primary" plain @click="againClick" v-hasPermi="['sense:eventsReport:add']">批量重试</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="eventsReportList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" v-if="activeTab === '1'" />
        <el-table-column label="上报平台" align="center" prop="reportPlatform" />
        <el-table-column label="监控点名称" align="center" prop="pointName" />
        <el-table-column label="事件名称" align="center" prop="eventName" />
        <!-- <el-table-column label="数据详情" align="center" prop="dataContent" /> -->
        <el-table-column label="数据类型" align="center" prop="dataType" />
        <el-table-column label="上报状态" align="center" prop="reportStatus">
          <template #default="scope">
            {{ scope.row.reportStatus == 0 ? '发送成功' : '发送失败' }}
          </template>
        </el-table-column>
        <el-table-column label="描述" align="center" prop="reportDesc" />
        <el-table-column label="新增时间" align="center" prop="createTime" />
        <template v-if="activeTab === '1'">
          <el-table-column label="更新时间" align="center" prop="updateTime" />
          <el-table-column label="重试次数" align="center" prop="retryNum" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="primary" @click="againClick(scope.row)">重试</el-button>
            </template>
          </el-table-column>
        </template>

      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="EventsReport" lang="ts">
import { listEventsReport, againReport } from '@/api/sense/eventsReport';
import { EventsReportVO } from '@/api/sense/eventsReport/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const eventsReportList = ref<EventsReportVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const activeTab = ref('0')
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  pointName: undefined,
  eventName: undefined,
})


/** 查询推送记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listEventsReport(Object.assign({}, queryParams.value, { reportStatus: activeTab.value }));
  eventsReportList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}


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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: EventsReportVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

const againClick = async (row) => {
  const id = row?.id || ids.value;
  if(Array.isArray(id)&&id.length === 0)
  return proxy?.$modal.msgError("请勾选记录");
  await againReport(id)
  proxy?.$modal.msgSuccess("重试成功");
  await getList();
}

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.custom-tabs {
  text-align: center;
  margin: 10px 0;
}
</style>
