<?php

session_start();

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

$page_title = 'View Homework';

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

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

/*
|--------------------------------------------------------------------------
| VALIDATE ID
|--------------------------------------------------------------------------
*/

$id = (int)($_GET['id'] ?? 0);

if (!$id) {

    die('Invalid Homework ID');
}

/*
|--------------------------------------------------------------------------
| 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.id = ?
    AND h.school_id = ?

    LIMIT 1

");

$stmt->execute([

    $id,
    $_SESSION['school_id']

]);

$homework = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$homework) {

    die('Homework not found.');
}

?>

<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 Details

                    </h1>

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

                        View homework information

                    </p>

                </div>

                <a
                    href="index.php"
                    class="btn btn-secondary"
                >

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

                    Back

                </a>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

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

                <div class="card-body">

                    <div class="row">

                        <!-- TITLE -->

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

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

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

                            </h3>

                        </div>

                        <!-- CLASS -->

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

                            <label class="detail-label">

                                Class

                            </label>

                            <div class="detail-box">

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

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

                            </div>

                        </div>

                        <!-- SUBJECT -->

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

                            <label class="detail-label">

                                Subject

                            </label>

                            <div class="detail-box">

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

                            </div>

                        </div>

                        <!-- STATUS -->

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

                            <label class="detail-label">

                                Status

                            </label>

                            <div class="detail-box">

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

                                    <span class="badge badge-success">

                                        Active

                                    </span>

                                <?php else: ?>

                                    <span class="badge badge-danger">

                                        Inactive

                                    </span>

                                <?php endif; ?>

                            </div>

                        </div>

                        <!-- HOMEWORK DATE -->

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

                            <label class="detail-label">

                                Homework Date

                            </label>

                            <div class="detail-box">

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

                            </div>

                        </div>

                        <!-- SUBMISSION DATE -->

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

                            <label class="detail-label">

                                Submission Date

                            </label>

                            <div class="detail-box">

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

                            </div>

                        </div>

                        <!-- DESCRIPTION -->

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

                            <label class="detail-label">

                                Description

                            </label>

                            <div class="detail-box description-box">

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

                            </div>

                        </div>

                        <!-- ATTACHMENT -->

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

                            <label class="detail-label">

                                Attachment

                            </label>

                            <div class="detail-box">

                                <?php if(!empty($homework['attachment'])): ?>

                                    <a
                                        href="../uploads/homework/<?= $homework['attachment'] ?>"
                                        target="_blank"
                                        class="btn btn-primary"
                                    >

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

                                        Download Attachment

                                    </a>

                                <?php else: ?>

                                    No Attachment

                                <?php endif; ?>

                            </div>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<style>

.card {

    border-radius: 18px;
}

.detail-label {

    font-weight: 600;
    margin-bottom: 8px;
    display: block;
}

.detail-box {

    background: #f8f9fa;
    border-radius: 12px;
    padding: 15px;
    min-height: 55px;
}

.description-box {

    line-height: 1.8;
}

.badge {

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

.btn {

    border-radius: 10px;
}

</style>

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