<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

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']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($current_page - 1) * $records_per_page;

// Fetch total number of customers
$total_result = $con->query("SELECT COUNT(*) FROM customers WHERE regstatus = 'visible'");
$total_rows = $total_result->fetch_row()[0];
$total_pages = ceil($total_rows / $records_per_page);

// Fetch customers for the current page
$sql = "SELECT * FROM customers WHERE regstatus = 'visible' LIMIT $records_per_page OFFSET $offset";
$result = $con->query($sql);
$customers = $result->fetch_all(MYSQLI_ASSOC);
$result->free();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Manage Customers</title>
    <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="">

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

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

    <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">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
</head>
<body>

<main>
    <?php include "include/sidebar.php" ?>
    <div class="page-content">
        <?php include "include/topbar.php" ?>
        <div class="page-content-wrapper p-xxl-4">

            <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">Manage Customers</h1>
                        <a href="#" class="btn btn-primary-soft mb-0" onclick="window.print()">
                            <i class="fas fa-print me-2"></i> Print Customers
                        </a>
                    </div>
                </div>
            </div>

            <div class="card shadow">
                <div class="card-header border-bottom">
                    <h5 class="card-title mb-0">Customer Accounts</h5>
                </div>
                <div class="card-body">

                    <div class="bg-light rounded p-3 d-none d-lg-block">
                        <div class="row g-4">
                            <div class="col-lg-2"><h6 class="mb-0">ID</h6></div>
                            <div class="col-lg-3"><h6 class="mb-0">Login ID</h6></div>
                            <div class="col-lg-3"><h6 class="mb-0">Name</h6></div>
                            <div class="col-lg-2"><h6 class="mb-0">Status</h6></div>
                            <div class="col-lg-2"><h6 class="mb-0">Action</h6></div>
                        </div>
                    </div>

                    <?php if (!empty($customers)): ?>
                        <?php foreach ($customers as $customer): ?>
                            <?php
                            $status_class = '';
                            $status_text = '';
                            switch ($customer['accstatus']) {
                                case 'active':
                                    $status_class = 'bg-success bg-opacity-10 text-success';
                                    $status_text = 'Active';
                                    break;
                                case 'inactive':
                                    $status_class = 'bg-secondary bg-opacity-10 text-secondary';
                                    $status_text = 'Inactive';
                                    break;
                                case 'blocked':
                                    $status_class = 'bg-danger bg-opacity-10 text-danger';
                                    $status_text = 'Blocked';
                                    break;
                                case 'verifying':
                                    $status_class = 'bg-warning bg-opacity-10 text-orange';
                                    $status_text = 'Verifying';
                                    break;
                                default:
                                    $status_class = 'bg-info bg-opacity-10 text-info';
                                    $status_text = htmlspecialchars($customer['accstatus']);
                                    break;
                            }
                            ?>
                            <div class="row g-4 align-items-center border-bottom px-2 py-4">
                                <div class="col-6 col-lg-2">
                                    <h6 class="mb-0 fw-normal">
                                        <span class="d-lg-none">ID: </span><?= htmlspecialchars($customer['customerid']); ?>
                                    </h6>
                                </div>
                                <div class="col-6 col-lg-3">
                                    <h6 class="mb-0 fw-normal">
                                        <span class="d-lg-none">Login ID: </span><?= htmlspecialchars($customer['loginid']); ?>
                                    </h6>
                                </div>
                                <div class="col-6 col-lg-3">
                                    <h6 class="mb-0 fw-normal">
                                        <span class="d-lg-none">Name: </span><?= htmlspecialchars($customer['firstname'] . " " . $customer['lastname']); ?>
                                    </h6>
                                </div>
                                <div class="col-6 col-lg-2">
                                    <span class="d-lg-none">Status: </span><span class="badge <?= $status_class; ?>"><?= $status_text; ?></span>
                                </div>
                                <div class="col-6 col-lg-2">
                                    <a href='customer_details.php?custid=<?= htmlspecialchars($customer['customerid']); ?>' class='btn btn-sm btn-primary-soft'>More</a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="row px-2 py-4 text-center">
                            <div class="col">
                                <p class="mb-0">No customers 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 <?= min($offset + 1, $total_rows); ?> to <?= min($offset + $records_per_page, $total_rows); ?> of <?= $total_rows; ?> 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">
                            <li class="page-item <?= ($current_page <= 1) ? 'disabled' : ''; ?>">
                                <a class="page-link" href="?page=<?= max(1, $current_page - 1); ?>">Prev</a>
                            </li>
                            <?php for ($p = 1; $p <= $total_pages; $p++): ?>
                                <li class="page-item <?= ($p == $current_page) ? 'active' : ''; ?>">
                                    <a class="page-link" href="?page=<?= $p; ?>"><?= $p; ?></a>
                                </li>
                            <?php endfor; ?>
                            <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>
    </div>
</main>

<script src="assets/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<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>
<script src="assets/js/functions.js"></script>

</body>
</html>