<?php
session_start();

// Check if user is logged in and is a student
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'student') {
    header('Location: ../login.php');
    exit();
}

require_once '../config/database.php';

// Get student details
$stmt = $pdo->prepare("SELECT s.*, c.name as class_name, c.section as class_section FROM students s LEFT JOIN classes c ON s.class_id = c.id WHERE s.id = ?");
$stmt->execute([$_SESSION['student_id']]);
$student = $stmt->fetch();

if (!$student) {
    header('Location: ../login.php');
    exit();
}

// Get attendance records for the student
$attendance = [];
$month = isset($_GET['month']) ? $_GET['month'] : date('Y-m');
$attendance_stmt = $pdo->prepare("
    SELECT * FROM attendance 
    WHERE student_id = ? 
    AND DATE_FORMAT(date, '%Y-%m') = ?
    ORDER BY date DESC
");
$attendance_stmt->execute([$student['id'], $month]);
$attendance = $attendance_stmt->fetchAll();

// Calculate attendance statistics
$total_days = count($attendance);
$present_days = 0;
$absent_days = 0;

foreach ($attendance as $record) {
    if ($record['status'] == 'present') {
        $present_days++;
    } elseif ($record['status'] == 'absent') {
        $absent_days++;
    }
}

$attendance_percentage = $total_days > 0 ? round(($present_days / $total_days) * 100, 2) : 0;
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>উপস্থিতি - <?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body { 
            font-family: 'Hind Siliguri', sans-serif; 
            background-color: #f8f9fa;
        }
        .attendance-card {
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .stat-card {
            text-align: center;
            padding: 15px;
            border-radius: 10px;
        }
        .present {
            background-color: #d4edda;
            color: #155724;
        }
        .absent {
            background-color: #f8d7da;
            color: #721c24;
        }
        .on-leave {
            background-color: #fff3cd;
            color: #856404;
        }
        .holiday {
            background-color: #d1ecf1;
            color: #0c5460;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="../student_dashboard.php"><i class="fas fa-arrow-left"></i> ছাত্র ড্যাশবোর্ড</a>
            <div class="navbar-nav ms-auto">
                <span class="navbar-text me-3">
                    <i class="fas fa-user-graduate"></i> <?php echo htmlspecialchars($_SESSION['user_name']); ?>
                </span>
                <a class="nav-link" href="../logout.php">
                    <i class="fas fa-sign-out-alt"></i> লগআউট
                </a>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row">
            <div class="col-md-12">
                <h2><i class="fas fa-calendar-check"></i> উপস্থিতি রেকর্ড</h2>
                <p>ছাত্র নাম: <strong><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></strong> | 
                   ছাত্র আইডি: <strong><?php echo htmlspecialchars($student['student_id']); ?></strong> |
                   ক্লাস: <strong><?php echo htmlspecialchars($student['class_name'] . ' - ' . $student['class_section']); ?></strong></p>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-12">
                <form method="GET" class="form-inline">
                    <div class="form-group mb-2">
                        <label for="month" class="mr-2">মাস নির্বাচন করুন:</label>
                        <input type="month" class="form-control mx-2" id="month" name="month" value="<?php echo $month; ?>" onchange="this.form.submit()">
                    </div>
                </form>
            </div>
        </div>

        <!-- Attendance Statistics -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card stat-card bg-light">
                    <div class="card-body">
                        <h4 class="card-title"><?php echo $total_days; ?></h4>
                        <p class="card-text">মোট দিন</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card stat-card present">
                    <div class="card-body">
                        <h4 class="card-title"><?php echo $present_days; ?></h4>
                        <p class="card-text">উপস্থিত</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card stat-card absent">
                    <div class="card-body">
                        <h4 class="card-title"><?php echo $absent_days; ?></h4>
                        <p class="card-text">অনুপস্থিত</p>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card stat-card bg-success text-white">
                    <div class="card-body">
                        <h4 class="card-title"><?php echo $attendance_percentage; ?>%</h4>
                        <p class="card-text">উপস্থিতির হার</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Attendance Records -->
        <div class="card attendance-card">
            <div class="card-header bg-info text-white">
                <h5><i class="fas fa-list"></i> উপস্থিতির বিস্তারিত</h5>
            </div>
            <div class="card-body">
                <?php if (!empty($attendance)): ?>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>তারিখ</th>
                                    <th>বৃহস্পতিবার</th>
                                    <th>উপস্থিতি</th>
                                    <th>ক্লাস</th>
                                    <th>বিষয়</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($attendance as $record): ?>
                                    <tr>
                                        <td><?php echo date('d/m/Y', strtotime($record['date'])); ?></td>
                                        <td><?php echo date('l', strtotime($record['date'])); ?></td>
                                        <td>
                                            <?php if ($record['status'] == 'present'): ?>
                                                <span class="badge bg-success">উপস্থিত</span>
                                            <?php elseif ($record['status'] == 'absent'): ?>
                                                <span class="badge bg-danger">অনুপস্থিত</span>
                                            <?php elseif ($record['status'] == 'on-leave'): ?>
                                                <span class="badge bg-warning">ছুটি</span>
                                            <?php else: ?>
                                                <span class="badge bg-info">ছুটির দিন</span>
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo htmlspecialchars($student['class_name'] . ' - ' . $student['class_section']); ?></td>
                                        <td><?php echo htmlspecialchars($record['subject'] ?? 'N/A'); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle"></i> এই মাসে কোন উপস্থিতি রেকর্ড পাওয়া যায়নি।
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>