<?php
// Filename: daily_report.php
require 'config.php';
requireLogin();

$uid = $_SESSION['user_id'];
$date = $_GET['date'] ?? date('Y-m-d');

// 1. GET CLASS INFO
$my_class = null;
if (isset($_GET['class_id'])) {
    $stmt = $pdo->prepare("SELECT * FROM classes WHERE id = ?");
    $stmt->execute([$_GET['class_id']]);
    $my_class = $stmt->fetch();
} else {
    $stmt = $pdo->prepare("SELECT * FROM classes WHERE class_teacher_id = ?");
    $stmt->execute([$uid]);
    $my_class = $stmt->fetch();
}

if (!$my_class) die("<div class='p-4 text-center'>Class info not found. <a href='dashboard.php'>Back</a></div>");

// 2. FETCH ATTENDANCE SUMMARY (Who is absent today?)
// We use DISTINCT to handle cases where they might be marked multiple times, 
// ensuring the student only appears once in the list.
$absent_sql = "
    SELECT DISTINCT s.full_name, s.ic_number, sa.reason, sa.recorded_by
    FROM student_attendance sa
    JOIN students s ON sa.student_id = s.id
    WHERE s.class_id = ? AND sa.date = ? AND sa.status = 'absent'
    ORDER BY s.full_name ASC
";
$a_stmt = $pdo->prepare($absent_sql);
$a_stmt->execute([$my_class['id'], $date]);
$absentees = $a_stmt->fetchAll();

// 3. FETCH DISCIPLINE FOR DATE
$disc_sql = "
    SELECT sd.*, s.full_name 
    FROM student_discipline sd 
    JOIN students s ON sd.student_id = s.id 
    WHERE s.class_id = ? AND sd.reported_date = ?
    ORDER BY sd.created_at DESC
";
$d_stmt = $pdo->prepare($disc_sql);
$d_stmt->execute([$my_class['id'], $date]);
$discipline_cases = $d_stmt->fetchAll();

// 4. TOTAL STUDENTS
$count_stmt = $pdo->prepare("SELECT COUNT(*) FROM students WHERE class_id = ?");
$count_stmt->execute([$my_class['id']]);
$total_students = $count_stmt->fetchColumn();

$present_count = $total_students - count($absentees);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
    <title>Daily Report</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">

    <style>
        :root { --primary-grad: <?= getHeaderTheme() ?>; ... }
        body { font-family: 'Plus Jakarta Sans', sans-serif; background-color: var(--bg-color); padding-bottom: 90px; }

        /* HEADER */
        .app-header {
            background: var(--primary-grad); padding: 30px 20px 80px 20px;
            border-radius: 0 0 35px 35px; color: white; text-align: center;
        }

        /* FLOATING SUMMARY CARD */
        .floating-card {
            background: white; margin: -60px 20px 20px 20px;
            border-radius: 24px; padding: 20px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.08);
            position: relative; z-index: 10;
        }

        .stat-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; margin-top: 15px; }
        .stat-box { background: #f8fafc; border-radius: 16px; padding: 10px; text-align: center; border: 1px solid #e2e8f0; }
        .stat-num { font-size: 1.2rem; font-weight: 800; color: #334155; line-height: 1; }
        .stat-label { font-size: 0.65rem; color: #64748b; text-transform: uppercase; font-weight: 700; margin-top: 4px; }

        /* ACCORDION STYLES */
        .accordion-item { border: none; background: transparent; margin-bottom: 10px; }
        .accordion-button { 
            border-radius: 16px !important; background: white; color: var(--text-dark); 
            font-weight: 700; box-shadow: 0 2px 5px rgba(0,0,0,0.02); 
        }
        .accordion-button:not(.collapsed) { background: #f5f3ff; color: #8B5CF6; }
        .accordion-button::after { background-size: 1rem; }
        .accordion-body { background: white; border-radius: 0 0 16px 16px; padding: 0; margin-top: -5px; }

        /* LIST ITEMS */
        .list-row { display: flex; align-items: center; padding: 12px 15px; border-bottom: 1px solid #f1f5f9; }
        .list-row:last-child { border-bottom: none; }
        
        .avatar-box {
            width: 40px; height: 40px; border-radius: 12px; flex-shrink: 0;
            display: flex; align-items: center; justify-content: center; margin-right: 12px;
            font-size: 1.2rem;
        }
        .bg-absent { background: #fee2e2; color: #ef4444; }
        .bg-disc { background: #fef3c7; color: #d97706; }

        /* BOTTOM NAV 
        .bottom-nav { position: fixed; bottom: 0; left: 0; right: 0; background: white; padding: 10px 30px 25px 30px; display: flex; justify-content: space-between; border-radius: 30px 30px 0 0; box-shadow: 0 -5px 20px rgba(0,0,0,0.05); z-index: 100; }
        .nav-btn { display: flex; flex-direction: column; align-items: center; color: #94a3b8; text-decoration: none; font-size: 0.7rem; font-weight: 600; }
        .nav-btn.active { color: #8B5CF6; }
        .nav-btn i { font-size: 1.4rem; margin-bottom: 4px; }  */
    </style>
</head>
<body>

    <div class="app-header">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <a href="dashboard.php" class="text-white"><i class="bi bi-arrow-left fs-4"></i></a>
            <div class="fw-bold fs-5">Daily Report</div>
            <div style="width: 24px;"></div>
        </div>
        <h2 class="mb-0 fw-bold"><?= htmlspecialchars($my_class['class_name']) ?></h2>
        <div class="opacity-75 small">Summary for <?= date('d M Y', strtotime($date)) ?></div>
    </div>

    <div class="floating-card">
        <form method="GET">
            <input type="hidden" name="class_id" value="<?= $my_class['id'] ?>">
            <div class="input-group">
                <span class="input-group-text bg-light border-0 rounded-start-4 ps-3"><i class="bi bi-calendar3"></i></span>
                <input type="date" name="date" class="form-control border-0 bg-light fw-bold rounded-end-4" value="<?= $date ?>" onchange="this.form.submit()">
            </div>
        </form>

        <div class="stat-grid">
            <div class="stat-box">
                <div class="stat-num text-success"><?= $present_count ?></div>
                <div class="stat-label">Present</div>
            </div>
            <div class="stat-box">
                <div class="stat-num text-danger"><?= count($absentees) ?></div>
                <div class="stat-label">Absent</div>
            </div>
            <div class="stat-box">
                <div class="stat-num text-warning"><?= count($discipline_cases) ?></div>
                <div class="stat-label">Cases</div>
            </div>
        </div>
    </div>

    <div class="px-3 pb-4">
        
        <div class="accordion" id="reportAccordion">
            
            <div class="accordion-item">
                <h2 class="accordion-header">
                    <button class="accordion-button <?= empty($absentees) ? 'collapsed' : '' ?>" type="button" data-bs-toggle="collapse" data-bs-target="#collapseAbsent">
                        <span class="d-flex w-100 justify-content-between me-2">
                            <span><i class="bi bi-person-x-fill text-danger me-2"></i>Absent Today</span>
                            <?php if(count($absentees) > 0): ?>
                                <span class="badge bg-danger rounded-pill"><?= count($absentees) ?></span>
                            <?php endif; ?>
                        </span>
                    </button>
                </h2>
                <div id="collapseAbsent" class="accordion-collapse collapse <?= empty($absentees) ? '' : 'show' ?>" data-bs-parent="#reportAccordion">
                    <div class="accordion-body">
                        <?php if(empty($absentees)): ?>
                            <div class="text-center py-3 text-muted small"><i class="bi bi-check-circle me-1"></i> Full Attendance!</div>
                        <?php else: ?>
                            <?php foreach($absentees as $ab): ?>
                                <div class="list-row">
                                    <div class="avatar-box bg-absent"><i class="bi bi-person-dash"></i></div>
                                    <div>
                                        <div class="fw-bold text-dark small"><?= htmlspecialchars($ab['full_name']) ?></div>
                                        <div class="small text-danger fw-bold bg-danger bg-opacity-10 px-2 rounded d-inline-block" style="font-size:0.65rem;">
                                            <?= htmlspecialchars($ab['reason'] ?: 'Unknown') ?>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <div class="accordion-item">
                <h2 class="accordion-header">
                    <button class="accordion-button <?= empty($discipline_cases) ? 'collapsed' : '' ?>" type="button" data-bs-toggle="collapse" data-bs-target="#collapseDisc">
                        <span class="d-flex w-100 justify-content-between me-2">
                            <span><i class="bi bi-exclamation-triangle-fill text-warning me-2"></i>Discipline</span>
                            <?php if(count($discipline_cases) > 0): ?>
                                <span class="badge bg-warning text-dark rounded-pill"><?= count($discipline_cases) ?></span>
                            <?php endif; ?>
                        </span>
                    </button>
                </h2>
                <div id="collapseDisc" class="accordion-collapse collapse <?= empty($discipline_cases) ? '' : 'show' ?>" data-bs-parent="#reportAccordion">
                    <div class="accordion-body">
                        <?php if(empty($discipline_cases)): ?>
                            <div class="text-center py-3 text-muted small">No cases reported today.</div>
                        <?php else: ?>
                            <?php foreach($discipline_cases as $dc): ?>
                                <div class="list-row">
                                    <div class="avatar-box bg-disc"><i class="bi bi-exclamation-circle"></i></div>
                                    <div>
                                        <div class="fw-bold text-dark small"><?= htmlspecialchars($dc['full_name']) ?></div>
                                        <div class="small text-muted">
                                            <strong class="text-dark"><?= htmlspecialchars($dc['infraction']) ?></strong>
                                            <span class="ms-1"><?= !empty($dc['description']) ? '- '.htmlspecialchars($dc['description']) : '' ?></span>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <?php include 'menu.php'; ?>

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