<?php

session_name('super_admin_session');

if (!is_dir(dirname(__DIR__) . '/session_data/super')) { mkdir(dirname(__DIR__) . '/session_data/super', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/super');

session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);

session_start();

error_reporting(0);

include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];


if (strlen($_SESSION['sid'] == 0)) {

    header('location:logout.php');

}



$result = mysqli_query($con, "SELECT * FROM refund WHERE status = 'pending' ORDER BY created_at DESC");

?>





<!DOCTYPE html>

<html>

<?php @include("includes/head.php"); ?>



<body class="hold-transition sidebar-mini layout-fixed">

    <div class="wrapper">

        <?php @include("includes/header.php"); ?>

        <?php @include("includes/sidebar.php"); ?>



        <div class="content-wrapper">

            <section class="content-header premium-header">

    <div class="container-fluid">

        <div class="header-glass-card">

            <div class="row align-items-center">

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

                    <div class="title-section">

                        <div class="icon-box bg-primary-gradient shadow-sm">

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

                        </div>

                        <div>

                            <h1 class="premium-title text-primary">

                                Payment Management

                                <span class="premium-subtitle text-secondary">Refundable Payment</span>

                            </h1>
                            <div class="mt-1">
                              <?php if ($currentSessionName): ?>
                                <span class="badge badge-primary mr-1">
                                  <i class="fas fa-calendar-alt"></i>
                                  <?= htmlspecialchars($currentSessionName) ?>
                                </span>
                              <?php endif; ?>
                              <?php if ($currentTermName): ?>
                                <span class="badge badge-success">
                                  <i class="fas fa-bookmark"></i>
                                  <?= htmlspecialchars($currentTermName) ?>
                                </span>
                              <?php else: ?>
                                <span class="badge badge-warning">
                                  <i class="fas fa-exclamation-triangle"></i> No active term
                                </span>
                              <?php endif; ?>
                            </div>

                        </div>

                    </div>

                </div>

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

                    <nav aria-label="breadcrumb">

                        <ol class="breadcrumb premium-breadcrumb">

                            <li class="breadcrumb-item"><a href="dashboard.php" class="text-primary">Home</a></li>

                            <li class="breadcrumb-item active" aria-current="page">Refund Payment</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section>



            <section class="content">

                <div class="container-fluid">

                    <div class="row">

                        <div class="col-12">

                            <div class="card card-primary">

                                <div class="card-header">

                                    <h3 class="card-title"><i class="fas fa-history"></i> Payment to be Refunded</h3>

                                    <div class="card-tools">

                                    </div>

                                </div>

                                <div class="card-body">

                                    <?php if (mysqli_num_rows($result) == 0) { ?>

                                        <div class="alert alert-info">No pending refunds available.</div>

                                    <?php } ?>

                                    <table id="example1" class="table table-bordered table-hover">

                                    <thead>

                                        <tr>

                                            <th>Admission No</th>

                                            <th>Fullname</th>

                                            <th>Class</th>

                                            <th>Session</th>

                                            <th>Amount</th>

                                            <th>Payment ID</th>

                                            <th>Date</th>

                                            <th>Action</th>

                                        </tr>

                                    </thead>

                                    <tbody>

                                        <?php while ($row = mysqli_fetch_assoc($result)) { ?>

                                            <tr>

                                                <td><?php echo $row['studentno']; ?></td>

                                                <td><?php echo $row['studentName']; ?></td>

                                                <td><?php echo $row['class']; ?></td>

                                                <td><?php echo $row['session']; ?></td>

                                                <td><?php echo '₦' . number_format($row['amount'], 2); ?></td>

                                                <td><?php echo $row['pay_id']; ?></td>

                                                <td><?php echo $row['created_at']; ?></td>

                                                <td>

                                                    <form method="post" action="process_refund.php" onsubmit="return confirmRefundAction(this);">

                                                        <input type="hidden" name="refund_id" value="<?php echo $row['id']; ?>">

                                                        <button type="submit" name="approve" class="btn btn-success btn-sm">Approve</button>

                                                        <button type="submit" name="reject" class="btn btn-danger btn-sm">Reject</button>

                                                    </form>



                                                </td>

                                            </tr>

                                        <?php } ?>

                                    </tbody>

                                </table>





                            </div>

                        </div>

                    </div>

                </div>

            </section>

        </div>



        <?php @include("includes/footer.php"); ?>

    </div>



    <?php @include("includes/foot.php"); ?>



    <script>

        function confirmRefundAction(form) {

            // Check which button was clicked

            const approveBtn = form.querySelector("button[name='approve']");

            const rejectBtn = form.querySelector("button[name='reject']");



            // Use `activeElement` to detect which button triggered the form

            const clicked = document.activeElement;



            if (clicked === approveBtn) {

                return confirm("Are you sure you want to APPROVE this refund?");

            }



            if (clicked === rejectBtn) {

                return confirm("Are you sure you want to REJECT this refund?");

            }



            return true; // fallback

        }

    </script>

</body>



</html>