import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:intl/intl.dart'; import 'package:queue_mgr/backend/DebugHelper.dart'; import 'package:queue_mgr/backend/Dialogs.dart'; // DateFormat dateFormat; final String API_ENDPOINT= "https://vps.playpoolstudios.com/qms/api/"; DateFormat dateFormat = DateFormat("yyyy/MM/dd"); DateFormat timeFormat = DateFormat("hh:mm a"); DateFormat dateTimeFormat = DateFormat("yyyy/MM/dd hh:mm a"); class DataManager{ static _DataManager? m_instance = null; static _DataManager instance(){ m_instance ??= _DataManager(); return m_instance!; } } class _DataManager{ _DataManager(); List services = []; Future GetData() async{ try{ var response = (await http.post( Uri.parse('${API_ENDPOINT}get_services.php'), body: {})); Debug.LogResponse(response.body.toString()); services = jsonDecode(response.body.toString()); Debug.LogResponse(services); }catch(e){ Debug.LogError(e); } await ProcessServices(); } Future ProcessServices() async{ List _services= []; for (var s in services) { Map se = jsonDecode(s); if(DateTime.now().isAfter(DateTime.parse(se['end_time']))){ continue; } try{ var response = (await http.post( Uri.parse('${API_ENDPOINT}get_appointments.php'), body: { 'serviceId':se['id'] })); se.putIfAbsent("appointments", () => jsonDecode(response.body.toString())); }catch(e){ Debug.LogError(e); } _services.add(se); } services = _services; } Future AddService(String name, DateTime sTime, DateTime eTime,Duration duration) async { try{ var response = (await http.post( Uri.parse('${API_ENDPOINT}add_service.php'), body: { 'name':name, 'stime':sTime.toString(), 'etime':eTime.toString(), 'duration':"${duration.ToHoursAndMinutes()}" })); Debug.LogResponse(response.body.toString()); services = jsonDecode(response.body.toString()); Debug.LogResponse(services); }catch(e){ Debug.LogError(e); } await ProcessServices(); } Future DeleteService(String id) async { try{ var response = (await http.post( Uri.parse('${API_ENDPOINT}remove_service.php'), body: { 'id':id, })); Debug.LogResponse(response.body.toString()); services = jsonDecode(response.body.toString()); Debug.LogResponse(services); }catch(e){ Debug.LogError(e); } await ProcessServices(); } Future EditService(String id,String name, DateTime sTime, DateTime eTime,Duration duration) async { try{ var response = (await http.post( Uri.parse('${API_ENDPOINT}edit_service.php'), body: { 'id':id, 'name':name, 'stime':sTime.toString(), 'etime':eTime.toString(), 'duration':"${duration.ToHoursAndMinutes()}" })); Debug.LogResponse(response.body.toString()); services = jsonDecode(response.body.toString()); Debug.LogResponse(services); }catch(e){ Debug.LogError(e); } await ProcessServices(); } Future CompleteService(dynamic serviceId, dynamic tokenId) async { String res = ""; Debug.Log("Completing $tokenId from Service $serviceId"); try{ var response = (await http.post( Uri.parse('${API_ENDPOINT}complete_token.php'), body: { 'service_id':serviceId.toString(), 'user_id':tokenId.toString(), })); res = response.body.toString(); Debug.LogResponse(response.body.toString()); services = jsonDecode(response.body.toString()); await ProcessServices(); Debug.LogResponse(services); return "0"; }catch(e){ Debug.LogError(e); return res; } } Map GetServiceById(String id){ for (var service in services) { Map s = (service); if(s['id']==id){ return s; } } return {}; } } class Helpers{ static Duration ParseDuration(String input){ if(input.contains(":")){ return Duration(hours: int.parse(input.split(":")[0]), minutes: int.parse(input.split(":")[1])); } return Duration(minutes: 0); } } extension DurationExtensions on Duration{ String ToHoursAndMinutes(){ int mins = this.inMinutes %60; return "${this.inHours}:${mins}"; } }