199 lines
6.3 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart';
import 'package:marco/helpers/services/storage/local_storage.dart';
import 'package:intl/intl.dart';
class ApiService {
static const String baseUrl = "https://api.marcoaiot.com/api";
// Fetch the list of projects
static Future<List<dynamic>?> getProjects() async {
try {
String? jwtToken = LocalStorage.getJwtToken();
if (jwtToken == null) {
print("No JWT token found. Please log in.");
return null;
}
final response = await http.get(
Uri.parse("$baseUrl/project/list"),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $jwtToken', // Add Authorization header
},
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
print("Response body: ${response.body}");
if (json['success'] == true) {
return json['data']; // Return the data if success is true
} else {
print("Error: ${json['message']}");
}
} else {
print("Error fetching projects: ${response.statusCode}");
print("Response body: ${response.body}");
}
} catch (e) {
print("Exception while fetching projects: $e");
}
return null;
}
// Fetch employees by project ID
static Future<List<dynamic>?> getEmployeesByProject(int projectId) async {
try {
String? jwtToken = LocalStorage.getJwtToken();
if (jwtToken == null) {
print("No JWT token found. Please log in.");
return null;
}
final response = await http.get(
Uri.parse(
"$baseUrl/attendance/project/team?projectId=$projectId"), // Ensure correct endpoint
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $jwtToken',
},
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
print("Response body: ${response.body}");
if (json['success'] == true) {
return json['data']; // Return employee data
} else {
print("Error: ${json['message']}");
}
} else {
print("Error fetching employees: ${response.statusCode}");
print("Response body: ${response.body}");
}
} catch (e) {
print("Exception while fetching employees: $e");
}
return null;
}
static String generateImageName(int employeeId, int count) {
final now = DateTime.now();
final formattedDate = "${now.year.toString().padLeft(4, '0')}"
"${now.month.toString().padLeft(2, '0')}"
"${now.day.toString().padLeft(2, '0')}_"
"${now.hour.toString().padLeft(2, '0')}"
"${now.minute.toString().padLeft(2, '0')}"
"${now.second.toString().padLeft(2, '0')}";
final imageNumber = count.toString().padLeft(3, '0');
return "${employeeId}_${formattedDate}_$imageNumber.jpg";
}
static Future<bool> uploadAttendanceImage(
int employeeId, XFile imageFile, double latitude, double longitude,
{required String imageName,
required int projectId,
String comment = "",
int action = 0}) async {
try {
String? jwtToken = LocalStorage.getJwtToken();
if (jwtToken == null) {
print("No JWT token found. Please log in.");
return false;
}
final bytes = await imageFile.readAsBytes();
final base64Image = base64Encode(bytes);
final fileSize = await imageFile.length();
final contentType = "image/${imageFile.path.split('.').last}";
final imageObject = {
"FileName": imageName,
"Base64Data": base64Image,
"ContentType": contentType,
"FileSize": fileSize,
"Description": "Employee attendance photo"
};
final now = DateTime.now();
// You can now include the attendance record directly in the main body
final response = await http.post(
Uri.parse("$baseUrl/attendance/record"),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $jwtToken',
},
body: jsonEncode({
"ID": null,
"employeeId": employeeId,
"projectId": projectId,
"markTime": DateFormat('hh:mm a').format(now),
"comment": comment,
"action": action,
"date": DateFormat('yyyy-MM-dd').format(now),
"latitude": latitude,
"longitude": longitude,
"image": [imageObject], // Directly included in the body
}),
);
print('body: ${jsonEncode({
"employeeId": employeeId,
"projectId": projectId,
"markTime": DateFormat('hh:mm a').format(now),
"comment": comment,
"action": action,
"date": DateFormat('yyyy-MM-dd').format(now),
"latitude": latitude,
"longitude": longitude,
"image": [imageObject],
})}');
print('uploadAttendanceImage: $baseUrl/attendance/record');
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return json['success'] == true;
} else {
print("Error uploading image: ${response.statusCode}");
print("Response: ${response.body}");
}
} catch (e) {
print("Exception during image upload: $e");
}
return false;
}
static Future<List<dynamic>?> getAttendanceLogs(int projectId) async {
try {
String? jwtToken = LocalStorage.getJwtToken();
if (jwtToken == null) {
print("No JWT token found. Please log in.");
return null;
}
final response = await http.get(
Uri.parse(
"$baseUrl/attendance/project/team?projectId=$projectId"),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $jwtToken',
},
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
if (json['success'] == true) {
return json['data'];
} else {
print("Error: ${json['message']}");
}
} else {
print("Error fetching logs: ${response.statusCode}");
}
} catch (e) {
print("Exception while fetching logs: $e");
}
return null;
}
}