<script setup lang="ts">
import { Page, useVbenModal, type VbenFormProps } from '@vben/common-ui';
import { getVxePopupContainer } from '@vben/utils';

import { Modal, Popconfirm, Space } from 'ant-design-vue';
import {
  useVbenVxeGrid,
  vxeCheckboxChecked,
  type VxeGridProps,
} from '#/adapter/vxe-table';

import { columns, querySchema } from './data';
import warningModal from './warning-modal.vue';
import warningDetail from './warning-detail.vue';
import { alarmEventsListCurr, alarmEventsRemove } from '#/api/sis/alarmEvents';
import type { AlarmEventsForm } from '#/api/sis/alarmEvents/model';

/* 搜索栏配置 */
const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 100,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
};

/* table栏配置 */
const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },
  // 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
  // columns: columns(),
  columns,
  height: 'auto',
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await alarmEventsListCurr({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          states: [20, 30, 31, 32],
          ...formValues,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  id: 'video-warning-processing-index',
};

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

// 监听数据变化，强制重新渲染表格

const [WarningModal, modalApi] = useVbenModal({
  connectedComponent: warningModal,
});

const [WarningDetail, detailApi] = useVbenModal({
  connectedComponent: warningDetail,
});

// 查看详情
async function handleView(row: any) {
  detailApi.setData({ id: row.id, data: row });
  detailApi.open();
}

// 编辑
async function handleEdit(row: any) {
  modalApi.setData({ id: row.id, data: row });
  modalApi.open();
}

// 删除
async function handleDelete(row: any) {
  const index = row.id;
  if (index !== -1) {
    await tableApi.query();
  }
}

// 批量删除
function handleMultiDelete() {
  const rows = tableApi.grid.getCheckboxRecords();
  const ids = rows.map((row: Required<AlarmEventsForm>) => row.id);
  Modal.confirm({
    title: '提示',
    okType: 'danger',
    content: `确认删除选中的${ids.length}条记录吗？`,
    onOk: async () => {
      await alarmEventsRemove(ids);
      await tableApi.query();
    },
  });
}
</script>
<template>
  <Page :auto-content-height="true">
    <BasicTable class="flex-1 overflow-hidden">
      <template #toolbar-tools>
        <Space>
          <a-button
            :disabled="!vxeCheckboxChecked(tableApi)"
            danger
            type="primary"
            v-access:code="['video:warning:remove']"
            @click="handleMultiDelete"
          >
            {{ $t('pages.common.delete') }}
          </a-button>
        </Space>
      </template>
      <template #action="{ row }">
        <Space>
          <!-- <ghost-button
            v-access:code="['video:warning:level']"
            @click.stop="handleLevelSetting(row)"
          >
            级别设置
          </ghost-button> -->
          <ghost-button
            v-access:code="['video:warning:view']"
            @click.stop="handleView(row)"
          >
            {{ $t('pages.common.info') }}
          </ghost-button>

          <ghost-button
            v-access:code="['video:warning:edit']"
            @click.stop="handleEdit(row)"
          >
            处理
          </ghost-button>

          <Popconfirm
            :get-popup-container="getVxePopupContainer"
            placement="left"
            title="确认删除？"
            @confirm="handleDelete(row)"
          >
            <ghost-button
              danger
              v-access:code="['video:warning:remove']"
              @click.stop=""
            >
              {{ $t('pages.common.delete') }}
            </ghost-button>
          </Popconfirm>
        </Space>
      </template>
    </BasicTable>
    <WarningModal @reload="tableApi.query()" />
    <WarningDetail />
  </Page>
</template>
<style scoped lang="scss">
.ant-table-wrapper {
  .ant-table-thead > tr > th {
    background-color: #fafafa;
    font-weight: 600;
  }
}

.ant-pagination {
  .ant-pagination-item-active {
    background-color: #1890ff;
    border-color: #1890ff;
  }
}
</style>
