<?php

declare(strict_types=1);

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

require_once __DIR__ . '/helpers/result-helper.php';

allow_roles(['admin', 'teacher']);

requirePostRequest();

verifyCsrfToken();

$school_id = (int) $_SESSION['school_id'];

$session_id = (int) ($_SESSION['session_id'] ?? 0);

$page_title = 'Final Marksheet';

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

$result_id = validateId(
    $_POST['result_id'] ?? 0
);

if ($result_id <= 0) {

    header('Location: result-view.php');

    exit;
}

/*
|--------------------------------------------------------------------------
| FETCH RESULT
|--------------------------------------------------------------------------
*/

$resultStmt = $pdo->prepare("
    SELECT

        er.*,

        s.name,
        s.photo,

        sp.father_name,

        c.class_name,

        et.exam_name,

        ss.roll_no

    FROM exam_results er

    INNER JOIN students s
        ON s.id = er.student_id
        AND s.school_id = er.school_id

    INNER JOIN student_sessions ss
        ON ss.student_id = er.student_id
        AND ss.school_id = er.school_id
        AND ss.session_id = er.session_id
        AND ss.class_id = er.class_id

    LEFT JOIN student_parents sp
        ON sp.student_id = s.id
        AND sp.school_id = s.school_id
        AND sp.session_id = er.session_id

    INNER JOIN classes c
        ON c.id = er.class_id
        AND c.school_id = er.school_id

    INNER JOIN exam_types et
        ON et.id = er.exam_type_id
        AND et.school_id = er.school_id

    WHERE er.school_id = ?
    AND er.session_id = ?
    AND er.id = ?

    LIMIT 1
");

$resultStmt->execute([

    $school_id,
    $session_id,
    $result_id

]);

$result = $resultStmt->fetch(PDO::FETCH_ASSOC);

if (!$result) {

    header('Location: result-view.php');

    exit;
}

/*
|--------------------------------------------------------------------------
| SCHOOL SETTINGS
|--------------------------------------------------------------------------
*/

$schoolStmt = $pdo->prepare("
    SELECT

        school_name,
        school_logo,
        school_address,
        school_phone

    FROM school_settings

    WHERE school_id = ?

    LIMIT 1
");

$schoolStmt->execute([$school_id]);

$school = $schoolStmt->fetch(PDO::FETCH_ASSOC);

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

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

<div class="content-wrapper">

    <section class="content-header">

        <div class="container-fluid">

            <div class="row mb-2">

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

                    <h1>

                        Final Marksheet

                    </h1>

                </div>

                <div class="col-sm-6 text-right">

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

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

                        Print

                    </button>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <div class="card card-outline card-success">

                <div class="card-body">

                    <!-- HEADER -->

                    <div class="row mb-4">

                        <div class="col-md-2 text-center">

                            <?php

                            $logo = !empty($school['school_logo'])
                                ? WEB_ROOT . 'uploads/school/' . $school['school_logo']
                                : 'https://placehold.co/100x100?text=Logo';

                            ?>

                            <img
                                src="<?= htmlspecialchars($logo); ?>"
                                class="img-fluid"
                                width="100"
                                alt="School Logo"
                            >

                        </div>

                        <div class="col-md-8 text-center">

                            <h2>

                                <?= htmlspecialchars($school['school_name'] ?? 'School'); ?>

                            </h2>

                            <p class="mb-1">

                                <?= htmlspecialchars($school['school_address'] ?? ''); ?>

                            </p>

                            <p>

                                <?= htmlspecialchars($school['school_phone'] ?? ''); ?>

                            </p>

                            <h3 class="mt-3">

                                Final Report Card

                            </h3>

                        </div>

                        <div class="col-md-2 text-center">

                            <?php

                            $photo = !empty($result['photo'])
                                ? WEB_ROOT . 'student/uploads/' . $result['photo']
                                : 'https://placehold.co/100x100?text=Photo';

                            ?>

                            <img
                                src="<?= htmlspecialchars($photo); ?>"
                                class="img-thumbnail"
                                width="100"
                                alt="Student Photo"
                            >

                        </div>

                    </div>

                    <!-- STUDENT DETAILS -->

                    <div class="row mb-4">

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

                            <table class="table table-bordered">

                                <tr>

                                    <th width="40%">

                                        Student Name

                                    </th>

                                    <td>

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

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Father Name

                                    </th>

                                    <td>

                                        <?= htmlspecialchars($result['father_name'] ?? '-'); ?>

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Roll No

                                    </th>

                                    <td>

                                        <?= htmlspecialchars($result['roll_no']); ?>

                                    </td>

                                </tr>

                            </table>

                        </div>

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

                            <table class="table table-bordered">

                                <tr>

                                    <th width="40%">

                                        Class

                                    </th>

                                    <td>

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

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Final Percentage

                                    </th>

                                    <td>

                                        <?= htmlspecialchars($result['percentage']); ?>%

                                    </td>

                                </tr>

                                <tr>

                                    <th>

                                        Final Grade

                                    </th>

                                    <td>

                                        <?= htmlspecialchars($result['overall_grade']); ?>

                                    </td>

                                </tr>

                            </table>

                        </div>

                    </div>

                    <!-- FINAL SUMMARY -->

                    <div class="table-responsive">

                        <table class="table table-bordered">

                            <thead>

                                <tr>

                                    <th>

                                        Total Marks

                                    </th>

                                    <th>

                                        Obtained Marks

                                    </th>

                                    <th>

                                        Percentage

                                    </th>

                                    <th>

                                        Grade

                                    </th>

                                    <th>

                                        Result Status

                                    </th>

                                    <th>

                                        Rank

                                    </th>

                                </tr>

                            </thead>

                            <tbody>

                                <tr>

                                    <td>

                                        <?= htmlspecialchars($result['total_marks']); ?>

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($result['obtained_marks']); ?>

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($result['percentage']); ?>%

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($result['overall_grade']); ?>

                                    </td>

                                    <td>

                                        <?php if ($result['result_status'] === 'Pass'): ?>

                                            <span class="badge badge-success">

                                                Pass

                                            </span>

                                        <?php else: ?>

                                            <span class="badge badge-danger">

                                                Fail

                                            </span>

                                        <?php endif; ?>

                                    </td>

                                    <td>

                                        <?= htmlspecialchars($result['rank_position']); ?>

                                    </td>

                                </tr>

                            </tbody>

                        </table>

                    </div>

                    <!-- SIGNATURES -->

                    <div class="row mt-5">

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

                            <hr>

                            <strong>

                                Class Teacher

                            </strong>

                        </div>

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

                            <hr>

                            <strong>

                                Principal

                            </strong>

                        </div>

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

                            <hr>

                            <strong>

                                Parent Signature

                            </strong>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<link
    rel="stylesheet"
    href="assets/css/marksheet.css"
>

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