<?php 
require_once 'db_connect.php';
include 'includes/user_header.php'; 
if(file_exists('includes/sidebar.php')) { include 'includes/sidebar.php'; } 

$student_id = $_SESSION['student_id'] ?? null;
if (!$student_id) { echo "<script>window.location.href='login.php';</script>"; exit; }

$total = 0; $present = 0; $absent = 0; $percent = 0; $logs = [];

try {
    $stmt = $pdo->prepare("SELECT status, COUNT(*) as count FROM attendance WHERE student_id = ? GROUP BY status");
    $stmt->execute([$student_id]);
    $stats = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); 
    
    $present = $stats['present'] ?? 0;
    $absent = $stats['absent'] ?? 0;
    $total = $present + $absent;
    $percent = ($total > 0) ? round(($present / $total) * 100) : 0;

    $log_stmt = $pdo->prepare("SELECT a.status, a.remarks, a.attendance_date, c.title FROM attendance a JOIN courses c ON a.course_id = c.id WHERE a.student_id = ? ORDER BY a.attendance_date DESC LIMIT 20");
    $log_stmt->execute([$student_id]);
    $logs = $log_stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {}
?>

<style>
    .main-content { background: #f8fafc; min-height: 100vh; padding: 25px; }
    .stat-card { background: #fff; border-radius: 20px; padding: 25px; border: none; box-shadow: 0 10px 30px rgba(0,0,0,0.03); }
    .progress-custom { height: 10px; border-radius: 50px; background: #e2e8f0; overflow: hidden; }
    .progress-bar-custom { background: linear-gradient(90deg, #10b981, #34d399); }
    .status-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 5px; }
</style>

<div class="main-content">
    <h3 class="fw-bold mb-4">My Attendance Analytics</h3>
    
    <div class="row g-4 mb-4">
        <div class="col-md-8">
            <div class="stat-card h-100">
                <div class="d-flex justify-content-between align-items-end mb-3">
                    <div>
                        <h6 class="text-muted fw-bold small text-uppercase">Presence Rate</h6>
                        <h2 class="fw-bold mb-0 text-dark"><?= $percent ?>%</h2>
                    </div>
                    <div class="text-end">
                        <span class="badge bg-light text-success border">Target: 80%</span>
                    </div>
                </div>
                <div class="progress progress-custom">
                    <div class="progress-bar progress-bar-custom" style="width: <?= $percent ?>%"></div>
                </div>
                <div class="mt-3 d-flex gap-4">
                    <small class="fw-bold text-muted"><span class="status-dot bg-success"></span> Present: <?= $present ?></small>
                    <small class="fw-bold text-muted"><span class="status-dot bg-danger"></span> Absent: <?= $absent ?></small>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card h-100 d-flex flex-column justify-content-center text-center">
                <div class="display-6 fw-bold text-primary"><?= $total ?></div>
                <div class="small fw-bold text-muted text-uppercase">Total Sessions</div>
            </div>
        </div>
    </div>

    <div class="stat-card">
        <h5 class="fw-bold mb-4">History Log</h5>
        <div class="table-responsive">
            <table class="table align-middle">
                <thead><tr class="text-muted small"><th>SESSION DATE</th><th>COURSE</th><th>STATUS</th></tr></thead>
                <tbody>
                    <?php foreach($logs as $log): ?>
                    <tr>
                        <td class="fw-bold text-dark"><?= date('d M, Y', strtotime($log['attendance_date'])) ?></td>
                        <td><?= htmlspecialchars($log['title']) ?></td>
                        <td>
                            <?php if($log['status'] == 'present'): ?>
                                <span class="badge bg-success-subtle text-success px-3 rounded-pill">Present</span>
                            <?php else: ?>
                                <span class="badge bg-danger-subtle text-danger px-3 rounded-pill">Absent</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php include 'includes/user_footer.php'; ?>