import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:marco/controller/my_controller.dart'; import 'package:marco/helpers/widgets/my_form_validator.dart'; import 'package:marco/helpers/services/api_service.dart'; import 'package:get/get.dart'; import 'package:logger/logger.dart'; import 'package:marco/helpers/widgets/my_snackbar.dart'; import 'package:marco/controller/task_planing/daily_task_planing_controller.dart'; final Logger logger = Logger(); enum ApiStatus { idle, loading, success, failure } final DailyTaskPlaningController taskController = Get.put(DailyTaskPlaningController()); class ReportTaskController extends MyController { List files = []; MyFormValidator basicValidator = MyFormValidator(); RxBool isLoading = false.obs; Rx reportStatus = ApiStatus.idle.obs; Rx commentStatus = ApiStatus.idle.obs; @override void onInit() { super.onInit(); logger.i("Initializing ReportTaskController..."); // Add form fields to the validator basicValidator.addField( 'assigned_date', label: "Assigned Date", controller: TextEditingController(), ); basicValidator.addField( 'work_area', label: "Work Area", controller: TextEditingController(), ); basicValidator.addField( 'activity', label: "Activity", controller: TextEditingController(), ); basicValidator.addField( 'team_size', label: "Team Size", controller: TextEditingController(), ); basicValidator.addField( 'task_id', label: "Task Id", controller: TextEditingController(), ); basicValidator.addField( 'assigned', label: "Assigned", controller: TextEditingController(), ); basicValidator.addField( 'completed_work', label: "Completed Work", required: true, controller: TextEditingController(), ); basicValidator.addField( 'comment', label: "Comment", required: true, controller: TextEditingController(), ); basicValidator.addField( 'assigned_by', label: "Assigned By", controller: TextEditingController(), ); basicValidator.addField( 'team_members', label: "Team Members", controller: TextEditingController(), ); basicValidator.addField( 'planned_work', label: "Planned Work", controller: TextEditingController(), ); logger.i( "Fields initialized for assigned_date, work_area, activity, team_size, assigned, completed_work, and comment."); } Future reportTask({ required String projectId, required String comment, required int completedTask, required List> checklist, required DateTime reportedDate, }) async { logger.i("Starting task report..."); final completedWork = basicValidator.getController('completed_work')?.text.trim(); if (completedWork == null || completedWork.isEmpty) { showAppSnackbar( title: "Error", message: "Completed work is required.", type: SnackbarType.error, ); return; } final completedWorkInt = int.tryParse(completedWork); if (completedWorkInt == null || completedWorkInt <= 0) { showAppSnackbar( title: "Error", message: "Completed work must be a positive integer.", type: SnackbarType.error, ); return; } final commentField = basicValidator.getController('comment')?.text.trim(); if (commentField == null || commentField.isEmpty) { showAppSnackbar( title: "Error", message: "Comment is required.", type: SnackbarType.error, ); return; } try { isLoading.value = true; final success = await ApiService.reportTask( id: projectId, comment: commentField, completedTask: completedTask, checkList: checklist, ); if (success) { showAppSnackbar( title: "Success", message: "Task reported successfully!", type: SnackbarType.success, ); } else { showAppSnackbar( title: "Error", message: "Failed to report task.", type: SnackbarType.error, ); } } catch (e) { logger.e("Error reporting task: $e"); showAppSnackbar( title: "Error", message: "An error occurred while reporting the task.", type: SnackbarType.error, ); } finally { isLoading.value = false; } } Future commentTask({ required String projectId, required String comment, }) async { logger.i("Starting task comment..."); final commentField = basicValidator.getController('comment')?.text.trim(); if (commentField == null || commentField.isEmpty) { showAppSnackbar( title: "Error", message: "Comment is required.", type: SnackbarType.error, ); return; } try { isLoading.value = true; final success = await ApiService.commentTask( id: projectId, comment: commentField, ); if (success) { showAppSnackbar( title: "Success", message: "Task commented successfully!", type: SnackbarType.success, ); await taskController.fetchTaskData(projectId); } else { showAppSnackbar( title: "Error", message: "Failed to comment task.", type: SnackbarType.error, ); } } catch (e) { logger.e("Error commenting task: $e"); showAppSnackbar( title: "Error", message: "An error occurred while commenting the task.", type: SnackbarType.error, ); } finally { isLoading.value = false; } } }