<script setup lang="ts">
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { configAlarmApi } from '#/api';

import { Page, useVbenDrawer, useVbenModal } from '@vben/common-ui';
import { Plus } from '@vben/icons';
import { useUserStore } from '@vben/stores';

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

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteAlarmConfig, getAlarmConfigList } from '#/api';
import { $t } from '#/locales';

import { useColumns } from './data';
import createForm from './modules/createForm.vue';
import editForm from './modules/editForm.vue';

const userStore = useUserStore();

const [EditFormDrawer, editFormDrawerApi] = useVbenDrawer({
  connectedComponent: editForm,
  destroyOnClose: true,
});
const [CreateFormModal, createFormModalApi] = useVbenModal({
  connectedComponent: createForm,
  destroyOnClose: true,
});
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    proxyConfig: {
      response: {
        result: 'list',
        total: 'total',
        current: 'currentPage',
      },
      ajax: {
        query: async () => {
          const res = await getAlarmConfigList(
            userStore!.userInfo!.org_id,
            'alarm',
          );
          return res;
        },
      },
    },

    rowConfig: {
      keyField: 'ID',
    },
    toolbarConfig: {
      custom: true,
      export: false,
      refresh: { code: 'query' },
      search: true,
      zoom: true,
    },
    // pagerConfig: {
    //   enabled: false,
    // },
  } as VxeTableGridOptions<configAlarmApi.Alarm>,
});
async function onDelete(row: configAlarmApi.Alarm) {
  try {
    console.warn('ondelete', row);
    await deleteAlarmConfig(row.id);
  } catch (error) {
    console.error(error);
  }
}
function onActionClick(e: OnActionClickParams<configAlarmApi.Alarm>) {
  console.warn('event', e);
  switch (e.code) {
    case 'delete': {
      onDelete(e.row);
      break;
    }
    case 'edit': {
      onEdit(e.row);
      break;
    }
  }
}
function onRefresh() {
  gridApi.query();
}
function onEdit(row: configAlarmApi.Alarm) {
  editFormDrawerApi.setData(row).open();
}
function onCreate() {
  console.warn('create', createFormModalApi);
  createFormModalApi.open();
  createFormModalApi.setData({
    org_id: userStore!.userInfo!.org_id,
    type: 'alarm',
  });
  console.warn('formDrawer', createFormModalApi.getData());
  // createFormModalApi.open();
}
</script>
<template>
  <Page auto-content-height>
    <CreateFormModal
      @success="onRefresh"
      :data="{ orgId: userStore!.userInfo!.org_id, type: 'alarm' }"
    />
    <!-- <Form /> -->
    <EditFormDrawer @success="onRefresh" />
    <Grid :table-title="$t('config.Alarm.title')">
      <template #toolbar-tools>
        <Button type="primary" @click="onCreate">
          <Plus class="size-5" />
          {{ $t('ui.actionTitle.create', [$t('config.Alarm.name')]) }}
        </Button>
      </template>
    </Grid>
  </Page>
</template>
<style scoped></style>
