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

import { $t } from '#/locales';

export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'api_name',
      label: $t('system.api.apiName'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'api_code',
      label: $t('system.api.apiCode'),
      rules: 'required',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        class: 'w-full',
        placeholder: '请选择请求方法',
        options: [
          { label: 'GET', value: 'GET' },
          { label: 'POST', value: 'POST' },
          { label: 'PUT', value: 'PUT' },
          { label: 'DELETE', value: 'DELETE' },
        ],
      },
      fieldName: 'method',
      label: $t('system.api.method'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'url',
      label: $t('system.api.url'),
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.api.status'),
    },
    {
      component: 'Textarea',
      fieldName: 'description',
      label: $t('system.api.description'),
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'api_name',
      label: $t('system.api.apiName'),
    },
    {
      component: 'Input',
      fieldName: 'api_code',
      label: $t('system.api.apiCode'),
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        class: 'w-full',
        options: [
          { label: 'GET', value: 'GET' },
          { label: 'POST', value: 'POST' },
          { label: 'PUT', value: 'PUT' },
          { label: 'DELETE', value: 'DELETE' },
        ],
      },
      fieldName: 'method',
      label: $t('system.api.method'),
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        class: 'w-full',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
      },
      fieldName: 'status',
      label: $t('system.api.status'),
    },
    {
      component: 'RangePicker',
      fieldName: 'create_time',
      label: $t('system.api.createTime'),
    },
  ];
}

export function useColumns<T = SystemApiApi.SystemApiItem>(
  onActionClick: OnActionClickFn<T>,
  onStatusChange?: (newStatus: any, row: T) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'api_name',
      title: $t('system.api.apiName'),
      width: 200,
    },
    {
      field: 'api_code',
      title: $t('system.api.apiCode'),
      width: 200,
    },
    {
      field: 'method',
      title: $t('system.api.method'),
      width: 100,
    },
    {
      field: 'url',
      title: $t('system.api.url'),
      minWidth: 200,
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: onStatusChange ? 'CellSwitch' : 'CellTag',
      },
      field: 'status',
      title: $t('system.api.status'),
      width: 100,
    },
    {
      field: 'description',
      minWidth: 150,
      title: $t('system.api.description'),
    },
    {
      field: 'create_time',
      title: $t('system.api.createTime'),
      width: 200,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'api_name',
          nameTitle: $t('system.api.apiName'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit', // 默认的编辑按钮
          'delete', // 默认的删除按钮
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: $t('system.api.operation'),
      width: 130,
    },
  ];
}
