where('statusenabled', true) ->get(); return response()->json([ 'message' => 'Data kamar berhasil diambil', 'data' => $data ]); } catch (\Exception $e) { return response()->json([ 'message' => 'Gagal ambil data kamar', 'error' => $e->getMessage() ], 500); } } public function getDetailKamar($id) { try { $kamar = Ms_Kamar::with(['ruangan', 'kelas']) ->where('statusenabled', true) ->findOrFail($id); return response()->json([ 'message' => 'Data kamar ditemukan', 'data' => $kamar ]); } catch (\Exception $e) { return response()->json([ 'message' => 'Kamar tidak ditemukan', 'error' => $e->getMessage() ], 404); } } public function tambahKamar(Request $request) { DB::beginTransaction(); try { $validated = $request->validate([ 'nama_kamar' => 'required|string|max:100', 'ruangan_id' => 'required|numeric', 'kelas_id' => 'required|numeric', 'jumlah_tempat_tidur' => 'required|integer|min:0', ]); // Get last room entry and increment the ID for the next room $lastKamar = Ms_Kamar::orderBy('id', 'desc')->first(); $nextNumber = $lastKamar ? $lastKamar->id + 1 : 1; $kodeKamar = 'KMR-' . str_pad($nextNumber, 2, '0', STR_PAD_LEFT); $kamar = Ms_Kamar::create([ 'nama_kamar' => $validated['nama_kamar'], 'ruangan_id' => $validated['ruangan_id'], 'kelas_id' => $validated['kelas_id'], 'kode_kamar' => $kodeKamar, // Automatically generated kode kamar 'jumlah_tempat_tidur' => $validated['jumlah_tempat_tidur'], // Ensure jumlah_tempat_tidur is passed 'statusenabled' => true, ]); DB::commit(); return response()->json([ 'message' => 'Kamar berhasil ditambahkan', 'data' => $kamar ], 201); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'message' => 'Gagal tambah kamar', 'error' => $e->getMessage() ], 500); } } public function updateKamar(Request $request, $id) { DB::beginTransaction(); try { $validated = $request->validate([ 'nama_kamar' => 'required|string|max:100', 'ruangan_id' => 'required|numeric', 'kelas_id' => 'required|numeric', 'jumlah_tempat_tidur' => 'required|integer|min:0', ]); $kamar = Ms_Kamar::findOrFail($id); $kamar->update([ 'nama_kamar' => $validated['nama_kamar'], 'ruangan_id' => $validated['ruangan_id'], 'kelas_id' => $validated['kelas_id'], 'jumlah_tempat_tidur' => $validated['jumlah_tempat_tidur'], ]); DB::commit(); return response()->json([ 'message' => 'Kamar berhasil diperbarui', 'data' => $kamar ]); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'message' => 'Gagal update kamar', 'error' => $e->getMessage() ], 500); } } public function hapusKamar($id) { DB::beginTransaction(); try { $kamar = Ms_Kamar::findOrFail($id); $kamar->statusenabled = false; $kamar->save(); DB::commit(); return response()->json([ 'message' => 'Kamar berhasil dinonaktifkan', 'data' => $kamar ]); } catch (\Exception $e) { DB::rollBack(); return response()->json([ 'message' => 'Gagal hapus kamar', 'error' => $e->getMessage() ], 500); } } }