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

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

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export const useColumns = (
  onActionClick: OnActionClickFn<ScheduleApi.Schedule>,
): VxeTableGridOptions<ScheduleApi.Schedule>['columns'] => {
  return [
    {
      field: 'name',
      title: $t('device.schedule.name'),
      width: 150,
    },
    {
      field: 'invoke_target',
      title: $t('device.schedule.invoke_target'),
    },
    {
      field: 'enabled',
      title: $t('device.schedule.enabled'),
      width: 150,
    },
    { field: 'last_execute_at', title: $t('device.schedule.last_execute_at') },
    { field: 'next_execute_at', title: $t('device.schedule.next_execute_at') },
    { field: 'execute_count', title: $t('device.schedule.execute_count') },
    { field: 'success_count', title: $t('device.schedule.success_count') },
    { field: 'failed_count', title: $t('device.schedule.failed_count') },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: $t('system.dept.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          // {
          //   code: 'append',
          //   text: '新增下级',
          // },
          'edit', // 默认的编辑按钮
          {
            code: 'delete', // 默认的删除按钮
            disabled: (_row: ScheduleApi.Schedule) => {
              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',
      disabled: true,
      label: $t('device.schedule.name'),
      rules: z
        .number()
        .min(1, $t('ui.formRules.minLength', [$t('device.schedule.name'), 1]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('device.schedule.name'), 20]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'cron_expression',
      disabled: true,
      label: $t('device.schedule.cron_expression'),
      rules: z
        .string()
        .min(
          1,
          $t('ui.formRules.minLength', [
            $t('device.schedule.cron_expression'),
            1,
          ]),
        )
        .max(
          20,
          $t('ui.formRules.maxLength', [
            $t('device.schedule.cron_expression'),
            20,
          ]),
        ),
    },
  ];
}
export function useCreateSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('device.shcedule.name'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.nickName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('device.shcedule.name'), 20]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'cron_expression',
      label: $t('device.schedule.cron_expression'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.userName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.userName'), 20]),
        ),
    },
  ];
}
