<?php

session_start();

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

$page_title = 'Homework Reports';

require_once __DIR__ . '/../include/header.php';

require_once __DIR__ . '/../include/sidebar.php';

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

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

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

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

/*
|--------------------------------------------------------------------------
| FETCH CLASSES
|--------------------------------------------------------------------------
*/

$classStmt = $pdo->prepare("

    SELECT *

    FROM classes

    WHERE school_id = ?

    ORDER BY class_name ASC

");

$classStmt->execute([$_SESSION['school_id']]);

$classes = $classStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| 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']

];

/*
|--------------------------------------------------------------------------
| CLASS FILTER
|--------------------------------------------------------------------------
*/

if (!empty($class_id)) {

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

    $params[] = $class_id;
}

/*
|--------------------------------------------------------------------------
| DATE FILTER
|--------------------------------------------------------------------------
*/

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);

?>

<div class="content-wrapper">

    <!-- CONTENT HEADER -->

    <section class="content-header">

        <div class="container-fluid">

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

                <div>

                    <h1 class="font-weight-bold">

                        Homework Reports

                    </h1>

                    <p class="text-muted mb-0">

                        View class wise homework reports

                    </p>

                </div>
				<div>

    <a
        href="print.php?class_id=<?= $class_id ?>&from_date=<?= $from_date ?>&to_date=<?= $to_date ?>"
        target="_blank"
        class="btn btn-primary"
    >

        <i class="fas fa-print mr-1"></i>

        Print Report

    </a>

</div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- FILTER CARD -->

            <div class="card shadow-sm border-0 mb-4">

                <div class="card-body">

                    <form method="GET">

                        <div class="row">

                            <!-- CLASS -->

                            <div class="col-md-4">

                                <div class="form-group">

                                    <label>

                                        Class

                                    </label>

                                    <select
                                        name="class_id"
                                        class="form-control"
                                    >

                                        <option value="">

                                            All Classes

                                        </option>

                                        <?php foreach($classes as $class): ?>

                                            <option
                                                value="<?= $class['id'] ?>"
                                                <?= $class_id == $class['id'] ? 'selected' : '' ?>
                                            >

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

                                            </option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                            </div>

                            <!-- FROM DATE -->

                            <div class="col-md-3">

                                <div class="form-group">

                                    <label>

                                        From Date

                                    </label>

                                    <input
                                        type="date"
                                        name="from_date"
                                        class="form-control"
                                        value="<?= $from_date ?>"
                                    >

                                </div>

                            </div>

                            <!-- TO DATE -->

                            <div class="col-md-3">

                                <div class="form-group">

                                    <label>

                                        To Date

                                    </label>

                                    <input
                                        type="date"
                                        name="to_date"
                                        class="form-control"
                                        value="<?= $to_date ?>"
                                    >

                                </div>

                            </div>

                            <!-- BUTTON -->

                            <div class="col-md-2 d-flex align-items-end">

                                <button
                                    type="submit"
                                    class="btn btn-primary btn-block"
                                >

                                    <i class="fas fa-search mr-1"></i>

                                    Filter

                                </button>

                            </div>

                        </div>

                    </form>

                </div>

            </div>

            <!-- REPORT TABLE -->

            <div class="card shadow-sm border-0">

                <div class="card-body p-0">

                    <div class="table-responsive">

                        <table class="table table-hover mb-0">

                            <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" class="text-center py-5">

                                            No Reports Found

                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<style>

.card {

    border-radius: 18px;
}

.form-control {

    border-radius: 10px;
    height: 45px;
}

.table thead th {

    background: #f8f9fa;
    border: none;
    padding: 15px;
}

.table td {

    padding: 15px;
    vertical-align: middle;
}

.badge {

    border-radius: 30px;
    padding: 6px 12px;
}

.btn {

    border-radius: 10px;
}

</style>

<?php require_once __DIR__ . '/../include/footer.php'; ?>