<script lang="ts" setup>
/**
 * 站内信类型字典：维护 code/图标/主题色，供消息中心与铃铛展示
 */
import type { VxeGridListeners } from '#/adapter/vxe-table';

import { ref } from 'vue';

import { Page, useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { TableAction } from '#/components/table-action';

import {
  deleteAdminNoticeType,
  getAdminNoticeTypeList,
} from './api';
import FormModal from './components/modal.vue';
import { STATUS_OPTIONS } from './config/constants';

defineOptions({ name: 'AdminNoticeType' });

const hasTopTableDropDownActions = ref(false);

const formOptions = {
  schema: [
    {
      component: 'VbenInput',
      fieldName: 'code',
      label: '编码',
      componentProps: { placeholder: 'type_code' },
    },
    {
      component: 'VbenInput',
      fieldName: 'name',
      label: '名称',
      componentProps: { placeholder: '展示名' },
    },
    {
      component: 'VbenSelect',
      fieldName: 'status',
      label: '状态',
      componentProps: { options: STATUS_OPTIONS, allowClear: true },
    },
  ],
};

const gridOptions = {
  checkboxConfig: { highlight: true, labelField: '' },
  columns: [
    { type: 'checkbox', width: 60 },
    { field: 'id', title: 'ID', width: 80 },
    { field: 'code', title: '编码', minWidth: 160 },
    { field: 'name', title: '名称', minWidth: 120 },
    { field: 'icon', title: '图标', minWidth: 180 },
    { field: 'color', title: '主题色', width: 100 },
    { field: 'sort', title: '排序', width: 80 },
    { field: 'status_txt', title: '状态', width: 80 },
    { field: 'created_at', title: '创建时间', width: 170 },
    { title: '操作', slots: { default: 'action' }, width: 140, fixed: 'right' },
  ],
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }: any, formValues: any) => {
        return await getAdminNoticeTypeList({
          page: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  height: 'auto',
  toolbarConfig: {
    search: true,
    refresh: true,
    slots: { buttons: 'toolbar-actions' },
  },
};

const gridEvents: VxeGridListeners<any> = {
  checkboxChange() {
    hasTopTableDropDownActions.value =
      gridApi.grid.getCheckboxRecords().length > 0;
  },
  checkboxAll() {
    hasTopTableDropDownActions.value =
      gridApi.grid.getCheckboxRecords().length > 0;
  },
};

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions: gridOptions as any,
  gridEvents,
});

const [FormModalComp, formModalApi] = useVbenModal({
  connectedComponent: FormModal,
});

const showModal = (data: any = {}, isUpdate = false) => {
  formModalApi.setData({
    values: data,
    update: isUpdate,
    gridApi,
  });
  formModalApi.open();
};

const handleDelete = (row: any) => {
  deleteAdminNoticeType({ ids: [row.id] }).then(() => {
    message.success('删除成功');
    gridApi.query();
  });
};

const handleBatchDelete = () => {
  const rows = gridApi.grid.getCheckboxRecords();
  if (!rows.length) return;
  deleteAdminNoticeType({ ids: rows.map((r: any) => r.id) }).then(() => {
    message.success('删除成功');
    gridApi.query();
  });
};
</script>
<template>
  <Page auto-content-height>
    <FormModalComp />
    <Grid>
      <template #toolbar-actions>
        <TableAction
          :actions="[
            {
              label: '新增',
              type: 'primary',
              onClick: () => showModal({}, false),
            },
          ]"
          :drop-down-actions="
            hasTopTableDropDownActions
              ? [
                  {
                    label: '批量删除',
                    popConfirm: {
                      title: '确认删除选中类型？',
                      confirm: handleBatchDelete,
                    },
                  },
                ]
              : []
          "
        />
      </template>
      <template #action="{ row }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: () => showModal(row, true),
            },
            {
              label: '删除',
              popConfirm: {
                title: '确认删除该类型？',
                confirm: () => handleDelete(row),
              },
            },
          ]"
        />
      </template>
    </Grid>
  </Page>
</template>
