<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { useRouter } from 'vue-router';
import { Page, useVbenDrawer } from '@vben/common-ui';
import { useAccess } from '@vben/access';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getSecurityAlertList } from '#/api/aiAnalysis/aiAnalysis';

import { queryFormSchema, tableColumns } from './warning-data';
import WarningDrawerComp from './warning-drawer.vue';

const router = useRouter();

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: queryFormSchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
  fieldMappingTime: [
    [
      'alertTime',
      ['alertStartTime', 'alertEndTime'],
      ['YYYY-MM-DD HH:mm:ss', 'YYYY-MM-DD HH:mm:ss'],
    ],
  ],
};

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    highlight: true,
    reserve: true,
    trigger: 'default',
  },
  columns: tableColumns,
  size: 'medium',
  height: 'auto',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        const resp = await getSecurityAlertList({
          ...formValues,
          pageNum: page.currentPage,
          pageSize: page.pageSize,
        });
        return { items: resp.rows, total: resp.total };
      },
    },
  },
  rowConfig: {
    keyField: 'alertId',
  },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'aiAnalysis-warning-index',
};

const [BasicTable, basicTableApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
});

const [WarningDrawer, drawerApi] = useVbenDrawer({
  connectedComponent: WarningDrawerComp,
});

const { hasAccessByCodes } = useAccess();

// 处理预警
function handleProcess(row: any) {
  drawerApi.setData({ ...row, isProcess: true }).open();
}

// 查看预警详情
function handleView(row: any) {
  // 跳转到详情页面
  router.push({
    name: 'WarningDetail',
    params: { alertId: row.alertId },
  });
}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable table-title="预警记录列表">
      <template #action="{ row }">
        <ElSpace>
          <ElButton
            size="small"
            type="info"
            plain
            @click="handleView(row)"
            v-access:code="['aiAnalysis:warning:view']"
          >
            查看
          </ElButton>
          <ElButton
            v-if="row.status === 'unhandled'"
            size="small"
            type="primary"
            plain
            @click="handleProcess(row)"
            v-access:code="['aiAnalysis:warning:process']"
          >
            处理
          </ElButton>
        </ElSpace>
      </template>
    </BasicTable>
    <WarningDrawer @reload="basicTableApi.reload()" />
  </Page>
</template>

<style scoped lang="scss">
:deep(.el-tooltip__trigger:focus) {
  outline: none;
}
</style>
