<?php

declare(strict_types=1);

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

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

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

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

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

$page_title = 'Result Dashboard';

/*
|--------------------------------------------------------------------------
| TOTAL RESULTS
|--------------------------------------------------------------------------
*/

$totalResultStmt = $pdo->prepare("
    SELECT COUNT(*) AS total
    FROM exam_results
    WHERE school_id = ?
    AND session_id = ?
");

$totalResultStmt->execute([

    $school_id,
    $session_id
]);

$total_results = (int) $totalResultStmt->fetchColumn();

/*
|--------------------------------------------------------------------------
| PASS RESULTS
|--------------------------------------------------------------------------
*/

$passStmt = $pdo->prepare("
    SELECT COUNT(*) AS total
    FROM exam_results
    WHERE school_id = ?
    AND session_id = ?
    AND result_status = 'Pass'
");

$passStmt->execute([

    $school_id,
    $session_id
]);

$pass_results = (int) $passStmt->fetchColumn();

/*
|--------------------------------------------------------------------------
| FAIL RESULTS
|--------------------------------------------------------------------------
*/

$failStmt = $pdo->prepare("
    SELECT COUNT(*) AS total
    FROM exam_results
    WHERE school_id = ?
    AND session_id = ?
    AND result_status = 'Fail'
");

$failStmt->execute([

    $school_id,
    $session_id
]);

$fail_results = (int) $failStmt->fetchColumn();

/*
|--------------------------------------------------------------------------
| AVERAGE PERCENTAGE
|--------------------------------------------------------------------------
*/

$avgStmt = $pdo->prepare("
    SELECT AVG(percentage) AS average_percentage
    FROM exam_results
    WHERE school_id = ?
    AND session_id = ?
");

$avgStmt->execute([

    $school_id,
    $session_id
]);

$average_percentage = round(

    (float) $avgStmt->fetchColumn(),

    2
);

/*
|--------------------------------------------------------------------------
| PUBLISHED RESULTS
|--------------------------------------------------------------------------
*/

$publishedStmt = $pdo->prepare("
    SELECT COUNT(*) AS total
    FROM exam_results
    WHERE school_id = ?
    AND session_id = ?
    AND is_published = 1
");

$publishedStmt->execute([

    $school_id,
    $session_id
]);

$published_results = (int) $publishedStmt->fetchColumn();

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>

                        Result Dashboard

                    </h1>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <div class="row">

                <!-- TOTAL -->

                <div class="col-lg-3 col-md-6 col-12">

                    <div class="small-box bg-info">

                        <div class="inner">

                            <h3>

                                <?= $total_results; ?>

                            </h3>

                            <p>

                                Total Results

                            </p>

                        </div>

                        <div class="icon">

                            <i class="fas fa-file-alt"></i>

                        </div>

                    </div>

                </div>

                <!-- PASS -->

                <div class="col-lg-3 col-md-6 col-12">

                    <div class="small-box bg-success">

                        <div class="inner">

                            <h3>

                                <?= $pass_results; ?>

                            </h3>

                            <p>

                                Passed Students

                            </p>

                        </div>

                        <div class="icon">

                            <i class="fas fa-check-circle"></i>

                        </div>

                    </div>

                </div>

                <!-- FAIL -->

                <div class="col-lg-3 col-md-6 col-12">

                    <div class="small-box bg-danger">

                        <div class="inner">

                            <h3>

                                <?= $fail_results; ?>

                            </h3>

                            <p>

                                Failed Students

                            </p>

                        </div>

                        <div class="icon">

                            <i class="fas fa-times-circle"></i>

                        </div>

                    </div>

                </div>

                <!-- AVG -->

                <div class="col-lg-3 col-md-6 col-12">

                    <div class="small-box bg-warning">

                        <div class="inner">

                            <h3>

                                <?= $average_percentage; ?>%

                            </h3>

                            <p>

                                Average Percentage

                            </p>

                        </div>

                        <div class="icon">

                            <i class="fas fa-chart-line"></i>

                        </div>

                    </div>

                </div>

            </div>

            <!-- SECOND ROW -->

            <div class="row">

                <div class="col-lg-4 col-md-6 col-12">

                    <div class="info-box">

                        <span class="info-box-icon bg-primary">

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

                        </span>

                        <div class="info-box-content">

                            <span class="info-box-text">

                                Published Results

                            </span>

                            <span class="info-box-number">

                                <?= $published_results; ?>

                            </span>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

<link
    rel="stylesheet"
    href="assets/css/result-report.css"
>

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