<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once "../config/theconfig.php";

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


// ------------------------
// Fetch system settings
// ------------------------
// Default values
$cur               = '$'; 
$system_brand_logo = 'assets/images/brand/logo_1758475458.png';
$system_favicon    = 'assets/images/brand/favicon_1758475458.ico';
$system_bank_name  = 'Default Bank';
$system_email      = 'support@defaultbank.com';

// Query system settings
$system_settings_query  = "SELECT currency, brand_logo, favicon, bank_name, email FROM system_settings LIMIT 1";
$system_settings_result = $con->query($system_settings_query);

if ($system_settings_result && $system_settings_result->num_rows > 0) {
    $settings_row = $system_settings_result->fetch_assoc();

    // Override defaults only if values exist
    $cur               = !empty($settings_row['currency'])    ? $settings_row['currency']    : $cur;
    $system_brand_logo = !empty($settings_row['brand_logo'])  ? $settings_row['brand_logo']  : $system_brand_logo;
    $system_favicon    = !empty($settings_row['favicon'])     ? $settings_row['favicon']     : $system_favicon;
    $system_bank_name  = !empty($settings_row['bank_name'])   ? $settings_row['bank_name']   : $system_bank_name;
    $system_email      = !empty($settings_row['email'])       ? $settings_row['email']       : $system_email;
}



// ------------------------
// Get current page filename
// ------------------------
$current_page_filename = basename($_SERVER['PHP_SELF']);
?>

<nav class="navbar sidebar navbar-expand-xl navbar-light">
    <div class="d-flex align-items-center">
        <?php if (!empty($system_brand_logo)): ?>
            <a class="navbar-brand" href="dashboard.php">
                <img class="light-mode-item navbar-brand-item" src="../<?= htmlspecialchars($system_brand_logo); ?>" alt="logo">
            </a>
        <?php else: ?>
            <a class="navbar-brand" href="dashboard.php">
                <img class="light-mode-item navbar-brand-item" src="../assets/images/brand/logo_default.png" alt="logo">
            </a>
        <?php endif; ?>
    </div>

    <div class="offcanvas offcanvas-start flex-row custom-scrollbar h-100" data-bs-backdrop="true" tabindex="-1" id="offcanvasSidebar">
        <div class="offcanvas-body sidebar-content d-flex flex-column pt-4">

            <ul class="navbar-nav flex-column" id="navbar-sidebar">
                <li class="nav-item">
                    <a href="dashboard.php" class="nav-link <?php echo ($current_page_filename == 'dashboard.php') ? 'active' : ''; ?>">Dashboard</a>
                </li>

                <li class="nav-item ms-2 my-2">Admin Account</li>

                <!-- Customers Menu -->
                <?php 
                $customer_pages = ['new_account.php','manage_customers.php','pendingtransfer.php','viewalltransfers.php','credit_customer.php'];
                $is_customer_menu_active = in_array($current_page_filename, $customer_pages);
                ?>
                <li class="nav-item">
                    <a class="nav-link <?php echo $is_customer_menu_active ? '' : ''; ?>" data-bs-toggle="collapse" href="#collapseCustomers" role="button" aria-expanded="<?php echo $is_customer_menu_active ? 'true' : 'false'; ?>" aria-controls="collapseCustomers">
                        Customers
                    </a>
                    <ul class="nav collapse flex-column <?php echo $is_customer_menu_active ? 'show' : ''; ?>" id="collapseCustomers" data-bs-parent="#navbar-sidebar">
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'new_account.php') ? 'active' : ''; ?>" href="new_account.php">New Customer</a></li>
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'manage_customers.php') ? 'active' : ''; ?>" href="manage_customers.php">Manage Customers</a></li>
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'pendingtransfer.php') ? 'active' : ''; ?>" href="pendingtransfer.php">Pending Transfer</a></li>
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'viewalltransfers.php') ? 'active' : ''; ?>" href="viewalltransfers.php">View All Transfer</a></li>
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'credit_customer.php') ? 'active' : ''; ?>" href="credit_customer.php">Update Account</a></li>
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'stock.php') ? 'active' : ''; ?>" href="stock.php">User Stock History</a></li>
                    </ul>
                </li>

                <!-- Branch Menu -->
<?php 
$is_branch_active = ($current_page_filename == 'createbranch.php');
?>
<li class="nav-item">
    <a class="nav-link" data-bs-toggle="collapse" href="#collapseBranch" role="button" aria-expanded="<?php echo $is_branch_active ? 'true' : 'false'; ?>" aria-controls="collapseBranch">
        Branch
    </a>
    <ul class="nav collapse flex-column <?php echo $is_branch_active ? 'show' : ''; ?>" id="collapseBranch" data-bs-parent="#navbar-sidebar">
        <li class="nav-item">
            <a class="nav-link <?php echo $is_branch_active ? 'active' : ''; ?>" href="createbranch.php">Create Branch</a>
        </li>
    </ul>
</li>

<!-- Account Type Menu -->
<?php 
$is_account_type_active = ($current_page_filename == 'accounttype.php');
?>
<li class="nav-item">
    <a class="nav-link" data-bs-toggle="collapse" href="#collapseAccountType" role="button" aria-expanded="<?php echo $is_account_type_active ? 'true' : 'false'; ?>" aria-controls="collapseAccountType">
        Account Type
    </a>
    <ul class="nav collapse flex-column <?php echo $is_account_type_active ? 'show' : ''; ?>" id="collapseAccountType" data-bs-parent="#navbar-sidebar">
        <li class="nav-item">
            <a class="nav-link <?php echo $is_account_type_active ? 'active' : ''; ?>" href="accounttype.php">Manage Account Types</a>
        </li>
    </ul>
</li>


                <!-- History Menu -->
                <?php 
                $history_pages = ['debithistory.php','credithistory.php'];
                $is_history_menu_active = in_array($current_page_filename, $history_pages);
                ?>
                <li class="nav-item">
                    <a class="nav-link" data-bs-toggle="collapse" href="#collapseHistory" role="button" aria-expanded="<?php echo $is_history_menu_active ? 'true' : 'false'; ?>" aria-controls="collapseHistory">
                        History
                    </a>
                    <ul class="nav collapse flex-column <?php echo $is_history_menu_active ? 'show' : ''; ?>" id="collapseHistory" data-bs-parent="#navbar-sidebar">
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'debithistory.php') ? 'active' : ''; ?>" href="debithistory.php">Debit History</a></li>
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'credithistory.php') ? 'active' : ''; ?>" href="credithistory.php">Credit History</a></li>
                    </ul>
                </li>

                <!-- Settings Menu -->
                <?php 
                $settings_pages = ['transfer_codes.php','systemsettings.php','adminpass.php'];
                $is_settings_menu_active = in_array($current_page_filename, $settings_pages);
                ?>
                <li class="nav-item">
                    <a class="nav-link" data-bs-toggle="collapse" href="#collapseSettings" role="button" aria-expanded="<?php echo $is_settings_menu_active ? 'true' : 'false'; ?>" aria-controls="collapseSettings">
                        Settings
                    </a>
                    <ul class="nav collapse flex-column <?php echo $is_settings_menu_active ? 'show' : ''; ?>" id="collapseSettings" data-bs-parent="#navbar-sidebar">
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'transfer_codes.php') ? 'active' : ''; ?>" href="transfer_codes.php">Transfer Codes / Pin</a></li>
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'systemsettings.php') ? 'active' : ''; ?>" href="systemsettings.php">Settings</a></li>
                        <li class="nav-item"><a class="nav-link <?php echo ($current_page_filename == 'adminpass.php') ? 'active' : ''; ?>" href="adminpass.php">Admin Password</a></li>
                    </ul>
                </li>
                <li class="nav-item"><a class="nav-link" href="email_manager.php">Email Users</a></li>
                <li class="nav-item"><a class="nav-link" href="logout.php">Logout</a></li>
            </ul>   

            <div class="d-flex align-items-center justify-content-between text-primary-hover mt-auto p-3">
    <a class="h6 fw-light mb-0 text-body" href="https://wa.me/2349116768366" target="_blank" data-bs-toggle="tooltip" data-bs-placement="top" aria-label="Contact Developer">
        <i class="bi bi-whatsapp"></i> Contact Developer
    </a>
     
</div>  


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