<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

include("check_installation.php");
include("fetch_settings.php");

if (!isset($_SESSION["adminid"])) {
    header("Location: login.php");
    exit;
}

// Pagination setup
$records_per_page = 10;
$current_page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$offset = ($current_page - 1) * $records_per_page;

// Get total number of entries
$total_count_query = $con->prepare("SELECT COUNT(*) AS total FROM transactions WHERE type = 'debit'");
$total_count_query->execute();
$total_count_result = $total_count_query->get_result();
$total_records = $total_count_result->fetch_assoc()['total'];
$total_pages = ceil($total_records / $records_per_page);
$total_count_query->close();

// Fetch debit transactions with pagination
$transactions = [];
$stmt = $con->prepare("SELECT charged, amount, paymentdate FROM transactions WHERE type = 'debit' ORDER BY paymentdate DESC LIMIT ? OFFSET ?");
$stmt->bind_param('ii', $records_per_page, $offset);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    $transactions[] = $row;
}
$stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Transfer History</title>
    <!-- Meta Tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="author" content="">
    <meta name="description" content="">

    <!-- Favicon -->
    <link rel="icon" href="../<?= htmlspecialchars($system_favicon); ?>" type="image/<?= pathinfo($system_favicon, PATHINFO_EXTENSION); ?>">

    <!-- Google Font -->
    <link rel="preconnect" href="https://fonts.googleapis.com/">
    <link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;700&amp;family=Poppins:wght@400;500;700&amp;display=swap">

    <!-- Plugins CSS -->
    <link rel="stylesheet" type="text/css" href="assets/vendor/font-awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="assets/vendor/bootstrap-icons/bootstrap-icons.css">
    <link rel="stylesheet" type="text/css" href="assets/vendor/overlay-scrollbar/css/overlayscrollbars.min.css">
    <link rel="stylesheet" type="text/css" href="assets/vendor/apexcharts/css/apexcharts.css">

    <!-- Theme CSS -->
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
</head>

<body>

<!-- **************** MAIN CONTENT START **************** -->
<main>
    
    <!-- Sidebar START -->
    <?php include "include/sidebar.php" ?>
    <!-- Sidebar END -->
    
    <!-- Page content START -->
    <div class="page-content">
        <!-- Top bar START -->
        <?php include "include/topbar.php" ?>
        <!-- Top bar END -->
    
        <!-- Page main content START -->
        <div class="page-content-wrapper p-xxl-4">
    
            <!-- Title -->
            <div class="row">
                <div class="col-12 mb-4 mb-sm-5">
                    <div class="d-sm-flex justify-content-between align-items-center">
                        <h1 class="h3 mb-2 mb-sm-0">Debit History</h1>
                    </div>
                </div>
            </div>

            <!-- Table -->
            <div class="card shadow">
                <div class="card-header border-bottom">
                    <h5 class="card-title mb-0">Recent Debits</h5>
                </div>

                <div class="card-body">
                    <div class="bg-light rounded p-3 d-none d-sm-block">
                        <div class="row row-cols-3 g-4">
                            <div class="col"><h6 class="mb-0">Account Number</h6></div>
                            <div class="col"><h6 class="mb-0">Amount</h6></div>
                            <div class="col"><h6 class="mb-0">Date</h6></div>
                        </div>
                    </div>

                    <?php if (!empty($transactions)): ?>
                        <?php foreach ($transactions as $transaction): ?>
                            <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-4 align-items-center border-bottom px-2 py-4">
                                <div class="col">
                                    <small class="d-block d-sm-none">Account Number:</small>
                                    <h6 class="mb-0 fw-normal"><?= htmlspecialchars($transaction['charged']); ?></h6>
                                </div>
                                <div class="col">
                                    <small class="d-block d-sm-none">Amount:</small>
                                    <h6 class="mb-0 fw-normal"><?= number_format($transaction['amount'], 2); ?></h6>
                                </div>
                                <div class="col">
                                    <small class="d-block d-sm-none">Date:</small>
                                    <h6 class="mb-0 fw-normal"><?= date("l jS F Y", strtotime($transaction['paymentdate'])); ?></h6>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="row px-2 py-4 text-center">
                            <div class="col">
                                <p class="mb-0">No transfers found</p>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>

                <div class="card-footer pt-0">
                    <div class="d-sm-flex justify-content-sm-between align-items-sm-center">
                        <p class="mb-sm-0 text-center text-sm-start">
                            Showing <?= $offset + 1 ?> to <?= min($offset + $records_per_page, $total_records) ?> of <?= $total_records ?> entries
                        </p>

                        <nav class="mb-sm-0 d-flex justify-content-center" aria-label="navigation">
                            <ul class="pagination pagination-sm pagination-primary-soft mb-0">

                                <!-- Previous button -->
                                <li class="page-item <?= ($current_page <= 1) ? 'disabled' : '' ?>">
                                    <a class="page-link" href="?page=<?= max(1, $current_page - 1) ?>">Previous</a>
                                </li>

                                <?php
                                $visible_pages = 3; // how many pages to show around current page

                                // Always show first page
                                echo '<li class="page-item ' . ($current_page == 1 ? 'active' : '') . '">';
                                echo '<a class="page-link" href="?page=1">1</a>';
                                echo '</li>';

                                if ($current_page > ($visible_pages + 2)) {
                                    echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
                                }

                                // Middle range
                                for ($i = max(2, $current_page - $visible_pages); $i <= min($total_pages - 1, $current_page + $visible_pages); $i++) {
                                    echo '<li class="page-item ' . ($i == $current_page ? 'active' : '') . '">';
                                    echo '<a class="page-link" href="?page=' . $i . '">' . $i . '</a>';
                                    echo '</li>';
                                }

                                // Always show last page
                                if ($total_pages > 1) {
                                    if ($current_page < $total_pages - ($visible_pages + 1)) {
                                        echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
                                    }
                                    echo '<li class="page-item ' . ($current_page == $total_pages ? 'active' : '') . '">';
                                    echo '<a class="page-link" href="?page=' . $total_pages . '">' . $total_pages . '</a>';
                                    echo '</li>';
                                }
                                ?>

                                <!-- Next button -->
                                <li class="page-item <?= ($current_page >= $total_pages) ? 'disabled' : '' ?>">
                                    <a class="page-link" href="?page=<?= min($total_pages, $current_page + 1) ?>">Next</a>
                                </li>

                            </ul>
                        </nav>
                    </div>
                </div>

            </div>
    
        </div>
        <!-- Page main content END -->
    </div>
    <!-- Page content END -->
</main>
<!-- **************** MAIN CONTENT END **************** -->

<!-- Bootstrap JS -->
<script src="assets/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script>

<!-- Vendor -->
<script src="assets/vendor/overlay-scrollbar/js/overlayscrollbars.min.js"></script>
<script src="assets/vendor/apexcharts/js/apexcharts.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js"></script>

<!-- ThemeFunctions -->
<script src="assets/js/functions.js"></script>
</body>
</html>
