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 REPLACEMENT_TYPE_OPTIONS = [
  { label: '临时替换', value: 1 },
  { label: '永久替换', value: 2 },
];

export const REPLACEMENT_STATUS_OPTIONS = [
  { label: '草稿', value: 0 },
  { label: '审批中', value: 1 },
  { label: '已通过', value: 2 },
  { label: '执行中', value: 3 },
  { label: '已完成', value: 4 },
  { label: '已驳回', value: 5 },
  { label: '已撤回', value: 6 },
];

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'replacementCode',
      label: '替换单号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入替换单号',
      },
    },
    {
      fieldName: 'replacementType',
      label: '替换类型',
      component: 'Select',
      componentProps: {
        placeholder: '请选择替换类型',
        options: REPLACEMENT_TYPE_OPTIONS,
      },
    },
    {
      fieldName: 'customerName',
      label: '客户名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入客户名称',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        options: REPLACEMENT_STATUS_OPTIONS,
      },
    },
  ];
}

/** 表格列配置 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 60, fixed: 'left' },
    {
      field: 'replacementCode',
      title: '替换单号',
      minWidth: 160,
      fixed: 'left',
    },
    {
      field: 'replacementType',
      title: '替换类型',
      minWidth: 100,
      formatter({ cellValue }: { cellValue: number }) {
        const option = REPLACEMENT_TYPE_OPTIONS.find(
          (item) => item.value === cellValue,
        );
        return option?.label ?? '';
      },
    },
    {
      field: 'contractCode',
      title: '合同编号',
      minWidth: 160,
    },
    {
      field: 'customerName',
      title: '客户名称',
      minWidth: 150,
    },
    {
      field: 'originalPlateNo',
      title: '原车牌号',
      minWidth: 120,
    },
    {
      field: 'newPlateNo',
      title: '新车牌号',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 100,
      formatter({ cellValue }: { cellValue: number }) {
        const option = REPLACEMENT_STATUS_OPTIONS.find(
          (item) => item.value === cellValue,
        );
        return option?.label ?? '';
      },
    },
    {
      field: 'expectedDate',
      title: '预计替换日期',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    useActionColumn(3, true),
  ];
}

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'replacementType',
      label: '替换类型',
      component: 'Select',
      componentProps: {
        placeholder: '请选择替换类型',
        options: REPLACEMENT_TYPE_OPTIONS,
      },
      rules: z.number({ message: '请选择替换类型' }),
    },
    {
      fieldName: 'contractId',
      label: '关联合同',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择关联合同',
        api: () =>
          import('#/api/asset/contract').then((m) =>
            m.getSimpleContractList(),
          ),
        labelField: 'contractCode',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) =>
          (option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
      },
      rules: z.number({ message: '请选择关联合同' }),
    },
    {
      fieldName: 'originalVehicleId',
      label: '原车辆',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择原车辆',
        api: () =>
          import('#/api/asset/vehicle').then((m) =>
            m.getVehicleSimpleList(),
          ),
        labelField: 'plateNo',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) =>
          (option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
      },
      rules: z.number({ message: '请选择原车辆' }),
    },
    {
      fieldName: 'newVehicleId',
      label: '新车辆',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择新车辆',
        api: () =>
          import('#/api/asset/vehicle').then((m) =>
            m.getVehicleSimpleList(),
          ),
        labelField: 'plateNo',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) =>
          (option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
      },
      rules: z.number({ message: '请选择新车辆' }),
    },
    {
      fieldName: 'deliveryOrderId',
      label: '交车单ID',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入交车单ID',
        class: 'w-full',
      },
    },
    {
      fieldName: 'replacementReason',
      label: '替换原因',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入替换原因',
        rows: 3,
      },
      rules: z.string().min(1, { message: '请输入替换原因' }),
    },
    {
      fieldName: 'expectedDate',
      label: '预计替换日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择预计替换日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'returnDate',
      label: '预计归还日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择预计归还日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
      dependencies: {
        triggerFields: ['replacementType'],
        show(values) {
          return values.replacementType === 1;
        },
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        rows: 3,
      },
    },
  ];
}
