import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:hris_selfservice_mobile/constants.dart'; import 'package:intl/intl.dart'; import 'package:progress_dialog_null_safe/progress_dialog_null_safe.dart'; import 'dart:developer' as logDev; import '../AjukanCuti/backgroundHistory.dart'; import 'RequestHttp/historyAbsensi_post.dart'; class HistoryAbsensi extends StatefulWidget { const HistoryAbsensi({Key? key}) : super(key: key); @override State createState() => _HistoryAbsensi(); } class _HistoryAbsensi extends State { late List id_List; late List employee_name_List; late List check_in_List; late List check_out_List; late List worked_hours_List; late List dayDate_List; late List date_List; late List month_List; //late List workingHours_List; late List showCheckIn_List; late List showCheckOut_List; int HistoryLength = 0; //List Visibility Double Date late List visibilityDate2; @override initState() { super.initState(); id_List = [""]; employee_name_List = [""]; check_in_List = [""]; check_out_List = [""]; worked_hours_List = [""]; dayDate_List = [""]; date_List = [""]; month_List = [""]; //workingHours_List = [""]; showCheckIn_List = [""]; showCheckOut_List = [""]; visibilityDate2 = [false]; WidgetsBinding.instance.addPostFrameCallback((_) async { getHistoryData(); }); } getHistoryData() async { ProgressDialog loading = ProgressDialog(context); loading = ProgressDialog(context, type: ProgressDialogType.normal, isDismissible: false, showLogs: true); loading.style( message: 'Please Wait .....', borderRadius: 3, backgroundColor: Colors.white, progressWidget: CircularProgressIndicator(), elevation: 10.0, padding: EdgeInsets.all(10), insetAnimCurve: Curves.easeInOut, progress: 0.0, maxProgress: 100.0, progressTextStyle: TextStyle( color: Colors.black, fontSize: 10.0, fontWeight: FontWeight.w400), messageTextStyle: TextStyle( color: Colors.black, fontSize: 15.0, fontWeight: FontWeight.w600)); loading.show(); HistoryAbsensi_Post.connectToAPI().then((valueResult) async { Map object = jsonDecode(valueResult); if (object.containsKey("result").toString() == "true") { String result = object['result'].toString(); if (result.contains("failed")) { loading.hide(); alertDialogFailedRetrievedData(context); } else { List historyAbsensi = object['result']; loading.hide(); setState(() { for (int i = 0; i < historyAbsensi.length; i++){ String id = historyAbsensi[i]['id'].toString(); String employee_name = historyAbsensi[i]['employee_name'].toString(); String check_in = historyAbsensi[i]['check_in'].toString(); String check_out = historyAbsensi[i]['check_out'].toString(); String worked_hours = historyAbsensi[i]['worked_hours'].toString(); String coordinate = historyAbsensi[i]['coordinate'].toString(); double hours = double.parse(worked_hours); if (check_in != "false"){ //Convert UTC to Local Time - Check In Time DateTime checkInTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(check_in, true); String showCheckInTime = checkInTime.toLocal().toString().substring(11, 19); showCheckIn_List.add(showCheckInTime.substring(0,5)); } else if (check_in == "false"){ String show_check_in = "-"; showCheckIn_List.add(show_check_in); } if (check_out != "false"){ //Convert UTC to Local Time - Check Out Time DateTime checkOutTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(check_out, true); String showCheckOutTime = checkOutTime.toLocal().toString().substring(11, 19); showCheckOut_List.add(showCheckOutTime.substring(0,5)); if (check_in.substring(0, 10) == check_out.substring(0,10)){ //Get Day Check In String dayDateIn = DateFormat('EEEE').format(DateTime.parse(check_in)); String dateIn = DateFormat('dd').format(DateTime.parse(check_in)); String monthIn = DateFormat('MMM').format(DateTime.parse(check_in)); dayDate_List.add(dayDateIn.substring(0,3)); date_List.add(dateIn); month_List.add(monthIn); } else if (check_in.substring(0, 10) != check_out.substring(0,10)){ //Get Day Check In & Check Out String dayDateIn = DateFormat('EEEE').format(DateTime.parse(check_in)); String dateIn = DateFormat('dd').format(DateTime.parse(check_in)); String monthIn = DateFormat('MMM').format(DateTime.parse(check_in)); String dayDateOut = DateFormat('EEEE').format(DateTime.parse(check_out)); String dateOut = DateFormat('dd').format(DateTime.parse(check_out)); String monthOut = DateFormat('MMM').format(DateTime.parse(check_out)); dayDate_List.add(dayDateIn.substring(0,3) + "\t|\t" + dayDateOut.substring(0,3)); date_List.add(dateIn + "\t|\t" + dateOut); month_List.add(monthIn + "\t|\t" + monthOut); } } else if (check_out == "false"){ String show_check_out = "-"; showCheckOut_List.add(show_check_out); String dayDateIn = DateFormat('EEEE').format(DateTime.parse(check_in)); String dateIn = DateFormat('dd').format(DateTime.parse(check_in)); String monthIn = DateFormat('MMM').format(DateTime.parse(check_in)); dayDate_List.add(dayDateIn.substring(0,3)); date_List.add(dateIn); month_List.add(monthIn); } if (check_in == "true" && check_out == "false"){ visibilityDate2.add(false); } else if (check_in == "false" && check_out == "false"){ visibilityDate2.add(false); } else if (check_in == "true" && check_out == "true"){ visibilityDate2.add(true); } id_List.add(id); employee_name_List.add(employee_name); check_in_List.add(check_in); check_out_List.add(check_out); worked_hours_List.add(hours.toStringAsFixed(3)); } id_List.removeAt(0); employee_name_List.removeAt(0); check_in_List.removeAt(0); check_out_List.removeAt(0); worked_hours_List.removeAt(0); dayDate_List.removeAt(0); date_List.removeAt(0); month_List.removeAt(0); //workingHours_List.removeAt(0); showCheckIn_List.removeAt(0); showCheckOut_List.removeAt(0); visibilityDate2.removeAt(0); HistoryLength = historyAbsensi.length; }); } } else { alertDialogFailedRetrievedData(context); } }); loading.hide(); } @override Widget build(BuildContext context) { var size = MediaQuery.of(context).size; return Scaffold( body: Stack( children: [ Column( children: [ Stack( children: [ WavyHeader(), Container( margin: EdgeInsets.only( top: (size.height / 6) - 20), padding: EdgeInsets.fromLTRB(0, 5, 25, 5), child: Row( mainAxisAlignment: MainAxisAlignment.end, crossAxisAlignment: CrossAxisAlignment.end, children: [ Text( 'Attendance History\t\t', maxLines: 1, style: GoogleFonts.luckiestGuy( fontSize: 28, color: Color(0xFF4858A7), //fontWeight: FontWeight.bold, fontStyle: FontStyle.italic, ), ), Image.asset( 'assets/images/ic_history.png', width: 40, height: 40, ), ], ) ), ], ), ], ), Container( margin: EdgeInsets.only(top: (MediaQuery.of(context).size.height / 6) + 40, left: 5, right: 5, bottom: 10), child: ListView.builder( scrollDirection: Axis.vertical, shrinkWrap: true, itemCount: HistoryLength, itemBuilder: (context, int i) { return Container( margin: EdgeInsets.fromLTRB(5, 5, 5, 5), child: InkWell( child: Card( elevation: 8, child: Container( padding: EdgeInsets.all(10), child: Row( children: [ Expanded( flex: 3, child: Container( alignment: Alignment.center, padding: EdgeInsets.all(5), decoration: BoxDecoration( border: Border.all(color: Colors.black), borderRadius: BorderRadius.all(Radius.circular(3)), ), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text(dayDate_List[i], style: GoogleFonts.fredokaOne(fontSize: 15, color: Colors.blueGrey), textAlign: TextAlign.center,), Text(date_List[i], style: GoogleFonts.fredokaOne(fontSize: 15, color: Colors.blueGrey), textAlign: TextAlign.center), Text(month_List[i], style: GoogleFonts.fredokaOne(fontSize: 15, color: Colors.blueGrey), textAlign: TextAlign.center), ], ) , ) ), Expanded( flex: 2, child: Column( children: [ Text("Check In", style: GoogleFonts.fredokaOne(fontSize: 16),textAlign: TextAlign.center), Text(showCheckIn_List[i], style: GoogleFonts.barlowSemiCondensed(fontSize: 16),textAlign: TextAlign.center), ], ), ), Expanded( flex: 2, child: Column( children: [ Text("Check Out", style: GoogleFonts.fredokaOne(fontSize: 16),textAlign: TextAlign.center), Text(showCheckOut_List[i], style: GoogleFonts.barlowSemiCondensed(fontSize: 16),textAlign: TextAlign.center), ], ), ), Expanded( flex: 2, child: Column( children: [ Text("Working\nHours", style: GoogleFonts.fredokaOne(fontSize: 16), textAlign: TextAlign.center), Text(worked_hours_List[i], style: GoogleFonts.barlowSemiCondensed(fontSize: 16),textAlign: TextAlign.center), ], ), ), ], ), ), ), onTap: (){ setState(() { //visible[i] = !visible[i]; }); }, ), ); }, ), ) ]), ); } } alertDialogFailedRetrievedData(BuildContext context) { Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => HistoryAbsensi())); }, ); Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text(appName), content: Text("Failed to Retrieve Data"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); } alertDialogFailedResponse(BuildContext context){ Widget okButton = TextButton( child: Text("Refresh"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pushReplacement(context, MaterialPageRoute( builder: (context) => HistoryAbsensi())); }, ); Widget noButton = TextButton( child: Text("Back"), onPressed: () { Navigator.of(context, rootNavigator: true).pop(); Navigator.pop(context); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: Text(appName), content: Text("Server Response Error"), actions: [ noButton, okButton, ], ); // show the dialog showDialog( context: context, barrierDismissible: false, builder: (BuildContext context) { return alert; }, ); }