181 lines
5.0 KiB
Dart
181 lines
5.0 KiB
Dart
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';
|
|
|
|
final Logger logger = Logger();
|
|
enum ApiStatus { idle, loading, success, failure }
|
|
class ReportTaskController extends MyController {
|
|
List<PlatformFile> files = [];
|
|
MyFormValidator basicValidator = MyFormValidator();
|
|
RxBool isLoading = false.obs;
|
|
Rx<ApiStatus> reportStatus = ApiStatus.idle.obs;
|
|
Rx<ApiStatus> 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<void> reportTask({
|
|
required String projectId,
|
|
required String comment,
|
|
required int completedTask,
|
|
required List<Map<String, dynamic>> checklist,
|
|
required DateTime reportedDate,
|
|
}) async {
|
|
logger.i("Starting task report...");
|
|
|
|
final completedWork =
|
|
basicValidator.getController('completed_work')?.text.trim();
|
|
|
|
if (completedWork == null || completedWork.isEmpty) {
|
|
Get.snackbar("Error", "Completed work is required.");
|
|
return;
|
|
}
|
|
final completedWorkInt = int.tryParse(completedWork);
|
|
if (completedWorkInt == null || completedWorkInt <= 0) {
|
|
Get.snackbar("Error", "Completed work must be a positive integer.");
|
|
return;
|
|
}
|
|
final commentField = basicValidator.getController('comment')?.text.trim();
|
|
|
|
if (commentField == null || commentField.isEmpty) {
|
|
Get.snackbar("Error", "Comment is required.");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
isLoading.value = true;
|
|
|
|
final success = await ApiService.reportTask(
|
|
id: projectId,
|
|
comment: commentField,
|
|
completedTask: completedTask,
|
|
checkList: checklist,
|
|
);
|
|
|
|
if (success) {
|
|
Get.snackbar("Success", "Task reported successfully!");
|
|
} else {
|
|
Get.snackbar("Error", "Failed to report task.");
|
|
}
|
|
} catch (e) {
|
|
logger.e("Error reporting task: $e");
|
|
Get.snackbar("Error", "An error occurred while reporting the task.");
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
|
|
Future<void> commentTask({
|
|
required String projectId,
|
|
required String comment,
|
|
required int completedTask,
|
|
required List<Map<String, dynamic>> checklist,
|
|
required DateTime reportedDate,
|
|
}) async {
|
|
logger.i("Starting task report...");
|
|
|
|
final completedWork =
|
|
basicValidator.getController('completed_work')?.text.trim();
|
|
final commentField = basicValidator.getController('comment')?.text.trim();
|
|
|
|
if (completedWork == null || completedWork.isEmpty) {
|
|
Get.snackbar("Error", "Completed work is required.");
|
|
return;
|
|
}
|
|
|
|
if (commentField == null || commentField.isEmpty) {
|
|
Get.snackbar("Error", "Comment is required.");
|
|
return;
|
|
}
|
|
|
|
try {
|
|
isLoading.value = true;
|
|
|
|
final success = await ApiService.commentTask(
|
|
id: projectId,
|
|
comment: commentField,
|
|
);
|
|
|
|
if (success) {
|
|
Get.snackbar("Success", "Task commented successfully!");
|
|
} else {
|
|
Get.snackbar("Error", "Failed to comment task.");
|
|
}
|
|
} catch (e) {
|
|
logger.e("Error commenting task: $e");
|
|
Get.snackbar("Error", "An error occurred while commenting the task.");
|
|
} finally {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
}
|