import 'package:flutter/material.dart'; import 'package:marco/helpers/utils/attendance_actions.dart'; enum ButtonActions { approve, reject } class RegularizeActionButton extends StatefulWidget { final dynamic attendanceController; // Replace dynamic with your controller's type final dynamic log; // Replace dynamic with your log model type final String uniqueLogKey; final ButtonActions action; const RegularizeActionButton({ Key? key, required this.attendanceController, required this.log, required this.uniqueLogKey, required this.action, }) : super(key: key); @override State createState() => _RegularizeActionButtonState(); } class _RegularizeActionButtonState extends State { bool isUploading = false; static const Map _buttonTexts = { ButtonActions.approve: "Approve", ButtonActions.reject: "Reject", }; static const Map _buttonComments = { ButtonActions.approve: "Accepted", ButtonActions.reject: "Rejected", }; static const Map _buttonActionCodes = { ButtonActions.approve: 4, ButtonActions.reject: 5, }; Color get backgroundColor { // Use string keys for correct color lookup return AttendanceActionColors.colors[_buttonTexts[widget.action]!] ?? Colors.grey; } Future _handlePress() async { if (widget.attendanceController.selectedProjectId == null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text("Please select a project first")), ); return; } setState(() { isUploading = true; }); widget.attendanceController.uploadingStates[widget.uniqueLogKey]?.value = true; final success = await widget.attendanceController.captureAndUploadAttendance( widget.log.id, widget.log.employeeId, widget.attendanceController.selectedProjectId!, comment: _buttonComments[widget.action]!, action: _buttonActionCodes[widget.action]!, imageCapture: false, ); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(success ? '${_buttonTexts[widget.action]} marked successfully!' : 'Failed to mark ${_buttonTexts[widget.action]}.'), ), ); if (success) { widget.attendanceController.fetchEmployeesByProject(widget.attendanceController.selectedProjectId!); widget.attendanceController.fetchAttendanceLogs(widget.attendanceController.selectedProjectId!); await widget.attendanceController.fetchRegularizationLogs(widget.attendanceController.selectedProjectId!); await widget.attendanceController.fetchProjectData(widget.attendanceController.selectedProjectId!); } widget.attendanceController.uploadingStates[widget.uniqueLogKey]?.value = false; setState(() { isUploading = false; }); } @override Widget build(BuildContext context) { final buttonText = _buttonTexts[widget.action]!; return ConstrainedBox( constraints: const BoxConstraints(minWidth: 70, maxWidth: 120), child: SizedBox( height: 30, child: ElevatedButton( onPressed: isUploading ? null : _handlePress, style: ElevatedButton.styleFrom( backgroundColor: backgroundColor, foregroundColor: Colors.white, // Ensures visibility on all backgrounds padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 6), minimumSize: const Size(60, 20), textStyle: const TextStyle(fontSize: 12), ), child: isUploading ? const SizedBox( width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2), ) : FittedBox( fit: BoxFit.scaleDown, child: Text( buttonText, overflow: TextOverflow.ellipsis, ), ), ), ), ); } }