import type { VehicleRecord, VehicleFilterForm } from '@/types/vehicle';

/** 模拟省-市二级地区 */
export const regionOptions = [
  {
    value: 'guangdong',
    label: '广东省',
    children: [
      { value: 'guangzhou', label: '广州市' },
      { value: 'shenzhen', label: '深圳市' },
      { value: 'dongguan', label: '东莞市' },
    ],
  },
  {
    value: 'zhejiang',
    label: '浙江省',
    children: [
      { value: 'hangzhou', label: '杭州市' },
      { value: 'ningbo', label: '宁波市' },
    ],
  },
  {
    value: 'jiangsu',
    label: '江苏省',
    children: [
      { value: 'nanjing', label: '南京市' },
      { value: 'suzhou', label: '苏州市' },
    ],
  },
];

/** 车辆类型（从车辆类型表拉取） */
export const vehicleTypeOptions = [
  { value: 'light_truck', label: '轻卡' },
  { value: 'van', label: '厢式车' },
  { value: 'suv', label: 'SUV' },
  { value: 'sedan', label: '轿车' },
];

/** 品牌（从型号参数表拉取） */
export const brandOptions = [
  { value: 'brand_a', label: '品牌A' },
  { value: 'brand_b', label: '品牌B' },
  { value: 'brand_c', label: '品牌C' },
];

/** 型号（从型号参数表拉取，可按品牌联动） */
export const modelOptions: Record<string, { value: string; label: string }[]> = {
  brand_a: [
    { value: 'model_a1', label: '型号A1' },
    { value: 'model_a2', label: '型号A2' },
  ],
  brand_b: [
    { value: 'model_b1', label: '型号B1' },
    { value: 'model_b2', label: '型号B2' },
  ],
  brand_c: [
    { value: 'model_c1', label: '型号C1' },
  ],
};

/** 客户名称（租赁/自营合同客户，含「无」） */
export const customerOptions = [
  { value: '__none__', label: '无' },
  { value: 'customer_1', label: '客户甲' },
  { value: 'customer_2', label: '客户乙' },
  { value: 'customer_3', label: '客户丙' },
];

/** 归属业务部门（含「无」） */
export const businessDeptOptions = [
  { value: '__none__', label: '无' },
  { value: 'dept_1', label: '业务一部' },
  { value: 'dept_2', label: '业务二部' },
  { value: 'dept_3', label: '业务三部' },
];

/** 合同编号列表（支持模糊匹配，此处为示例） */
export const contractNoOptions = [
  { value: 'HT-2024-001', label: 'HT-2024-001' },
  { value: 'HT-2024-002', label: 'HT-2024-002' },
  { value: 'HT-2024-003', label: 'HT-2024-003' },
  { value: 'ZY-2024-001', label: 'ZY-2024-001' },
];

/** 登记所有权列表（支持模糊匹配） */
export const registeredOwnerOptions = [
  { value: 'owner_1', label: '某某物流有限公司' },
  { value: 'owner_2', label: '某某租赁有限公司' },
];

function randomItem<T>(arr: T[]): T {
  return arr[Math.floor(Math.random() * arr.length)];
}

/** 生成模拟列表数据 */
export function mockVehicleList(params: VehicleFilterForm & { plateNo?: string; page?: number; pageSize?: number }): { list: VehicleRecord[]; total: number } {
  const total = 48;
  const list: VehicleRecord[] = [];
  const cities = ['广东省广州市', '广东省深圳市', '浙江省杭州市', '江苏省南京市'];
  for (let i = 1; i <= (params.pageSize ?? 20); i++) {
    const idx = ((params.page ?? 1) - 1) * (params.pageSize ?? 20) + i;
    if (idx > total) break;
    list.push({
      id: `v-${idx}`,
      operationCity: randomItem(cities),
      vin: `L${String(idx).padStart(6, '0')}${Math.random().toString(36).slice(2, 11).toUpperCase()}`,
      plateNo: `粤A${String(10000 + idx).slice(-5)}`,
      vehicleNo: idx <= 10 ? `VN-${idx}` : '',
      vehicleType: randomItem(vehicleTypeOptions).label,
      brand: randomItem(brandOptions).label,
      model: randomItem(modelOptions.brand_a).label,
      bodyColor: ['白', '黑', '银', '蓝'][idx % 4],
      parkingLot: idx % 3 === 0 ? '-' : `停车场${(idx % 3) + 1}`,
      customerName: idx % 4 === 0 ? '-' : `客户${(idx % 4)}`,
      businessDept: idx % 4 === 0 ? '-' : `业务${(idx % 4)}部`,
      businessOwner: idx % 4 === 0 ? '-' : `负责人${idx % 5}`,
      operationStatus: randomItem(['待运营', '库存', '租赁', '自营', '退出运营']),
      libStatus: randomItem(['库存车-可交付车', '已交付车-租赁交车', '新车入库-待验车']),
      outboundStatus: randomItem(['无', '租赁交车', '异动出库']),
      preemptStatus: '-',
      prepStatus: randomItem(['待整备', '整备中', '正常', '无']),
      transferStatus: randomItem(['无', '过户中', '内部过户完成']),
      repairStatus: randomItem(['待服务站接单', '维修中', '正常']),
      licenseStatus: randomItem(['正常', '异常']),
      scrapStatus: randomItem(['无', '报废中', '已报废']),
      registeredOwner: '某某物流有限公司',
      onlineStatus: randomItem(['在线', '离线']),
      manufactureYear: `${2020 + (idx % 5)}`,
      mileage: Number((10000 + idx * 500 + Math.random() * 200).toFixed(2)),
      purchaseTime: `2022-0${(idx % 9) + 1}-15`,
      licenseRegisterDate: `2022-0${(idx % 9) + 1}-01`,
      licenseExpiry: `2025-0${(idx % 9) + 1}-01`,
      lastDeliveryTime: idx % 2 === 0 ? `2024-01-${String(10 + (idx % 20)).padStart(2, '0')}` : '-',
      lastDeliveryMileage: idx % 2 === 0 ? Number((12000 + idx * 100).toFixed(2)) : 0,
      lastReturnTime: idx % 2 === 1 ? `2024-02-${String(5 + (idx % 20)).padStart(2, '0')}` : '-',
      lastReturnMileage: idx % 2 === 1 ? Number((12500 + idx * 100).toFixed(2)) : 0,
      forceScrapDate: `2030-12-31`,
      contractNo: idx % 3 === 0 ? '-' : `HT-2024-00${(idx % 3) + 1}`,
      currentLocation: `广东省广州市天河区某某路${idx}号`,
      gpsLastTime: `2024-02-13 ${String(10 + (idx % 12)).padStart(2, '0')}:${String(idx % 60).padStart(2, '0')}`,
    });
  }
  return { list, total };
}
