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

import { useUserStore } from '@vben/stores';

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

const userStore = useUserStore();
/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export const useColumns = (
  onActionClick: OnActionClickFn<productionApi.production>,
): VxeTableGridOptions<productionApi.production>['columns'] => {
  return [
    {
      field: 'name',
      title: $t('device.production.name'),
      width: 150,
    },
    {
      field: 'category_id',
      title: $t('device.production.data.category_id'),
    },
    {
      field: 'desc',
      title: $t('device.production.data.desc'),
    },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          // {
          //   code: 'append',
          //   text: '新增下级',
          // },
          'edit', // 默认的编辑按钮
          {
            code: 'delete', // 默认的删除按钮
            disabled: (_row: productionApi.production) => {
              return false;
              // return !!(row.children && row.children.length > 0);
            },
          },
        ],
      },
      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('device.production.name'),
      rules: z
        .string()
        .min(1, $t('ui.formRules.minLength', [$t('device.production.name'), 1]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('device.production.name'), 20]),
        ),
    },
    {
      component: 'ApiSelect',
      fieldName: 'category_id',
      label: $t('device.production.data.category_id'),
      componentProps: {
        autoSelect: 'first',
        class: 'w-full',
        api: async () => {
          const { list } = await getClassificationFlatList(
            userStore?.userInfo?.org_id || '',
          );
          return list;
        },
        labelField: 'name',
        valueField: 'id',
      },
      rules: z.string(),
    },
    {
      component: 'Input',
      fieldName: 'desc',
      label: $t('device.production.data.desc'),
    },
  ];
}
export function useCreateSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('device.production.name'),
      rules: z
        .string()
        .min(1, $t('ui.formRules.minLength', [$t('device.production.name'), 1]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('device.production.name'), 20]),
        ),
    },
    {
      component: 'ApiSelect',
      fieldName: 'category_id',
      label: $t('device.production.data.category_id'),
      componentProps: {
        autoSelect: 'first',
        class: 'w-full',
        api: async () => {
          const { list } = await getClassificationFlatList(
            userStore?.userInfo?.org_id || '',
          );
          console.warn(list);
          return list;
        },
        labelField: 'name',
        valueField: 'id',
      },
      rules: z.string(),
    },
    {
      component: 'Input',
      fieldName: 'desc',
      label: $t('device.production.data.desc'),
    },
  ];
}
