<?php 
include 'koneksi.php';
cek_akses(['Kepala', 'Super Admin']);

$current_page = basename($_SERVER['PHP_SELF']);

$filter_unit = ($_SESSION['role'] == 'Kepala') ? "AND unit='{$_SESSION['unit']}'" : "";
$guru_list = mysqli_query($conn, "SELECT * FROM users WHERE role='Guru' $filter_unit ORDER BY full_name");
$kelas_list = mysqli_query($conn, "SELECT * FROM classes ORDER BY unit, name");

// Simpan jadwal supervisi
if(isset($_POST['simpan'])) {
    $teacher_id = $_POST['teacher_id'];
    $schedule_date = $_POST['schedule_date'];
    $schedule_time = $_POST['schedule_time'];
    $class_id = $_POST['class_id'];
    $notes = mysqli_real_escape_string($conn, $_POST['notes']);
    
    $query = "INSERT INTO supervisions (teacher_id, supervisor_id, schedule_date, schedule_time, class_id, notes) 
              VALUES ('$teacher_id', '{$_SESSION['user_id']}', '$schedule_date', '$schedule_time', '$class_id', '$notes')";
    if(mysqli_query($conn, $query)) {
        echo "<script>alert('Jadwal supervisi berhasil dibuat!'); window.location='supervisi.php';</script>";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Buat Jadwal Supervisi</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Inter', sans-serif; background: #f5f7fb; }
        .wrapper { display: flex; min-height: 100vh; }
        
        /* Sidebar */
        .main-sidebar {
            width: 280px;
            background: linear-gradient(180deg, #0b5e4e 0%, #064e3b 100%);
            color: #fff;
            position: fixed;
            top: 0;
            left: 0;
            height: 100vh;
            overflow-y: auto;
            z-index: 1000;
            transition: all 0.3s;
        }
        .sidebar-brand {
            padding: 25px 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        .sidebar-brand h3 { font-size: 20px; font-weight: 700; margin-bottom: 5px; }
        .sidebar-brand p { font-size: 11px; opacity: 0.7; }
        .user-panel {
            padding: 20px;
            background: rgba(255,255,255,0.08);
            margin: 15px;
            border-radius: 12px;
            text-align: center;
        }
        .user-panel .name { font-weight: 700; font-size: 15px; }
        .user-panel .role { font-size: 11px; opacity: 0.7; margin-top: 5px; }
        
        .sidebar-menu {
            list-style: none;
            padding: 0;
            margin: 20px 0;
        }
        .sidebar-menu .menu-title {
            padding: 10px 20px;
            font-size: 11px;
            text-transform: uppercase;
            letter-spacing: 1px;
            opacity: 0.5;
            font-weight: 600;
        }
        .sidebar-menu .menu-item { margin: 2px 0; }
        .sidebar-menu .menu-item a {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 20px;
            color: #fff;
            text-decoration: none;
            font-size: 14px;
            transition: all 0.2s;
            border-left: 3px solid transparent;
        }
        .sidebar-menu .menu-item a i { width: 22px; font-size: 16px; }
        .sidebar-menu .menu-item a:hover {
            background: rgba(255,255,255,0.08);
            border-left-color: #10b981;
        }
        .sidebar-menu .menu-item.active a {
            background: rgba(16, 185, 129, 0.15);
            border-left-color: #10b981;
        }
        
        /* Content */
        .content-wrapper {
            flex: 1;
            margin-left: 280px;
            padding: 20px 25px;
        }
        .content-header h1 {
            font-size: 24px;
            font-weight: 700;
            color: #1e293b;
            margin: 0;
        }
        .breadcrumb {
            background: transparent;
            padding: 0;
            margin: 5px 0 0;
        }
        .breadcrumb-item a { color: #0b5e4e; text-decoration: none; }
        
        .card-custom {
            background: white;
            border-radius: 16px;
            margin-bottom: 25px;
            overflow: hidden;
            box-shadow: 0 1px 3px rgba(0,0,0,0.05);
            border: none;
        }
        .card-header-custom {
            background: linear-gradient(135deg, #0b5e4e 0%, #10b981 100%);
            color: white;
            padding: 14px 20px;
            font-weight: 600;
            font-size: 14px;
        }
        .card-header-custom i { margin-right: 8px; }
        
        .form-group { margin-bottom: 20px; }
        .form-group label { font-weight: 600; font-size: 13px; color: #1e293b; }
        .form-control, .form-select {
            border-radius: 10px;
            border: 1px solid #e2e8f0;
            padding: 10px 15px;
            font-size: 13px;
        }
        .form-control:focus, .form-select:focus {
            border-color: #0b5e4e;
            box-shadow: 0 0 0 3px rgba(11, 94, 78, 0.1);
        }
        .btn-save {
            background: linear-gradient(135deg, #0b5e4e 0%, #10b981 100%);
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 10px;
            font-weight: 600;
            cursor: pointer;
            width: 100%;
        }
        .btn-save:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(11,94,78,0.3); }
        
        .main-footer {
            background: white;
            padding: 15px 20px;
            text-align: center;
            font-size: 12px;
            color: #64748b;
            border-radius: 12px;
            margin-top: 20px;
        }
        
        .mobile-toggle {
            display: none;
            position: fixed;
            top: 15px;
            left: 15px;
            z-index: 1100;
            background: #0b5e4e;
            color: white;
            border: none;
            padding: 10px 12px;
            border-radius: 8px;
            cursor: pointer;
        }
        
        @media (max-width: 768px) {
            .mobile-toggle { display: block; }
            .main-sidebar { transform: translateX(-100%); }
            .main-sidebar.open { transform: translateX(0); }
            .content-wrapper { margin-left: 0; padding: 60px 15px 15px 15px; }
        }
    </style>
</head>
<body>

<button class="mobile-toggle" onclick="document.getElementById('sidebar').classList.toggle('open')">
    <i class="fas fa-bars"></i>
</button>

<div class="wrapper">
    <!-- Sidebar -->
    <div class="main-sidebar" id="sidebar">
        <div class="sidebar-brand">
            <h3><i class="fas fa-school"></i> ADMINISTRASI GURU</h3>
            <p>SIM ISTIQLAL</p>
        </div>
        <div class="user-panel">
            <div class="name"><i class="fas fa-user-circle"></i> <?= htmlspecialchars($_SESSION['full_name'] ?? 'Super Admin') ?></div>
            <div class="role"><?= htmlspecialchars($_SESSION['role'] ?? 'Super Admin') ?> | MU</div>
        </div>
        <ul class="sidebar-menu">
            <li class="menu-title">MAIN NAVIGATION</li>
            <li class="menu-item <?= $current_page == 'dashboard.php' ? 'active' : '' ?>">
                <a href="dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a>
            </li>
            
            <li class="menu-title">MASTER DATA</li>
            <li class="menu-item <?= $current_page == 'kelola_kelas.php' ? 'active' : '' ?>">
                <a href="kelola_kelas.php"><i class="fas fa-chalkboard"></i> Kelola Kelas</a>
            </li>
            <li class="menu-item <?= $current_page == 'kelola_siswa.php' ? 'active' : '' ?>">
                <a href="kelola_siswa.php"><i class="fas fa-users"></i> Kelola Siswa</a>
            </li>
            <li class="menu-item <?= $current_page == 'import_excel.php' ? 'active' : '' ?>">
                <a href="import_excel.php"><i class="fas fa-file-excel"></i> Import Excel</a>
            </li>
            
            <li class="menu-title">MANAGEMENT</li>
            <li class="menu-item <?= $current_page == 'kelola_user.php' ? 'active' : '' ?>">
                <a href="kelola_user.php"><i class="fas fa-user-cog"></i> Kelola User</a>
            </li>
            
            <li class="menu-title">MONITORING</li>
            <li class="menu-item <?= $current_page == 'monitoring.php' ? 'active' : '' ?>">
                <a href="monitoring.php"><i class="fas fa-chart-line"></i> Monitoring</a>
            </li>
            
            <li class="menu-title">SUPERVISI</li>
            <li class="menu-item <?= $current_page == 'supervisi.php' ? 'active' : '' ?>">
                <a href="supervisi.php"><i class="fas fa-clipboard-list"></i> Buat Supervisi</a>
            </li>
        </ul>
    </div>

    <!-- Content -->
    <div class="content-wrapper">
        <div class="content-header">
            <h1><i class="fas fa-calendar-alt"></i> Buat Jadwal Supervisi</h1>
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                <li class="breadcrumb-item active">Buat Supervisi</li>
            </ol>
        </div>

        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card-custom">
                    <div class="card-header-custom">
                        <i class="fas fa-edit"></i> Form Jadwal Supervisi
                    </div>
                    <div style="padding:25px;">
                        <form method="POST">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label><i class="fas fa-user"></i> Pilih Guru <span class="text-danger">*</span></label>
                                        <select name="teacher_id" class="form-select" required>
                                            <option value="">-- Pilih Guru --</option>
                                            <?php while($guru = mysqli_fetch_assoc($guru_list)): ?>
                                            <option value="<?= $guru['id'] ?>"><?= $guru['full_name'] ?> (<?= $guru['unit'] ?>)</option>
                                            <?php endwhile; ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-calendar"></i> Tanggal Supervisi <span class="text-danger">*</span></label>
                                        <input type="date" name="schedule_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-clock"></i> Jam Supervisi <span class="text-danger">*</span></label>
                                        <input type="time" name="schedule_time" class="form-control" value="08:00" required>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label><i class="fas fa-chalkboard"></i> Kelas <span class="text-danger">*</span></label>
                                <select name="class_id" class="form-select" required>
                                    <option value="">-- Pilih Kelas --</option>
                                    <?php while($kelas = mysqli_fetch_assoc($kelas_list)): ?>
                                    <option value="<?= $kelas['id'] ?>"><?= $kelas['name'] ?> (<?= $kelas['unit'] ?>)</option>
                                    <?php endwhile; ?>
                                </select>
                            </div>
                            <div class="form-group">
                                <label><i class="fas fa-comment"></i> Catatan</label>
                                <textarea name="notes" class="form-control" rows="3" placeholder="Isi catatan supervisi (opsional)"></textarea>
                            </div>
                            <button type="submit" name="simpan" class="btn-save">
                                <i class="fas fa-save"></i> Simpan Jadwal
                            </button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <footer class="main-footer">
            <strong>&copy; <?= date('Y') ?> SIM ISTIQLAL - Sistem Administrasi Guru</strong>
        </footer>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>