114 lines
3.1 KiB
Dart
114 lines
3.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:marco/helpers/services/api_service.dart';
|
|
import 'package:marco/helpers/widgets/my_snackbar.dart';
|
|
import 'package:marco/helpers/services/app_logger.dart';
|
|
|
|
class ProjectStatus {
|
|
final String id;
|
|
final String name;
|
|
|
|
ProjectStatus({required this.id, required this.name});
|
|
}
|
|
|
|
class CreateProjectController extends GetxController {
|
|
// Observables
|
|
var isSubmitting = false.obs;
|
|
var statusList = <ProjectStatus>[].obs;
|
|
ProjectStatus? selectedStatus;
|
|
|
|
/// Text controllers for form fields
|
|
final nameCtrl = TextEditingController();
|
|
final shortNameCtrl = TextEditingController();
|
|
final addressCtrl = TextEditingController();
|
|
final contactCtrl = TextEditingController();
|
|
final startDateCtrl = TextEditingController();
|
|
final endDateCtrl = TextEditingController();
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
loadHardcodedStatuses();
|
|
}
|
|
|
|
/// Hardcoded project statuses
|
|
void loadHardcodedStatuses() {
|
|
final List<ProjectStatus> statuses = [
|
|
ProjectStatus(
|
|
id: "b74da4c2-d07e-46f2-9919-e75e49b12731", name: "Active"),
|
|
ProjectStatus(
|
|
id: "cdad86aa-8a56-4ff4-b633-9c629057dfef", name: "In Progress"),
|
|
ProjectStatus(
|
|
id: "603e994b-a27f-4e5d-a251-f3d69b0498ba", name: "On Hold"),
|
|
ProjectStatus(
|
|
id: "ef1c356e-0fe0-42df-a5d3-8daee355492d", name: "In Active"),
|
|
ProjectStatus(
|
|
id: "33deaef9-9af1-4f2a-b443-681ea0d04f81", name: "Completed"),
|
|
];
|
|
statusList.assignAll(statuses);
|
|
}
|
|
|
|
/// Create project API call using ApiService
|
|
Future<bool> createProject({
|
|
required String name,
|
|
required String projectAddress,
|
|
required String shortName,
|
|
required String contactPerson,
|
|
required DateTime startDate,
|
|
required DateTime endDate,
|
|
required String projectStatusId,
|
|
}) async {
|
|
try {
|
|
isSubmitting.value = true;
|
|
|
|
final success = await ApiService.createProjectApi(
|
|
name: name,
|
|
projectAddress: projectAddress,
|
|
shortName: shortName,
|
|
contactPerson: contactPerson,
|
|
startDate: startDate,
|
|
endDate: endDate,
|
|
projectStatusId: projectStatusId,
|
|
);
|
|
|
|
if (success) {
|
|
showAppSnackbar(
|
|
title: "Success",
|
|
message: "Project created successfully",
|
|
type: SnackbarType.success,
|
|
);
|
|
} else {
|
|
showAppSnackbar(
|
|
title: "Error",
|
|
message: "Failed to create project",
|
|
type: SnackbarType.error,
|
|
);
|
|
}
|
|
|
|
return success;
|
|
} catch (e, stack) {
|
|
logSafe("Create project error: $e", level: LogLevel.error);
|
|
logSafe("Stacktrace: $stack", level: LogLevel.debug);
|
|
showAppSnackbar(
|
|
title: "Error",
|
|
message: "An unexpected error occurred",
|
|
type: SnackbarType.error,
|
|
);
|
|
return false;
|
|
} finally {
|
|
isSubmitting.value = false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
nameCtrl.dispose();
|
|
shortNameCtrl.dispose();
|
|
addressCtrl.dispose();
|
|
contactCtrl.dispose();
|
|
startDateCtrl.dispose();
|
|
endDateCtrl.dispose();
|
|
super.onClose();
|
|
}
|
|
}
|