<?php
// admin/denemesonuc/index.php
require_once '../../includes/db.php';
require_once '../header.php';

// --- İSTATİSTİKLERİ ÇEKELİM ---
// 1. Toplam Tanımlı Sınav Sayısı
$totalExams = $db->query("SELECT COUNT(*) FROM exam_analysis_exams")->fetchColumn();

// 2. Toplam Öğrenci Analizi (Kaç kez form doldurulmuş)
$totalResults = $db->query("SELECT COUNT(*) FROM exam_analysis_student_results")->fetchColumn();

// 3. Toplam Konu Sayısı
$totalTopics = $db->query("SELECT COUNT(*) FROM konular")->fetchColumn();

// 4. Son Eklenen 5 Sınav
$lastExams = $db->query("SELECT e.id, p.name, e.exam_type, e.created_at 
                         FROM exam_analysis_exams e 
                         JOIN products p ON e.product_id = p.id 
                         ORDER BY e.id DESC LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="page-wrapper">
    <div class="page-breadcrumb">
        <div class="row">
            <div class="col-12 d-flex no-block align-items-center">
                <h4 class="page-title"><i class="mdi mdi-view-dashboard"></i> Sınav Analiz Modülü Yönetimi</h4>
            </div>
        </div>
    </div>

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-lg-4 col-xlg-3">
                <div class="card card-hover">
                    <div class="box bg-cyan text-center text-white" style="border-radius: 12px;">
                        <h1 class="font-light"><i class="mdi mdi-library-books"></i></h1>
                        <h6 class="mb-0 text-dark fw-bold"><?= $totalExams ?> Tanımlı Sınav</h6>
                    </div>
                </div>
            </div>
            <div class="col-md-6 col-lg-4 col-xlg-3">
                <div class="card card-hover">
                    <div class="box bg-success text-center text-white" style="border-radius: 12px;">
                        <h1 class="font-light"><i class="mdi mdi-account-check"></i></h1>
                        <h6 class="text-white"><?= $totalResults ?> Öğrenci Analizi</h6>
                    </div>
                </div>
            </div>
            <div class="col-md-6 col-lg-4 col-xlg-3">
                <div class="card card-hover">
                    <div class="box bg-warning text-center text-white" style="border-radius: 12px;">
                        <h1 class="font-light"><i class="mdi mdi-format-list-bulleted-type"></i></h1>
                        <h6 class="text-white"><?= $totalTopics ?> Aktif Konu</h6>
                    </div>
                </div>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-12">
                <h5 class="fw-bold mb-3"><i class="mdi mdi-rocket me-2"></i>Hızlı İşlemler</h5>
            </div>
            <div class="col-md-4 mb-3">
                <a href="sonuc-ekle.php" class="card card-hover border-start border-primary border-4 shadow-sm text-decoration-none">
                    <div class="card-body d-flex align-items-center">
                        <div class="btn btn-primary btn-circle btn-lg"><i class="mdi mdi-plus"></i></div>
                        <div class="ms-3">
                            <h5 class="mb-0 text-dark fw-bold">Yeni Cevap Anahtarı</h5>
                            <small class="text-muted">Excel veya Manuel giriş yapın.</small>
                        </div>
                    </div>
                </a>
            </div>
            <div class="col-md-4 mb-3">
                <a href="girilmis-denemeler.php" class="card card-hover border-start border-success border-4 shadow-sm text-decoration-none">
                    <div class="card-body d-flex align-items-center">
                        <div class="btn btn-success btn-circle btn-lg text-white"><i class="mdi mdi-settings"></i></div>
                        <div class="ms-3">
                            <h5 class="mb-0 text-dark fw-bold">Sınavları Yönet</h5>
                            <small class="text-muted">Düzenle, Sil veya İptal Et.</small>
                        </div>
                    </div>
                </a>
            </div>
            <div class="col-md-4 mb-3">
                <a href="konular.php" class="card card-hover border-start border-warning border-4 shadow-sm text-decoration-none">
                    <div class="card-body d-flex align-items-center">
                        <div class="btn btn-warning btn-circle btn-lg text-white"><i class="mdi mdi-database-edit"></i></div>
                        <div class="ms-3">
                            <h5 class="mb-0 text-dark fw-bold">Konu Havuzu</h5>
                            <small class="text-muted">Ders ve konu başlıklarını düzenleyin.</small>
                        </div>
                    </div>
                </a>
            </div>
        </div>

        <div class="row mt-4">
            <div class="col-md-12">
                <div class="card shadow-sm border-0">
                    <div class="card-header bg-white py-3">
                        <h5 class="mb-0 fw-bold text-dark"><i class="mdi mdi-clock-outline me-2"></i>Son Eklenen 5 Sınav</h5>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light text-uppercase" style="font-size: 11px;">
                                <tr>
                                    <th>Sınav Adı</th>
                                    <th>Tür</th>
                                    <th>Eklenme Tarihi</th>
                                    <th class="text-end">İşlem</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($lastExams as $le): ?>
                                <tr>
                                    <td class="fw-bold"><?= htmlspecialchars($le['name']) ?></td>
                                    <td><span class="badge bg-light text-primary border border-primary"><?= $le['exam_type'] ?></span></td>
                                    <td class="text-muted small"><?= date('d.m.Y H:i', strtotime($le['created_at'])) ?></td>
                                    <td class="text-end">
                                        <button class="btn btn-sm btn-outline-secondary" onclick="window.location.href='girilmis-denemeler.php'">
                                            <i class="mdi mdi-eye"></i> Yönet
                                        </button>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                                <?php if(empty($lastExams)): ?>
                                    <tr><td colspan="4" class="text-center py-4 text-muted">Henüz sınav eklenmemiş.</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .card-hover:hover {
        transform: translateY(-3px);
        transition: all 0.2s ease;
        box-shadow: 0 5px 15px rgba(0,0,0,0.1) !important;
    }
    .btn-circle.btn-lg {
        width: 50px;
        height: 50px;
        padding: 10px 10px;
        border-radius: 50%;
        font-size: 18px;
        line-height: 1.33;
    }
    .box {
        padding: 20px;
        border-radius: 12px;
    }
</style>

<?php require_once '../footer.php'; ?>