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

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

// ========== 车辆类型选项 ==========
export const VEHICLE_TYPE_OPTIONS = [
  { label: '轻型厢式货车', value: '轻型厢式货车' },
  { label: '重型厢式货车', value: '重型厢式货车' },
  { label: '重型半挂牵引车', value: '重型半挂牵引车' },
  { label: '重型集装箱半挂车', value: '重型集装箱半挂车' },
  { label: '小型普通客车', value: '小型普通客车' },
  { label: '重型平板半挂车', value: '重型平板半挂车' },
  { label: '叉车', value: '叉车' },
  { label: '油车', value: '油车' },
  { label: '观光车', value: '观光车' },
];

// ========== 氢量单位选项 ==========
export const HYDROGEN_UNIT_OPTIONS = [
  { label: '%', value: '%' },
  { label: 'MPa', value: 'MPa' },
  { label: 'kg', value: 'kg' },
];

// ========== 整备类型选项 ==========
export const PREP_TYPE_OPTIONS = [
  { label: '新车整备', value: '新车整备' },
  { label: '日常整备', value: '日常整备' },
  { label: '还车整备', value: '还车整备' },
  { label: '替换车整备', value: '替换车整备' },
];

// ========== 备车检查项 ==========
export const CHECK_CATEGORIES = [
  {
    category: '车灯',
    items: ['大灯', '转向灯', '小灯', '示廓灯', '刹车灯', '倒车灯', '牌照灯', '防雾灯', '室内灯'],
  },
  {
    category: '仪表盘',
    items: ['氢系统指示', '电控系统指示', '数值清晰准确', '故障报警灯'],
  },
  {
    category: '驾驶室',
    items: [
      '点烟器', '车窗升降', '按键开关', '雨刮器', '内后视镜',
      '内/外门把手', '安全带', '空调冷暖风', '仪表盘', '门锁功能',
      '手刹', '车钥匙功能', '喇叭', '音响功能', '遮阳板',
      '主副驾座椅', '方向盘', '内饰干净整洁',
    ],
  },
  {
    category: '轮胎',
    items: ['左前轮', '右前轮', '左后轮', '右后轮'],
    type: 'input' as const,
  },
  {
    category: '液位检查',
    items: ['机油', '冷却液', '制动液', '转向助力液'],
  },
  {
    category: '外观检查',
    items: ['车漆', '车身', '挡风玻璃', '后视镜'],
  },
  {
    category: '车辆外观',
    items: ['前保险杠', '后保险杠', '车顶', '底盘'],
  },
  {
    category: '其他',
    items: ['灭火器', '三角警示牌', '急救包'],
  },
  {
    category: '随车工具',
    items: ['千斤顶', '轮胎扳手', '拖车钩'],
  },
  {
    category: '随车证件',
    items: ['行驶证', '保险卡', '年检标志'],
  },
  {
    category: '整车',
    items: ['整车清洁', '整车功能'],
  },
  {
    category: '燃料电池系统',
    items: ['燃料电池状态', '氢气瓶', '氢气管路'],
  },
  {
    category: '冷机',
    items: ['制冷系统', '温控系统'],
  },
  {
    category: '制动系统',
    items: ['制动片', '制动盘', '手刹功能'],
  },
];

// ========== 搜索表单 ==========
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'completeTime',
      label: '备车日期',
      component: 'RangePicker',
      componentProps: getRangePickerDefaultProps(),
    },
    {
      fieldName: 'plateNo',
      label: '车牌号',
      component: 'Input',
      componentProps: { placeholder: '请输入车牌号' },
    },
    {
      fieldName: 'vehicleType',
      label: '车辆类型',
      component: 'Select',
      componentProps: {
        placeholder: '请选择车辆类型',
        allowClear: true,
        options: VEHICLE_TYPE_OPTIONS,
      },
    },
    {
      fieldName: 'brand',
      label: '品牌',
      component: 'Input',
      componentProps: { placeholder: '请输入品牌' },
    },
    {
      fieldName: 'parkingLot',
      label: '停车场',
      component: 'Input',
      componentProps: { placeholder: '请输入停车场' },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: getRangePickerDefaultProps(),
    },
  ];
}

// ========== 已完成列表列 ==========
export function useCompletedColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'completeTime', title: '备车时间', minWidth: 160, formatter: 'formatDateTime' },
    { field: 'creator', title: '备车人', minWidth: 100 },
    { field: 'vehicleType', title: '车辆类型', minWidth: 120 },
    { field: 'brand', title: '品牌', minWidth: 100 },
    { field: 'model', title: '型号', minWidth: 100 },
    { field: 'plateNo', title: '车牌号', minWidth: 120 },
    { field: 'vin', title: '车辆识别代码', minWidth: 180 },
    { field: 'parkingLot', title: '停车场', minWidth: 120 },
    {
      field: 'hasBodyAd',
      title: '车身广告及放大字',
      minWidth: 130,
      formatter: ({ cellValue }: { cellValue: boolean }) => cellValue ? '有' : '无',
    },
    {
      field: 'hasTailLift',
      title: '尾板',
      minWidth: 80,
      formatter: ({ cellValue }: { cellValue: boolean }) => cellValue ? '有' : '无',
    },
    {
      field: 'spareTireDepth',
      title: '备胎胎纹深度',
      minWidth: 120,
      formatter: ({ cellValue }: { cellValue: number }) => cellValue != null ? `${cellValue}mm` : '',
    },
    useActionColumn(1),
  ];
}

// ========== 待提交列表列 ==========
export function usePendingColumns(): VxeTableGridOptions['columns'] {
  return [
    { field: 'createTime', title: '创建时间', minWidth: 160, formatter: 'formatDateTime' },
    { field: 'creator', title: '创建人', minWidth: 100 },
    { field: 'vehicleType', title: '车辆类型', minWidth: 120 },
    { field: 'brand', title: '品牌', minWidth: 100 },
    { field: 'model', title: '型号', minWidth: 100 },
    { field: 'plateNo', title: '车牌号', minWidth: 120 },
    { field: 'vin', title: '车辆识别代码', minWidth: 180 },
    { field: 'parkingLot', title: '停车场', minWidth: 120 },
    {
      field: 'hasBodyAd',
      title: '车身广告及放大字',
      minWidth: 130,
      formatter: ({ cellValue }: { cellValue: boolean }) => cellValue ? '有' : '无',
    },
    {
      field: 'hasTailLift',
      title: '尾板',
      minWidth: 80,
      formatter: ({ cellValue }: { cellValue: boolean }) => cellValue ? '有' : '无',
    },
    {
      field: 'spareTireDepth',
      title: '备胎胎纹深度',
      minWidth: 120,
      formatter: ({ cellValue }: { cellValue: number }) => cellValue != null ? `${cellValue}mm` : '',
    },
    useActionColumn(3, true),
  ];
}

// ========== 兼容旧引用 ==========
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return usePendingColumns();
}
