1
0

dutyinfo_page.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // ignore_for_file: unused_import
  2. import 'dart:developer';
  3. import 'package:collection/collection.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_riverpod/flutter_riverpod.dart';
  7. import 'package:gap/gap.dart';
  8. import 'package:go_router/go_router.dart';
  9. import 'package:google_fonts/google_fonts.dart';
  10. import 'package:jiffy/jiffy.dart';
  11. import 'package:linear_progress_bar/linear_progress_bar.dart';
  12. import 'package:tp5/core/core.dart';
  13. import 'package:tp5/csv/csv_data.dart';
  14. import 'package:tp5/csv/data.dart';
  15. import 'package:tp5/flightslist/w_flt.dart';
  16. import 'package:tp5/fltinfo/delaycodes.dart';
  17. import 'package:tp5/fltinfo/widget/w_cadre.dart';
  18. import 'package:tp5/ftl/widget/w_shadowbox.dart';
  19. import 'package:tp5/lido/lido_api.dart';
  20. import 'package:tp5/lido/model/Lidoapi_list.dart';
  21. import 'package:tp5/lido/model/Lidoapi_ofp.dart';
  22. import 'package:tp5/lido/w_lidoflt.dart';
  23. import 'package:tp5/pdf/pdf_page.dart';
  24. import 'package:tp5/roster/api/crewlink_api.dart';
  25. import 'package:tp5/roster/models/duty.dart';
  26. import 'package:tp5/roster/widgets/w_airport.dart';
  27. import 'package:tp5/roster/widgets/w_citypair.dart';
  28. import 'package:tp5/roster/widgets/w_crew.dart';
  29. import 'package:tp5/roster/widgets/w_day.dart';
  30. import 'package:tp5/roster/widgets/w_fnum.dart';
  31. import 'package:tp5/roster/widgets/w_hour.dart';
  32. import 'package:tp5/widgets/my_row.dart';
  33. class DutyinfoPage extends ConsumerStatefulWidget {
  34. const DutyinfoPage({required this.params, super.key});
  35. final DutyinfoParams params;
  36. @override
  37. ConsumerState<ConsumerStatefulWidget> createState() => _DutyinfoPageState();
  38. }
  39. class _DutyinfoPageState extends ConsumerState<DutyinfoPage> {
  40. @override
  41. void initState() {
  42. Future.delayed(Duration.zero, () {
  43. crewPnlegLoad();
  44. });
  45. super.initState();
  46. }
  47. // List crew = [];
  48. Jiffy _now = Jiffy.now().toUtc();
  49. @override
  50. Widget build(BuildContext context) {
  51. //final data = ref.watch(dataProvider);
  52. //final pnleg = data.pnleg;
  53. // final _dateNotifiedcrew = data.pnlegupdate;
  54. _now = ref.watch(clockProvider);
  55. //final qualif = ref.watch(dataProvider).qualif;
  56. return Scaffold(
  57. appBar: AppBar(
  58. title: MyRow(
  59. // mainAxisAlignment: MainAxisAlignment.spaceBetween,
  60. children: [
  61. Text(
  62. '${widget.params.dutytype ?? ""} ${widget.params.label ?? ""} ${widget.params.dep}${widget.params.des != null ? "-${widget.params.des}" : ""}'),
  63. const Gap(10),
  64. Text(widget.params.jdep!.format(pattern: "EEE ddMMM'yy"),
  65. style: const TextStyle(fontSize: 12)),
  66. ],
  67. ),
  68. leading: IconButton(
  69. icon: const Icon(Icons.arrow_back),
  70. onPressed: () {
  71. context.pop(context);
  72. },
  73. ),
  74. actions: [
  75. Text(
  76. '${widget.params.dutytype ?? "Duty"} info',
  77. style: TextStyle(
  78. fontSize: 12,
  79. fontWeight: FontWeight.w700,
  80. color: Colors.yellow),
  81. ),
  82. Gap(10)
  83. ],
  84. ),
  85. body: ListView(
  86. padding: const EdgeInsets.all(5),
  87. children: [
  88. WCadre(
  89. child: Column(
  90. children: [
  91. WDay(date: widget.params.jdep!),
  92. if (widget.params.label != null)
  93. Text(
  94. widget.params.label!,
  95. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
  96. )
  97. else if (widget.params.dutytype != null)
  98. Text(
  99. widget.params.dutytype!,
  100. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
  101. ),
  102. Row(
  103. mainAxisAlignment: MainAxisAlignment.center,
  104. mainAxisSize: MainAxisSize.max,
  105. children: [
  106. if (widget.params.des != null)
  107. WCitypair(
  108. iata1: "${widget.params.dep}",
  109. iata2: widget.params.des ?? "---")
  110. else
  111. WAirport(iata: widget.params.dep ?? "---"),
  112. ],
  113. ),
  114. Row(
  115. mainAxisAlignment: MainAxisAlignment.center,
  116. mainAxisSize: MainAxisSize.max,
  117. children: [
  118. WHour(jiffy: widget.params.jdep!),
  119. if (widget.params.jdes != null) ...[
  120. const Gap(20),
  121. WHour(jiffy: widget.params.jdes!)
  122. ],
  123. ]),
  124. const Gap(10)
  125. ],
  126. ),
  127. ),
  128. const Gap(20),
  129. if (widget.params.dutytype == "standby")
  130. _stdbyCrew()
  131. else
  132. _notifiedCrew(),
  133. ],
  134. ),
  135. );
  136. }
  137. Jiffy? _dateNotifiedcrew;
  138. static List<Pnleg> filterPnleg(Map data) {
  139. final List<Pnleg> pnleg = data["pnleg"];
  140. final DutyinfoParams filter = data["filter"];
  141. return pnleg
  142. .where((leg) => ((filter.jdep == null ||
  143. (filter.sameday
  144. ? (leg.jdep?.startOf(Unit.day) ==
  145. filter.jdep?.startOf(Unit.day))
  146. : ((leg.jdep == filter.jdep) &&
  147. (filter.jdes == null || leg.jarr == filter.jdes)))) &&
  148. (filter.dep == null || leg.dep == filter.dep) &&
  149. (filter.des == null || leg.arr == filter.des) &&
  150. (filter.label == null || leg.label == filter.label) &&
  151. (filter.dutytype == null || leg.dutytype == filter.dutytype)))
  152. .toList();
  153. }
  154. List<Pnleg> crewPnleg = [];
  155. bool crewPnlegLoading = false;
  156. crewPnlegLoad() async {
  157. crewPnlegLoading = true;
  158. setState(() {});
  159. final data = ref.watch(dataProvider);
  160. final pnleg = data.pnleg;
  161. _dateNotifiedcrew = data.pnlegupdate;
  162. crewPnleg =
  163. await compute(filterPnleg, {"pnleg": pnleg, "filter": widget.params});
  164. crewPnlegLoading = false;
  165. setState(() {});
  166. }
  167. _notifiedCrew() {
  168. final qualif = ref.watch(qualifProvider(CrewFilter()));
  169. return WCadre(
  170. titleright: crewPnlegLoading
  171. ? Padding(
  172. padding: const EdgeInsets.all(5.0),
  173. child: CircularProgressIndicator(),
  174. )
  175. : ElevatedButton.icon(
  176. label: const Text("Update", style: TextStyle(fontSize: 9)),
  177. onPressed: () async {
  178. await crewPnlegLoad();
  179. },
  180. icon: const Icon(Icons.update)),
  181. title: "Notified Crew (alt)",
  182. bottomright: _dateNotifiedcrew != null
  183. ? Text("data retrieved ${_dateNotifiedcrew?.from(_now)}",
  184. style: GoogleFonts.robotoMono(
  185. fontSize: 10, fontWeight: FontWeight.w300))
  186. : null,
  187. child: crewPnleg.isEmpty
  188. ? const Text("No data found !")
  189. : Column(
  190. children: [
  191. Padding(
  192. padding: const EdgeInsets.all(8.0),
  193. child: Column(
  194. children: crewPnleg
  195. .mapIndexed((int i, leg) => {
  196. "index": i,
  197. "college": (["F", ""].contains(leg.type))
  198. ? "DH"
  199. : (qualif
  200. .firstWhereOrNull((e) =>
  201. (e.tlc == leg.tlc) &&
  202. (e.ac == leg.actype))
  203. ?.college ??
  204. qualif
  205. .firstWhereOrNull(
  206. (e) => e.tlc == leg.tlc)
  207. ?.college ??
  208. "--")
  209. })
  210. .sortedByCompare(
  211. (e) => e["college"].toString(), collegeSort)
  212. .map<Widget>((e) {
  213. final x = crewPnleg.elementAt(e["index"] as int);
  214. final pn = qualif.firstWhereOrNull((e) => e.tlc == x.tlc);
  215. return WCrew(
  216. college: pn?.college,
  217. mle: x.tlc,
  218. base: pn?.base,
  219. name: "${pn?.lname}, ${pn?.fname}",
  220. );
  221. }).toList()),
  222. )
  223. ],
  224. ),
  225. );
  226. }
  227. _stdbyCrew() {
  228. final qualif = ref.watch(qualifProvider(CrewFilter()));
  229. return WCadre(
  230. titleright: crewPnlegLoading
  231. ? Padding(
  232. padding: const EdgeInsets.all(5.0),
  233. child: CircularProgressIndicator(),
  234. )
  235. : ElevatedButton.icon(
  236. label: const Text("Update", style: TextStyle(fontSize: 9)),
  237. onPressed: () async {
  238. await crewPnlegLoad();
  239. },
  240. icon: const Icon(Icons.update)),
  241. title: "STDBY Crew",
  242. bottomright: _dateNotifiedcrew != null
  243. ? Text("data retrieved ${_dateNotifiedcrew?.from(_now)}",
  244. style: GoogleFonts.robotoMono(
  245. fontSize: 10, fontWeight: FontWeight.w300))
  246. : null,
  247. child: crewPnleg.isEmpty
  248. ? const Text("No data found !")
  249. : Column(
  250. children: [
  251. Padding(
  252. padding: const EdgeInsets.all(8.0),
  253. child: Column(
  254. children: crewPnleg
  255. .mapIndexed((int i, leg) {
  256. // print(leg);
  257. return {
  258. "index": i,
  259. "college": (qualif
  260. .firstWhereOrNull((e) => e.tlc == leg.tlc)
  261. ?.college ??
  262. "--"),
  263. // "jdep": (leg.jdep?.millisecondsSinceEpoch ?? 0)
  264. "jdep": (leg.label ?? "0")
  265. };
  266. })
  267. .sortedByCompare(
  268. (e) => [
  269. e["jdep"].toString(),
  270. e["college"].toString()
  271. ],
  272. jdepcollegeSort)
  273. .map<Widget>((e) {
  274. final x = crewPnleg.elementAt(e["index"] as int);
  275. final pn =
  276. qualif.firstWhereOrNull((e) => e.tlc == x.tlc);
  277. return Row(
  278. children: [
  279. Text("${e["jdep"]}"),
  280. WCrew(
  281. college: pn?.college,
  282. mle: x.tlc,
  283. base: pn?.base,
  284. name: "${pn?.lname}, ${pn?.fname}",
  285. ),
  286. ],
  287. );
  288. })
  289. .toList()),
  290. )
  291. ],
  292. ),
  293. );
  294. }
  295. }
  296. class DutyinfoParams {
  297. const DutyinfoParams({
  298. this.sameday = false,
  299. this.date,
  300. this.start,
  301. this.end,
  302. this.label,
  303. this.dutytype,
  304. this.jdep,
  305. this.jdes,
  306. this.dep,
  307. this.des,
  308. });
  309. final bool sameday;
  310. final Jiffy? start;
  311. final Jiffy? end;
  312. final String? date;
  313. final String? label;
  314. final String? dutytype;
  315. final Jiffy? jdep;
  316. final Jiffy? jdes;
  317. final String? dep;
  318. final String? des;
  319. }
  320. int collegeSort(String a, String b) {
  321. // Define the order based on your requirements
  322. final order = ['CP', 'FO', 'PU', 'SE', 'ST', 'JU'];
  323. // Get the index of each element in the order list
  324. final indexA = order.indexOf(a);
  325. final indexB = order.indexOf(b);
  326. // Compare based on the index
  327. return indexA.compareTo(indexB);
  328. }
  329. int jdepcollegeSort(List<String> a, List<String> b) {
  330. final String x = a[0];
  331. final String y = b[0];
  332. final res = x.compareTo(y);
  333. if (res == 0) {
  334. return collegeSort(a[1], b[1]);
  335. } else {
  336. return res;
  337. }
  338. }