<?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'];

include('includes/flash.php');

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

    header('location:logout.php');

}



if (isset($_GET['res'])) {

    $id = intval($_GET['res']); // Securely get the id from the 'res' parameter



    // Start transaction

    mysqli_begin_transaction($con);



    try {

        // Query to restore fee details from backup table to main table

        $restoreQuery = "INSERT INTO fees (id, name, class, stream, amount, student_type) 

                         SELECT id, name, class, stream, amount, student_type 

                         FROM fees_backup WHERE id = ?";

        $stmt = mysqli_prepare($con, $restoreQuery);

        mysqli_stmt_bind_param($stmt, "i", $id);



        if (mysqli_stmt_execute($stmt)) {

            // Query to remove the record from the backup table after restoration

            $deleteBackupQuery = "DELETE FROM fees_backup WHERE id = ?";

            $stmtDelete = mysqli_prepare($con, $deleteBackupQuery);

            mysqli_stmt_bind_param($stmtDelete, "i", $id);



            if (mysqli_stmt_execute($stmtDelete)) {

                // Commit transaction after successful restoration and deletion

                mysqli_commit($con);

                flash_alert('Fee details restored successfully.');

                header('Location: restore_fees.php'); exit;

            } else {

                throw new Exception("Error deleting fee from backup table.");

            }



            mysqli_stmt_close($stmtDelete);

        } else {

            throw new Exception("Error restoring fee details.");

        }



        mysqli_stmt_close($stmt);

    } catch (Exception $e) {

        // Rollback transaction in case of error

        mysqli_rollback($con);

        flash_alert($e->getMessage());

    }

}

?>



<!DOCTYPE html>

<html>

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



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

    <div class="wrapper">

        <!-- Navbar -->

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

        <!-- /.navbar -->



        <!-- Main Sidebar Container -->

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



        <!-- Content Wrapper. Contains page content -->

        <div class="content-wrapper">

            <!-- Content Header (Page header) -->

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

                            Fees Management

                                <span class="premium-subtitle text-secondary">Manage Fee</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">Restore Fee</li>

                        </ol>

                    </nav>

                </div>

            </div>

        </div>

    </div>

</section> 



            <!-- Main content -->

            <section class="content">

                <div class="container-fluid">

                    <div class="row">

                        <div class="col-12">

                            <div class="card">

                                <div class="card-header">

                                    <h3 class="card-title">Manage Deleted Fees</h3>

                                    <div class="card-tools">

                                        <a href="restore_fees.php"><button type="button" class="btn btn-sm btn-warning"><span style="color: #fff;"><i class="fas fa-undo"></i> Restore Fees</span></button></a>

                                        <a href="addfees.php"><button type="button" class="btn btn-sm btn-primary"><span style="color: #fff;"><i class="ion ion-person-add"></i> Add New Fees</span></button></a>

                                    </div>

                                </div>

                                <!-- /.card-header -->

                                <div id="editData" class="modal fade">

                                    <div class="modal-dialog modal-xl">

                                        <div class="modal-content">

                                            <div class="modal-header">

                                                <h5 class="modal-title">Fees Details</h5>

                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                                                    <span aria-hidden="true">&times;</span>

                                                </button>

                                            </div>

                                            <div class="modal-body" id="info_update">
<!-- loaded via AJAX -->
</div>

                                            <div class="modal-footer">

                                                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>

                                            </div>

                                        </div>

                                    </div>

                                </div>



                                <div class="card-body mt-2">

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

                                        <thead>

                                            <tr>

                                                <th>#</th>

                                                <th>Fees Name</th>

                                                <th>Class</th>

                                                <th>School Type</th>

                                                <th>Amount</th>

                                                <th>Action</th>

                                            </tr>

                                        </thead>

                                        <tbody>

                                            <?php

                                            $query = mysqli_query($con, "SELECT * FROM fees_backup");

                                            $cnt = 1;

                                            while ($row = mysqli_fetch_array($query)) {

                                            ?>

                                                <tr>

                                                    <td><?php echo htmlentities($cnt); ?></td>

                                                    <td><?php echo htmlentities($row['name']); ?></td>

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

                                                    <td><?php echo htmlentities($row['stream']); ?></td>

                                                    <td><?php echo htmlentities($row['amount']); ?></td>

                                                    <td>

                                                        <a href="restore_fees.php?res=<?php echo $row['id']; ?>" onClick="return confirm('Are you sure you want to restore this fee record?')" class="btn btn-warning btn-xs"><i class="fas fa-undo"></i> Restore</a>

                                                    </td>

                                                </tr>

                                            <?php $cnt = $cnt + 1; } ?>

                                        </tbody>

                                    </table>

                                </div>

                                <!-- /.card-body -->

                            </div>

                            <!-- /.card -->

                        </div>

                        <!-- /.col -->

                    </div>

                    <!-- /.row -->

                </div>

                <!-- /.container-fluid -->

            </section>

            <!-- /.content -->

        </div>

        <!-- /.content-wrapper -->

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



        <!-- Control Sidebar -->

        <aside class="control-sidebar control-sidebar-dark">

            <!-- Control sidebar content goes here -->

        </aside>

        <!-- /.control-sidebar -->

    </div>

    <!-- ./wrapper -->



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



    <script type="text/javascript">

        $(document).ready(function(){

            $(document).on('click', '.edit_data', function(){

                var edit_id = $(this).attr('id');

                $.ajax({

                    url: "editfees.php",

                    type: "post",

                    data: {edit_id: edit_id},

                    success: function(data){

                        $("#info_update").html(data);

                        $("#editData").data('ajax-src', 'editfees.php').modal('show');
            $("#editData").find('form:not([action])').attr('data-ajax-action', 'editfees.php');

                    }

                });

            });

        });



        $(document).ready(function(){

            $(document).on('click', '.edit_data2', function(){

                var edit_id2 = $(this).attr('id');

                $.ajax({

                    url: "view_fees_info.php",

                    type: "post",

                    data: {edit_id2: edit_id2},

                    success: function(data){

                        $("#info_update2").html(data);

                        $("#editData2").data('ajax-src', 'view_fees_info.php').modal('show');
            $("#editData2").find('form:not([action])').attr('data-ajax-action', 'view_fees_info.php');

                    }

                });

            });

        });

    </script>

</body>

</html>

