<?php
session_start();
include 'db_connection.php';

/* ================= AUTH ================= */
if (!isset($_SESSION['student_id']) || $_SESSION['role'] !== 'student') {
    header("Location: student_login.php?error=Unauthorized access");
    exit();
}

$student_id = $_SESSION['student_id'];

/* ================= STUDENT INFO ================= */
$stmt = $conn->prepare("
SELECT s.name, c.class_name
FROM students s
JOIN classes c ON s.class_id = c.class_id
WHERE s.student_id = ?
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$student = $stmt->get_result()->fetch_assoc();

/* ================= ATTENDANCE SUMMARY ================= */
$stmt = $conn->prepare("
SELECT 
    COUNT(*) AS total_days,
    SUM(status='present') AS present_days,
    SUM(status='absent') AS absent_days
FROM student_attendance
WHERE student_id = ?
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$summary = $stmt->get_result()->fetch_assoc();

$total_days = $summary['total_days'] ?? 0;
$present_days = $summary['present_days'] ?? 0;
$absent_days = $summary['absent_days'] ?? 0;

$attendance_percent = ($total_days > 0)
    ? round(($present_days / $total_days) * 100)
    : 0;

/* ================= ATTENDANCE HISTORY ================= */
$stmt = $conn->prepare("
SELECT date, status, created_at
FROM student_attendance
WHERE student_id = ?
ORDER BY date DESC
LIMIT 30
");
$stmt->bind_param("i", $student_id);
$stmt->execute();
$attendance = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Attendance</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

<style>
body {
    background: #f4f6f9;
    font-family: 'Segoe UI', sans-serif;
}

/* ================= SIDEBAR ================= */
.sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: 250px;
    height: 100vh;
    background: linear-gradient(180deg, #1f2d3d, #111827);
    color: white;
    padding-top: 20px;
}

.sidebar h4 {
    text-align: center;
}

.sidebar p {
    text-align: center;
    font-size: 14px;
    color: #cbd5e1;
}

.sidebar a {
    display: block;
    padding: 12px 20px;
    color: white;
    text-decoration: none;
    transition: 0.3s;
}

.sidebar a:hover {
    background: #374151;
    padding-left: 25px;
}

.sidebar i {
    margin-right: 10px;
}

/* ================= MAIN ================= */
.main {
    margin-left: 250px;
    padding: 20px;
}

/* ================= CARDS ================= */
.card {
    border: none;
    border-radius: 12px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.08);
}

.badge-present {
    background: #16a34a;
}

.badge-absent {
    background: #dc2626;
}
</style>
</head>

<body>

<!-- ================= SIDEBAR ================= -->
<div class="sidebar">
    <h4><i class="fas fa-user-graduate"></i></h4>
    <p><?php echo htmlspecialchars($student['name']); ?></p>

    <a href="student_dashboard.php"><i class="fas fa-home"></i> Dashboard</a>
    <a href="view_homework.php"><i class="fas fa-book"></i> Homework</a>
    <a href="view_results.php"><i class="fas fa-chart-line"></i> Results</a>
    <a href="attendance.php"><i class="fas fa-check-circle"></i> Attendance</a>
    <a href="view_fees.php"><i class="fas fa-wallet"></i> Fees</a>
    <a href="view_timetable.php"><i class="fas fa-calendar"></i> Timetable</a>
    <a href="student_profile.php"><i class="fas fa-user"></i> Profile</a>
    <a href="logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a>
</div>

<!-- ================= MAIN CONTENT ================= -->
<div class="main">

<!-- HEADER -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <h3><i class="fas fa-check-circle"></i> My Attendance</h3>
    <div class="text-muted">
        <?php echo $student['class_name'] ?? 'Class'; ?>
    </div>
</div>

<!-- SUMMARY -->
<div class="row g-3 mb-4">

    <div class="col-md-3">
        <div class="card p-3 text-center">
            <h6>Total Days</h6>
            <h3><?php echo $total_days; ?></h3>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card p-3 text-center">
            <h6>Present</h6>
            <h3 class="text-success"><?php echo $present_days; ?></h3>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card p-3 text-center">
            <h6>Absent</h6>
            <h3 class="text-danger"><?php echo $absent_days; ?></h3>
        </div>
    </div>

    <div class="col-md-3">
        <div class="card p-3 text-center">
            <h6>Attendance %</h6>
            <h3 class="text-primary"><?php echo $attendance_percent; ?>%</h3>
        </div>
    </div>

</div>

<!-- ATTENDANCE HISTORY -->
<div class="card p-4">
    <h5><i class="fas fa-calendar-alt"></i> Attendance History</h5>
    <hr>

    <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Status</th>
                    <th>Marked On</th>
                </tr>
            </thead>

            <tbody>
            <?php if ($attendance->num_rows > 0): ?>
                <?php while ($row = $attendance->fetch_assoc()): ?>
                    <tr>
                        <td><?php echo $row['date']; ?></td>
                        <td>
                            <?php if ($row['status'] === 'present'): ?>
                                <span class="badge badge-present">Present</span>
                            <?php else: ?>
                                <span class="badge badge-absent">Absent</span>
                            <?php endif; ?>
                        </td>
                        <td><?php echo $row['created_at']; ?></td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr>
                    <td colspan="3" class="text-center">No attendance records found</td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>

</div>

</div>

</body>
</html>