/// 用车申请单，对应 [OA_VEHICLE] 表。
///
/// 字段名采用 camelCase，fromJson 将大写下划线映射。
/// 瞬态字段：applicantName、deptName、isAuditApproved。
class VehicleModel {
  final String ycNo;         // 用车申请单号 (YC_NO)
  final DateTime? ycDd;     // 申请日期 (YC_DD)
  final String salNo;        // 申请人 (SAL_NO)
  final String dep;          // 所属部门 (DEP)
  final String purpose;      // 用车目的 (PURPOSE)
  final String reason;       // 用车事由 (REASON)
  final String origin;       // 始发地地址 (ORIGIN)
  final double? originLongitude; // 始发地经度 (ORIGIN_LONGITUDE)
  final double? originLatitude;  // 始发地纬度 (ORIGIN_LATITUDE)
  final String destinAdr;    // 目的地地址 (DESTIN_ADR)
  final double? destLongitude; // 目的地经度 (DEST_LONGITUDE)
  final double? destLatitude;  // 目的地纬度 (DEST_LATITUDE)
  final int passengerCount;  // 同行人数 (PASSENGER_COUNT)
  final String passengerName; // 同行人姓名 (PASSENGER_NAME)
  final String licensePlate; // 车牌号 (LICENSEPLATE)
  final String vehicleType;  // 车辆类型 (VEHICLE_TYPE)
  final String brand;        // 品牌型号 (BRAND)
  final int seats;           // 核定座位数 (SEATS)
  final String driverName;   // 默认驾驶员 (DRIVER_NAME)
  final DateTime? startTime; // 预计出车时间 (START_TIME)
  final DateTime? endTime;   // 预计还车时间 (END_TIME)
  final String usr;          // 录入人 (USR)
  final DateTime? recordDd;  // 录入日期 (RECORD_DD)
  final String chkMan;       // 审核人 (CHK_MAN)
  final DateTime? clsDate;   // 终审日期 (CLS_DATE)
  final bool isReturn;       // 是否已还车 (IS_RETURN: 'T'/'F')
  final DateTime? returnTime; // 实际归还时间 (RETURN_TIME)
  final double odometer;     // 里程表读数 (ODOMETER)
  final double amtn;         // 路桥费/停车费等实际费用 (AMTN)
  final String remark;       // 费用明细备注 (REMARK)

  // ── 瞬态字段（API 从 ERP 实时查询，非存储） ──
  final String applicantName;
  final String deptName;
  final bool isAuditApproved;

  const VehicleModel({
    this.ycNo = '',
    this.ycDd,
    this.salNo = '',
    this.dep = '',
    this.purpose = '',
    this.reason = '',
    this.origin = '',
    this.originLongitude,
    this.originLatitude,
    this.destinAdr = '',
    this.destLongitude,
    this.destLatitude,
    this.passengerCount = 1,
    this.passengerName = '',
    this.licensePlate = '',
    this.vehicleType = '',
    this.brand = '',
    this.seats = 0,
    this.driverName = '',
    this.startTime,
    this.endTime,
    this.usr = '',
    this.recordDd,
    this.chkMan = '',
    this.clsDate,
    this.isReturn = false,
    this.returnTime,
    this.odometer = 0.0,
    this.amtn = 0.0,
    this.remark = '',
    this.applicantName = '',
    this.deptName = '',
    this.isAuditApproved = false,
  });

  factory VehicleModel.fromJson(Map<String, dynamic> json) {
    return VehicleModel(
      ycNo: json['YC_NO'] as String? ?? '',
      ycDd: json['YC_DD'] != null ? DateTime.tryParse(json['YC_DD'] as String) : null,
      salNo: json['SAL_NO'] as String? ?? '',
      dep: json['DEP'] as String? ?? '',
      purpose: json['PURPOSE'] as String? ?? '',
      reason: json['REASON'] as String? ?? '',
      origin: json['ORIGIN'] as String? ?? '',
      originLongitude: (json['ORIGIN_LONGITUDE'] as num?)?.toDouble(),
      originLatitude: (json['ORIGIN_LATITUDE'] as num?)?.toDouble(),
      destinAdr: json['DESTIN_ADR'] as String? ?? json['DESTIN_ADR'] as String? ?? '',
      destLongitude: (json['DEST_LONGITUDE'] as num?)?.toDouble(),
      destLatitude: (json['DEST_LATITUDE'] as num?)?.toDouble(),
      passengerCount: json['PASSENGER_COUNT'] as int? ?? 1,
      passengerName: json['PASSENGER_NAME'] as String? ?? '',
      licensePlate: json['LICENSEPLATE'] as String? ?? '',
      vehicleType: json['VEHICLE_TYPE'] as String? ?? '',
      brand: json['BRAND'] as String? ?? '',
      seats: json['SEATS'] as int? ?? 0,
      driverName: json['DRIVER_NAME'] as String? ?? '',
      startTime: json['START_TIME'] != null ? DateTime.tryParse(json['START_TIME'] as String) : null,
      endTime: json['END_TIME'] != null ? DateTime.tryParse(json['END_TIME'] as String) : null,
      usr: json['USR'] as String? ?? '',
      recordDd: json['RECORD_DD'] != null ? DateTime.tryParse(json['RECORD_DD'] as String) : null,
      chkMan: json['CHK_MAN'] as String? ?? '',
      clsDate: json['CLS_DATE'] != null ? DateTime.tryParse(json['CLS_DATE'] as String) : null,
      isReturn: json['IS_RETURN'] == 'T',
      returnTime: json['RETURN_TIME'] != null ? DateTime.tryParse(json['RETURN_TIME'] as String) : null,
      odometer: (json['ODOMETER'] as num?)?.toDouble() ?? 0.0,
      amtn: (json['AMTN'] as num?)?.toDouble() ?? 0.0,
      remark: json['REMARK'] as String? ?? '',
      applicantName: json['applicantName'] as String? ?? json['SAL_NO'] as String? ?? '',
      deptName: json['deptName'] as String? ?? json['DEP'] as String? ?? '',
      isAuditApproved: (json['clsDate'] != null && (json['clsDate'] as String).isNotEmpty) ||
          (json['CHK_MAN'] as String?)?.isNotEmpty == true,
    );
  }

  Map<String, dynamic> toJson() => {
    'YC_NO': ycNo,
    'YC_DD': ycDd?.toIso8601String(),
    'SAL_NO': salNo,
    'DEP': dep,
    'PURPOSE': purpose,
    'REASON': reason,
    'ORIGIN': origin,
    'ORIGIN_LONGITUDE': originLongitude,
    'ORIGIN_LATITUDE': originLatitude,
    'DESTIN_ADR': destinAdr,
    'DEST_LONGITUDE': destLongitude,
    'DEST_LATITUDE': destLatitude,
    'PASSENGER_COUNT': passengerCount,
    'PASSENGER_NAME': passengerName,
    'LICENSEPLATE': licensePlate,
    'VEHICLE_TYPE': vehicleType,
    'BRAND': brand,
    'SEATS': seats,
    'DRIVER_NAME': driverName,
    'START_TIME': startTime?.toIso8601String(),
    'END_TIME': endTime?.toIso8601String(),
    'USR': usr,
    'RECORD_DD': recordDd?.toIso8601String(),
    'CHK_MAN': chkMan,
    'CLS_DATE': clsDate?.toIso8601String(),
    'IS_RETURN': isReturn ? 'T' : 'F',
    'RETURN_TIME': returnTime?.toIso8601String(),
    'ODOMETER': odometer,
    'AMTN': amtn,
    'REMARK': remark,
  };

  VehicleModel copyWith({
    String? ycNo,
    DateTime? ycDd,
    String? salNo,
    String? dep,
    String? purpose,
    String? reason,
    String? origin,
    double? originLongitude,
    double? originLatitude,
    String? destinAdr,
    double? destLongitude,
    double? destLatitude,
    int? passengerCount,
    String? passengerName,
    String? licensePlate,
    String? vehicleType,
    String? brand,
    int? seats,
    String? driverName,
    DateTime? startTime,
    DateTime? endTime,
    String? usr,
    DateTime? recordDd,
    String? chkMan,
    DateTime? clsDate,
    bool? isReturn,
    DateTime? returnTime,
    double? odometer,
    double? amtn,
    String? remark,
    String? applicantName,
    String? deptName,
    bool? isAuditApproved,
  }) {
    return VehicleModel(
      ycNo: ycNo ?? this.ycNo,
      ycDd: ycDd ?? this.ycDd,
      salNo: salNo ?? this.salNo,
      dep: dep ?? this.dep,
      purpose: purpose ?? this.purpose,
      reason: reason ?? this.reason,
      origin: origin ?? this.origin,
      originLongitude: originLongitude ?? this.originLongitude,
      originLatitude: originLatitude ?? this.originLatitude,
      destinAdr: destinAdr ?? this.destinAdr,
      destLongitude: destLongitude ?? this.destLongitude,
      destLatitude: destLatitude ?? this.destLatitude,
      passengerCount: passengerCount ?? this.passengerCount,
      passengerName: passengerName ?? this.passengerName,
      licensePlate: licensePlate ?? this.licensePlate,
      vehicleType: vehicleType ?? this.vehicleType,
      brand: brand ?? this.brand,
      seats: seats ?? this.seats,
      driverName: driverName ?? this.driverName,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      usr: usr ?? this.usr,
      recordDd: recordDd ?? this.recordDd,
      chkMan: chkMan ?? this.chkMan,
      clsDate: clsDate ?? this.clsDate,
      isReturn: isReturn ?? this.isReturn,
      returnTime: returnTime ?? this.returnTime,
      odometer: odometer ?? this.odometer,
      amtn: amtn ?? this.amtn,
      remark: remark ?? this.remark,
      applicantName: applicantName ?? this.applicantName,
      deptName: deptName ?? this.deptName,
      isAuditApproved: isAuditApproved ?? this.isAuditApproved,
    );
  }
}
