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

import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { PageQuery } from '#/api/baseModel';
import type { DictItemApi } from '#/api/system/dict/dictItem';

import { ref } from 'vue';

import { useVbenDrawer } from '@vben/common-ui';
import { Plus } from '@vben/icons';

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

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { apiDelete, apiPage } from '#/api/system/dict/dictItem';

import { emitter } from '../mitt';
import { querySchema, useColumns } from './data';
import Form from './form.vue';

const dictType = ref('');

const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 70,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema,
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
};

const [FormDrawer, formDrawerApi] = useVbenDrawer({
  connectedComponent: Form,
});

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions: {
    columns: useColumns(onActionClick),
    height: 'auto',
    keepSource: true,
    pagerConfig: {
      enabled: true,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues = {}) => {
          const params: PageQuery = {
            pageNum: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          };
          if (dictType.value) {
            params.dictType = dictType.value;
          }
          return await apiPage(params);
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      // 高亮当前行
      isCurrent: true,
    },
    id: 'system-dict-item-index',
    rowClassName: 'hover:cursor-pointer',
  } as VxeTableGridOptions,
});

function onActionClick({
  code,
  row,
}: OnActionClickParams<DictItemApi.DictItem>) {
  switch (code) {
    case 'delete': {
      onDelete(row);
      break;
    }
    case 'edit': {
      onEdit(row);
      break;
    }
    default: {
      break;
    }
  }
}

function onRefresh() {
  gridApi.query();
}
function onEdit(row: DictItemApi.DictItem) {
  formDrawerApi.setData(row).open();
}
function onCreate() {
  formDrawerApi.setData({ dictType: dictType.value }).open();
}

function onDelete(row: DictItemApi.DictItem) {
  const hideLoading = message.loading({
    content: `正在删除${row.dictLabel}...`,
    duration: 0,
    key: 'action_process_msg',
  });
  apiDelete(row.id || 0)
    .then(() => {
      message.success({
        content: `${row.dictLabel}删除成功`,
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}

emitter.on('rowClick', async (value) => {
  dictType.value = value;
  await gridApi.query();
});
</script>
<template>
  <div>
    <FormDrawer @success="onRefresh" />
    <Grid table-title="字典明细列表">
      <template #toolbar-tools>
        <Button
          type="primary"
          v-access:code="['system:dict:add']"
          @click="onCreate"
          :disabled="dictType === ''"
        >
          <Plus class="size-5" />
          新增
        </Button>
      </template>
      <template #dictLabel="{ row }">
        <Tag :color="row.listClass">{{ row.dictLabel }}</Tag>
      </template>
    </Grid>
  </div>
</template>
