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

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 { deleteUser, getUserList } from '#/api';
import { $t } from '#/locales';

import { useColumns } from './data';
import addForm 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: addForm,
  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 getUserList(userStore!.userInfo!.org_id);
          return res;
        },
      },
    },

    rowConfig: {
      keyField: 'id',
    },
    toolbarConfig: {
      custom: true,
      export: false,
      refresh: { code: 'query' },
      search: true,
      zoom: true,
    },
    // pagerConfig: {
    //   enabled: false,
    // },
  } as VxeTableGridOptions<SystemUserApi.SystemUser>,
});
function onDelete(row: SystemUserApi.SystemUser) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.name]),
    duration: 0,
    key: 'action_process_msg',
  });
  console.warn('ondelete', row);
  deleteUser(row.id)
    .then(() => {
      message.success({
        content: $t('ui.actionMessage.deleteSuccess', [row.name]),
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}
function onActionClick(e: OnActionClickParams<SystemUserApi.SystemUser>) {
  switch (e.code) {
    case 'delete': {
      onDelete(e.row);
      break;
    }
    case 'edit': {
      onEdit(e.row);
      break;
    }
  }
}
function onRefresh() {
  gridApi.query();
}
function onEdit(row: SystemUserApi.SystemUser) {
  editFormDrawerApi.setData(row).open();
}
function onCreate() {
  createFormDrawerApi.setData({}).open();
}
</script>
<template>
  <Page auto-content-height>
    <!-- <Form /> -->
    <CreateFormDrawer @success="onRefresh" />
    <EditFormDrawer @success="onRefresh" />
    <Grid :table-title="$t('system.user.title')">
      <template #toolbar-tools>
        <Button
          type="primary"
          @click="onCreate"
          v-if="userStore.buttonPermissions.includes('addUser')"
        >
          <Plus class="size-5" />
          {{ $t('ui.actionTitle.create', [$t('system.user.name')]) }}
        </Button>
      </template>
    </Grid>
  </Page>
</template>
<style scoped></style>
