import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AssetVehicleRegistrationApi } from '#/api/asset/vehicle-registration';

import { $t } from '@vben/locales';
import { useActionColumn } from '#/utils/table';

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

/** 新增/修改的表单 - 基本信息 */
export function useBasicFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'vin',
      label: '车辆识别代号(VIN)',
      component: 'Input',
      componentProps: {
        placeholder: '请输入VIN码',
      },
      rules: z.string().min(1, { message: '请输入VIN码' }),
    },
    {
      fieldName: 'plateNo',
      label: '车牌号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入车牌号',
      },
      rules: z.string().min(1, { message: '请输入车牌号' }),
    },
    {
      fieldName: 'plateDate',
      label: '上牌日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: '请选择上牌日期',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
      rules: 'required',
    },
    {
      fieldName: 'operator',
      label: '操作员',
      component: 'Input',
      componentProps: {
        placeholder: '请输入操作员',
      },
    },
  ];
}

/** 新增/修改的表单 - OCR识别信息 */
export function useOcrFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'recognizedBrand',
      label: '品牌型号',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'recognizedModel',
      label: '车型',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'vehicleType',
      label: '车辆类型',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'owner',
      label: '所有人',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'useCharacter',
      label: '使用性质',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'engineNo',
      label: '发动机号码',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'registerDate',
      label: '注册日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: 'OCR自动识别',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'issueDate',
      label: '发证日期',
      component: 'DatePicker',
      componentProps: {
        placeholder: 'OCR自动识别',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'inspectionRecord',
      label: '检验记录',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'scrapDate',
      label: '强制报废期止',
      component: 'DatePicker',
      componentProps: {
        placeholder: 'OCR自动识别',
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'curbWeight',
      label: '整备质量(kg)',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'totalMass',
      label: '总质量(kg)',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
    {
      fieldName: 'approvedPassengerCapacity',
      label: '核定载人数',
      component: 'Input',
      componentProps: {
        placeholder: 'OCR自动识别',
      },
    },
  ];
}

/** 新增/修改的表单 - 其他信息 */
export function useOtherFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'vehicleModelId',
      label: '匹配车型',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择车型',
        api: () =>
          import('#/api/asset/vehicle-model').then((m) =>
            m.getSimpleVehicleModelList(),
          ),
        labelField: 'modelLabel',
        valueField: 'id',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注',
        rows: 3,
      },
    },
  ];
}

/** 搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'vin',
      label: 'VIN码',
      component: 'Input',
      componentProps: {
        placeholder: '请输入VIN码',
      },
    },
    {
      fieldName: 'plateNo',
      label: '车牌号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入车牌号',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        placeholder: '请选择状态',
        options: [
          { label: '待确认', value: 0 },
          { label: '已确认', value: 1 },
          { label: '已作废', value: 2 },
        ],
      },
    },
    {
      fieldName: 'plateDate',
      label: '上牌日期',
      component: 'RangePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        placeholder: ['开始日期', '结束日期'],
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: getRangePickerDefaultProps(),
    },
  ];
}

/** 表格列配置 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 50, fixed: 'left' },
    {
      field: 'vin',
      title: 'VIN码',
      width: 180,
      fixed: 'left',
    },
    {
      field: 'plateNo',
      title: '车牌号',
      width: 120,
    },
    {
      field: 'plateDate',
      title: '上牌日期',
      width: 120,
    },
    {
      field: 'recognizedBrand',
      title: '品牌型号',
      width: 150,
    },
    {
      field: 'vehicleType',
      title: '车辆类型',
      width: 120,
    },
    {
      field: 'owner',
      title: '所有人',
      width: 150,
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: { name: 'CellDict', props: { type: 'tag' } },
      params: {
        dictType: 'asset_vehicle_registration_status',
      },
    },
    {
      field: 'operator',
      title: '操作员',
      width: 100,
    },
    {
      field: 'createTime',
      title: '创建时间',
      width: 160,
      formatter: 'formatDateTime',
    },
    useActionColumn(3),
  ];
}
