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

import { z } from '#/adapter/form';
import { useActionColumn } from '#/utils/table';

export const BIZ_TYPE_OPTIONS = [
  { label: '备车', value: 1 },
  { label: '交车', value: 2 },
  { label: '还车', value: 3 },
];

export const STATUS_OPTIONS = [
  { label: '启用', value: 1 },
  { label: '禁用', value: 0 },
];

export const INPUT_TYPE_OPTIONS = [
  { label: '勾选(合格/不合格)', value: 'checkbox' },
  { label: '数值', value: 'number' },
  { label: '文本', value: 'text' },
];

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '模板名称',
      component: 'Input',
      componentProps: { placeholder: '请输入模板名称' },
    },
    {
      fieldName: 'bizType',
      label: '适用业务',
      component: 'Select',
      componentProps: {
        placeholder: '请选择',
        options: BIZ_TYPE_OPTIONS,
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择',
        options: STATUS_OPTIONS,
        allowClear: true,
      },
    },
  ];
}

/** 表格列 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 60, fixed: 'left' },
    { field: 'code', title: '模板编码', minWidth: 140, fixed: 'left' },
    { field: 'name', title: '模板名称', minWidth: 180 },
    {
      field: 'bizType',
      title: '适用业务',
      minWidth: 100,
      formatter({ cellValue }: any) {
        return BIZ_TYPE_OPTIONS.find((o) => o.value === cellValue)?.label ?? cellValue;
      },
    },
    { field: 'vehicleType', title: '适用车型', minWidth: 120 },
    {
      field: 'status',
      title: '状态',
      minWidth: 80,
      formatter({ cellValue }: any) {
        return cellValue === 1 ? '启用' : '禁用';
      },
    },
    { field: 'remark', title: '备注', minWidth: 150 },
    { field: 'createTime', title: '创建时间', minWidth: 180, formatter: 'formatDateTime' },
    useActionColumn(3),
  ];
}

/** 模板基本信息表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: { triggerFields: [''], show: () => false },
    },
    {
      fieldName: 'code',
      label: '模板编码',
      component: 'Input',
      componentProps: { placeholder: '请输入模板编码' },
      rules: z.string().min(1, { message: '请输入模板编码' }),
    },
    {
      fieldName: 'name',
      label: '模板名称',
      component: 'Input',
      componentProps: { placeholder: '请输入模板名称' },
      rules: z.string().min(1, { message: '请输入模板名称' }),
    },
    {
      fieldName: 'bizType',
      label: '适用业务',
      component: 'Select',
      componentProps: {
        placeholder: '请选择适用业务',
        options: BIZ_TYPE_OPTIONS,
      },
      rules: z.number({ message: '请选择适用业务' }),
    },
    {
      fieldName: 'vehicleType',
      label: '适用车型',
      component: 'Input',
      componentProps: { placeholder: '留空表示通用模板' },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: STATUS_OPTIONS,
      },
      defaultValue: 1,
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: { placeholder: '请输入备注', rows: 2 },
    },
  ];
}
