<?php
/**
 * Kullanıcı Paneli - Deneme Analiz Merkezi
 * Konum: /magaza/kullanici/deneme-analiz.php
 */

require_once '../includes/db.php';
require_once '../includes/functions.php';

// Proje ana dizini
$kok_dizin = "/magaza/";

// Oturum Kontrolü
if (!isset($_SESSION['user_id'])) {
    header("Location: " . $kok_dizin . "giris.php");
    exit;
}

$user_id = (int)$_SESSION['user_id'];

// SQL Sorgusu: Teslim edilen, iade edilmeyen ve cevap anahtarı olan denemeler
$sql = "SELECT 
            p.id as product_id, p.name as product_name, p.exam_type,
            b.name as brand_name, e.id as exam_id, o.id as order_id,
            o.order_number, res.net_score, res.id as result_id
        FROM orders o
        JOIN order_items oi ON o.id = oi.order_id
        JOIN products p ON oi.product_id = p.id
        LEFT JOIN brands b ON p.brand_id = b.id
        JOIN exam_analysis_exams e ON p.id = e.product_id
        LEFT JOIN order_returns orr ON o.id = orr.order_id
        LEFT JOIN exam_analysis_student_results res ON (res.exam_id = e.id AND res.user_id = :user_id)
        WHERE o.user_id = :user_id 
        AND o.status = 'delivered' 
        AND o.is_cancel_requested = 0
        AND orr.id IS NULL
        AND p.type = 'single_exam'
        ORDER BY o.id DESC";

$stmt = $db->prepare($sql);
$stmt->execute([':user_id' => $user_id]);
$denemeler = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = "Deneme Analizlerim";
require_once '../header.php';
?>

<main class="container py-5 text-dark">
    <div class="row">
        
        <div class="col-lg-3 mb-4">
            <?php include 'sidebar.php'; ?>
        </div>

        <div class="col-lg-9">
            
            <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
                <div>
                    <h2 class="h4 fw-bold mb-0 text-primary">
                        <i class="bi bi-graph-up-arrow me-2"></i>Deneme Analizlerim
                    </h2>
                    <p class="text-muted small mt-1 mb-0">Çözdüğünüz denemelerin sonuçlarını girerek gelişiminizi takip edin.</p>
                </div>
            </div>

            <div class="alert alert-info border-0 shadow-sm rounded-4 mb-4 d-flex align-items-center">
                <i class="bi bi-info-circle-fill fs-4 me-3"></i>
                <div class="small">
                    Sadece kargosu <b>teslim edilen</b> denemeler listelenir. "Sonuçları Gir" butonuna tıklayarak dijital optik formu doldurabilirsiniz.
                </div>
            </div>

            <div class="row g-4">
                <?php if (count($denemeler) > 0): ?>
                    <?php foreach ($denemeler as $dn): ?>
                        <div class="col-md-6 col-xl-4">
                            <div class="card border-0 shadow-sm rounded-4 h-100 overflow-hidden transition-card">
                                <div class="card-body p-4">
                                    <div class="d-flex justify-content-between align-items-center mb-3">
                                        <span class="badge bg-primary bg-opacity-10 text-primary px-3 py-2 rounded-pill fw-bold" style="font-size: 0.7rem;">
                                            <?= htmlspecialchars($dn['exam_type']) ?>
                                        </span>
                                        <span class="text-muted small fw-bold">#<?= $dn['order_number'] ?></span>
                                    </div>

                                    <h6 class="fw-bold text-dark mb-1 lh-base" style="min-height: 40px;">
                                        <?= htmlspecialchars($dn['product_name']) ?>
                                    </h6>
                                    <p class="text-muted small mb-4">
                                        <i class="bi bi-bookmark-fill me-1 text-secondary"></i>
                                        <?= htmlspecialchars($dn['brand_name'] ?? 'Yayınevi Belirtilmemiş') ?>
                                    </p>

                                    <?php if ($dn['result_id']): ?>
                                        <div class="text-center py-3 rounded-4 bg-success bg-opacity-10 border border-success border-opacity-25 mb-3">
                                            <span class="d-block small text-success fw-bold mb-1" style="font-size: 0.65rem;">ANALİZ TAMAMLANDI</span>
                                            <h3 class="mb-0 text-success fw-bold"><?= number_format($dn['net_score'], 2) ?> <small class="fs-6">Net</small></h3>
                                        </div>
                                        <div class="row g-2">
                                            <div class="col-8">
                                                <a href="kullanici/deneme-analiz-sonuc.php?id=<?= $dn['result_id'] ?>" class="btn btn-primary btn-sm w-100 rounded-pill fw-bold py-2 shadow-sm">Karneyi Gör</a>
                                            </div>
                                            <div class="col-4">
                                                <a href="kullanici/deneme-optik-form.php?exam_id=<?= $dn['exam_id'] ?>&order_id=<?= $dn['order_id'] ?>" class="btn btn-outline-secondary btn-sm w-100 rounded-pill py-2" title="Güncelle">
                                                    <i class="bi bi-arrow-repeat"></i>
                                                </a>
                                            </div>
                                        </div>
                                    <?php else: ?>
                                        <div class="text-center py-3 rounded-4 bg-warning bg-opacity-10 border border-warning border-opacity-25 mb-3">
                                            <span class="d-block small text-warning fw-bold mb-1" style="font-size: 0.65rem;">SONUÇ BEKLİYOR</span>
                                            <p class="small text-muted mb-0">Cevaplarını gir, analizi gör.</p>
                                        </div>
                                        <a href="kullanici/deneme-optik-form.php?exam_id=<?= $dn['exam_id'] ?>&order_id=<?= $dn['order_id'] ?>" class="btn btn-success btn-sm w-100 rounded-pill fw-bold py-2 text-white shadow-sm">
                                            <i class="bi bi-plus-lg me-1"></i>Sonuçları Gir
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <div class="col-12">
                        <div class="text-center py-5 bg-white rounded-4 shadow-sm border">
                            <i class="bi bi-journal-x text-muted opacity-25" style="font-size: 4rem;"></i>
                            <h5 class="fw-bold mt-3 text-dark">Analiz Edilecek Deneme Bulunamadı.</h5>
                            <p class="text-muted">Satın aldığınız denemeler kargo ile teslim edildikten sonra burada görünecektir.</p>
                        </div>
                    </div>
                <?php endif; ?>
            </div>

        </div>
    </div>
</main>

<style>
    .transition-card {
        transition: all 0.3s ease;
    }
    .transition-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 25px rgba(0,0,0,0.1) !important;
    }
</style>

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