import '../lookup.dart';

class ServiceRequest {
  String? id;
  String? requestCode;
  String? deviceSerialNumber;
  String? deviceId;
  String? deviceArName;
  String? deviceEnName;
  List<String>? devicePhotos;
  String? maintenanceIssue;
  String? hospitalName;
  String? hospitalId;
  String? departmentName;
  String? engineerName;
  String? date;
  String? audio;
  int? statusValue;
  String? statusLabel;

  bool? viewReport;
  String? engineerMobile;
  String? deviceModel;
  String? faultDescription;
  String? workPerformed;
  String? visitDate;
  DateTime? nextVisitDate;
  String? jobSheetNumber;
  String? reportID;
  String? deviceNumber;
  Lookup? priority;
  Lookup? defectType;

  ServiceRequest({
    this.id,
    this.date,
    this.requestCode,
    this.hospitalName,
    this.deviceArName,
    this.deviceEnName,
    this.devicePhotos,
    this.hospitalId,
    this.deviceSerialNumber,
    this.maintenanceIssue,
    this.statusLabel,
    this.statusValue,
    this.departmentName,
    this.deviceId,
    this.audio,
    this.engineerName,
    this.viewReport = false,
    this.deviceModel,
    this.engineerMobile,
    this.faultDescription,
    this.jobSheetNumber,
    this.visitDate,
    this.nextVisitDate,
    this.workPerformed,
    this.reportID,
    this.defectType,
    this.priority,
    this.deviceNumber,
  });

  factory ServiceRequest.fromJson(Map<String, dynamic> parsedJson) {
    return ServiceRequest(
      id: parsedJson["nid"],
      requestCode: parsedJson["call_id"] ?? parsedJson["jobcode"],
      hospitalName: parsedJson["call_client"],
      deviceNumber: parsedJson["device_no"],
      deviceId: parsedJson["deviceid"],
      audio: parsedJson["audio"] ?? "",
      deviceArName: parsedJson["equipment_arabic_name"] == false ? "No Name found" : parsedJson["equipment_arabic_name"],
      deviceEnName: parsedJson["equipment_english_name"] == false ? "No Name found" : parsedJson["equipment_english_name"],
      devicePhotos: List<String>.from(parsedJson["image"]),
      deviceSerialNumber: parsedJson["call_sn"],
      date: parsedJson["call_data"],
      maintenanceIssue: parsedJson["call_complaint"] ?? parsedJson["complaint"],
      statusLabel: parsedJson["status_value"],
      statusValue: int.tryParse(parsedJson["status"] ?? "-1"),
      departmentName: parsedJson["department_name"],
      engineerName: parsedJson["employee_name"],
      hospitalId: parsedJson["client"],
      reportID: parsedJson["service_report_nid"] is String ? parsedJson["service_report_nid"] : null,
      viewReport: parsedJson["service_report_nid"] is bool ? false : true,
      deviceModel: parsedJson["device_model"],
      engineerMobile: parsedJson["engineer_mobile"],
      faultDescription: parsedJson["fault_desc"],
      jobSheetNumber: parsedJson["job_sheet_number"],
      visitDate: parsedJson["visit_date"],
      nextVisitDate: parsedJson["next_visit_date"] == null
          ? null
          : DateTime.fromMillisecondsSinceEpoch(
              (int.tryParse(parsedJson["next_visit_date"]) ?? 0) * 1000,
            ),
      workPerformed: parsedJson["work_performed"],
    );
  }
}
