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

// Filter berdasarkan unit jika role adalah Kepala
$filter = "";
if($_SESSION['role'] == 'Kepala') {
    $filter = "AND guru.unit = '{$_SESSION['unit']}'";
}

// Query ambil semua jadwal supervisi
$query = "SELECT s.*, 
          guru.full_name as guru_name, 
          guru.unit as guru_unit,
          guru.id as guru_id,
          supervisor.full_name as supervisor_name,
          c.name as class_name 
    FROM supervisions s 
    JOIN users guru ON s.teacher_id = guru.id 
    JOIN users supervisor ON s.supervisor_id = supervisor.id 
    JOIN classes c ON s.class_id = c.id 
    WHERE 1=1 $filter 
    ORDER BY s.schedule_date DESC, s.schedule_time ASC";

$jadwal = mysqli_query($conn, $query);

// Proses update status supervisi
if(isset($_POST['update_status'])) {
    $id = $_POST['id'];
    $status = $_POST['status'];
    mysqli_query($conn, "UPDATE supervisions SET status='$status' WHERE id=$id");
    echo "<script>alert('Status berhasil diupdate!'); window.location='jadwal_supervisi.php';</script>";
}

// Proses hapus jadwal
if(isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    mysqli_query($conn, "DELETE FROM supervisions WHERE id=$id");
    echo "<script>alert('Jadwal berhasil dihapus!'); window.location='jadwal_supervisi.php';</script>";
}

// Proses detail - ambil data untuk ditampilkan
if(isset($_GET['detail'])) {
    $detail_id = $_GET['detail'];
    $detail_query = "SELECT s.*, 
              guru.full_name as guru_name, 
              guru.unit as guru_unit,
              supervisor.full_name as supervisor_name,
              c.name as class_name 
        FROM supervisions s 
        JOIN users guru ON s.teacher_id = guru.id 
        JOIN users supervisor ON s.supervisor_id = supervisor.id 
        JOIN classes c ON s.class_id = c.id 
        WHERE s.id = '$detail_id'";
    $detail_result = mysqli_query($conn, $detail_query);
    $detail = mysqli_fetch_assoc($detail_result);
}

// Proses edit status - tampilkan form edit status
if(isset($_GET['edit_status'])) {
    $edit_id = $_GET['edit_status'];
    $edit_query = "SELECT s.*, guru.full_name as guru_name 
        FROM supervisions s 
        JOIN users guru ON s.teacher_id = guru.id 
        WHERE s.id = '$edit_id'";
    $edit_result = mysqli_query($conn, $edit_query);
    $edit = mysqli_fetch_assoc($edit_result);
}

// Statistik
$total_jadwal = mysqli_num_rows($jadwal);
mysqli_data_seek($jadwal, 0);

$jadwal_selesai = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM supervisions s JOIN users guru ON s.teacher_id=guru.id WHERE s.status='Completed' $filter"));
$jadwal_terjadwal = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM supervisions s JOIN users guru ON s.teacher_id=guru.id WHERE s.status='Scheduled' $filter"));
?>

<!DOCTYPE html>
<html>
<head>
    <title>Jadwal Supervisi - SIM ISTIQLAL</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <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">
    <link rel="stylesheet" href="assets/css/theme-istiqlal-green.css">
    <style>
        /* Reset & Base Styles */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background: #f0f2f5;
        }
        
        .content-wrapper {
            background: #f0f2f5;
            padding: 0 20px;
        }
        
        /* Card Styles */
        .card {
            border-radius: 12px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
            margin-bottom: 25px;
            border: none;
            overflow: hidden;
        }
        
        .card-header {
            padding: 15px 20px;
            border-bottom: 1px solid #e9ecef;
            background: linear-gradient(135deg, #0b5e4e 0%, #10b981 100%);
            color: white;
        }
        
        .card-header .card-title {
            font-size: 16px;
            font-weight: 600;
            margin: 0;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .card-header .card-title i {
            font-size: 18px;
        }
        
        .card-header .card-tools .btn {
            background: rgba(255,255,255,0.2);
            border: none;
            color: white;
            transition: all 0.3s;
        }
        
        .card-header .card-tools .btn:hover {
            background: rgba(255,255,255,0.3);
            transform: translateY(-2px);
        }
        
        /* Stats Cards Grid */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 25px;
        }
        
        .stats-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            position: relative;
            overflow: hidden;
            transition: transform 0.2s, box-shadow 0.2s;
            box-shadow: 0 2px 8px rgba(0,0,0,0.06);
        }
        
        .stats-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(0,0,0,0.1);
        }
        
        .stats-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 4px;
            height: 100%;
        }
        
        .stats-card.info::before {
            background: linear-gradient(135deg, #0ea5e9 0%, #3b82f6 100%);
        }
        
        .stats-card.warning::before {
            background: linear-gradient(135deg, #f59e0b 0%, #f97316 100%);
        }
        
        .stats-card.success::before {
            background: linear-gradient(135deg, #10b981 0%, #0b5e4e 100%);
        }
        
        .stats-card .stats-number {
            font-size: 32px;
            font-weight: 800;
            margin-bottom: 5px;
        }
        
        .stats-card.info .stats-number {
            color: #0ea5e9;
        }
        
        .stats-card.warning .stats-number {
            color: #f59e0b;
        }
        
        .stats-card.success .stats-number {
            color: #10b981;
        }
        
        .stats-card .stats-label {
            font-size: 13px;
            color: #6c757d;
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .stats-card .stats-icon {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 48px;
            opacity: 0.1;
        }
        
        /* Table Styles */
        .table-responsive {
            overflow-x: auto;
        }
        
        .table {
            margin-bottom: 0;
            min-width: 900px;
        }
        
        .table thead th {
            background: linear-gradient(135deg, #0b5e4e 0%, #10b981 100%);
            color: white;
            font-weight: 600;
            padding: 12px 15px;
            font-size: 13px;
            border: none;
            white-space: nowrap;
        }
        
        .table tbody td {
            padding: 12px 15px;
            vertical-align: middle;
            font-size: 13px;
            border-color: #e9ecef;
        }
        
        .table tbody tr:hover {
            background-color: #f8f9fa;
        }
        
        /* Badge Styles */
        .badge-status {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 11px;
            font-weight: 600;
        }
        
        .badge-scheduled {
            background: #fef3c7;
            color: #d97706;
        }
        
        .badge-completed {
            background: #d1fae5;
            color: #065f46;
        }
        
        .badge-cancelled {
            background: #fee2e2;
            color: #dc2626;
        }
        
        .badge-unit {
            background: #e0e7ff;
            color: #4338ca;
            padding: 4px 10px;
            border-radius: 12px;
            font-size: 11px;
        }
        
        /* Button Group */
        .btn-group {
            display: flex;
            gap: 6px;
            flex-wrap: wrap;
        }
        
        .btn-sm {
            padding: 5px 12px;
            font-size: 11px;
            border-radius: 6px;
            transition: all 0.2s;
        }
        
        .btn-sm:hover {
            transform: translateY(-2px);
        }
        
        /* Detail Card */
        .detail-card {
            background: white;
            border-radius: 12px;
            padding: 25px;
            margin-top: 20px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
            border-left: 4px solid #10b981;
        }
        
        .detail-title {
            font-size: 18px;
            font-weight: 700;
            color: #1f2937;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .info-row {
            display: flex;
            padding: 12px 0;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .info-label {
            width: 180px;
            font-weight: 600;
            color: #4b5563;
        }
        
        .info-value {
            flex: 1;
            color: #1f2937;
        }
        
        /* Header Styles */
        .content-header {
            padding: 20px 0;
        }
        
        .content-header h1 {
            font-size: 24px;
            font-weight: 700;
            color: #1f2937;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .content-header h1 i {
            color: #10b981;
        }
        
        /* Alert Unit */
        .alert-unit {
            background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
            border: none;
            border-left: 4px solid #0b5e4e;
            border-radius: 8px;
            color: #065f46;
            font-size: 13px;
            padding: 12px 15px;
            margin-bottom: 20px;
        }
        
        /* Form Styles */
        .form-control {
            border-radius: 8px;
            border: 1px solid #e2e8f0;
            padding: 8px 12px;
            font-size: 13px;
        }
        
        .form-control:focus {
            border-color: #10b981;
            box-shadow: 0 0 0 3px rgba(16,185,129,0.1);
        }
        
        /* Footer */
        .main-footer {
            background: white;
            padding: 15px;
            text-align: center;
            font-size: 12px;
            color: #6c757d;
            border-top: 1px solid #e9ecef;
            margin-top: 20px;
        }
        
        @media (max-width: 768px) {
            .content-wrapper {
                padding: 0 10px;
            }
            
            .stats-grid {
                grid-template-columns: 1fr;
            }
            
            .info-label {
                width: 120px;
                font-size: 12px;
            }
            
            .btn-group {
                flex-direction: column;
            }
            
            .btn-sm {
                width: 100%;
            }
        }
    </style>
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
    <?php include 'sidebar.php'; ?>
    
    <div class="content-wrapper">
        <div class="content-header">
            <div class="container-fluid">
                <div class="row align-items-center mb-3">
                    <div class="col-md-8">
                        <h1>
                            <i class="fas fa-calendar-alt"></i>
                            Jadwal Supervisi
                        </h1>
                    </div>
                    <div class="col-md-4">
                        <nav aria-label="breadcrumb">
                            <ol class="breadcrumb justify-content-md-end mb-0">
                                <li class="breadcrumb-item"><a href="dashboard.php" style="color: #0b5e4e;">Home</a></li>
                                <li class="breadcrumb-item active" aria-current="page">Jadwal Supervisi</li>
                            </ol>
                        </nav>
                    </div>
                </div>
                
                <?php if($_SESSION['role'] == 'Kepala'): ?>
                <div class="alert-unit">
                    <i class="fas fa-building me-2"></i> 
                    Menampilkan data untuk unit: <strong><?= htmlspecialchars($_SESSION['unit']) ?></strong>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <div class="content">
            <div class="container-fluid">
                
                <!-- Statistik Cards -->
                <div class="stats-grid">
                    <div class="stats-card info">
                        <div class="stats-number"><?= $total_jadwal ?></div>
                        <div class="stats-label">Total Jadwal Supervisi</div>
                        <div class="stats-icon"><i class="fas fa-calendar-check"></i></div>
                    </div>
                    <div class="stats-card warning">
                        <div class="stats-number"><?= $jadwal_terjadwal ?></div>
                        <div class="stats-label">Jadwal Tertunda</div>
                        <div class="stats-icon"><i class="fas fa-clock"></i></div>
                    </div>
                    <div class="stats-card success">
                        <div class="stats-number"><?= $jadwal_selesai ?></div>
                        <div class="stats-label">Supervisi Selesai</div>
                        <div class="stats-icon"><i class="fas fa-check-circle"></i></div>
                    </div>
                </div>

                <!-- Tabel Jadwal -->
                <div class="card">
                    <div class="card-header">
                        <div class="card-title">
                            <i class="fas fa-list"></i>
                            Daftar Jadwal Supervisi
                        </div>
                        <div class="card-tools">
                            <a href="supervisi.php" class="btn btn-sm">
                                <i class="fas fa-plus"></i> Buat Jadwal Baru
                            </a>
                        </div>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th width="5%">No</th>
                                        <th width="10%">Tanggal</th>
                                        <th width="8%">Jam</th>
                                        <th width="15%">Guru</th>
                                        <th width="12%">Unit</th>
                                        <th width="12%">Supervisor</th>
                                        <th width="10%">Kelas</th>
                                        <th width="10%">Status</th>
                                        <th width="18%">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    $no = 1;
                                    if(mysqli_num_rows($jadwal) > 0):
                                        while($row = mysqli_fetch_assoc($jadwal)): 
                                            $status_class = '';
                                            $status_text = '';
                                            if($row['status'] == 'Scheduled') {
                                                $status_class = 'badge-scheduled';
                                                $status_text = 'Terjadwal';
                                            } elseif($row['status'] == 'Completed') {
                                                $status_class = 'badge-completed';
                                                $status_text = 'Selesai';
                                            } else {
                                                $status_class = 'badge-cancelled';
                                                $status_text = 'Dibatalkan';
                                            }
                                    ?>
                                    <tr>
                                        <td class="text-center"><?= $no++ ?></td>
                                        <td><?= date('d-m-Y', strtotime($row['schedule_date'])) ?></td>
                                        <td><?= date('H:i', strtotime($row['schedule_time'])) ?> WIB</div>
                                        <td>
                                            <strong><?= htmlspecialchars($row['guru_name']) ?></strong>
                                            <br>
                                            <small class="text-muted">ID: <?= $row['teacher_id'] ?></small>
                                         </div>
                                        <td><span class="badge-unit"><?= htmlspecialchars($row['guru_unit']) ?></span></div>
                                        <td><?= htmlspecialchars($row['supervisor_name']) ?></div>
                                        <td><?= htmlspecialchars($row['class_name']) ?></div>
                                        <td><span class="badge-status <?= $status_class ?>"><?= $status_text ?></span></div>
                                        <td>
                                            <div class="btn-group">
                                                <a href="supervisi/supervisi_menu.php?guru_id=<?= $row['guru_id'] ?>&guru_nama=<?= urlencode($row['guru_name']) ?>&jadwal_id=<?= $row['id'] ?>" class="btn btn-sm btn-success" title="Isi Formulir">
                                                    <i class="fas fa-clipboard-list"></i> Formulir
                                                </a>
                                                <a href="?detail=<?= $row['id'] ?>" class="btn btn-sm btn-info">
                                                    <i class="fas fa-eye"></i> Detail
                                                </a>
                                                <a href="?edit_status=<?= $row['id'] ?>" class="btn btn-sm btn-warning">
                                                    <i class="fas fa-edit"></i> Status
                                                </a>
                                                <a href="jadwal_supervisi.php?hapus=<?= $row['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus jadwal ini?')">
                                                    <i class="fas fa-trash"></i> Hapus
                                                </a>
                                            </div>
                                         </div>
                                     </div>
                                    <?php endwhile; else: ?>
                                    <tr><td colspan="9" class="text-center py-4">
                                        <i class="fas fa-calendar-times fa-2x text-muted mb-2 d-block"></i>
                                        Belum ada jadwal supervisi
                                     </div>
                                    <?php endif; ?>
                                </tbody>
                             </div>
                        </div>
                    </div>
                </div>
                
                <!-- Halaman Detail -->
                <?php if(isset($_GET['detail']) && isset($detail)): 
                    $detail_status_class = '';
                    if($detail['status'] == 'Scheduled') {
                        $detail_status_class = 'badge-scheduled';
                        $detail_status_text = 'Terjadwal';
                    } elseif($detail['status'] == 'Completed') {
                        $detail_status_class = 'badge-completed';
                        $detail_status_text = 'Selesai';
                    } else {
                        $detail_status_class = 'badge-cancelled';
                        $detail_status_text = 'Dibatalkan';
                    }
                ?>
                <div class="detail-card">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <div class="detail-title">
                            <i class="fas fa-info-circle" style="color: #10b981;"></i>
                            Detail Supervisi
                        </div>
                        <a href="jadwal_supervisi.php" class="btn btn-secondary btn-sm">
                            <i class="fas fa-arrow-left"></i> Kembali
                        </a>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="info-row">
                                <div class="info-label">Tanggal Supervisi</div>
                                <div class="info-value">: <?= date('d F Y', strtotime($detail['schedule_date'])) ?></div>
                            </div>
                            <div class="info-row">
                                <div class="info-label">Jam Supervisi</div>
                                <div class="info-value">: <?= date('H:i', strtotime($detail['schedule_time'])) ?> WIB</div>
                            </div>
                            <div class="info-row">
                                <div class="info-label">Guru Yang Disupervisi</div>
                                <div class="info-value">: <?= htmlspecialchars($detail['guru_name']) ?></div>
                            </div>
                            <div class="info-row">
                                <div class="info-label">Unit</div>
                                <div class="info-value">: <?= htmlspecialchars($detail['guru_unit']) ?></div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="info-row">
                                <div class="info-label">Supervisor</div>
                                <div class="info-value">: <?= htmlspecialchars($detail['supervisor_name']) ?></div>
                            </div>
                            <div class="info-row">
                                <div class="info-label">Kelas</div>
                                <div class="info-value">: <?= htmlspecialchars($detail['class_name']) ?></div>
                            </div>
                            <div class="info-row">
                                <div class="info-label">Status</div>
                                <div class="info-value">: <span class="badge-status <?= $detail_status_class ?>"><?= $detail_status_text ?></span></div>
                            </div>
                            <div class="info-row">
                                <div class="info-label">Dibuat Pada</div>
                                <div class="info-value">: <?= date('d F Y H:i', strtotime($detail['created_at'])) ?></div>
                            </div>
                        </div>
                    </div>
                    <div class="mt-3">
                        <div class="info-row" style="border-bottom: none;">
                            <div class="info-label">Catatan Supervisi</div>
                            <div class="info-value">: <?= !empty($detail['notes']) ? nl2br(htmlspecialchars($detail['notes'])) : '<em class="text-muted">Tidak ada catatan</em>' ?></div>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
                
                <!-- Halaman Edit Status -->
                <?php if(isset($_GET['edit_status']) && isset($edit)): ?>
                <div class="detail-card">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <div class="detail-title">
                            <i class="fas fa-edit" style="color: #f59e0b;"></i>
                            Edit Status Supervisi
                        </div>
                        <a href="jadwal_supervisi.php" class="btn btn-secondary btn-sm">
                            <i class="fas fa-times"></i> Batal
                        </a>
                    </div>
                    <form method="POST">
                        <input type="hidden" name="id" value="<?= $edit['id'] ?>">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="info-row">
                                    <div class="info-label">Guru</div>
                                    <div class="info-value">: <?= htmlspecialchars($edit['guru_name']) ?></div>
                                </div>
                                <div class="info-row">
                                    <div class="info-label">Tanggal</div>
                                    <div class="info-value">: <?= date('d F Y', strtotime($edit['schedule_date'])) ?></div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="info-row">
                                    <div class="info-label">Status Saat Ini</div>
                                    <div class="info-value">
                                        <select name="status" class="form-control" style="width: auto; display: inline-block;">
                                            <option value="Scheduled" <?= $edit['status']=='Scheduled'?'selected':'' ?>>Terjadwal</option>
                                            <option value="Completed" <?= $edit['status']=='Completed'?'selected':'' ?>>Selesai</option>
                                            <option value="Cancelled" <?= $edit['status']=='Cancelled'?'selected':'' ?>>Dibatalkan</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="text-center mt-4">
                            <button type="submit" name="update_status" class="btn btn-success">
                                <i class="fas fa-save"></i> Simpan Perubahan
                            </button>
                            <a href="jadwal_supervisi.php" class="btn btn-secondary">
                                <i class="fas fa-times"></i> Batal
                            </a>
                        </div>
                    </form>
                </div>
                <?php endif; ?>
                
            </div>
        </div>
    </div>

    <footer class="main-footer">
        <strong>&copy; <?= date('Y') ?> SIM ISTIQLAL - Sistem Administrasi Guru</strong>
    </footer>
</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>
<script src="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/js/adminlte.min.js"></script>
</body>
</html>