214 lines
7.0 KiB
Dart
214 lines
7.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:fl_chart/fl_chart.dart';
|
|
import 'package:get/get.dart';
|
|
|
|
import '../../constant/links.dart';
|
|
import '../../models/model/passengers_model.dart';
|
|
import '../../print.dart';
|
|
import '../functions/crud.dart';
|
|
|
|
class StaticController extends GetxController {
|
|
Map<String, dynamic> jsonData1 = {};
|
|
Map<String, dynamic> jsonData2 = {};
|
|
List staticList = [];
|
|
var chartDataPassengers;
|
|
var chartDataDrivers;
|
|
var chartDataDriversCalling;
|
|
var chartDataRides;
|
|
var chartDataEmployee;
|
|
var chartDataEmployeeMaryam;
|
|
var chartDataEmployeeRawda;
|
|
var chartDataEmployeeMena;
|
|
var chartDataEmployeeSefer4;
|
|
var chartDataDriversMatchingNotes;
|
|
bool isLoading = false;
|
|
String totalMonthlyPassengers = '';
|
|
String totalMonthlyRides = '';
|
|
String totalMonthlyEmployee = '';
|
|
String totalMonthlyDrivers = '';
|
|
late List<MonthlyPassengerInstall> passengersData;
|
|
late List<MonthlyRidesInstall> ridesData;
|
|
late List<MonthlyEmployeeData> employeeData;
|
|
late List<MonthlyDriverInstall> driversData;
|
|
|
|
Future<void> fetch() async {
|
|
isLoading = true;
|
|
update(); // Notify the observers about the loading state change
|
|
|
|
var res = await CRUD().get(
|
|
link: AppLink.getPassengersStatic,
|
|
payload: {},
|
|
);
|
|
jsonData1 = jsonDecode(res);
|
|
var jsonResponse = jsonDecode(res) as Map<String, dynamic>;
|
|
isLoading = false;
|
|
final List<dynamic> jsonData = jsonResponse['message'];
|
|
totalMonthlyPassengers = jsonData[0]['totalMonthly'].toString();
|
|
passengersData = jsonData.map<MonthlyPassengerInstall>((item) {
|
|
return MonthlyPassengerInstall.fromJson(item);
|
|
}).toList();
|
|
final List<FlSpot> spots = passengersData
|
|
.map((data) => FlSpot(
|
|
data.day.toDouble(),
|
|
data.totalPassengers.toDouble(),
|
|
))
|
|
.toList();
|
|
chartDataPassengers = spots;
|
|
|
|
update(); // Notify the observers about the data and loading state change
|
|
}
|
|
|
|
Future<void> fetchRides() async {
|
|
isLoading = true;
|
|
update(); // Notify the observers about the loading state change
|
|
|
|
var res = await CRUD().get(
|
|
link: AppLink.getRidesStatic,
|
|
payload: {},
|
|
);
|
|
jsonData1 = jsonDecode(res);
|
|
var jsonResponse = jsonDecode(res) as Map<String, dynamic>;
|
|
isLoading = false;
|
|
final List<dynamic> jsonData = jsonResponse['message'];
|
|
totalMonthlyRides = jsonData[0]['totalMonthly'].toString();
|
|
ridesData = jsonData.map<MonthlyRidesInstall>((item) {
|
|
return MonthlyRidesInstall.fromJson(item);
|
|
}).toList();
|
|
final List<FlSpot> spots = ridesData
|
|
.map((data) => FlSpot(
|
|
data.day.toDouble(),
|
|
data.totalRides.toDouble(),
|
|
))
|
|
.toList();
|
|
chartDataRides = spots;
|
|
|
|
update(); // Notify the observers about the data and loading state change
|
|
}
|
|
|
|
Future<void> fetchEmployee() async {
|
|
isLoading = true;
|
|
update();
|
|
|
|
var res = await CRUD().get(link: AppLink.getEmployeeStatic, payload: {});
|
|
var jsonResponse = jsonDecode(res) as Map<String, dynamic>;
|
|
isLoading = false;
|
|
|
|
final List<dynamic> jsonData = jsonResponse['message'];
|
|
totalMonthlyRides = jsonData[0]['totalMonthly'].toString();
|
|
|
|
// Group data by employee
|
|
Map<String, List<MonthlyEmployeeData>> employeeDataMap = {};
|
|
|
|
for (var item in jsonData) {
|
|
var employeeData = MonthlyEmployeeData.fromJson(item);
|
|
if (!employeeDataMap.containsKey(employeeData.name)) {
|
|
employeeDataMap[employeeData.name] = [];
|
|
}
|
|
employeeDataMap[employeeData.name]!.add(employeeData);
|
|
}
|
|
|
|
// Create FlSpot data for each employee
|
|
List<FlSpot> chartDataMaryam = [];
|
|
List<FlSpot> chartDataRawda = [];
|
|
List<FlSpot> chartDataMena = [];
|
|
List<FlSpot> chartDataSefer4 = [];
|
|
|
|
for (int day = 1; day <= DateTime.now().day; day++) {
|
|
chartDataMaryam.add(FlSpot(
|
|
day.toDouble(),
|
|
employeeDataMap['maryam']
|
|
?.firstWhere((e) => e.day == day,
|
|
orElse: () => MonthlyEmployeeData(
|
|
day: day, totalEmployees: 0, name: 'maryam'))
|
|
.totalEmployees
|
|
.toDouble() ??
|
|
0));
|
|
chartDataRawda.add(FlSpot(
|
|
day.toDouble(),
|
|
employeeDataMap['yasmine']
|
|
?.firstWhere((e) => e.day == day,
|
|
orElse: () => MonthlyEmployeeData(
|
|
day: day, totalEmployees: 0, name: 'yasmine'))
|
|
.totalEmployees
|
|
.toDouble() ??
|
|
0));
|
|
chartDataMena.add(FlSpot(
|
|
day.toDouble(),
|
|
employeeDataMap['mena']
|
|
?.firstWhere((e) => e.day == day,
|
|
orElse: () => MonthlyEmployeeData(
|
|
day: day, totalEmployees: 0, name: 'mena'))
|
|
.totalEmployees
|
|
.toDouble() ??
|
|
0));
|
|
chartDataSefer4.add(FlSpot(
|
|
day.toDouble(),
|
|
employeeDataMap['ashjan']
|
|
?.firstWhere((e) => e.day == day,
|
|
orElse: () => MonthlyEmployeeData(
|
|
day: day, totalEmployees: 0, name: 'ashjan'))
|
|
.totalEmployees
|
|
.toDouble() ??
|
|
0));
|
|
}
|
|
|
|
// Combine spots into a single list if needed or keep them separate
|
|
chartDataEmployeeMaryam = chartDataMaryam;
|
|
chartDataEmployeeRawda = chartDataRawda;
|
|
chartDataEmployeeMena = chartDataMena;
|
|
chartDataEmployeeSefer4 = chartDataSefer4;
|
|
|
|
update();
|
|
}
|
|
|
|
Future<void> fetchDrivers() async {
|
|
isLoading = true;
|
|
update(); // Notify the observers about the loading state change
|
|
|
|
var res = await CRUD().get(
|
|
link: AppLink.getdriverstotalMonthly,
|
|
payload: {},
|
|
);
|
|
jsonData2 = jsonDecode(res);
|
|
var jsonResponse = jsonDecode(res) as Map<String, dynamic>;
|
|
isLoading = false;
|
|
final List<dynamic> jsonData = jsonResponse['message'];
|
|
staticList = jsonData;
|
|
totalMonthlyDrivers = jsonData[0]['totalDrivers'].toString();
|
|
driversData = jsonData.map<MonthlyDriverInstall>((item) {
|
|
return MonthlyDriverInstall.fromJson(item);
|
|
}).toList();
|
|
final List<FlSpot> spots = driversData
|
|
.map((data) => FlSpot(
|
|
data.day.toDouble(),
|
|
data.dailyTotalDrivers.toDouble(),
|
|
))
|
|
.toList();
|
|
chartDataDrivers = spots;
|
|
final List<FlSpot> spotsCalling = driversData
|
|
.map((data) => FlSpot(
|
|
data.day.toDouble(),
|
|
data.dailyTotalCallingDrivers.toDouble(),
|
|
))
|
|
.toList();
|
|
chartDataDriversCalling = spotsCalling;
|
|
final List<FlSpot> spotsTotalMatchingNotes = driversData
|
|
.map((data) => FlSpot(
|
|
data.day.toDouble(),
|
|
data.dailyMatchingNotes.toDouble(),
|
|
))
|
|
.toList();
|
|
chartDataDriversMatchingNotes = spotsTotalMatchingNotes;
|
|
|
|
update(); // Notify the observers about the data and loading state change
|
|
}
|
|
|
|
Future getAll() async {
|
|
await fetch();
|
|
await fetchRides();
|
|
await fetchDrivers();
|
|
await fetchEmployee();
|
|
}
|
|
}
|