marco.pms.mobileapp/lib/view/dashboard/attendanceScreen.dart

793 lines
32 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_lucide/flutter_lucide.dart';
import 'package:get/get.dart';
import 'package:marco/helpers/theme/app_theme.dart';
import 'package:marco/helpers/utils/mixins/ui_mixin.dart';
import 'package:marco/helpers/utils/my_shadow.dart';
import 'package:marco/helpers/widgets/my_breadcrumb.dart';
import 'package:marco/helpers/widgets/my_breadcrumb_item.dart';
import 'package:marco/helpers/widgets/my_card.dart';
import 'package:marco/helpers/widgets/my_container.dart';
import 'package:marco/helpers/widgets/my_flex.dart';
import 'package:marco/helpers/widgets/my_flex_item.dart';
import 'package:marco/helpers/widgets/my_list_extension.dart';
import 'package:marco/helpers/widgets/my_spacing.dart';
import 'package:marco/helpers/widgets/my_text.dart';
import 'package:marco/view/layouts/layout.dart';
import 'package:marco/controller/dashboard/attendance_screen_controller.dart';
import 'package:intl/intl.dart';
import 'package:marco/controller/permission_controller.dart';
import 'package:marco/helpers/utils/permission_constants.dart';
import 'package:marco/helpers/utils/attendance_actions.dart';
import 'package:marco/helpers/widgets/my_refresh_wrapper.dart';
import 'package:marco/model/my_paginated_table.dart';
class AttendanceScreen extends StatefulWidget {
const AttendanceScreen({super.key});
@override
State<AttendanceScreen> createState() => _AttendanceScreenState();
}
class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
AttendanceController attendanceController = Get.put(AttendanceController());
PermissionController permissionController = Get.put(PermissionController());
@override
Widget build(BuildContext context) {
return Layout(
child: MyRefreshWrapper(
onRefresh: () async {
if (attendanceController.selectedProjectId != null) {
await attendanceController.fetchEmployeesByProject(
attendanceController.selectedProjectId!);
await attendanceController
.fetchAttendanceLogs(attendanceController.selectedProjectId!);
} else {
await attendanceController.fetchProjects();
}
},
child: GetBuilder(
init: attendanceController,
tag: 'attendance_dashboard_controller',
builder: (controller) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: MySpacing.x(flexSpacing),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
MyText.titleMedium("Attendance",
fontSize: 18, fontWeight: 600),
MyBreadcrumb(
children: [
MyBreadcrumbItem(name: 'Dashboard'),
MyBreadcrumbItem(name: 'Attendance', active: true),
],
),
],
),
),
MySpacing.height(flexSpacing),
Padding(
padding: MySpacing.x(flexSpacing / 2),
child: MyFlex(
children: [
// Popup Menu for Project Selection
MyFlexItem(
sizes: 'lg-12',
child: MyContainer.bordered(
padding: MySpacing.xy(8, 8),
child: PopupMenuButton<String>(
onSelected: (value) {
setState(() {
attendanceController.selectedProjectId = value;
attendanceController
.fetchEmployeesByProject(value);
attendanceController.fetchAttendanceLogs(value);
});
},
itemBuilder: (BuildContext context) {
if (attendanceController.projects.isEmpty) {
return [
PopupMenuItem<String>(
value: '',
child: MyText.bodySmall('No Data',
fontWeight: 600),
)
];
}
return attendanceController.projects
.map((project) {
return PopupMenuItem<String>(
value: project.id.toString(),
height: 32,
child: MyText.bodySmall(
project.name,
color: theme.colorScheme.onSurface,
fontWeight: 600,
),
);
}).toList();
},
color: theme.cardTheme.color,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
MyText.labelSmall(
attendanceController.selectedProjectId != null
? attendanceController.projects
.firstWhereOrNull((proj) =>
proj.id.toString() ==
attendanceController
.selectedProjectId)
?.name ??
'Select a Project'
: 'Select a Project',
color: theme.colorScheme.onSurface,
),
Icon(LucideIcons.chevron_down,
size: 20,
color: theme.colorScheme.onSurface),
],
),
),
),
),
// Tabs for Employee List, Logs, and Regularization
MyFlexItem(
sizes: 'lg-12',
child: Obx(() {
bool hasRegularizationPermission =
permissionController.hasPermission(
Permissions.regularizeAttendance);
final tabs = <Tab>[
const Tab(text: 'Employee List'),
const Tab(text: 'Logs'),
if (hasRegularizationPermission)
const Tab(text: 'Regularization'),
];
final views = <Widget>[
employeeListTab(),
reportsTab(context),
if (hasRegularizationPermission)
regularizationTab(context),
];
return DefaultTabController(
length: tabs.length,
child: MyCard.bordered(
borderRadiusAll: 4,
border:
Border.all(color: Colors.grey.withAlpha(50)),
shadow: MyShadow(
elevation: 1,
position: MyShadowPosition.bottom),
paddingAll: 10,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TabBar(
labelColor: theme.colorScheme.primary,
unselectedLabelColor: theme
.colorScheme.onSurface
.withAlpha(150),
tabs: tabs,
),
MySpacing.height(16),
SizedBox(
height: 550,
child: TabBarView(children: views),
),
],
),
),
);
}),
),
],
),
),
],
);
},
),
),
);
}
Widget employeeListTab() {
if (attendanceController.employees.isEmpty) {
return Center(
child: MyText.bodySmall("No Employees Found", fontWeight: 600),
);
}
final columns = <DataColumn>[
DataColumn(
label: MyText.labelLarge('Name', color: contentTheme.primary),
),
DataColumn(
label: MyText.labelLarge('Actions', color: contentTheme.primary),
),
];
final rows = attendanceController.employees.mapIndexed((index, employee) {
int? activity = employee.activity;
String buttonText = (activity == 0 || activity == 4)
? ButtonActions.checkIn
: ButtonActions.checkOut;
return DataRow(cells: [
DataCell(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
MyText.bodyMedium(employee.name, fontWeight: 600),
SizedBox(height: 2),
MyText.bodySmall(employee.designation, color: Colors.grey),
],
),
),
DataCell(
ElevatedButton(
onPressed: () async {
if (attendanceController.selectedProjectId == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Please select a project first")),
);
return;
}
int updatedAction = (activity == 0 || activity == 4) ? 0 : 1;
String actionText = (updatedAction == 0)
? ButtonActions.checkIn
: ButtonActions.checkOut;
final success =
await attendanceController.captureAndUploadAttendance(
employee.id,
employee.employeeId,
int.parse(attendanceController.selectedProjectId!),
comment: actionText,
action: updatedAction,
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(success
? 'Attendance marked successfully!'
: 'Image upload failed.'),
),
);
if (success) {
attendanceController.fetchEmployeesByProject(
attendanceController.selectedProjectId!);
attendanceController.fetchAttendanceLogs(
attendanceController.selectedProjectId!);
}
},
style: ElevatedButton.styleFrom(
backgroundColor: AttendanceActionColors.colors[buttonText],
padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 6),
minimumSize: const Size(60, 20),
textStyle: const TextStyle(fontSize: 12),
),
child: Text(buttonText),
),
),
]);
}).toList();
return Padding(
padding: const EdgeInsets.all(8.0), // You can adjust this as needed
child: SingleChildScrollView(
child: MyPaginatedTable(
columns: columns,
rows: rows,
),
),
);
}
Widget reportsTab(BuildContext context) {
final attendanceController = Get.find<AttendanceController>();
final columns = [
DataColumn(label: MyText.labelLarge('Name', color: contentTheme.primary)),
DataColumn(
label: MyText.labelLarge('Check-In', color: contentTheme.primary)),
DataColumn(
label: MyText.labelLarge('Check-Out', color: contentTheme.primary)),
DataColumn(label: MyText.labelLarge('View', color: contentTheme.primary)),
DataColumn(
label: MyText.labelLarge('Action', color: contentTheme.primary)),
];
final rows = attendanceController.attendanceLogs.mapIndexed((index, log) {
return DataRow(cells: [
DataCell(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
MyText.bodyMedium(log.name, fontWeight: 600),
SizedBox(height: 2),
MyText.bodySmall(log.role, color: Colors.grey),
],
),
),
DataCell(
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MyText.bodyMedium(
log.checkIn != null
? DateFormat('dd MMM yyyy').format(log.checkIn!)
: '-',
fontWeight: 600,
),
MyText.bodyMedium(
log.checkIn != null
? DateFormat('hh:mm a').format(log.checkIn!)
: '',
fontWeight: 600,
),
],
),
),
DataCell(
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MyText.bodyMedium(
log.checkOut != null
? DateFormat('dd MMM yyyy').format(log.checkOut!)
: '-',
fontWeight: 600,
),
MyText.bodyMedium(
log.checkOut != null
? DateFormat('hh:mm a').format(log.checkOut!)
: '',
fontWeight: 600,
),
],
),
),
DataCell(
IconButton(
icon: const Icon(Icons.visibility, size: 18),
onPressed: () async {
await attendanceController.fetchLogsView(log.id.toString());
showModalBottomSheet(
context: context,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
),
backgroundColor: Theme.of(context).cardColor,
builder: (context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MyText.titleMedium("Attendance Log Details",
fontWeight: 700),
const SizedBox(height: 16),
if (attendanceController
.attendenceLogsView.isNotEmpty) ...[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: MyText.bodyMedium("Date",
fontWeight: 600)),
Expanded(
child: MyText.bodyMedium("Time",
fontWeight: 600)),
Expanded(
child: MyText.bodyMedium("Description",
fontWeight: 600)),
Expanded(
child: MyText.bodyMedium("Image",
fontWeight: 600)),
],
),
const Divider(thickness: 1, height: 24),
],
if (attendanceController.attendenceLogsView.isNotEmpty)
...attendanceController.attendenceLogsView
.map((log) => Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: MyText.bodyMedium(
log.formattedDate ?? '-',
fontWeight: 600)),
Expanded(
child: MyText.bodyMedium(
log.formattedTime ?? '-',
fontWeight: 600)),
Expanded(
child: MyText.bodyMedium(
log.comment ?? '-',
fontWeight: 600)),
Expanded(
child: GestureDetector(
onTap: () {
if (log.preSignedUrl != null) {
showDialog(
context: context,
builder: (_) => Dialog(
child: Image.network(
log.preSignedUrl!,
fit: BoxFit.cover,
height: 400,
errorBuilder: (context,
error, stackTrace) {
return Icon(
Icons.broken_image,
size: 50,
color: Colors.grey);
},
),
),
);
}
},
child: log.thumbPreSignedUrl != null
? Image.network(
log.thumbPreSignedUrl!,
height: 40,
width: 40,
fit: BoxFit.cover,
errorBuilder: (context, error,
stackTrace) {
return Icon(
Icons.broken_image,
size: 40,
color: Colors.grey);
},
)
: Icon(Icons.broken_image,
size: 40, color: Colors.grey),
),
),
],
)),
Align(
alignment: Alignment.centerRight,
child: ElevatedButton(
onPressed: () => Navigator.pop(context),
child: const Text("Close"),
),
),
],
),
);
},
);
},
),
),
DataCell(
ElevatedButton(
onPressed: () async {
if (attendanceController.selectedProjectId == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Please select a project first")),
);
return;
}
int updatedAction;
String actionText;
if (log.activity == 0 || log.activity == 4) {
updatedAction = 0;
actionText = "Check In";
} else if (log.activity == 1) {
updatedAction = 1;
actionText = "Check Out";
} else if (log.activity == 2) {
updatedAction = 2;
actionText = "Request Regularize";
} else {
updatedAction = 0;
actionText = "Unknown Action";
}
final success =
await attendanceController.captureAndUploadAttendance(
log.id,
log.employeeId,
int.parse(attendanceController.selectedProjectId!),
comment: actionText,
action: updatedAction,
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(success
? 'Attendance marked successfully!'
: 'Image upload failed.')),
);
if (success) {
attendanceController.fetchEmployeesByProject(
attendanceController.selectedProjectId!);
attendanceController.fetchAttendanceLogs(
attendanceController.selectedProjectId!);
}
},
style: ElevatedButton.styleFrom(
backgroundColor: AttendanceActionColors.colors[
(log.activity == 0 || log.activity == 4)
? ButtonActions.checkIn
: ButtonActions.checkOut],
padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 6),
minimumSize: const Size(60, 20),
textStyle: const TextStyle(fontSize: 12),
),
child: Text(
(log.activity == 0 || log.activity == 4)
? ButtonActions.checkIn
: (log.activity == 2)
? ButtonActions.requestRegularize
: ButtonActions.checkOut,
),
),
),
]);
}).toList();
return SingleChildScrollView(
// Wrap the Column in SingleChildScrollView to handle overflow
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextButton.icon(
icon: const Icon(Icons.date_range),
label: const Text("Select Date Range for Attendance"),
onPressed: () => attendanceController
.selectDateRangeForAttendance(context, attendanceController),
),
),
if (attendanceController.attendanceLogs.isEmpty)
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 40),
child: MyText.bodySmall(
"No Attendance Records Found",
fontWeight: 600,
),
),
)
else
SingleChildScrollView(
child: MyPaginatedTable(
columns: columns,
rows: rows,
columnSpacing: 8.0,
),
),
],
),
);
}
Widget regularizationTab(BuildContext context) {
final attendanceController = Get.find<AttendanceController>();
final columns = [
DataColumn(label: MyText.labelLarge('Name', color: contentTheme.primary)),
DataColumn(
label: MyText.labelLarge('Check-In', color: contentTheme.primary)),
DataColumn(
label: MyText.labelLarge('Check-Out', color: contentTheme.primary)),
DataColumn(
label: MyText.labelLarge('Action', color: contentTheme.primary)),
];
final rows = attendanceController.regularizationLogs
.mapIndexed((index, log) => DataRow(cells: [
DataCell(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
MyText.bodyMedium(log.name, fontWeight: 600),
SizedBox(height: 2),
MyText.bodySmall(log.role, color: Colors.grey),
],
),
),
DataCell(
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MyText.bodyMedium(
log.checkIn != null
? DateFormat('dd/MMM/yyyy').format(log.checkIn!)
: '-',
fontWeight: 600,
),
MyText.bodyMedium(
log.checkIn != null
? DateFormat('hh:mm a').format(log.checkIn!)
: '',
fontWeight: 600,
),
],
),
),
DataCell(
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
MyText.bodyMedium(
log.checkOut != null
? DateFormat('dd MMM yyyy').format(log.checkOut!)
: '-',
fontWeight: 600,
),
MyText.bodyMedium(
log.checkOut != null
? DateFormat('hh:mm a').format(log.checkOut!)
: '',
fontWeight: 600,
),
],
),
),
DataCell(
Row(
children: [
// Approve Button
ElevatedButton(
onPressed: () async {
if (attendanceController.selectedProjectId == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Please select a project first")),
);
return;
}
final success = await attendanceController
.captureAndUploadAttendance(
log.id,
log.employeeId,
int.parse(attendanceController.selectedProjectId!),
comment: "Accepted",
action: 4, // Approve action
imageCapture: false,
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(success
? 'Approval marked successfully!'
: 'Failed to mark approval.')),
);
if (success) {
attendanceController.fetchEmployeesByProject(
attendanceController.selectedProjectId!);
attendanceController.fetchAttendanceLogs(
attendanceController.selectedProjectId!);
}
},
style: ElevatedButton.styleFrom(
backgroundColor: AttendanceActionColors
.colors[ButtonActions.approve],
padding: const EdgeInsets.symmetric(
vertical: 4, horizontal: 6),
minimumSize: const Size(60, 20),
textStyle: const TextStyle(fontSize: 12),
),
child: const Text("Approve"),
),
// Space between buttons
const SizedBox(width: 8),
// Reject Button
ElevatedButton(
onPressed: () async {
if (attendanceController.selectedProjectId == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Please select a project first")),
);
return;
}
final success = await attendanceController
.captureAndUploadAttendance(
log.id,
log.employeeId,
int.parse(attendanceController.selectedProjectId!),
comment: "Rejected",
action: 5, // Reject action
imageCapture: false,
);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(success
? 'Attendance marked as Rejected!'
: 'Failed to mark attendance.')),
);
if (success) {
attendanceController.fetchEmployeesByProject(
attendanceController.selectedProjectId!);
attendanceController.fetchAttendanceLogs(
attendanceController.selectedProjectId!);
}
},
style: ElevatedButton.styleFrom(
backgroundColor:
AttendanceActionColors.colors[ButtonActions.reject],
padding: const EdgeInsets.symmetric(
vertical: 4, horizontal: 6),
minimumSize: const Size(60, 20),
textStyle: const TextStyle(fontSize: 12),
),
child: const Text("Reject"),
),
],
),
),
]))
.toList();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
),
if (attendanceController.regularizationLogs.isEmpty)
Expanded(
child: Center(
child: MyText.bodySmall("No Regularization Records Found",
fontWeight: 600),
),
)
else
Expanded(
child: SingleChildScrollView(
child: MyPaginatedTable(
// Use MyPaginatedTable here for pagination
columns: columns,
rows: rows,
columnSpacing: 15.0,
),
),
),
],
);
}
}