<?php

session_start();

require_once __DIR__ . '/../library/config.php';

/*
|--------------------------------------------------------------------------
| FILTERS
|--------------------------------------------------------------------------
*/

$class_id = $_GET['class_id'] ?? '';

$from_date = $_GET['from_date'] ?? '';

$to_date = $_GET['to_date'] ?? '';

/*
|--------------------------------------------------------------------------
| QUERY
|--------------------------------------------------------------------------
*/

$query = "

    SELECT

        h.*,
        c.class_name

    FROM homework h

    LEFT JOIN classes c
    ON c.id = h.class_id

    WHERE h.school_id = ?

";

$params = [

    $_SESSION['school_id']

];

/*
|--------------------------------------------------------------------------
| FILTERS
|--------------------------------------------------------------------------
*/

if (!empty($class_id)) {

    $query .= " AND h.class_id = ? ";

    $params[] = $class_id;
}

if (!empty($from_date)) {

    $query .= " AND h.homework_date >= ? ";

    $params[] = $from_date;
}

if (!empty($to_date)) {

    $query .= " AND h.homework_date <= ? ";

    $params[] = $to_date;
}

/*
|--------------------------------------------------------------------------
| ORDER
|--------------------------------------------------------------------------
*/

$query .= " ORDER BY h.id DESC ";

$stmt = $pdo->prepare($query);

$stmt->execute($params);

$reports = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>

        Homework Report Print

    </title>

    <link
        rel="stylesheet"
        href="../assets/plugins/fontawesome-free/css/all.min.css"
    >

    <style>

        body {

            font-family: Arial, sans-serif;
            padding: 20px;
        }

        h2 {

            margin-bottom: 20px;
        }

        table {

            width: 100%;
            border-collapse: collapse;
        }

        table th,
        table td {

            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }

        table th {

            background: #f5f5f5;
        }

        .badge {

            padding: 5px 10px;
            border-radius: 20px;
            color: #fff;
            font-size: 12px;
        }

        .badge-success {

            background: green;
        }

        .badge-danger {

            background: red;
        }

        @media print {

            .no-print {

                display: none;
            }
        }

    </style>

</head>

<body>

    <!-- HEADER -->

    <div class="d-flex justify-content-between align-items-center mb-3">

        <h2>

            Homework Report

        </h2>

        <button
            onclick="window.print()"
            class="no-print"
        >

            Print Report

        </button>

    </div>

    <!-- TABLE -->

    <table>

        <thead>

            <tr>

                <th>ID</th>

                <th>Title</th>

                <th>Class</th>

                <th>Homework Date</th>

                <th>Submission Date</th>

                <th>Status</th>

            </tr>

        </thead>

        <tbody>

            <?php if($reports): ?>

                <?php foreach($reports as $report): ?>

                    <tr>

                        <td>

                            #<?= $report['id'] ?>

                        </td>

                        <td>

                            <?= htmlspecialchars($report['title']) ?>

                        </td>

                        <td>

                            <?= htmlspecialchars($report['class_name']) ?>

                        </td>

                        <td>

                            <?= date('d M Y', strtotime($report['homework_date'])) ?>

                        </td>

                        <td>

                            <?= !empty($report['submission_date']) ? date('d M Y', strtotime($report['submission_date'])) : '-' ?>

                        </td>

                        <td>

                            <?php if($report['status'] == 'Active'): ?>

                                <span class="badge badge-success">

                                    Active

                                </span>

                            <?php else: ?>

                                <span class="badge badge-danger">

                                    Inactive

                                </span>

                            <?php endif; ?>

                        </td>

                    </tr>

                <?php endforeach; ?>

            <?php else: ?>

                <tr>

                    <td colspan="6">

                        No Records Found

                    </td>

                </tr>

            <?php endif; ?>

        </tbody>

    </table>

    <script>

        window.onload = function () {

            window.print();

        };

    </script>

</body>

</html>