import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:ui_loker/Model/locker_model.dart'; import 'package:ui_loker/Model/penitipan_model.dart'; import 'package:intl/intl.dart'; class ApiService { static const String baseUrl = 'https://smartlocker.punyapadias.my.id/api'; /// Get All Lockers static Future> getAllLockers() async { final response = await http.get(Uri.parse('$baseUrl/lokers')); if (response.statusCode == 200) { final data = jsonDecode(response.body); return (data['data'] as List) .map((e) => LockerModel.fromJson(e)) .toList(); } else { throw Exception('Gagal mengambil data loker'); } } /// Add New Locker static Future> addLocker(int number) async { final res = await http.post( Uri.parse('$baseUrl/lokers/store'), headers: {'Content-Type': 'application/json'}, body: jsonEncode({ 'nomor_loker': 'Loker $number', 'status': 'servis', }), ); if (res.statusCode == 200) { return jsonDecode(res.body); } else { throw Exception('Gagal menambahkan loker'); } } /// Kirim Data Penitipan static Future kirimPenitipan({ required String user, required int lokerId, }) async { final res = await http.post( Uri.parse('$baseUrl/penitipan/store'), headers: {'Content-Type': 'application/json'}, body: jsonEncode({ 'nama': user, 'loker_id': lokerId, }), ); if (res.statusCode == 201) { final jsonRes = jsonDecode(res.body); return PenitipanModel.fromJson(jsonRes['data']); } else { throw Exception('Gagal menyimpan penitipan: ${res.body}'); } } /// Hapus Locker Berdasarkan ID static Future deleteLocker(int id) async { final res = await http.delete( Uri.parse('$baseUrl/lokers/$id'), headers: {'Content-Type': 'application/json'}, ); if (res.statusCode == 200) { return true; } else { throw Exception('Gagal menghapus loker'); } } static Future> getPenitipanByLoker(int lokerId) async { final response = await http.get( Uri.parse('$baseUrl/lokers/$lokerId/penitipan'), ); if (response.statusCode == 200) { final data = jsonDecode(response.body); return (data['data'] as List) .map((e) => PenitipanModel.fromJson(e)) .toList(); } else { throw Exception('Gagal mengambil riwayat penitipan'); } } static Future> getPenggunaLoker(int lokerId) async { final response = await http.get(Uri.parse('$baseUrl/penitipan/loker/$lokerId')); if (response.statusCode == 200) { final List jsonList = jsonDecode(response.body); return jsonList.map((json) => PenitipanModel.fromJson(json)).toList(); } else { throw Exception('Gagal memuat data pengguna'); } } static Future> etPenitipanByLokerg(int lokerId) async { final response = await http.get(Uri.parse('$baseUrl/penitipan/loker/$lokerId')); if (response.statusCode == 200) { List jsonList = jsonDecode(response.body); return jsonList.map((json) => PenitipanModel.fromJson(json)).toList(); } else { throw Exception('Gagal memuat data pengguna'); } } static Future updateLokerStatus(int lokerId, String newStatus) async { final url = Uri.parse('$baseUrl/lokers/$lokerId/status'); final response = await http.put( url, headers: {'Content-Type': 'application/json'}, body: jsonEncode({'status': newStatus}), ); if (response.statusCode == 200) { return true; } else { print('Gagal update status. Status code: ${response.statusCode}'); print('Response: ${response.body}'); throw Exception('Gagal update status loker'); } } Future getPenghasilanHariIni() async { final response = await http.get(Uri.parse("$baseUrl/today")); if (response.statusCode == 200) { final data = json.decode(response.body); return data['total_penghasilan'] ?? 0; } else { throw Exception("Gagal mengambil data hari ini"); } } Future getPenghasilanByTanggal(String tanggal) async { final response = await http.post( Uri.parse("$baseUrl/count"), headers: {"Content-Type": "application/json"}, body: json.encode({"tanggal": tanggal}), ); if (response.statusCode == 200) { final data = json.decode(response.body); return data['total_penghasilan'] ?? 0; } else { throw Exception("Gagal mengambil data berdasarkan tanggal"); } } }