<?php

session_start();

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

$page_title = 'Edit 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 *

    FROM homework

    WHERE id = ?
    AND school_id = ?

    LIMIT 1

");

$stmt->execute([

    $id,
    $_SESSION['school_id']

]);

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

if (!$homework) {

    die('Homework not found.');
}

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

/*
|--------------------------------------------------------------------------
| FETCH SUBJECTS
|--------------------------------------------------------------------------
*/

$subjectStmt = $pdo->prepare("

    SELECT *

    FROM subjects

    WHERE school_id = ?

    ORDER BY id ASC

");

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

$subjects = $subjectStmt->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">

                        Edit Homework

                    </h1>

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

                        Update homework details

                    </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">

                    <form
                        action="process.php?action=update&id=<?= $homework['id'] ?>"
                        method="POST"
                        enctype="multipart/form-data"
                    >

                        <div class="row">

                            <!-- CLASS -->

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

                                <div class="form-group">

                                    <label>

                                        Class

                                    </label>

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

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

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

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

                                            </option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                            </div>

                            <!-- SECTION -->

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

                                <div class="form-group">

                                    <label>

                                        Section

                                    </label>

                                    <input
                                        type="text"
                                        name="section"
                                        class="form-control"
                                        value="<?= htmlspecialchars($homework['section']) ?>"
                                    >

                                </div>

                            </div>

                            <!-- SUBJECT -->

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

                                <div class="form-group">

                                    <label>

                                        Subject

                                    </label>

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

                                        <option value="">

                                            Select Subject

                                        </option>

                                        <?php foreach($subjects as $subject): ?>

                                            <option
                                                value="<?= $subject['id'] ?>"
                                                <?= $homework['subject_id'] == $subject['id'] ? 'selected' : '' ?>
                                            >

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

                                            </option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                            </div>

                            <!-- HOMEWORK DATE -->

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

                                <div class="form-group">

                                    <label>

                                        Homework Date

                                    </label>

                                    <input
                                        type="date"
                                        name="homework_date"
                                        class="form-control"
                                        value="<?= $homework['homework_date'] ?>"
                                        required
                                    >

                                </div>

                            </div>

                            <!-- SUBMISSION DATE -->

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

                                <div class="form-group">

                                    <label>

                                        Submission Date

                                    </label>

                                    <input
                                        type="date"
                                        name="submission_date"
                                        class="form-control"
                                        value="<?= $homework['submission_date'] ?>"
                                    >

                                </div>

                            </div>

                            <!-- TITLE -->

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

                                <div class="form-group">

                                    <label>

                                        Homework Title

                                    </label>

                                    <input
                                        type="text"
                                        name="title"
                                        class="form-control"
                                        value="<?= htmlspecialchars($homework['title']) ?>"
                                        required
                                    >

                                </div>

                            </div>

                            <!-- DESCRIPTION -->

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

                                <div class="form-group">

                                    <label>

                                        Description

                                    </label>

                                    <textarea
                                        name="description"
                                        rows="5"
                                        class="form-control"
                                    ><?= htmlspecialchars($homework['description']) ?></textarea>

                                </div>

                            </div>

                            <!-- ATTACHMENT -->

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

                                <div class="form-group">

                                    <label>

                                        Attachment

                                    </label>

                                    <input
                                        type="file"
                                        name="attachment"
                                        class="form-control"
                                    >

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

                                        <small class="text-success">

                                            Current File:
                                            <?= htmlspecialchars($homework['attachment']) ?>

                                        </small>

                                    <?php endif; ?>

                                </div>

                            </div>

                        </div>

                        <!-- BUTTON -->

                        <button
                            type="submit"
                            class="btn btn-primary px-4"
                        >

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

                            Update Homework

                        </button>

                    </form>

                </div>

            </div>

        </div>

    </section>

</div>

<style>

.card {

    border-radius: 18px;
}

.form-control {

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

textarea.form-control {

    height: auto;
}

.btn {

    border-radius: 10px;
}

</style>

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