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

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

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

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteDevice, getDeviceList } 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 [CreateFormDrawer, createformDrawerApi] = useVbenDrawer({
  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 getDeviceList(userStore!.userInfo!.org_id);
          // console.warn('',res)
          return res;
        },
      },
    },

    rowConfig: {
      keyField: 'ID',
    },
    toolbarConfig: {
      custom: true,
      export: false,
      refresh: { code: 'query' },
      search: true,
      zoom: true,
    },
    // pagerConfig: {
    //   enabled: false,
    // },
  } as VxeTableGridOptions<DeviceApi.Device>,
});
function onDelete(row: DeviceApi.Device) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.name]),
    duration: 0,
    key: 'action_process_msg',
  });
  console.warn('ondelete', row);
  deleteDevice(row.id)
    .then(() => {
      message.success({
        content: $t('ui.actionMessage.deleteSuccess', [row.name]),
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}
function onActionClick(e: OnActionClickParams<DeviceApi.Device>) {
  console.warn('event', e);
  switch (e.code) {
    case 'delete': {
      onDelete(e.row);
      break;
    }
    case 'edit': {
      onEdit(e.row);
      break;
    }
  }
}
function onRefresh() {
  gridApi.query();
}
async function onEdit(row: DeviceApi.Device) {
  editformDrawerApi.setData(row).open();
  // onRefresh();
}
function onCreate() {
  createformDrawerApi.setData({}).open();
  // onRefresh();
}
</script>
<template>
  <Page auto-content-height>
    <EditFormDrawer
      :title="`${$t('ui.actionTitle.edit')}${$t('device.name')}`"
      @success="onRefresh"
    />
    <CreateFormDrawer
      :title="`${$t('ui.actionTitle.create')}${$t('device.name')}`"
      @success="onRefresh"
    />
    <Grid :table-title="$t('device.list.title')">
      <template #toolbar-tools>
        <Button type="primary" @click="onCreate">
          <Plus class="size-5" />
          {{ $t('ui.actionTitle.create', [$t('device.name')]) }}
        </Button>
      </template>
    </Grid>
  </Page>
</template>
<style scoped></style>
