<?php

session_start();

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

$page_title = 'Homework List';

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

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

/*
|--------------------------------------------------------------------------
| FETCH HOMEWORK
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("

    SELECT

        h.*,
        c.class_name,
        s.name

    FROM homework h

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

    LEFT JOIN subjects s
    ON s.id = h.subject_id

    WHERE h.school_id = ?

    ORDER BY h.id DESC

");

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

$homeworks = $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 Management

                    </h1>

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

                        Manage class wise homework

                    </p>

                </div>

                <a
                    href="create.php"
                    class="btn btn-primary"
                >

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

                    Add Homework

                </a>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- SUCCESS -->

            <?php if(isset($_SESSION['success'])): ?>

                <div class="alert alert-success">

                    <?= $_SESSION['success']; ?>

                </div>

                <?php unset($_SESSION['success']); ?>

            <?php endif; ?>

            <!-- ERROR -->

            <?php if(isset($_SESSION['error'])): ?>

                <div class="alert alert-danger">

                    <?= $_SESSION['error']; ?>

                </div>

                <?php unset($_SESSION['error']); ?>

            <?php endif; ?>

            <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>Subject</th>

                                    <th>Homework Date</th>

                                    <th>Submission Date</th>

                                    <th>Status</th>

                                    <th width="180">

                                        Action

                                    </th>

                                </tr>

                            </thead>

                            <tbody>

                                <?php if($homeworks): ?>

                                    <?php foreach($homeworks as $homework): ?>

                                        <tr>

                                            <!-- ID -->

                                            <td>

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

                                            </td>

                                            <!-- TITLE -->

                                            <td>

                                                <strong>

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

                                                </strong>

                                            </td>

                                            <!-- CLASS -->

                                            <td>

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

                                                <?= !empty($homework['section']) ? ' - ' . htmlspecialchars($homework['section']) : '' ?>

                                            </td>

                                            <!-- SUBJECT -->

                                            <td>

                                                <?= htmlspecialchars($homework['name'] ?? '-') ?>

                                            </td>

                                            <!-- HOMEWORK DATE -->

                                            <td>

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

                                            </td>

                                            <!-- SUBMISSION DATE -->

                                            <td>

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

                                            </td>

                                            <!-- STATUS -->

                                            <td>

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

                                                    <span class="badge badge-success">

                                                        Active

                                                    </span>

                                                <?php else: ?>

                                                    <span class="badge badge-danger">

                                                        Inactive

                                                    </span>

                                                <?php endif; ?>

                                            </td>

                                            <!-- ACTION -->

                                            <td>

                                                <a
                                                    href="view.php?id=<?= $homework['id'] ?>"
                                                    class="btn btn-info btn-sm"
                                                >

                                                    <i class="fas fa-eye"></i>

                                                </a>

                                                <a
                                                    href="edit.php?id=<?= $homework['id'] ?>"
                                                    class="btn btn-warning btn-sm"
                                                >

                                                    <i class="fas fa-edit"></i>

                                                </a>

                                                <a
                                                    href="process.php?action=delete&id=<?= $homework['id'] ?>"
                                                    class="btn btn-danger btn-sm"
                                                    onclick="return confirm('Delete this homework?')"
                                                >

                                                    <i class="fas fa-trash"></i>

                                                </a>

                                            </td>

                                        </tr>

                                    <?php endforeach; ?>

                                <?php else: ?>

                                    <tr>

                                        <td colspan="8" class="text-center py-5">

                                            <i class="fas fa-book fa-3x text-muted mb-3"></i>

                                            <h5>

                                                No Homework Found

                                            </h5>

                                        </td>

                                    </tr>

                                <?php endif; ?>

                            </tbody>

                        </table>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<style>

.card {

    border-radius: 18px;
}

.table thead th {

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

.table td {

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

.badge {

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

.btn {

    border-radius: 10px;
}

</style>

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