<?php

declare(strict_types=1);

/*
|--------------------------------------------------------------------------
| EXPECTED VARIABLES
|--------------------------------------------------------------------------
|
| $results
|
*/

?>

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

    <div class="card-header">

        <h3 class="card-title">

            Student Results

        </h3>

    </div>

    <div class="card-body table-responsive">

        <table
            id="resultTable"
            class="table table-bordered table-striped"
        >

            <thead>

                <tr>

                    <th>

                        Rank

                    </th>

                    <th>

                        Roll No

                    </th>

                    <th>

                        Student Name

                    </th>

                    <th>

                        Percentage

                    </th>

                    <th>

                        Grade

                    </th>

                    <th>

                        Status

                    </th>

                    <th>

                        Publish

                    </th>

                    <th width="15%">

                        Actions

                    </th>

                </tr>

            </thead>

            <tbody>

                <?php foreach ($results as $row): ?>

                    <tr>

                        <td>

                            <?= (int) $row['rank_position']; ?>

                        </td>

                        <td>

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

                        </td>

                        <td>

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

                        </td>

                        <td>

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

                        </td>

                        <td>

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

                        </td>

                        <td>

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

                                <span class="badge badge-success">

                                    Pass

                                </span>

                            <?php else: ?>

                                <span class="badge badge-danger">

                                    Fail

                                </span>

                            <?php endif; ?>

                        </td>

                        <td>

                            <?php if ((int) $row['is_published'] === 1): ?>

                                <span class="badge badge-success">

                                    Published

                                </span>

                            <?php else: ?>

                                <span class="badge badge-warning">

                                    Pending

                                </span>

                            <?php endif; ?>

                        </td>

                        <td>

                            <div class="d-flex">

                                <!-- VIEW -->

                                <form
                                    method="POST"
                                    action="student-marksheet.php"
                                    class="mr-1"
                                >

                                    <?= csrfField(); ?>

                                    <input
                                        type="hidden"
                                        name="result_id"
                                        value="<?= (int) $row['id']; ?>"
                                    >

                                    <button
                                        type="submit"
                                        class="btn btn-primary btn-sm"
                                        title="View"
                                    >

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

                                    </button>

                                </form>

                                <!-- DELETE -->

                                <form
                                    method="POST"
                                    class="deleteResultForm"
                                >

                                    <?= csrfField(); ?>

                                    <input
                                        type="hidden"
                                        name="result_id"
                                        value="<?= (int) $row['id']; ?>"
                                    >

                                    <button
                                        type="submit"
                                        class="btn btn-danger btn-sm"
                                        title="Delete"
                                    >

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

                                    </button>

                                </form>

                            </div>

                        </td>

                    </tr>

                <?php endforeach; ?>

            </tbody>

        </table>

    </div>

</div>