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

import type {
  OnActionClickParams,
  VxeGridListeners,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { DictTypeApi } from '#/api/system/dict/dictType';

import { ref } from 'vue';

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

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

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

import { emitter } from '../mitt';
import { querySchema, useColumns } from './data';
import dictTypeModal from './model.vue';

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

const [DictTypeModal, modalApi] = useVbenModal({
  connectedComponent: dictTypeModal,
});

const lastDictType = ref('');

const gridEvents: VxeGridListeners<DictTypeApi.DictType> = {
  cellClick: ({ row }) => {
    // const dictId = row.id;
    // if (!dictId) return;

    if (lastDictType.value === row.dictType) {
      return;
    }
    emitter.emit('rowClick', row.dictType);
    lastDictType.value = row.dictType;
  },
};

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

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

function onRefresh() {
  gridApi.query();
}
function onEdit(row: DictTypeApi.DictType) {
  modalApi.setData(row).open();
}
function onCreate() {
  modalApi.setData({}).open();
}

function onDelete(row: DictTypeApi.DictType) {
  const hideLoading = message.loading({
    content: `正在删除${row.dictName}...`,
    duration: 0,
    key: 'action_process_msg',
  });
  apiDelete(row.id || 0)
    .then(() => {
      message.success({
        content: `${row.dictName}删除成功`,
        key: 'action_process_msg',
      });
      onRefresh();
    })
    .catch(() => {
      hideLoading();
    });
}
</script>
<template>
  <div>
    <DictTypeModal @success="onRefresh" />
    <Grid table-title="字典类型列表">
      <template #toolbar-tools>
        <Button
          type="primary"
          v-access:code="['system:dict:add']"
          @click="onCreate"
        >
          <Plus class="size-5" />
          新增
        </Button>
      </template>
    </Grid>
  </div>
</template>
