update 15 mei
This commit is contained in:
parent
94daad0c1f
commit
4970220a24
@ -50,10 +50,16 @@ func main() {
|
||||
|
||||
// Bookings
|
||||
protected.POST("/bookings", controllers.CreateBooking)
|
||||
protected.GET("/bookings", controllers.GetUserBookings)
|
||||
protected.PATCH("/bookings/:id", controllers.UpdateBookingStatus)
|
||||
protected.GET("/bookings", controllers.GetAllBookings)
|
||||
protected.PUT("/bookings/:id/status", controllers.UpdateBookingStatus) // <-- Cukup tulis satu kali saja
|
||||
|
||||
// Admin (Manage Rooms)
|
||||
protected.PUT("/admin/rooms/:id/status", controllers.UpdateRoomStatus) // <-- Pastikan baris ini ada
|
||||
}
|
||||
|
||||
// 5. Jalur IoT ESP32 (Di luar protected)
|
||||
r.POST("/api/sensor/energy", controllers.UpdateRoomPower)
|
||||
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
||||
@ -70,4 +76,4 @@ func CORSMiddleware() gin.HandlerFunc {
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,37 +110,44 @@ type UpdateStatusInput struct {
|
||||
|
||||
// UPDATE STATUS (ADMIN)
|
||||
func UpdateBookingStatus(c *gin.Context) {
|
||||
bookingID := c.Param("id")
|
||||
bookingID := c.Param("id")
|
||||
var input UpdateStatusInput
|
||||
|
||||
var booking models.Booking
|
||||
if err := config.DB.First(&booking, "booking_id = ?", bookingID).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Booking tidak ditemukan"})
|
||||
return
|
||||
}
|
||||
// 1. Validasi Input JSON
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Format data tidak valid"})
|
||||
return
|
||||
}
|
||||
|
||||
var input UpdateStatusInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
var booking models.Booking
|
||||
// 2. Gunakan "id = ?" jika primary key di DB adalah 'id'
|
||||
// Jika di model kamu pakai 'RoomID', pastikan konsisten
|
||||
if err := config.DB.Where("id = ?", bookingID).First(&booking).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Data booking tidak ditemukan di database"})
|
||||
return
|
||||
}
|
||||
|
||||
booking.Status = input.Status
|
||||
// 3. Update Status
|
||||
booking.Status = input.Status
|
||||
|
||||
//REDEEM CODE
|
||||
if input.Status == "Approved" {
|
||||
if booking.RedeemCode == "" {
|
||||
booking.RedeemCode = helpers.GenerateRedeemCode()
|
||||
}
|
||||
} else if input.Status == "Rejected" || input.Status == "Cancelled" {
|
||||
booking.RedeemCode = ""
|
||||
}
|
||||
// REVISI CLEAN CODE: Menggunakan Switch
|
||||
switch input.Status {
|
||||
case "Approved":
|
||||
if booking.RedeemCode == "" {
|
||||
booking.RedeemCode = helpers.GenerateRedeemCode()
|
||||
}
|
||||
case "Rejected", "Cancelled":
|
||||
booking.RedeemCode = ""
|
||||
}
|
||||
if err := config.DB.Save(&booking).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Gagal menyimpan perubahan ke database"})
|
||||
return
|
||||
}
|
||||
|
||||
config.DB.Save(&booking)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Status booking berhasil diperbarui!",
|
||||
"data": booking,
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Status berhasil diperbarui!",
|
||||
"data": booking,
|
||||
})
|
||||
}
|
||||
|
||||
// INPUT UNTUK ESP3
|
||||
@ -174,3 +181,22 @@ func VerifyRedeemCode(c *gin.Context) {
|
||||
"status": "success",
|
||||
})
|
||||
}
|
||||
|
||||
// GET ALL BOOKINGS (Untuk Calendar View semua user)
|
||||
func GetAllBookings(c *gin.Context) {
|
||||
var bookings []models.Booking
|
||||
|
||||
// Preload digunakan untuk mengambil data Relasi (Nama Ruangan & Nama Peminjam)
|
||||
if err := config.DB.Preload("Room").Preload("User").Order("created_at desc").Find(&bookings).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// Perhatikan: responsenya menggunakan "data: bookings" agar cocok dengan Frontend React-mu
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": "success",
|
||||
"data": bookings,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -30,4 +30,69 @@ func CreateRoom(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Ruangan berhasil ditambahkan!", "data": input})
|
||||
}
|
||||
}
|
||||
|
||||
// --- STRUKTUR DATA UNTUK MENERIMA JSON ---
|
||||
|
||||
type UpdateRoomStatusInput struct {
|
||||
Status string `json:"status" binding:"required"`
|
||||
}
|
||||
|
||||
type EnergySensorInput struct {
|
||||
RoomID int `json:"room_id" binding:"required"`
|
||||
Power float64 `json:"power" binding:"required"`
|
||||
}
|
||||
|
||||
// --- FUNGSI HANDLER ---
|
||||
|
||||
// UPDATE ROOM STATUS (Admin: Available <-> Maintenance)
|
||||
func UpdateRoomStatus(c *gin.Context) {
|
||||
roomID := c.Param("id")
|
||||
var input UpdateRoomStatusInput
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var room models.Room
|
||||
// KUNCI PERBAIKANNYA: Cukup masukkan `roomID` langsung.
|
||||
// GORM akan otomatis mencari berdasarkan Primary Key tanpa peduli nama kolomnya.
|
||||
if err := config.DB.First(&room, roomID).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Ruangan tidak ditemukan di tabel"})
|
||||
return
|
||||
}
|
||||
|
||||
// Ubah status dan simpan
|
||||
room.Status = input.Status
|
||||
if err := config.DB.Save(&room).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Gagal menyimpan perubahan"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Status ruangan berhasil diperbarui",
|
||||
"data": room,
|
||||
})
|
||||
}
|
||||
|
||||
// UPDATE ROOM POWER (Menerima data Watt dari ESP32 / Tuya)
|
||||
func UpdateRoomPower(c *gin.Context) {
|
||||
var input EnergySensorInput
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Format data sensor salah"})
|
||||
return
|
||||
}
|
||||
|
||||
var room models.Room
|
||||
if err := config.DB.First(&room, "room_id = ?", input.RoomID).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Ruangan tidak ditemukan"})
|
||||
return
|
||||
}
|
||||
|
||||
// Pastikan kolom ini sesuai dengan nama di models.Room milikmu (misal: PowerConsumption)
|
||||
room.PowerConsumption = input.Power
|
||||
config.DB.Save(&room)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Data daya ESP32 berhasil disimpan"})
|
||||
}
|
||||
|
||||
@ -25,6 +25,7 @@ type Room struct {
|
||||
Capacity int `gorm:"not null" json:"capacity"`
|
||||
Floor string `gorm:"not null" json:"floor"`
|
||||
Status string `gorm:"type:room_status;default:'Available'" json:"status"`
|
||||
PowerConsumption float64 `json:"power_consumption" gorm:"default:0"`
|
||||
}
|
||||
|
||||
type Booking struct {
|
||||
|
||||
100
frontend/app/admin/approvals/page.tsx
Normal file
100
frontend/app/admin/approvals/page.tsx
Normal file
@ -0,0 +1,100 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { ShieldCheck, Check, X, Clock } from "lucide-react";
|
||||
|
||||
export default function ApprovalsPage() {
|
||||
const [activeTab, setActiveTab] = useState("Pending");
|
||||
const [bookings, setBookings] = useState<any[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
// Data Dummy Sementara
|
||||
setBookings([
|
||||
{ id: 1, user: { full_name: "Andreas Budi" }, room: { name: "Kelas D101" }, start_time: new Date().toISOString(), purpose: "Rapat Evaluasi BEM", status: "Pending" },
|
||||
{ id: 2, user: { full_name: "Siska Saraswati" }, room: { name: "Kelas D105" }, start_time: new Date().toISOString(), purpose: "Sidang Skripsi", status: "Pending" },
|
||||
{ id: 3, user: { full_name: "Bima Arya" }, room: { name: "Kelas D102" }, start_time: new Date().toISOString(), purpose: "Kelas Pengganti", status: "Approved" },
|
||||
{ id: 4, user: { full_name: "Citra Kirana" }, room: { name: "Kelas D104" }, start_time: new Date().toISOString(), purpose: "Latihan Band", status: "Rejected" },
|
||||
]);
|
||||
}, []);
|
||||
|
||||
const filteredBookings = bookings.filter(b => b.status === activeTab);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center gap-3 mb-6">
|
||||
<div className="bg-blue-100 p-3 rounded-lg text-blue-600">
|
||||
<ShieldCheck size={28} />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-800">Manajemen Persetujuan</h2>
|
||||
<p className="text-gray-500 text-sm mt-1">Kelola permohonan peminjaman ruangan S-CLASS.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* TABS */}
|
||||
<div className="flex gap-4 border-b border-gray-200">
|
||||
{["Pending", "Approved", "Rejected"].map((tab) => (
|
||||
<button
|
||||
key={tab}
|
||||
onClick={() => setActiveTab(tab)}
|
||||
className={`pb-3 px-4 font-bold text-sm transition-all ${
|
||||
activeTab === tab
|
||||
? "border-b-2 border-blue-600 text-blue-600"
|
||||
: "text-gray-400 hover:text-gray-600"
|
||||
}`}
|
||||
>
|
||||
{tab}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* TABEL */}
|
||||
<div className="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left">
|
||||
<thead className="bg-gray-50 text-gray-500 text-xs font-bold uppercase">
|
||||
<tr>
|
||||
<th className="p-4">Peminjam</th>
|
||||
<th className="p-4">Ruangan</th>
|
||||
<th className="p-4">Waktu Mulai</th>
|
||||
<th className="p-4">Keperluan</th>
|
||||
<th className="p-4 text-center">Status / Aksi</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-50">
|
||||
{filteredBookings.map((b) => (
|
||||
<tr key={b.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<td className="p-4 font-bold text-gray-800 text-sm">{b.user?.full_name}</td>
|
||||
<td className="p-4 text-sm font-medium text-gray-600">{b.room?.name}</td>
|
||||
<td className="p-4 text-sm text-gray-600">{new Date(b.start_time).toLocaleString('id-ID')}</td>
|
||||
<td className="p-4 text-sm text-gray-600">{b.purpose}</td>
|
||||
<td className="p-4 text-center">
|
||||
{b.status === "Pending" ? (
|
||||
<div className="flex justify-center gap-2">
|
||||
<button className="flex items-center gap-1 px-3 py-1.5 bg-green-50 text-green-600 font-bold text-xs rounded-lg hover:bg-green-100 transition-colors">
|
||||
<Check size={14} /> Setujui
|
||||
</button>
|
||||
<button className="flex items-center gap-1 px-3 py-1.5 bg-red-50 text-red-600 font-bold text-xs rounded-lg hover:bg-red-100 transition-colors">
|
||||
<X size={14} /> Tolak
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<span className={`px-3 py-1 rounded-full text-xs font-bold ${
|
||||
b.status === 'Approved' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'
|
||||
}`}>
|
||||
{b.status}
|
||||
</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{filteredBookings.length === 0 && (
|
||||
<tr><td colSpan={5} className="p-8 text-center text-gray-400 font-medium">Tidak ada data di kategori ini.</td></tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
110
frontend/app/admin/layout.tsx
Normal file
110
frontend/app/admin/layout.tsx
Normal file
@ -0,0 +1,110 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { useRouter, usePathname } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
LogOut,
|
||||
LayoutDashboard,
|
||||
Activity,
|
||||
ShieldCheck,
|
||||
Settings2
|
||||
} from "lucide-react";
|
||||
|
||||
export default function AdminLayout({ children }: { children: React.ReactNode }) {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const [user, setUser] = useState<any>(null);
|
||||
|
||||
// State isSidebarOpen sudah kita buang sepenuhnya
|
||||
|
||||
useEffect(() => {
|
||||
const userData = localStorage.getItem("user");
|
||||
if (userData) {
|
||||
const parsed = JSON.parse(userData);
|
||||
if (parsed.role !== 'admin') {
|
||||
router.push("/dashboard");
|
||||
} else {
|
||||
setUser(parsed);
|
||||
}
|
||||
} else {
|
||||
router.push("/login");
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.clear();
|
||||
router.push("/login");
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-white flex flex-col font-sans">
|
||||
|
||||
{/* HEADER ADMIN */}
|
||||
<header className="bg-[#e5e7eb] flex items-center justify-between px-6 py-4 shadow-sm z-20 relative">
|
||||
<div>
|
||||
<h2 className="text-blue-600 text-sm font-medium mb-1">Admin Panel,</h2>
|
||||
<h1 className="text-blue-600 text-3xl font-bold">S-CLASS</h1>
|
||||
</div>
|
||||
<div className="flex items-center gap-8">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="bg-slate-800 text-white rounded-full h-10 w-10 flex items-center justify-center text-lg font-bold shadow-sm">
|
||||
{user?.full_name ? user.full_name.charAt(0).toUpperCase() : "A"}
|
||||
</div>
|
||||
<div className="hidden sm:flex flex-col">
|
||||
<span className="font-semibold text-gray-800 text-sm">{user?.full_name || "Administrator"}</span>
|
||||
<span className="font-medium text-blue-600 text-xs uppercase tracking-wider">System Controller</span>
|
||||
</div>
|
||||
</div>
|
||||
<button onClick={handleLogout} className="flex items-center gap-2 text-red-500 font-medium hover:text-red-600 transition-colors">
|
||||
Log Out <LogOut size={20} />
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div className="flex flex-1 overflow-hidden relative">
|
||||
|
||||
{/* SIDEBAR ADMIN (Lebar Permanen: w-64) */}
|
||||
<aside className="bg-[#b0c4d9] w-64 flex flex-col py-10 shadow-inner z-10">
|
||||
<nav className="flex flex-col gap-6 px-4">
|
||||
|
||||
<Link href="/admin" className={`flex items-center gap-4 font-semibold transition-colors group ${pathname === '/admin' ? 'text-blue-700' : 'text-gray-800 hover:text-blue-700'}`}>
|
||||
<div className={`p-2 rounded-lg transition-colors ${pathname === '/admin' ? 'bg-white/60 shadow-sm' : 'bg-white/30 group-hover:bg-white/50'}`}>
|
||||
<LayoutDashboard size={22} />
|
||||
</div>
|
||||
<span>Dashboard</span>
|
||||
</Link>
|
||||
|
||||
<Link href="/admin/monitoring" className={`flex items-center gap-4 font-semibold transition-colors group ${pathname === '/admin/monitoring' ? 'text-blue-700' : 'text-gray-800 hover:text-blue-700'}`}>
|
||||
<div className={`p-2 rounded-lg transition-colors ${pathname === '/admin/monitoring' ? 'bg-white/60 shadow-sm' : 'bg-white/30 group-hover:bg-white/50'}`}>
|
||||
<Activity size={22} />
|
||||
</div>
|
||||
<span>Power Monitoring</span>
|
||||
</Link>
|
||||
|
||||
<Link href="/admin/approvals" className={`flex items-center gap-4 font-semibold transition-colors group ${pathname === '/admin/approvals' ? 'text-blue-700' : 'text-gray-800 hover:text-blue-700'}`}>
|
||||
<div className={`p-2 rounded-lg transition-colors ${pathname === '/admin/approvals' ? 'bg-white/60 shadow-sm' : 'bg-white/30 group-hover:bg-white/50'}`}>
|
||||
<ShieldCheck size={22} />
|
||||
</div>
|
||||
<span>Approvals</span>
|
||||
</Link>
|
||||
|
||||
<Link href="/admin/rooms" className={`flex items-center gap-4 font-semibold transition-colors group ${pathname === '/admin/rooms' ? 'text-blue-700' : 'text-gray-800 hover:text-blue-700'}`}>
|
||||
<div className={`p-2 rounded-lg transition-colors ${pathname === '/admin/rooms' ? 'bg-white/60 shadow-sm' : 'bg-white/30 group-hover:bg-white/50'}`}>
|
||||
<Settings2 size={22} />
|
||||
</div>
|
||||
<span>Manage Rooms</span>
|
||||
</Link>
|
||||
|
||||
</nav>
|
||||
</aside>
|
||||
|
||||
{/* KONTEN UTAMA */}
|
||||
<main className="flex-1 overflow-y-auto bg-[#f8fafc] p-8">
|
||||
{children}
|
||||
</main>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
84
frontend/app/admin/monitoring/page.tsx
Normal file
84
frontend/app/admin/monitoring/page.tsx
Normal file
@ -0,0 +1,84 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { Activity, Power, ZapOff, AlertTriangle } from "lucide-react";
|
||||
|
||||
export default function PowerMonitoringPage() {
|
||||
const [rooms, setRooms] = useState<any[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
// Simulasi Data Ruangan dari IoT
|
||||
const dummyRooms = [
|
||||
{ id: 1, name: "Kelas D101", power: 1250, isRelayOn: true, lastUpdate: "Baru saja" },
|
||||
{ id: 2, name: "Kelas D102", power: 0, isRelayOn: false, lastUpdate: "2 mnt lalu" },
|
||||
{ id: 3, name: "Kelas D103", power: 45, isRelayOn: true, lastUpdate: "Baru saja" },
|
||||
{ id: 4, name: "Kelas D104", power: 0, isRelayOn: false, lastUpdate: "10 mnt lalu" },
|
||||
];
|
||||
setRooms(dummyRooms);
|
||||
}, []);
|
||||
|
||||
const handleCutOff = (roomName: string) => {
|
||||
if(confirm(`PERINGATAN: Anda yakin ingin mematikan daya secara paksa di ${roomName}?`)) {
|
||||
alert(`Sinyal pemutusan daya dikirim ke Relay ${roomName}.`);
|
||||
// Nanti di sini kamu pasang axios.post ke Golang -> MQTT -> ESP32
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center gap-3 mb-6">
|
||||
<div className="bg-blue-100 p-3 rounded-lg text-blue-600">
|
||||
<Activity size={28} />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-800">Power Monitoring & Control</h2>
|
||||
<p className="text-gray-500 text-sm mt-1">Pantau konsumsi daya kWh meter dan kendalikan *relay* sirkuit ruangan.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-6">
|
||||
{rooms.map((room) => (
|
||||
<div key={room.id} className="bg-white p-6 rounded-xl border border-gray-100 shadow-sm relative overflow-hidden">
|
||||
|
||||
{/* Indikator Status di Pojok Kanan Atas */}
|
||||
<div className={`absolute top-0 right-0 px-4 py-1.5 text-[10px] font-black uppercase tracking-wider rounded-bl-xl
|
||||
${room.isRelayOn ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'}`}
|
||||
>
|
||||
{room.isRelayOn ? 'Sirkuit Aktif' : 'Sirkuit Terputus'}
|
||||
</div>
|
||||
|
||||
<h3 className="text-lg font-bold text-gray-800 mb-4">{room.name}</h3>
|
||||
|
||||
<div className="flex items-end gap-2 mb-6">
|
||||
<span className={`text-5xl font-black tracking-tight ${room.power > 1000 ? 'text-orange-500' : 'text-gray-800'}`}>
|
||||
{room.power}
|
||||
</span>
|
||||
<span className="text-gray-500 font-bold mb-1.5">Watts</span>
|
||||
</div>
|
||||
|
||||
{room.power > 1000 && (
|
||||
<div className="flex items-center gap-2 text-xs font-bold text-orange-600 bg-orange-50 p-2 rounded-lg mb-4">
|
||||
<AlertTriangle size={14} /> Beban Tinggi Terdeteksi!
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-between mt-auto pt-4 border-t border-gray-100">
|
||||
<span className="text-xs text-gray-400 font-medium">Update: {room.lastUpdate}</span>
|
||||
|
||||
<button
|
||||
onClick={() => handleCutOff(room.name)}
|
||||
disabled={!room.isRelayOn}
|
||||
className={`flex items-center gap-2 px-4 py-2 rounded-lg text-xs font-bold transition-all
|
||||
${room.isRelayOn
|
||||
? 'bg-red-50 text-red-600 hover:bg-red-600 hover:text-white border border-red-200 hover:border-red-600'
|
||||
: 'bg-gray-100 text-gray-400 cursor-not-allowed'}`}
|
||||
>
|
||||
{room.isRelayOn ? <><Power size={14} /> Cut Off Power</> : <><ZapOff size={14} /> Offline</>}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -2,153 +2,143 @@
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import axios from "axios";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { LogOut, CheckCircle, XCircle, Clock } from "lucide-react";
|
||||
import { Zap, Check, X, Info, Activity } from "lucide-react";
|
||||
|
||||
export default function AdminDashboard() {
|
||||
const router = useRouter();
|
||||
const [admin, setAdmin] = useState<any>(null);
|
||||
const [bookings, setBookings] = useState<any[]>([]);
|
||||
const [pendingBookings, setPendingBookings] = useState<any[]>([]);
|
||||
const [roomStats, setRoomStats] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem("token");
|
||||
const userData = localStorage.getItem("user");
|
||||
|
||||
if (!token || !userData) {
|
||||
router.push("/login");
|
||||
return;
|
||||
}
|
||||
|
||||
const userObj = JSON.parse(userData);
|
||||
// Tendang kalau bukan admin yang masuk
|
||||
if (userObj.role !== "admin") {
|
||||
router.push("/dashboard");
|
||||
return;
|
||||
}
|
||||
|
||||
setAdmin(userObj);
|
||||
fetchBookings(token);
|
||||
fetchAdminData();
|
||||
// Refresh data konsumsi daya setiap 10 detik agar terlihat real-time
|
||||
const interval = setInterval(fetchAdminData, 10000);
|
||||
return () => clearInterval(interval);
|
||||
}, []);
|
||||
|
||||
const fetchBookings = async (token: string) => {
|
||||
const fetchAdminData = async () => {
|
||||
try {
|
||||
const response = await axios.get("http://localhost:8080/api/bookings", {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
// 1. Ambil SEMUA booking pakai endpoint yang sudah ada, lalu filter di Frontend
|
||||
const bookRes = await axios.get("http://localhost:8080/api/bookings", {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
});
|
||||
setBookings(response.data.data || []);
|
||||
} catch (error) {
|
||||
console.error("Gagal ambil data booking:", error);
|
||||
|
||||
const allBookings = bookRes.data.data || [];
|
||||
// Saring hanya yang statusnya "Pending" atau kosong
|
||||
const pendingOnly = allBookings.filter((b: any) => b.status === "Pending" || !b.status);
|
||||
setPendingBookings(pendingOnly);
|
||||
|
||||
// 2. Ambil daftar ruangan biasa, lalu kita sisipkan "Dummy Power" untuk UI
|
||||
const roomRes = await axios.get("http://localhost:8080/api/rooms", {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
});
|
||||
|
||||
const roomsWithDummyPower = (roomRes.data.data || []).map((room: any) => ({
|
||||
...room,
|
||||
// Simulasi daya acak antara 0 sampai 15 Watt untuk keperluan UI
|
||||
power_consumption: Math.floor(Math.random() * 15)
|
||||
}));
|
||||
setRoomStats(roomsWithDummyPower);
|
||||
|
||||
} catch (err) {
|
||||
console.error("Gagal ambil data admin", err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleUpdateStatus = async (bookingId: string, newStatus: string) => {
|
||||
const token = localStorage.getItem("token");
|
||||
const handleAction = async (id: number, status: string) => {
|
||||
try {
|
||||
await axios.patch(
|
||||
`http://localhost:8080/api/bookings/${bookingId}`,
|
||||
{ status: newStatus },
|
||||
const token = localStorage.getItem("token");
|
||||
// Mengirim status 'Approved' atau 'Rejected' ke backend
|
||||
await axios.put(`http://localhost:8080/api/bookings/${id}/status`,
|
||||
{ status: status },
|
||||
{ headers: { Authorization: `Bearer ${token}` } }
|
||||
);
|
||||
alert(`Booking berhasil di-${newStatus}!`);
|
||||
fetchBookings(token as string); // Refresh data setelah update
|
||||
} catch (error) {
|
||||
alert("Gagal mengubah status booking.");
|
||||
console.error(error);
|
||||
alert(`Permintaan berhasil di-${status}`);
|
||||
fetchAdminData(); // Refresh data setelah aksi
|
||||
} catch (err) {
|
||||
alert("Gagal memproses pendaftaran.");
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem("token");
|
||||
localStorage.removeItem("user");
|
||||
router.push("/login");
|
||||
};
|
||||
|
||||
if (loading) return <div className="p-10 text-center">Memuat panel admin...</div>;
|
||||
if (loading) return <div className="p-10 font-medium text-gray-500">Memasuki Mode Admin...</div>;
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-100">
|
||||
{/* Navbar Admin */}
|
||||
<nav className="bg-slate-800 shadow-sm px-6 py-4 flex justify-between items-center text-white sticky top-0 z-10">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-blue-400">S-CLASS | ADMIN PANEL</h1>
|
||||
<p className="text-xs text-gray-300">Welcome, {admin?.full_name}</p>
|
||||
<div className="space-y-8">
|
||||
{/* SECTION 1: POWER MONITORING CARDS */}
|
||||
<div>
|
||||
<h2 className="text-xl font-bold text-gray-800 mb-4 flex items-center gap-2">
|
||||
<Activity className="text-blue-500" /> Real-time Room Energy
|
||||
</h2>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
|
||||
{roomStats.map((room) => (
|
||||
<div key={room.id} className="bg-white p-5 rounded-xl border border-gray-100 shadow-sm">
|
||||
<div className="flex justify-between items-start mb-3">
|
||||
<span className="text-sm font-bold text-gray-400 uppercase tracking-tight">{room.name}</span>
|
||||
<div className={`h-3 w-3 rounded-full ${room.power_consumption > 5 ? 'bg-orange-500 animate-pulse' : 'bg-gray-300'}`} />
|
||||
</div>
|
||||
<div className="flex items-end gap-2">
|
||||
<span className="text-3xl font-black text-gray-800">{room.power_consumption || 0}</span>
|
||||
<span className="text-gray-500 font-bold mb-1">Watts</span>
|
||||
</div>
|
||||
<p className="text-[10px] text-gray-400 mt-2 font-medium">Last updated: Just now</p>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="flex items-center gap-2 text-sm bg-red-500 hover:bg-red-600 px-4 py-2 rounded-lg transition"
|
||||
>
|
||||
<LogOut size={16} /> Keluar
|
||||
</button>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
{/* Konten Utama */}
|
||||
<main className="max-w-6xl mx-auto p-6">
|
||||
<h2 className="text-2xl font-bold text-gray-800 mb-6">Manajemen Peminjaman Ruangan</h2>
|
||||
|
||||
<div className="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
|
||||
<table className="w-full text-left border-collapse">
|
||||
<thead>
|
||||
<tr className="bg-gray-50 text-gray-600 text-sm border-b">
|
||||
<th className="p-4">Ruangan</th>
|
||||
{/* SECTION 2: APPROVAL TABLE */}
|
||||
<div className="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
|
||||
<div className="p-6 border-b border-gray-50 flex justify-between items-center">
|
||||
<h2 className="text-xl font-bold text-gray-800">Pending Approvals</h2>
|
||||
<span className="bg-blue-100 text-blue-700 px-3 py-1 rounded-full text-xs font-bold">
|
||||
{pendingBookings.length} Requests
|
||||
</span>
|
||||
</div>
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left">
|
||||
<thead className="bg-gray-50 text-gray-500 text-xs font-bold uppercase">
|
||||
<tr>
|
||||
<th className="p-4">Peminjam</th>
|
||||
<th className="p-4">Ruangan</th>
|
||||
<th className="p-4">Waktu</th>
|
||||
<th className="p-4">Keperluan</th>
|
||||
<th className="p-4">Status</th>
|
||||
<th className="p-4 text-center">Aksi</th>
|
||||
<th className="p-4 text-center">Tindakan</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{bookings.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={6} className="text-center p-8 text-gray-500">Belum ada pengajuan peminjaman.</td>
|
||||
<tbody className="divide-y divide-gray-50">
|
||||
{pendingBookings.map((b) => (
|
||||
<tr key={b.id} className="hover:bg-gray-50/50 transition-colors">
|
||||
<td className="p-4 font-bold text-gray-800 text-sm">{b.user?.full_name}</td>
|
||||
<td className="p-4 text-sm font-medium text-gray-600">{b.room?.name}</td>
|
||||
<td className="p-4 text-xs text-gray-500 leading-relaxed">
|
||||
{new Date(b.start_time).toLocaleString('id-ID')}
|
||||
</td>
|
||||
<td className="p-4 text-sm text-gray-600 truncate max-w-37.5">{b.purpose}</td>
|
||||
<td className="p-4">
|
||||
<div className="flex justify-center gap-2">
|
||||
<button onClick={() => handleAction(b.id, 'Approved')} className="p-2 bg-green-50 text-green-600 rounded-lg hover:bg-green-100 transition-colors">
|
||||
<Check size={18} />
|
||||
</button>
|
||||
<button onClick={() => handleAction(b.id, 'Rejected')} className="p-2 bg-red-50 text-red-600 rounded-lg hover:bg-red-100 transition-colors">
|
||||
<X size={18} />
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{pendingBookings.length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={5} className="p-10 text-center text-gray-400 text-sm italic">Tidak ada permintaan menunggu.</td>
|
||||
</tr>
|
||||
) : (
|
||||
bookings.map((b) => (
|
||||
<tr key={b.booking_id} className="border-b hover:bg-gray-50">
|
||||
<td className="p-4 font-semibold text-gray-800">{b.room?.name}</td>
|
||||
<td className="p-4 text-sm text-gray-600">ID User: {b.user_id.substring(0,8)}...</td>
|
||||
<td className="p-4 text-sm text-gray-600">
|
||||
<div>Mulai: {new Date(b.start_time).toLocaleString('id-ID')}</div>
|
||||
<div>Selesai: {new Date(b.end_time).toLocaleString('id-ID')}</div>
|
||||
</td>
|
||||
<td className="p-4 text-sm text-gray-600">{b.purpose}</td>
|
||||
<td className="p-4">
|
||||
<span className={`px-2 py-1 text-xs font-bold rounded-full ${
|
||||
b.status === 'Approved' ? 'bg-green-100 text-green-700' :
|
||||
b.status === 'Rejected' ? 'bg-red-100 text-red-700' :
|
||||
'bg-yellow-100 text-yellow-700'
|
||||
}`}>
|
||||
{b.status}
|
||||
</span>
|
||||
</td>
|
||||
<td className="p-4">
|
||||
{b.status === 'Pending' ? (
|
||||
<div className="flex justify-center gap-2">
|
||||
<button
|
||||
onClick={() => handleUpdateStatus(b.booking_id, 'Approved')}
|
||||
className="bg-green-500 hover:bg-green-600 text-white p-2 rounded-lg" title="Setujui">
|
||||
<CheckCircle size={18} />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleUpdateStatus(b.booking_id, 'Rejected')}
|
||||
className="bg-red-500 hover:bg-red-600 text-white p-2 rounded-lg" title="Tolak">
|
||||
<XCircle size={18} />
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-center text-gray-400 text-xs">- Selesai -</div>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
103
frontend/app/admin/rooms/page.tsx
Normal file
103
frontend/app/admin/rooms/page.tsx
Normal file
@ -0,0 +1,103 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import axios from "axios";
|
||||
import { Settings2, RefreshCcw, AlertCircle, CheckCircle2 } from "lucide-react";
|
||||
|
||||
export default function ManageRoomsPage() {
|
||||
const [rooms, setRooms] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
const fetchRooms = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
const res = await axios.get("http://localhost:8080/api/rooms", {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
});
|
||||
setRooms(res.data.data);
|
||||
} catch (err: any) {
|
||||
console.error("Error Detail:", err.response?.data || err.message);
|
||||
alert("Error dari Server saat ambil ruangan: " + (err.response?.data?.error || err.message));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchRooms();
|
||||
}, []);
|
||||
|
||||
// FUNGSI UTAMA: Mengubah status ruangan (Available <-> Maintenance)
|
||||
const toggleRoomStatus = async (roomId: number, currentStatus: string) => {
|
||||
const newStatus = currentStatus === "Available" ? "Maintenance" : "Available";
|
||||
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
await axios.put(`http://localhost:8080/api/admin/rooms/${roomId}/status`,
|
||||
{ status: newStatus },
|
||||
{ headers: { Authorization: `Bearer ${token}` } }
|
||||
);
|
||||
|
||||
alert(`Status Ruangan berhasil diubah menjadi ${newStatus}`);
|
||||
fetchRooms(); // Refresh data agar UI terupdate
|
||||
} catch (err: any) {
|
||||
console.error("Gagal update:", err.response?.data || err.message);
|
||||
alert("Gagal memperbarui status ruangan: " + (err.response?.data?.error || err.message));
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) return <div className="p-8 text-gray-500 font-medium text-center">Menghubungkan ke Database S-CLASS...</div>;
|
||||
|
||||
return (
|
||||
<div className="max-w-5xl mx-auto space-y-6">
|
||||
<div className="flex items-center justify-between mb-8">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="bg-blue-100 p-3 rounded-lg text-blue-600">
|
||||
<Settings2 size={28} />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-800">Manajemen Ruangan</h2>
|
||||
<p className="text-gray-500 text-sm mt-1">Atur ketersediaan operasional setiap ruangan di Gedung D.</p>
|
||||
</div>
|
||||
</div>
|
||||
<button onClick={fetchRooms} className="p-2 text-gray-400 hover:text-blue-600 transition-colors">
|
||||
<RefreshCcw size={20} />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
{rooms.map((room) => (
|
||||
<div key={room.room_id} className={`bg-white p-6 rounded-2xl border transition-all shadow-sm flex items-center justify-between
|
||||
${room.status === 'Maintenance' ? 'border-orange-200 bg-orange-50/20' : 'border-gray-100'}`}>
|
||||
|
||||
<div className="space-y-1">
|
||||
<div className="flex items-center gap-2 text-gray-400 text-xs font-bold uppercase tracking-wider">
|
||||
{room.category} • {room.floor}
|
||||
</div>
|
||||
<h3 className="text-xl font-bold text-gray-800">{room.name}</h3>
|
||||
|
||||
<div className={`flex items-center gap-1.5 text-sm font-bold mt-2
|
||||
${room.status === 'Available' ? 'text-green-600' : 'text-orange-600'}`}>
|
||||
{room.status === 'Available' ? <CheckCircle2 size={16} /> : <AlertCircle size={16} />}
|
||||
{room.status}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-col items-end gap-3">
|
||||
<button
|
||||
onClick={() => toggleRoomStatus(room.room_id, room.status)}
|
||||
className={`px-4 py-2 rounded-xl text-xs font-bold transition-all shadow-sm
|
||||
${room.status === 'Available'
|
||||
? 'bg-orange-100 text-orange-700 hover:bg-orange-600 hover:text-white'
|
||||
: 'bg-green-100 text-green-700 hover:bg-green-600 hover:text-white'}`}
|
||||
>
|
||||
Set to {room.status === 'Available' ? 'Maintenance' : 'Available'}
|
||||
</button>
|
||||
<span className="text-[10px] text-gray-400 font-medium italic">ID: {room.room_id}</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,245 +1,149 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import axios from "axios";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Calendar, Clock, Save, X, AlertTriangle } from "lucide-react";
|
||||
import { CalendarPlus, Clock, FileText, MapPin, CheckCircle2 } from "lucide-react";
|
||||
|
||||
export default function AddBookingPage() {
|
||||
const router = useRouter();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [rooms, setRooms] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [submitLoading, setSubmitLoading] = useState(false);
|
||||
|
||||
// State untuk form input
|
||||
const [formData, setFormData] = useState({
|
||||
room: "Ruang T-301",
|
||||
date: "",
|
||||
startTime: "",
|
||||
endTime: "",
|
||||
purpose: "",
|
||||
});
|
||||
// State Form
|
||||
const [selectedRoomId, setSelectedRoomId] = useState("");
|
||||
const [startTime, setStartTime] = useState("");
|
||||
const [endTime, setEndTime] = useState("");
|
||||
const [purpose, setPurpose] = useState("");
|
||||
|
||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>) => {
|
||||
setFormData({ ...formData, [e.target.name]: e.target.value });
|
||||
};
|
||||
|
||||
// --- LOGIKA VALIDASI (Business Logic) ---
|
||||
const validateBookingRules = () => {
|
||||
// 1. Cek kelengkapan data dasar
|
||||
if (!formData.date || !formData.startTime || !formData.endTime) {
|
||||
alert("Mohon lengkapi Tanggal dan Jam terlebih dahulu.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. Ambil Jam Mulai
|
||||
const startHour = parseInt(formData.startTime.split(":")[0]);
|
||||
|
||||
// 3. Cek Jam Khusus (Diatas 21:00 ATAU Sebelum 06:00)
|
||||
const isSpecialTime = startHour >= 21 || startHour < 6;
|
||||
|
||||
if (isSpecialTime) {
|
||||
const bookingDate = new Date(formData.date);
|
||||
const today = new Date();
|
||||
|
||||
// Reset jam agar perhitungan murni berdasarkan tanggal
|
||||
bookingDate.setHours(0, 0, 0, 0);
|
||||
today.setHours(0, 0, 0, 0);
|
||||
|
||||
// Hitung selisih hari
|
||||
const diffTime = bookingDate.getTime() - today.getTime();
|
||||
const diffDays = diffTime / (1000 * 3600 * 24);
|
||||
|
||||
// ATURAN: Harus H-3
|
||||
if (diffDays < 3) {
|
||||
alert(
|
||||
`Gagal! Peminjaman di jam khusus (${formData.startTime}) harus dilakukan minimal 3 hari sebelumnya.\n\n` +
|
||||
`Jarak peminjaman Anda: ${diffDays} hari dari sekarang.`
|
||||
);
|
||||
return false;
|
||||
// Ambil daftar ruangan untuk Dropdown
|
||||
useEffect(() => {
|
||||
const fetchRooms = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
const res = await axios.get("http://localhost:8080/api/rooms", {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
setRooms(res.data.data);
|
||||
} catch (error) {
|
||||
console.error("Gagal memuat ruangan", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
fetchRooms();
|
||||
}, []);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!selectedRoomId) return alert("Pilih ruangan terlebih dahulu!");
|
||||
|
||||
// Jalankan validasi
|
||||
if (!validateBookingRules()) return;
|
||||
setSubmitLoading(true);
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const startISO = new Date(startTime).toISOString();
|
||||
const endISO = new Date(endTime).toISOString();
|
||||
|
||||
// --- MOCK API CALL ---
|
||||
console.log("Mengirim Data:", formData);
|
||||
await axios.post("http://localhost:8080/api/bookings", {
|
||||
room_id: parseInt(selectedRoomId),
|
||||
start_time: startISO,
|
||||
end_time: endISO,
|
||||
purpose: purpose,
|
||||
}, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
alert("Berhasil! Jadwal telah disimpan.");
|
||||
setIsLoading(false);
|
||||
// Redirect kembali ke halaman List Booking
|
||||
router.push("/dashboard/bookings");
|
||||
}, 1500);
|
||||
alert("Booking Berhasil Diajukan!");
|
||||
router.push("/dashboard/bookings/calendar"); // Arahkan ke jadwal setelah sukses
|
||||
|
||||
} catch (error: any) {
|
||||
alert("GAGAL: " + (error.response?.data?.error || "Terjadi kesalahan"));
|
||||
} finally {
|
||||
setSubmitLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) return <div className="p-8 text-gray-500 font-medium">Memuat formulir...</div>;
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-270">
|
||||
{/* Header Halaman */}
|
||||
<div className="mb-6 flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<h2 className="text-2xl font-bold text-black">
|
||||
Buat Peminjaman Baru
|
||||
</h2>
|
||||
<nav>
|
||||
<ol className="flex items-center gap-2 text-sm">
|
||||
<li><a className="font-medium text-gray-500" href="/dashboard/bookings">Booking /</a></li>
|
||||
<li className="font-medium text-yellow-500">Add</li>
|
||||
</ol>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
{/* Form Container */}
|
||||
<div className="rounded-xl border border-stroke bg-white shadow-sm border-gray-200 dark:bg-boxdark">
|
||||
<div className="border-b border-stroke py-4 px-6.5 border-gray-200">
|
||||
<h3 className="font-medium text-black ">
|
||||
Formulir Pengajuan Jadwal
|
||||
</h3>
|
||||
<div className="max-w-3xl mx-auto bg-white rounded-xl shadow-sm border border-gray-100 p-8">
|
||||
<div className="flex items-center gap-3 mb-8 border-b pb-4">
|
||||
<div className="bg-blue-100 p-3 rounded-lg text-blue-600">
|
||||
<CalendarPlus size={28} />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-800">Buat Booking Baru</h2>
|
||||
<p className="text-gray-500 text-sm mt-1">Isi detail di bawah ini untuk memesan ruangan kelas S-CLASS.</p>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit}>
|
||||
<div className="p-6.5">
|
||||
|
||||
{/* --- Baris 1: Ruangan & Tanggal --- */}
|
||||
<div className="mb-4.5 flex flex-col gap-6 xl:flex-row">
|
||||
{/* Input Ruangan */}
|
||||
<div className="w-full xl:w-1/2">
|
||||
<label className="mb-2.5 block text-black font-medium">
|
||||
Pilih Ruangan <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="relative z-20 bg-white border border-gray-300 text-gray-900 focus:ring-yellow-500">
|
||||
<select
|
||||
name="room"
|
||||
value={formData.room}
|
||||
onChange={handleChange}
|
||||
className="relative z-20 w-full appearance-none rounded border border-stroke bg-transparent py-3 px-5 outline-none transition focus:border-yellow-500 active:border-yellow-500 dark:border-form-strokedark dark:bg-form-input dark:focus:border-yellow-500 text-black "
|
||||
>
|
||||
<option value="Ruang T-301">Ruang T-301 (Teori)</option>
|
||||
<option value="Ruang T-302">Ruang T-302 (Teori)</option>
|
||||
<option value="Lab Kendali">Lab Sistem Kendali</option>
|
||||
<option value="Lab IoT">Lab IoT & Embedded</option>
|
||||
<option value="Ruang Sidang">Ruang Sidang TA</option>
|
||||
</select>
|
||||
<span className="absolute top-1/2 right-4 z-30 -translate-y-1/2">
|
||||
<svg className="fill-current" width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6.98438 9.98438H17.0156C17.3781 9.98438 17.5656 10.4219 17.3094 10.6781L12.3094 15.6781C12.1406 15.8469 11.8594 15.8469 11.6906 15.6781L6.69062 10.6781C6.43438 10.4219 6.62188 9.98438 6.98438 9.98438Z" fill="gray" /></svg>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Input Tanggal */}
|
||||
<div className="w-full xl:w-1/2">
|
||||
<label className="mb-2.5 block text-black font-medium">
|
||||
Tanggal Peminjaman <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="date"
|
||||
name="date"
|
||||
required
|
||||
value={formData.date}
|
||||
onChange={handleChange}
|
||||
className="w-full rounded border border-stroke bg-gray-50 py-3 pl-11.5 pr-4.5 text-black focus:border-yellow-500 focus-visible:outline-none border-gray-200 dark:focus:border-yellow-500"
|
||||
/>
|
||||
<Calendar className="absolute left-4.5 top-3 text-gray-400" size={20} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* --- Baris 2: Waktu Mulai & Selesai --- */}
|
||||
<div className="mb-4.5 flex flex-col gap-6 xl:flex-row">
|
||||
<div className="w-full xl:w-1/2">
|
||||
<label className="mb-2.5 block text-black font-medium">
|
||||
Waktu Mulai <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="time"
|
||||
name="startTime"
|
||||
required
|
||||
value={formData.startTime}
|
||||
onChange={handleChange}
|
||||
className="w-full rounded border border-stroke bg-gray-50 py-3 pl-11.5 pr-4.5 text-black focus:border-yellow-500 focus-visible:outline-none border-gray-200 dark:focus:border-yellow-500"
|
||||
/>
|
||||
<Clock className="absolute left-4.5 top-3 text-gray-400" size={20} />
|
||||
</div>
|
||||
{/* Info Validasi */}
|
||||
<p className="mt-2 text-xs text-orange-500 flex items-center gap-1">
|
||||
<AlertTriangle size={12} />
|
||||
Peminjaman jam 21:00 - 06:00 wajib H-3.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="w-full xl:w-1/2">
|
||||
<label className="mb-2.5 block text-black font-medium">
|
||||
Waktu Selesai <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<div className="relative">
|
||||
<input
|
||||
type="time"
|
||||
name="endTime"
|
||||
required
|
||||
value={formData.endTime}
|
||||
onChange={handleChange}
|
||||
className="w-full rounded border border-stroke bg-gray-50 py-3 pl-11.5 pr-4.5 text-black focus:border-yellow-500 focus-visible:outline-none border-gray-200 dark:focus:border-yellow-500"
|
||||
/>
|
||||
<Clock className="absolute left-4.5 top-3 text-gray-400" size={20} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* --- Baris 3: Keperluan --- */}
|
||||
<div className="mb-6">
|
||||
<label className="mb-2.5 block text-black font-medium">
|
||||
Keperluan / Mata Kuliah <span className="text-red-500">*</span>
|
||||
</label>
|
||||
<textarea
|
||||
rows={4}
|
||||
name="purpose"
|
||||
required
|
||||
placeholder="Jelaskan keperluan peminjaman ruang..."
|
||||
value={formData.purpose}
|
||||
onChange={handleChange}
|
||||
className="w-full rounded border border-stroke bg-transparent py-3 px-5 text-black outline-none transition focus:border-yellow-500 active:border-yellow-500 dark:border-form-strokedark dark:bg-form-input dark:focus:border-yellow-500"
|
||||
></textarea>
|
||||
</div>
|
||||
|
||||
{/* --- Tombol Aksi --- */}
|
||||
<div className="flex gap-4 justify-end">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => router.push("/dashboard/bookings")}
|
||||
className="flex items-center justify-center gap-2 rounded border border-stroke py-2.5 px-6 font-medium text-black hover:shadow-1 border-gray-200 transition-all hover:bg-gray-100"
|
||||
>
|
||||
<X size={18} />
|
||||
Batal
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
className={`flex items-center justify-center gap-2 rounded bg-yellow-500 py-2.5 px-6 font-medium text-white hover:bg-yellow-600 transition-all ${
|
||||
isLoading ? "opacity-70 cursor-not-allowed" : ""
|
||||
}`}
|
||||
>
|
||||
{isLoading ? (
|
||||
"Menyimpan..."
|
||||
) : (
|
||||
<>
|
||||
<Save size={18} />
|
||||
Simpan Jadwal
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
{/* Pilih Ruangan */}
|
||||
<div>
|
||||
<label className="block text-sm font-semibold text-gray-700 mb-2">Pilih Ruangan</label>
|
||||
<div className="relative">
|
||||
<MapPin className="absolute left-3 top-3 text-gray-400 h-5 w-5" />
|
||||
<select
|
||||
required
|
||||
className="pl-10 w-full border border-gray-300 rounded-lg p-3 text-sm font-medium focus:ring-2 focus:ring-blue-500 outline-none bg-white appearance-none"
|
||||
value={selectedRoomId}
|
||||
onChange={(e) => setSelectedRoomId(e.target.value)}
|
||||
>
|
||||
<option value="" disabled>-- Pilih Ruangan yang Tersedia --</option>
|
||||
{rooms.map(room => (
|
||||
<option key={room.room_id} value={room.room_id}>
|
||||
{room.name} (Kapasitas: {room.capacity} | {room.floor})
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Waktu Mulai & Selesai (Grid 2 Kolom) */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||
<div>
|
||||
<label className="block text-sm font-semibold text-gray-700 mb-2">Waktu Mulai</label>
|
||||
<div className="relative">
|
||||
<Clock className="absolute left-3 top-3 text-gray-400 h-5 w-5" />
|
||||
<input type="datetime-local" required
|
||||
className="pl-10 w-full border border-gray-300 rounded-lg p-3 text-sm focus:ring-2 focus:ring-blue-500 outline-none"
|
||||
value={startTime} onChange={(e) => setStartTime(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-semibold text-gray-700 mb-2">Waktu Selesai</label>
|
||||
<div className="relative">
|
||||
<Clock className="absolute left-3 top-3 text-gray-400 h-5 w-5" />
|
||||
<input type="datetime-local" required
|
||||
className="pl-10 w-full border border-gray-300 rounded-lg p-3 text-sm focus:ring-2 focus:ring-blue-500 outline-none"
|
||||
value={endTime} onChange={(e) => setEndTime(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Keperluan */}
|
||||
<div>
|
||||
<label className="block text-sm font-semibold text-gray-700 mb-2">Keperluan Kegiatan</label>
|
||||
<div className="relative">
|
||||
<FileText className="absolute left-3 top-3 text-gray-400 h-5 w-5" />
|
||||
<textarea required rows={4} placeholder="Jelaskan keperluan peminjaman ruangan ini..."
|
||||
className="pl-10 w-full border border-gray-300 rounded-lg p-3 text-sm focus:ring-2 focus:ring-blue-500 outline-none"
|
||||
value={purpose} onChange={(e) => setPurpose(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="submit" disabled={submitLoading}
|
||||
className={`w-full flex items-center justify-center gap-2 py-3.5 rounded-lg text-white font-bold transition-all shadow-sm
|
||||
${submitLoading ? 'bg-blue-400 cursor-not-allowed' : 'bg-blue-600 hover:bg-blue-700'}`}
|
||||
>
|
||||
{submitLoading ? 'Memproses...' : <><CheckCircle2 size={20} /> Ajukan Booking Sekarang</>}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@ -1,246 +1,225 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { ChevronLeft, ChevronRight, Calendar as CalendarIcon, Clock, MapPin } from "lucide-react";
|
||||
import { useEffect, useState } from "react";
|
||||
import axios from "axios";
|
||||
import { CalendarDays, MapPin, Clock, ChevronLeft, ChevronRight, Calendar as CalendarIcon } from "lucide-react";
|
||||
|
||||
// --- Tipe Data Mock ---
|
||||
type Booking = {
|
||||
id: number;
|
||||
room: string;
|
||||
date: string; // Format YYYY-MM-DD
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
purpose: string;
|
||||
status: "Approved" | "Pending";
|
||||
};
|
||||
export default function CalendarViewPage() {
|
||||
const [bookings, setBookings] = useState<any[]>([]);
|
||||
const [rooms, setRooms] = useState<any[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
// --- Data Dummy (Mock Data) ---
|
||||
const mockBookings: Booking[] = [
|
||||
{
|
||||
id: 1,
|
||||
room: "Ruang T-301",
|
||||
date: "2026-02-05",
|
||||
startTime: "07:30",
|
||||
endTime: "10:00",
|
||||
purpose: "Kuliah Sistem Kendali",
|
||||
status: "Approved",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
room: "Lab IoT",
|
||||
date: "2026-02-05", // Tanggal sama dengan atas
|
||||
startTime: "13:00",
|
||||
endTime: "15:00",
|
||||
purpose: "Praktikum Embedded System",
|
||||
status: "Approved",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
room: "Ruang T-302",
|
||||
date: "2026-02-12",
|
||||
startTime: "09:00",
|
||||
endTime: "11:30",
|
||||
purpose: "Seminar Proposal",
|
||||
status: "Pending",
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
room: "Lab Kendali",
|
||||
date: "2026-02-20",
|
||||
startTime: "10:00",
|
||||
endTime: "12:00",
|
||||
purpose: "Riset Skripsi",
|
||||
status: "Approved",
|
||||
},
|
||||
];
|
||||
// --- STATE UNTUK NAVIGASI MINGGU ---
|
||||
const [startDate, setStartDate] = useState(() => {
|
||||
const d = new Date();
|
||||
d.setHours(0, 0, 0, 0);
|
||||
return d;
|
||||
});
|
||||
|
||||
export default function CalendarPage() {
|
||||
const [currentDate, setCurrentDate] = useState(new Date(2026, 1)); // Februari 2026
|
||||
const [selectedDate, setSelectedDate] = useState<string | null>(null);
|
||||
// Generate 7 Hari ke depan berdasarkan startDate
|
||||
const weekDates = Array.from({ length: 7 }).map((_, i) => {
|
||||
const d = new Date(startDate);
|
||||
d.setDate(startDate.getDate() + i);
|
||||
return d;
|
||||
});
|
||||
|
||||
// --- Helper Functions ---
|
||||
const getDaysInMonth = (year: number, month: number) => new Date(year, month + 1, 0).getDate();
|
||||
const getFirstDayOfMonth = (year: number, month: number) => new Date(year, month, 1).getDay();
|
||||
// FORMAT RENTANG TANGGAL
|
||||
const firstDay = weekDates[0].toLocaleDateString('id-ID', { day: 'numeric', month: 'short' });
|
||||
const lastDay = weekDates[6].toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric' });
|
||||
const dateRangeLabel = `${firstDay} - ${lastDay}`;
|
||||
|
||||
const handlePrevMonth = () => {
|
||||
setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() - 1));
|
||||
setSelectedDate(null);
|
||||
// Fungsi Navigasi
|
||||
const goToPreviousWeek = () => {
|
||||
setStartDate(prev => {
|
||||
const newDate = new Date(prev);
|
||||
newDate.setDate(prev.getDate() - 7);
|
||||
return newDate;
|
||||
});
|
||||
};
|
||||
|
||||
const handleNextMonth = () => {
|
||||
setCurrentDate(new Date(currentDate.getFullYear(), currentDate.getMonth() + 1));
|
||||
setSelectedDate(null);
|
||||
const goToNextWeek = () => {
|
||||
setStartDate(prev => {
|
||||
const newDate = new Date(prev);
|
||||
newDate.setDate(prev.getDate() + 7);
|
||||
return newDate;
|
||||
});
|
||||
};
|
||||
|
||||
const handleDateClick = (day: number) => {
|
||||
const year = currentDate.getFullYear();
|
||||
const month = String(currentDate.getMonth() + 1).padStart(2, "0");
|
||||
const dateStr = String(day).padStart(2, "0");
|
||||
const fullDate = `${year}-${month}-${dateStr}`;
|
||||
|
||||
// Toggle seleksi: Jika diklik lagi, tutup detailnya
|
||||
if (selectedDate === fullDate) {
|
||||
setSelectedDate(null);
|
||||
} else {
|
||||
setSelectedDate(fullDate);
|
||||
}
|
||||
const goToToday = () => {
|
||||
const d = new Date();
|
||||
d.setHours(0, 0, 0, 0);
|
||||
setStartDate(d);
|
||||
};
|
||||
// ------------------------------------------
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const token = localStorage.getItem("token");
|
||||
const [roomsRes, bookingsRes] = await Promise.all([
|
||||
axios.get("http://localhost:8080/api/rooms", { headers: { Authorization: `Bearer ${token}` } }),
|
||||
axios.get("http://localhost:8080/api/bookings", { headers: { Authorization: `Bearer ${token}` } })
|
||||
]);
|
||||
|
||||
setRooms(roomsRes.data.data || []);
|
||||
setBookings(bookingsRes.data.data || []);
|
||||
} catch (error) {
|
||||
console.error("Gagal memuat data", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
// Fungsi memfilter booking
|
||||
const getBookingsForCell = (roomId: number, dateObj: Date) => {
|
||||
return bookings.filter(b => {
|
||||
if (b.room_id !== roomId) return false;
|
||||
const bDate = new Date(b.start_time);
|
||||
return (
|
||||
bDate.getDate() === dateObj.getDate() &&
|
||||
bDate.getMonth() === dateObj.getMonth() &&
|
||||
bDate.getFullYear() === dateObj.getFullYear()
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
// --- Generate Calendar Grid ---
|
||||
const year = currentDate.getFullYear();
|
||||
const month = currentDate.getMonth();
|
||||
const daysInMonth = getDaysInMonth(year, month);
|
||||
const firstDay = getFirstDayOfMonth(year, month);
|
||||
|
||||
// Array kosong untuk padding hari sebelum tanggal 1
|
||||
const emptyDays = Array.from({ length: firstDay }, (_, i) => i);
|
||||
// Array tanggal 1 sampai akhir bulan
|
||||
const days = Array.from({ length: daysInMonth }, (_, i) => i + 1);
|
||||
// Format Jam
|
||||
const formatTime = (isoString: string) => {
|
||||
return new Date(isoString).toLocaleTimeString('id-ID', { hour: '2-digit', minute: '2-digit' });
|
||||
};
|
||||
|
||||
// Nama Bulan
|
||||
const monthNames = [
|
||||
"Januari", "Februari", "Maret", "April", "Mei", "Juni",
|
||||
"Juli", "Agustus", "September", "Oktober", "November", "Desember"
|
||||
];
|
||||
|
||||
// Filter booking untuk tanggal yang dipilih
|
||||
const selectedBookings = mockBookings.filter((b) => b.date === selectedDate);
|
||||
if (loading) return <div className="p-8 text-gray-500 font-medium">Menyusun matriks jadwal...</div>;
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-6 lg:flex-row">
|
||||
<div className="w-full">
|
||||
|
||||
{/* --- KIRI: KALENDER --- */}
|
||||
<div className="flex-1 rounded-xl border border-stroke bg-white shadow-sm border-gray-200 dark:bg-boxdark">
|
||||
{/* Header Kalender */}
|
||||
<div className="flex items-center justify-between border-b border-stroke p-4 border-gray-200 sm:p-6">
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="h-10 w-10 flex items-center justify-center rounded-full bg-yellow-50 text-yellow-600">
|
||||
<CalendarIcon size={20} />
|
||||
</div>
|
||||
<div>
|
||||
<h2 className="text-lg font-bold text-black ">
|
||||
{monthNames[month]} {year}
|
||||
</h2>
|
||||
<p className="text-xs text-gray-500">Pilih tanggal untuk melihat detail.</p>
|
||||
</div>
|
||||
{/* HEADER & NAVIGASI */}
|
||||
<div className="flex flex-col md:flex-row md:items-center justify-between gap-4 mb-8">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="bg-blue-100 p-3 rounded-lg text-blue-600">
|
||||
<CalendarDays size={28} />
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2">
|
||||
<button onClick={handlePrevMonth} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-meta-4 transition">
|
||||
<ChevronLeft size={20} />
|
||||
</button>
|
||||
<button onClick={handleNextMonth} className="p-2 rounded hover:bg-gray-100 dark:hover:bg-meta-4 transition">
|
||||
<ChevronRight size={20} />
|
||||
</button>
|
||||
<div>
|
||||
<h2 className="text-2xl font-bold text-gray-800">Weekly Calendar View</h2>
|
||||
<p className="text-gray-500 text-sm mt-1">Pantau kepadatan jadwal ruangan S-CLASS.</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Grid Kalender */}
|
||||
<div className="p-4 sm:p-6">
|
||||
{/* Nama Hari */}
|
||||
<div className="grid grid-cols-7 mb-2">
|
||||
{["Min", "Sen", "Sel", "Rab", "Kam", "Jum", "Sab"].map((d) => (
|
||||
<span key={d} className="text-center text-sm font-medium text-gray-500 py-2">
|
||||
{d}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
{/* Kontrol Navigasi Minggu */}
|
||||
<div className="flex items-center bg-white border border-gray-200 rounded-lg p-1 shadow-sm w-fit">
|
||||
<button
|
||||
onClick={goToPreviousWeek}
|
||||
className="p-2 hover:bg-gray-100 rounded-md transition-colors text-gray-600 hover:text-blue-600 shrink-0"
|
||||
title="Minggu Sebelumnya"
|
||||
>
|
||||
<ChevronLeft size={20} />
|
||||
</button>
|
||||
|
||||
<div className="w-px h-6 bg-gray-200 mx-1"></div>
|
||||
|
||||
{/* TULISAN RENTANG TANGGAL DINAMIS (Lebar Fix w-44 untuk menghilangkan warning Tailwind) */}
|
||||
<button
|
||||
onClick={goToToday}
|
||||
title="Kembali ke minggu ini"
|
||||
className="w-44 py-2 hover:bg-gray-100 rounded-md transition-colors text-sm font-semibold text-gray-700 hover:text-blue-600 flex items-center justify-center gap-2"
|
||||
>
|
||||
<CalendarIcon size={16} className="shrink-0" />
|
||||
<span className="truncate">{dateRangeLabel}</span>
|
||||
</button>
|
||||
|
||||
{/* Tanggal */}
|
||||
<div className="grid grid-cols-7 gap-2">
|
||||
{/* Render Empty Cells */}
|
||||
{emptyDays.map((_, i) => (
|
||||
<div key={`empty-${i}`} className="h-24 sm:h-32"></div>
|
||||
))}
|
||||
<div className="w-px h-6 bg-gray-200 mx-1"></div>
|
||||
|
||||
{/* Render Date Cells */}
|
||||
{days.map((day) => {
|
||||
// Format tanggal hari ini untuk pengecekan data
|
||||
const currentDayStr = `${year}-${String(month + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
|
||||
const hasBooking = mockBookings.some((b) => b.date === currentDayStr);
|
||||
const isSelected = selectedDate === currentDayStr;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={day}
|
||||
onClick={() => handleDateClick(day)}
|
||||
className={`relative flex h-24 sm:h-32 cursor-pointer flex-col justify-between rounded border p-2 transition hover:bg-gray-50 dark:hover:bg-meta-4
|
||||
${isSelected
|
||||
? "border-yellow-500 bg-yellow-50 dark:bg-slate-800"
|
||||
: "border-stroke border-gray-200 bg-white dark:bg-boxdark"
|
||||
}`}
|
||||
>
|
||||
<span className={`text-sm font-medium ${isSelected ? "text-yellow-600" : "text-black "}`}>
|
||||
{day}
|
||||
</span>
|
||||
|
||||
{/* Indikator Booking (Dot / Bar) */}
|
||||
{hasBooking && (
|
||||
<div className="flex flex-col gap-1">
|
||||
{mockBookings
|
||||
.filter(b => b.date === currentDayStr)
|
||||
.slice(0, 2) // Batasi tampilan max 2 baris di grid
|
||||
.map((b, idx) => (
|
||||
<div key={idx} className="truncate rounded-sm bg-blue-100 px-1 py-0.5 text-[10px] font-medium text-blue-700 dark:bg-blue-900 dark:text-blue-100">
|
||||
{b.startTime} - {b.room}
|
||||
</div>
|
||||
))}
|
||||
{mockBookings.filter(b => b.date === currentDayStr).length > 2 && (
|
||||
<span className="text-[10px] text-gray-400 text-center">+ Lainnya</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<button
|
||||
onClick={goToNextWeek}
|
||||
className="p-2 hover:bg-gray-100 rounded-md transition-colors text-gray-600 hover:text-blue-600 shrink-0"
|
||||
title="Minggu Depan"
|
||||
>
|
||||
<ChevronRight size={20} />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* --- KANAN: DETAIL SIDEBAR --- */}
|
||||
{selectedDate && (
|
||||
<div className="w-full lg:w-80 shrink-0 animate-in fade-in slide-in-from-right duration-300">
|
||||
<div className="rounded-xl border border-stroke bg-white p-6 shadow-sm border-gray-200 dark:bg-boxdark">
|
||||
<h3 className="mb-4 text-xl font-bold text-black border-b border-stroke pb-2 border-gray-200">
|
||||
Jadwal {selectedDate}
|
||||
</h3>
|
||||
|
||||
{selectedBookings.length > 0 ? (
|
||||
<div className="flex flex-col gap-4">
|
||||
{selectedBookings.map((booking) => (
|
||||
<div key={booking.id} className="rounded-lg border border-gray-100 bg-gray-50 p-4 border-gray-200 bg-gray-50">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<span className={`px-2 py-0.5 rounded text-xs font-medium ${
|
||||
booking.status === "Approved"
|
||||
? "bg-green-100 text-green-700"
|
||||
: "bg-yellow-100 text-yellow-700"
|
||||
}`}>
|
||||
{booking.status}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<h4 className="font-bold text-black mb-1">{booking.purpose}</h4>
|
||||
|
||||
<div className="flex items-center gap-2 text-sm text-gray-500 mt-2">
|
||||
<MapPin size={16} /> {booking.room}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-sm text-gray-500 mt-1">
|
||||
<Clock size={16} /> {booking.startTime} - {booking.endTime}
|
||||
</div>
|
||||
{/* Pembungkus Tabel */}
|
||||
<div className="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden relative">
|
||||
<div className="overflow-x-auto">
|
||||
{/* Lebar tabel dipertahankan menggunakan min-w-[1000px] karena ini ukuran presisi khusus */}
|
||||
<table className="w-full min-w-250 text-left border-collapse">
|
||||
|
||||
{/* HEADER TABEL: Sumbu X (Tanggal) */}
|
||||
<thead className="bg-gray-50">
|
||||
<tr>
|
||||
<th className="sticky left-0 z-10 bg-gray-50 border-b border-r border-gray-200 p-4 font-bold text-gray-700 w-48 shadow-[2px_0_5px_-2px_rgba(0,0,0,0.1)]">
|
||||
<div className="flex items-center gap-2">
|
||||
<MapPin size={18} className="text-blue-500" /> Ruangan
|
||||
</div>
|
||||
</th>
|
||||
|
||||
{weekDates.map((date, idx) => (
|
||||
<th key={idx} className="border-b border-gray-200 p-4 min-w-50 text-center">
|
||||
<div className="font-bold text-gray-800">
|
||||
{date.toLocaleDateString('id-ID', { weekday: 'long' })}
|
||||
</div>
|
||||
<div className="text-xs font-medium text-gray-500 mt-0.5">
|
||||
{date.toLocaleDateString('id-ID', { day: 'numeric', month: 'short', year: 'numeric' })}
|
||||
</div>
|
||||
</th>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="py-10 text-center">
|
||||
<p className="text-gray-400 text-sm">Tidak ada jadwal pada tanggal ini.</p>
|
||||
<button className="mt-4 text-sm font-medium text-yellow-600 hover:underline">
|
||||
+ Tambah Jadwal
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
{/* BODY TABEL: Sumbu Y (Ruangan) */}
|
||||
<tbody>
|
||||
{rooms.map((room) => (
|
||||
<tr key={room.room_id} className="hover:bg-blue-50/30 transition-colors">
|
||||
|
||||
<td className="sticky left-0 z-10 bg-white border-b border-r border-gray-200 p-4 shadow-[2px_0_5px_-2px_rgba(0,0,0,0.1)] group-hover:bg-blue-50/30 transition-colors">
|
||||
<div className="font-bold text-gray-800">{room.name}</div>
|
||||
<div className="text-xs text-gray-500">{room.category}</div>
|
||||
</td>
|
||||
|
||||
{weekDates.map((date, idx) => {
|
||||
const dailyBookings = getBookingsForCell(room.room_id, date);
|
||||
|
||||
return (
|
||||
<td key={idx} className="border-b border-gray-100 border-r border-r-gray-50 p-2 align-top">
|
||||
{dailyBookings.length > 0 ? (
|
||||
<div className="space-y-2">
|
||||
{dailyBookings.map((b, i) => (
|
||||
<div key={i} className={`border rounded-md p-2 shadow-sm border-l-4
|
||||
${b.status === 'Approved' ? 'bg-green-50 border-green-200 border-l-green-500' :
|
||||
b.status === 'Pending' ? 'bg-yellow-50 border-yellow-200 border-l-yellow-500' :
|
||||
'bg-blue-50 border-blue-200 border-l-blue-500'}`}>
|
||||
<p className="text-xs font-bold text-gray-800 truncate" title={b.purpose}>
|
||||
{b.purpose}
|
||||
</p>
|
||||
<div className="flex flex-wrap items-center gap-1 text-[10px] text-gray-600 font-semibold mt-1">
|
||||
<Clock size={10} />
|
||||
{formatTime(b.start_time)} - {formatTime(b.end_time)}
|
||||
<span className="ml-auto text-[9px] uppercase bg-white/60 px-1 rounded border border-gray-100">
|
||||
{b.status || 'Pending'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="h-full w-full min-h-10 flex items-center justify-center text-gray-300 text-xs">
|
||||
-
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
);
|
||||
})}
|
||||
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</div>
|
||||
);
|
||||
|
||||
@ -1,26 +1,128 @@
|
||||
import Header from "@/components/Header";
|
||||
import Sidebar from "@/components/Sidebar";
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { useRouter, usePathname } from "next/navigation";
|
||||
import Link from "next/link";
|
||||
import {
|
||||
LogOut,
|
||||
LayoutDashboard,
|
||||
CalendarPlus,
|
||||
CalendarDays
|
||||
} from "lucide-react";
|
||||
|
||||
export default function DashboardLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<div className="dark:bg-boxdark-2 bg-gray-100 min-h-screen">
|
||||
<div className="flex h-screen overflow-hidden">
|
||||
{/* Sidebar akan tampil di sini */}
|
||||
<Sidebar />
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const [user, setUser] = useState<any>(null);
|
||||
|
||||
{/* Area Konten Utama */}
|
||||
<div className="relative flex flex-1 flex-col overflow-y-auto overflow-x-hidden">
|
||||
<Header />
|
||||
<main>
|
||||
<div className="mx-auto max-w-screen-2xl p-4 md:p-6 2xl:p-10">
|
||||
{children}
|
||||
</div>
|
||||
</main>
|
||||
useEffect(() => {
|
||||
const userData = localStorage.getItem("user");
|
||||
if (userData) {
|
||||
setUser(JSON.parse(userData));
|
||||
} else {
|
||||
router.push("/login");
|
||||
}
|
||||
}, []);
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem("token");
|
||||
localStorage.removeItem("user");
|
||||
router.push("/login");
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-white flex flex-col font-sans">
|
||||
|
||||
{/* HEADER TINGKAT ATAS */}
|
||||
<header className="bg-[#e5e7eb] flex items-center justify-between px-6 py-4 shadow-sm z-20 relative">
|
||||
{/* Logo Kiri */}
|
||||
<div>
|
||||
<h2 className="text-blue-600 text-sm font-medium mb-1">Welcome to,</h2>
|
||||
<h1 className="text-blue-600 text-3xl font-bold">S-CLASS</h1>
|
||||
</div>
|
||||
|
||||
{/* Profil & Logout Kanan */}
|
||||
<div className="flex items-center gap-8">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="bg-blue-600 text-white rounded-full h-10 w-10 flex items-center justify-center text-lg font-bold shadow-sm">
|
||||
{user?.full_name ? user.full_name.charAt(0).toUpperCase() : "V"}
|
||||
</div>
|
||||
<div className="hidden sm:flex flex-col">
|
||||
<span className="font-semibold text-gray-800 text-sm">
|
||||
{user?.full_name || "Valentino Heman Budiarto"}
|
||||
</span>
|
||||
<span className="font-medium text-gray-500 text-xs capitalize">
|
||||
{user?.role === 'admin' ? 'Administrator' : 'Mahasiswa'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="flex items-center gap-2 text-red-500 font-medium hover:text-red-600 transition-colors"
|
||||
>
|
||||
Log Out <LogOut size={20} />
|
||||
</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{/* AREA BAWAH (SIDEBAR + KONTEN) */}
|
||||
<div className="flex flex-1 overflow-hidden relative">
|
||||
|
||||
{/* SIDEBAR KIRI (Kini Permanen/Statis) */}
|
||||
<aside className="bg-[#b0c4d9] w-64 flex flex-col py-10 shadow-inner z-10">
|
||||
<nav className="flex flex-col gap-6 px-4">
|
||||
|
||||
{/* Menu 1: View Rooms */}
|
||||
<Link
|
||||
href="/dashboard"
|
||||
className={`flex items-center gap-4 cursor-pointer font-semibold transition-colors group
|
||||
${pathname === '/dashboard' ? 'text-blue-700' : 'text-gray-800 hover:text-blue-700'}`}
|
||||
>
|
||||
<div className={`p-2 rounded-lg transition-colors
|
||||
${pathname === '/dashboard' ? 'bg-white/60 shadow-sm' : 'bg-white/30 group-hover:bg-white/50'}`}>
|
||||
<LayoutDashboard size={22} />
|
||||
</div>
|
||||
<span>View Rooms</span>
|
||||
</Link>
|
||||
|
||||
{/* Menu 2: Add Booking */}
|
||||
<Link
|
||||
href="/dashboard/bookings/add"
|
||||
className={`flex items-center gap-4 cursor-pointer font-semibold transition-colors group
|
||||
${pathname === '/dashboard/bookings/add' ? 'text-blue-700' : 'text-gray-800 hover:text-blue-700'}`}
|
||||
>
|
||||
<div className={`p-2 rounded-lg transition-colors
|
||||
${pathname === '/dashboard/bookings/add' ? 'bg-white/60 shadow-sm' : 'bg-white/30 group-hover:bg-white/50'}`}>
|
||||
<CalendarPlus size={22} />
|
||||
</div>
|
||||
<span>Add Booking</span>
|
||||
</Link>
|
||||
|
||||
{/* Menu 3: Calendar View */}
|
||||
<Link
|
||||
href="/dashboard/bookings/calendar"
|
||||
className={`flex items-center gap-4 cursor-pointer font-semibold transition-colors group
|
||||
${pathname === '/dashboard/bookings/calendar' ? 'text-blue-700' : 'text-gray-800 hover:text-blue-700'}`}
|
||||
>
|
||||
<div className={`p-2 rounded-lg transition-colors
|
||||
${pathname === '/dashboard/bookings/calendar' ? 'bg-white/60 shadow-sm' : 'bg-white/30 group-hover:bg-white/50'}`}>
|
||||
<CalendarDays size={22} />
|
||||
</div>
|
||||
<span>Calendar View</span>
|
||||
</Link>
|
||||
|
||||
</nav>
|
||||
</aside>
|
||||
|
||||
{/* KONTEN UTAMA */}
|
||||
<main className="flex-1 overflow-y-auto bg-[#f8fafc] p-8">
|
||||
{children}
|
||||
</main>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import axios from "axios";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { LogOut, MapPin, Users, Calendar, X, Clock, FileText } from "lucide-react";
|
||||
import { MapPin, Users, Calendar, X, Clock, FileText } from "lucide-react";
|
||||
|
||||
// Tipe Data
|
||||
interface Room {
|
||||
@ -57,12 +57,6 @@ export default function Dashboard() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem("token");
|
||||
localStorage.removeItem("user");
|
||||
router.push("/login");
|
||||
};
|
||||
|
||||
// --- BUKA MODAL ---
|
||||
const openBookingModal = (room: Room) => {
|
||||
setSelectedRoom(room);
|
||||
@ -81,8 +75,6 @@ export default function Dashboard() {
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
try {
|
||||
// Perlu convert string datetime-local ke Format ISO string (RFC3339) buat Golang
|
||||
// Contoh input: "2026-02-06T14:30" -> Output: "2026-02-06T14:30:00.000Z"
|
||||
const startISO = new Date(startTime).toISOString();
|
||||
const endISO = new Date(endTime).toISOString();
|
||||
|
||||
@ -103,7 +95,6 @@ export default function Dashboard() {
|
||||
setIsModalOpen(false); // Tutup modal
|
||||
|
||||
} catch (error: any) {
|
||||
// Tangkap error dari Backend (Misal: Bentrok / 409 Conflict)
|
||||
const errorMsg = error.response?.data?.error || "Gagal melakukan booking.";
|
||||
alert("GAGAL: " + errorMsg);
|
||||
} finally {
|
||||
@ -114,78 +105,64 @@ export default function Dashboard() {
|
||||
if (loading) return <div className="p-10 text-center">Memuat data...</div>;
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50 relative">
|
||||
{/* Navbar */}
|
||||
<nav className="bg-white shadow-sm px-6 py-4 flex justify-between items-center sticky top-0 z-10">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-blue-600">S-CLASS</h1>
|
||||
<p className="text-xs text-gray-500">Welcome, {user?.full_name}</p>
|
||||
</div>
|
||||
<div className="flex gap-4">
|
||||
{/* Tombol Cek History (Nanti kita buat halamannya) */}
|
||||
<button
|
||||
onClick={() => router.push('/history')}
|
||||
className="text-sm text-gray-600 hover:text-blue-600 font-medium"
|
||||
>
|
||||
Riwayat Saya
|
||||
</button>
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="flex items-center gap-2 text-sm text-red-500 hover:bg-red-50 px-3 py-2 rounded-lg transition"
|
||||
>
|
||||
<LogOut size={16} /> Keluar
|
||||
</button>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
{/* Konten Utama */}
|
||||
<main className="max-w-6xl mx-auto p-6">
|
||||
<h2 className="text-2xl font-bold text-gray-800 mb-6">Pilih Ruangan</h2>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{rooms.map((room) => (
|
||||
<div key={room.room_id} className="bg-white rounded-xl shadow-sm border border-gray-100 p-5 flex flex-col justify-between">
|
||||
<div>
|
||||
<div className="flex justify-between items-start mb-3">
|
||||
<span className={`px-2 py-1 text-xs font-semibold rounded-full ${
|
||||
room.status === 'Available' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'
|
||||
}`}>
|
||||
{room.status}
|
||||
</span>
|
||||
<span className="text-gray-400 text-xs">{room.category}</span>
|
||||
</div>
|
||||
<h3 className="text-lg font-bold text-gray-800 mb-1">{room.name}</h3>
|
||||
<div className="space-y-2 text-sm text-gray-600 mt-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<MapPin size={16} className="text-blue-400" /> {room.floor}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Users size={16} className="text-blue-400" /> Kapasitas: {room.capacity}
|
||||
</div>
|
||||
</div>
|
||||
<div className="w-full">
|
||||
|
||||
{/* Header Konten: Judul & Tombol Riwayat */}
|
||||
<div className="flex items-center justify-between mb-8">
|
||||
<h2 className="text-2xl font-bold text-gray-800">Pilih Ruangan</h2>
|
||||
<button
|
||||
onClick={() => router.push('/history')}
|
||||
className="flex items-center gap-2 bg-white border border-gray-300 text-gray-700 hover:bg-blue-50 hover:text-blue-600 hover:border-blue-300 px-4 py-2 rounded-lg text-sm font-medium shadow-sm transition-all"
|
||||
>
|
||||
<Clock size={18} />
|
||||
Lihat Riwayat Booking
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Grid Daftar Kelas */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
{rooms.map((room) => (
|
||||
<div key={room.room_id} className="bg-white rounded-xl shadow-sm border border-gray-100 p-5 flex flex-col justify-between hover:shadow-md transition-shadow">
|
||||
<div>
|
||||
<div className="flex justify-between items-start mb-3">
|
||||
<span className={`px-2 py-1 text-xs font-semibold rounded-full ${
|
||||
room.status === 'Available' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700'
|
||||
}`}>
|
||||
{room.status}
|
||||
</span>
|
||||
<span className="text-gray-400 text-xs font-medium">{room.category}</span>
|
||||
</div>
|
||||
<h3 className="text-lg font-bold text-gray-800 mb-1">{room.name}</h3>
|
||||
<div className="space-y-2 text-sm text-gray-600 mt-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<MapPin size={16} className="text-blue-500" /> {room.floor}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Users size={16} className="text-blue-500" /> Kapasitas: {room.capacity}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button
|
||||
className="mt-5 w-full bg-blue-600 text-white py-2 rounded-lg hover:bg-blue-700 transition flex justify-center items-center gap-2"
|
||||
onClick={() => openBookingModal(room)}
|
||||
>
|
||||
<Calendar size={16} />
|
||||
Booking Ruangan
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<button
|
||||
className="mt-6 w-full bg-blue-600 text-white font-medium py-2 rounded-lg hover:bg-blue-700 transition flex justify-center items-center gap-2 shadow-sm"
|
||||
onClick={() => openBookingModal(room)}
|
||||
>
|
||||
<Calendar size={16} />
|
||||
Booking Ruangan
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* --- MODAL POPUP FORMULIR --- */}
|
||||
{isModalOpen && selectedRoom && (
|
||||
<div className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4">
|
||||
<div className="bg-white rounded-2xl w-full max-w-md shadow-2xl overflow-hidden">
|
||||
<div className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4 backdrop-blur-sm">
|
||||
<div className="bg-white rounded-2xl w-full max-w-md shadow-xl overflow-hidden border border-gray-100">
|
||||
|
||||
{/* Header Modal */}
|
||||
<div className="bg-blue-600 p-4 flex justify-between items-center text-white">
|
||||
<h3 className="font-bold text-lg">Form Peminjaman</h3>
|
||||
<button onClick={() => setIsModalOpen(false)} className="hover:bg-blue-700 p-1 rounded">
|
||||
<h3 className="font-semibold text-lg">Form Peminjaman</h3>
|
||||
<button onClick={() => setIsModalOpen(false)} className="hover:bg-blue-700 p-1 rounded-lg transition-colors">
|
||||
<X size={20} />
|
||||
</button>
|
||||
</div>
|
||||
@ -194,7 +171,7 @@ export default function Dashboard() {
|
||||
<form onSubmit={handleBookingSubmit} className="p-6 space-y-4">
|
||||
<div className="bg-blue-50 p-3 rounded-lg border border-blue-100 mb-2">
|
||||
<p className="text-sm text-blue-800 font-semibold">Ruangan: {selectedRoom.name}</p>
|
||||
<p className="text-xs text-blue-600">{selectedRoom.floor}</p>
|
||||
<p className="text-xs text-blue-600 mt-0.5">{selectedRoom.floor}</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
@ -204,7 +181,7 @@ export default function Dashboard() {
|
||||
<input
|
||||
type="datetime-local"
|
||||
required
|
||||
className="pl-9 w-full border border-gray-300 rounded-lg p-2 text-sm focus:ring-blue-500 focus:border-blue-500"
|
||||
className="pl-9 w-full border border-gray-300 rounded-lg p-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-all"
|
||||
value={startTime}
|
||||
onChange={(e) => setStartTime(e.target.value)}
|
||||
/>
|
||||
@ -218,7 +195,7 @@ export default function Dashboard() {
|
||||
<input
|
||||
type="datetime-local"
|
||||
required
|
||||
className="pl-9 w-full border border-gray-300 rounded-lg p-2 text-sm focus:ring-blue-500 focus:border-blue-500"
|
||||
className="pl-9 w-full border border-gray-300 rounded-lg p-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-all"
|
||||
value={endTime}
|
||||
onChange={(e) => setEndTime(e.target.value)}
|
||||
/>
|
||||
@ -228,30 +205,30 @@ export default function Dashboard() {
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">Keperluan / Kegiatan</label>
|
||||
<div className="relative">
|
||||
<FileText className="absolute left-3 top-3 text-gray-400 h-4 w-4" />
|
||||
<FileText className="absolute left-3 top-2.5 text-gray-400 h-4 w-4" />
|
||||
<textarea
|
||||
required
|
||||
rows={3}
|
||||
placeholder="Contoh: Rapat Progres Skripsi"
|
||||
className="pl-9 w-full border border-gray-300 rounded-lg p-2 text-sm focus:ring-blue-500 focus:border-blue-500"
|
||||
className="pl-9 w-full border border-gray-300 rounded-lg p-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-all"
|
||||
value={purpose}
|
||||
onChange={(e) => setPurpose(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="pt-2 flex gap-3">
|
||||
<div className="pt-3 flex gap-3">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setIsModalOpen(false)}
|
||||
className="flex-1 py-2 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 text-sm font-medium"
|
||||
className="flex-1 py-2 border border-gray-300 rounded-lg text-gray-700 hover:bg-gray-50 text-sm font-medium transition-all"
|
||||
>
|
||||
Batal
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
disabled={submitLoading}
|
||||
className={`flex-1 py-2 rounded-lg text-white text-sm font-medium
|
||||
className={`flex-1 py-2 rounded-lg text-white text-sm font-medium shadow-sm transition-all
|
||||
${submitLoading ? 'bg-blue-400 cursor-not-allowed' : 'bg-blue-600 hover:bg-blue-700'}`}
|
||||
>
|
||||
{submitLoading ? 'Mengirim...' : 'Ajukan Booking'}
|
||||
@ -263,4 +240,4 @@ export default function Dashboard() {
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -3,12 +3,13 @@
|
||||
import { useState } from "react";
|
||||
import axios from "axios";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Lock, User } from "lucide-react"; // Ikon cantik
|
||||
import { Lock, User, Shield } from "lucide-react"; // Tambahan ikon Shield untuk Admin
|
||||
|
||||
export default function LoginPage() {
|
||||
const router = useRouter();
|
||||
|
||||
// State untuk menyimpan input user
|
||||
// State untuk menyimpan input user dan pilihan role
|
||||
const [loginRole, setLoginRole] = useState("user"); // "user" atau "admin"
|
||||
const [nrp, setNrp] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
@ -21,27 +22,30 @@ export default function LoginPage() {
|
||||
setError("");
|
||||
|
||||
try {
|
||||
// 1. Tembak API Backend
|
||||
// Tembak API Backend Golang
|
||||
// Opsional: Kamu bisa mengirimkan loginRole juga ke backend jika API-mu membutuhkannya
|
||||
const response = await axios.post("http://localhost:8080/api/auth/login", {
|
||||
nrp_nip: nrp,
|
||||
password: password,
|
||||
// role_attempt: loginRole // Buka komentar ini jika backend Golang minta parameter role
|
||||
});
|
||||
|
||||
// 2. Jika sukses, simpan Token & Data User ke LocalStorage (Browser)
|
||||
const { token, user } = response.data;
|
||||
localStorage.setItem("token", token);
|
||||
localStorage.setItem("user", JSON.stringify(user));
|
||||
|
||||
// 3. Pindah ke halaman Dashboard (Nanti kita buat)
|
||||
//alert("Login Berhasil! Selamat datang " + user.full_name); // Alert boleh dimatikan kalau mau
|
||||
if (user.role === "admin") {
|
||||
// Redirect berdasarkan role dari backend DAN pilihan di UI
|
||||
if (user.role === "admin" && loginRole === "admin") {
|
||||
router.push("/admin");
|
||||
} else if (user.role !== "admin" && loginRole === "user") {
|
||||
router.push("/dashboard"); // Halaman pemesanan kelas biasa
|
||||
} else {
|
||||
router.push("/dashboard");
|
||||
// Mencegah user biasa login lewat tab admin, atau sebaliknya
|
||||
setError("Role tidak sesuai dengan tipe akun Anda.");
|
||||
localStorage.clear(); // Bersihkan token yang terlanjur masuk
|
||||
}
|
||||
|
||||
} catch (err: any) {
|
||||
// Jika gagal, tampilkan pesan error dari backend
|
||||
setError(err.response?.data?.error || "Gagal terhubung ke server");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@ -52,33 +56,61 @@ export default function LoginPage() {
|
||||
<div className="flex min-h-screen items-center justify-center bg-gray-100">
|
||||
<div className="w-full max-w-md bg-white p-8 rounded-2xl shadow-lg">
|
||||
{/* Header / Logo */}
|
||||
<div className="text-center mb-8">
|
||||
<div className="text-center mb-6">
|
||||
<h1 className="text-3xl font-bold text-blue-600 mb-2">S-CLASS</h1>
|
||||
<p className="text-gray-500">Smart Classroom Booking System</p>
|
||||
</div>
|
||||
|
||||
{/* --- BAGIAN BARU: Toggle Role --- */}
|
||||
<div className="flex justify-center mb-6 bg-gray-100 p-1 rounded-lg">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => { setLoginRole("user"); setError(""); }}
|
||||
className={`w-1/2 py-2 text-sm font-semibold rounded-md transition-all ${
|
||||
loginRole === "user" ? "bg-white text-blue-600 shadow-sm" : "text-gray-500 hover:text-gray-700"
|
||||
}`}
|
||||
>
|
||||
Pengguna
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => { setLoginRole("admin"); setError(""); }}
|
||||
className={`w-1/2 py-2 text-sm font-semibold rounded-md transition-all ${
|
||||
loginRole === "admin" ? "bg-white text-blue-600 shadow-sm" : "text-gray-500 hover:text-gray-700"
|
||||
}`}
|
||||
>
|
||||
Administrator
|
||||
</button>
|
||||
</div>
|
||||
{/* ------------------------------- */}
|
||||
|
||||
{/* Form Login */}
|
||||
<form onSubmit={handleLogin} className="space-y-6">
|
||||
|
||||
{/* Pesan Error jika ada */}
|
||||
{error && (
|
||||
<div className="bg-red-100 text-red-600 p-3 rounded-lg text-sm text-center">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Input NRP */}
|
||||
{/* Input NRP / Username dinamis tergantung role */}
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">NRP / NIP</label>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
||||
{loginRole === "admin" ? "Username Admin" : "NRP / NIP"}
|
||||
</label>
|
||||
<div className="relative">
|
||||
<div className="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
|
||||
<User className="h-5 w-5 text-gray-400" />
|
||||
{loginRole === "admin" ? (
|
||||
<Shield className="h-5 w-5 text-gray-400" />
|
||||
) : (
|
||||
<User className="h-5 w-5 text-gray-400" />
|
||||
)}
|
||||
</div>
|
||||
<input
|
||||
type="text"
|
||||
required
|
||||
className="block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500"
|
||||
placeholder="Masukkan NRP..."
|
||||
placeholder={loginRole === "admin" ? "Masukkan Username..." : "Masukkan NRP..."}
|
||||
value={nrp}
|
||||
onChange={(e) => setNrp(e.target.value)}
|
||||
/>
|
||||
@ -107,14 +139,14 @@ export default function LoginPage() {
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading}
|
||||
className={`w-full flex justify-center py-3 px-4 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white
|
||||
${loading ? "bg-blue-400 cursor-not-allowed" : "bg-blue-600 hover:bg-blue-700 focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"}`}
|
||||
className={`w-full flex justify-center py-3 px-4 border border-transparent rounded-lg shadow-sm text-sm font-medium text-white transition-colors
|
||||
${loading ? "bg-blue-400 cursor-not-allowed" : loginRole === "admin" ? "bg-slate-800 hover:bg-slate-900" : "bg-blue-600 hover:bg-blue-700"}`}
|
||||
>
|
||||
{loading ? "Memproses..." : "Masuk Sekarang"}
|
||||
{loading ? "Memproses..." : loginRole === "admin" ? "Login sebagai Admin" : "Masuk Sekarang"}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{/* Footer Kecil */}
|
||||
{/* Footer */}
|
||||
<div className="mt-6 text-center text-xs text-gray-400">
|
||||
© 2026 Skripsi S-CLASS Project
|
||||
</div>
|
||||
|
||||
@ -1,37 +1,69 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { LogOut } from "lucide-react"; // Menggunakan ikon dari Lucide
|
||||
|
||||
export default function Header() {
|
||||
const router = useRouter();
|
||||
const [user, setUser] = useState<any>(null);
|
||||
|
||||
// Ambil data user yang sedang login dari localStorage
|
||||
useEffect(() => {
|
||||
const userData = localStorage.getItem("user");
|
||||
if (userData) {
|
||||
setUser(JSON.parse(userData));
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Fungsi untuk membersihkan sesi dan kembali ke login
|
||||
const handleLogout = () => {
|
||||
localStorage.removeItem("token");
|
||||
localStorage.removeItem("user");
|
||||
router.push("/login");
|
||||
};
|
||||
|
||||
return (
|
||||
<header className="sticky top-0 z-40 flex w-full bg-white drop-shadow-1 dark:bg-boxdark dark:drop-shadow-none">
|
||||
<div className="flex grow items-center justify-between px-4 py-4 shadow-2 md:px-6 2xl:px-11">
|
||||
{/* justify-end agar isinya merapat ke kanan */}
|
||||
<div className="flex grow items-center justify-end px-4 py-4 shadow-2 md:px-6 2xl:px-11">
|
||||
|
||||
{/* Search Bar (Hiasan) */}
|
||||
<div className="hidden sm:block">
|
||||
<form action="#" method="POST">
|
||||
<div className="relative">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Type to search..."
|
||||
className="w-full bg-transparent pl-9 pr-4 font-medium focus:outline-none xl:w-125"
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{/* User Area */}
|
||||
<div className="flex items-center gap-3 2xsm:gap-7">
|
||||
<div className="flex items-center gap-4">
|
||||
|
||||
<span className="hidden text-right lg:block">
|
||||
<span className="block text-sm font-medium text-black ">
|
||||
Valentino Heman
|
||||
{/* Nama dinamis, jika kosong tampilkan 'Admin S-CLASS' */}
|
||||
<span className="block text-sm font-bold text-black">
|
||||
{user?.full_name || "Admin S-CLASS"}
|
||||
</span>
|
||||
{/* Role dinamis */}
|
||||
<span className="block text-xs font-medium text-gray-500 capitalize">
|
||||
{user?.role || "Administrator"}
|
||||
</span>
|
||||
<span className="block text-xs font-medium">Mahasiswa</span>
|
||||
</span>
|
||||
|
||||
{/* Avatar Bulat */}
|
||||
<div className="h-10 w-10 rounded-full bg-slate-300 overflow-hidden">
|
||||
{/* Nanti bisa diisi Image */}
|
||||
<div className="flex items-center justify-center h-full text-slate-600 font-bold">VH</div>
|
||||
{/* Avatar Bulat Dinamis (Mengambil inisial nama depan) */}
|
||||
<div className="h-10 w-10 rounded-full bg-blue-100 border border-blue-200 overflow-hidden flex items-center justify-center">
|
||||
<span className="text-blue-600 font-bold text-lg">
|
||||
{user?.full_name ? user.full_name.charAt(0).toUpperCase() : "A"}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
{/* Garis Pembatas (Opsional untuk estetika) */}
|
||||
<div className="h-8 w-px bg-gray-200"></div>
|
||||
|
||||
{/* Tombol Keluar */}
|
||||
<button
|
||||
onClick={handleLogout}
|
||||
className="flex items-center gap-2 text-sm font-medium text-red-500 hover:text-red-700 hover:bg-red-50 px-3 py-2 rounded-lg transition-colors"
|
||||
>
|
||||
<LogOut size={18} />
|
||||
<span className="hidden sm:block">Keluar</span>
|
||||
</button>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user