import 'dart:developer'; import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:queuing_system/core/base/base_app_client.dart'; import 'package:queuing_system/core/config/config.dart'; import 'package:queuing_system/core/response_model/patient_call.dart'; const _getCallRequestInfoByClinicInfo = "/GetCallRequestInfo_ByIP"; const _callUpdateNotIsQueueRecordByIDAsync = "/CallRequest_QueueUpdate"; class MyHttpOverrides extends HttpOverrides { @override HttpClient createHttpClient(SecurityContext context) { return super.createHttpClient(context)..badCertificateCallback = (X509Certificate cert, String host, int port) => true; } } class API { static getCallRequestInfoByClinicInfo(String deviceIp, {@required Function(List, List) onSuccess, @required Function(dynamic) onFailure}) async { final body = {"ipAdress": deviceIp, "apiKey": apiKey}; BaseAppClient.post(_getCallRequestInfoByClinicInfo, body: body, onSuccess: (apiResp, status) { if (status == 200) { final response = apiResp["data"]; var calledByNurse = (response["nurseCallPatients"] as List).map((j) => Tickets.fromJson(j)).toList(); final patients = (response["drCallPatients"] as List).map((j) => Tickets.fromJson(j)).toList(); calledByNurse.addAll(patients); log("NurseCallPatients: ${calledByNurse.toString()} "); log("patients: ${patients.toString()} "); var isQueuePatients = calledByNurse.where((element) => element.isQueueNurse == false).toList(); calledByNurse.removeWhere((element) => element.isQueueNurse == false); // calledByNurse.sort((a, b) => a.callNo.compareTo(b.callNo)); calledByNurse.addAll(isQueuePatients.toList()); onSuccess(calledByNurse.reversed.toList(), isQueuePatients.reversed.toList()); } else { onFailure(apiResp); } }, onFailure: (error, status) => onFailure(error)); } static callUpdateNotIsQueueRecordByIDAsync(String deviceIp, {@required Tickets ticket, @required Function(List) onSuccess, @required Function(dynamic) onFailure}) async { if (ticket.id == null) { return; } List _ticketsUpdated = []; // for (var ticket in tickets) { final body = {"id": ticket.id, "apiKey": apiKey, "ipAddress": deviceIp}; await BaseAppClient.post(_callUpdateNotIsQueueRecordByIDAsync, body: body, onSuccess: (response, status) { if (status == 200) { ticket.callUpdated = true; _ticketsUpdated.add(ticket); } log("here response: $response"); }, onFailure: (error, status) => onFailure(error)); // } if (_ticketsUpdated.isNotEmpty) { onSuccess(_ticketsUpdated); } else { onFailure(false); } } }