class OutingLogModel {
  final String id;
  final String visitNo;
  final String salespersonId;
  final String salespersonName;
  final String deptId;
  final String deptName;
  final String customerId;
  final String customerName;
  final String contactId;
  final String contactName;
  final String contactPhone;
  final String contactPosition;
  final DateTime visitDate;
  final DateTime visitStartTime;
  final DateTime visitEndTime;
  final String visitType;
  final String visitPurpose;
  final String visitLocation;
  final String visitAddressDetail;
  final double longitude;
  final double latitude;
  final String signInPhoto;
  final String signOutPhoto;
  final List<String> visitPhotos;
  final String participants;
  final String visitSummary;
  final String visitResult;
  final String customerFeedback;
  final String competitorInfo;
  final DateTime nextVisitTime;
  final String nextVisitContent;
  final String checkInStatus;
  final int distanceDeviation;
  final String materials;
  final String status;
  final List<String> attachments;
  final DateTime createTime;
  final DateTime updateTime;

  const OutingLogModel({
    required this.id,
    required this.visitNo,
    required this.salespersonId,
    required this.salespersonName,
    required this.deptId,
    required this.deptName,
    this.customerId = '',
    required this.customerName,
    this.contactId = '',
    this.contactName = '',
    this.contactPhone = '',
    this.contactPosition = '',
    required this.visitDate,
    required this.visitStartTime,
    required this.visitEndTime,
    required this.visitType,
    required this.visitPurpose,
    required this.visitLocation,
    this.visitAddressDetail = '',
    this.longitude = 0.0,
    this.latitude = 0.0,
    this.signInPhoto = '',
    this.signOutPhoto = '',
    this.visitPhotos = const [],
    this.participants = '',
    required this.visitSummary,
    this.visitResult = '',
    this.customerFeedback = '',
    this.competitorInfo = '',
    required this.nextVisitTime,
    this.nextVisitContent = '',
    this.checkInStatus = '正常',
    this.distanceDeviation = 0,
    this.materials = '',
    this.status = '已完成',
    this.attachments = const [],
    required this.createTime,
    required this.updateTime,
  });

  factory OutingLogModel.fromJson(Map<String, dynamic> json) {
    return OutingLogModel(
      id: json['id'] as String,
      visitNo: json['visitNo'] as String? ?? '',
      salespersonId: json['salespersonId'] as String? ?? '',
      salespersonName: json['salespersonName'] as String? ?? '',
      deptId: json['deptId'] as String? ?? '',
      deptName: json['deptName'] as String? ?? '',
      customerId: json['customerId'] as String? ?? '',
      customerName: json['customerName'] as String? ?? '',
      contactId: json['contactId'] as String? ?? '',
      contactName: json['contactName'] as String? ?? '',
      contactPhone: json['contactPhone'] as String? ?? '',
      contactPosition: json['contactPosition'] as String? ?? '',
      visitDate: DateTime.parse(json['visitDate'] as String),
      visitStartTime: DateTime.parse(json['visitStartTime'] as String),
      visitEndTime: DateTime.parse(json['visitEndTime'] as String),
      visitType: json['visitType'] as String? ?? '常规拜访',
      visitPurpose: json['visitPurpose'] as String? ?? '',
      visitLocation: json['visitLocation'] as String? ?? '',
      visitAddressDetail: json['visitAddressDetail'] as String? ?? '',
      longitude: (json['longitude'] as num?)?.toDouble() ?? 0.0,
      latitude: (json['latitude'] as num?)?.toDouble() ?? 0.0,
      signInPhoto: json['signInPhoto'] as String? ?? '',
      signOutPhoto: json['signOutPhoto'] as String? ?? '',
      visitPhotos:
          (json['visitPhotos'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      participants: json['participants'] as String? ?? '',
      visitSummary: json['visitSummary'] as String? ?? '',
      visitResult: json['visitResult'] as String? ?? '',
      customerFeedback: json['customerFeedback'] as String? ?? '',
      competitorInfo: json['competitorInfo'] as String? ?? '',
      nextVisitTime: DateTime.parse(json['nextVisitTime'] as String),
      nextVisitContent: json['nextVisitContent'] as String? ?? '',
      checkInStatus: json['checkInStatus'] as String? ?? '正常',
      distanceDeviation: json['distanceDeviation'] as int? ?? 0,
      materials: json['materials'] as String? ?? '',
      status: json['status'] as String? ?? '已完成',
      attachments:
          (json['attachments'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: DateTime.parse(json['updateTime'] as String),
    );
  }

  Map<String, dynamic> toJson() => {
    'id': id,
    'visitNo': visitNo,
    'salespersonId': salespersonId,
    'salespersonName': salespersonName,
    'deptId': deptId,
    'deptName': deptName,
    'customerId': customerId,
    'customerName': customerName,
    'contactId': contactId,
    'contactName': contactName,
    'contactPhone': contactPhone,
    'contactPosition': contactPosition,
    'visitDate': visitDate.toIso8601String(),
    'visitStartTime': visitStartTime.toIso8601String(),
    'visitEndTime': visitEndTime.toIso8601String(),
    'visitType': visitType,
    'visitPurpose': visitPurpose,
    'visitLocation': visitLocation,
    'visitAddressDetail': visitAddressDetail,
    'longitude': longitude,
    'latitude': latitude,
    'signInPhoto': signInPhoto,
    'signOutPhoto': signOutPhoto,
    'visitPhotos': visitPhotos,
    'participants': participants,
    'visitSummary': visitSummary,
    'visitResult': visitResult,
    'customerFeedback': customerFeedback,
    'competitorInfo': competitorInfo,
    'nextVisitTime': nextVisitTime.toIso8601String(),
    'nextVisitContent': nextVisitContent,
    'checkInStatus': checkInStatus,
    'distanceDeviation': distanceDeviation,
    'materials': materials,
    'status': status,
    'attachments': attachments,
    'createTime': createTime.toIso8601String(),
    'updateTime': updateTime.toIso8601String(),
  };
}
