import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { classificationApi } from '#/api';

import { z } from '#/adapter/form';
import { $t } from '#/locales';

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export const useColumns = (
  onActionClick: OnActionClickFn<classificationApi.classification>,
): VxeTableGridOptions<classificationApi.classification>['columns'] => {
  return [
    {
      field: 'name',
      title: $t('config.classification.data.name'),
      width: 150,
      treeNode: true,
    },
    {
      field: 'desc',
      title: $t('config.classification.data.desc'),
    },
    {
      field: 'org_id',
      title: $t('config.classification.data.org_id'),
    },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          // {
          //   code: 'append',
          //   text: '新增下级',
          // },
          'edit', // 默认的编辑按钮
          {
            code: 'delete', // 默认的删除按钮
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('system.dept.operation'),
      width: 200,
    },
  ];
};

export function useEditSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('config.classification.data.name'),
      rules: z
        .string()
        .min(
          1,
          $t('ui.formRules.minLength', [
            $t('config.classification.data.name'),
            1,
          ]),
        )
        .max(
          20,
          $t('ui.formRules.maxLength', [
            $t('config.classification.data.name'),
            20,
          ]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'desc',
      label: $t('config.classification.data.desc'),
    },
  ];
}
export function useCreateSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('config.classification.data.name'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.nickName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [
            $t('config.classification.data.name'),
            20,
          ]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'desc',
      label: $t('config.classification.data.desc'),
    },
  ];
}
