<?
$a = $b = $c = $d = $e = $f = $g = $h = $i = $j = $k = $l = $m = $n= $o= $p= $q= $r= $s= $t= "";
$x = "class=\"active-menu\" ";

switch (basename($_SERVER["SCRIPT_FILENAME"], '.php')) {
  case "genealogy":
  $a = $x;
  break;

  case "list":
  case "registration":
  $b = $x;
  break;

  case "bonus":
  $c = $x;
  break;
  
  case "profile":
  $k = $x;
  break;
  
  case "stock_purchase":
  $l = $x;
  break;

  case "marketing":
  $d = $x;
  break;

  case "withdrawal":
  $e = $x;
  break;

  case "wallet":
  $r = $x;
  break;

  case "pin_transfer":
  case "buy_pin_fpx":      
  case "generate":
  case "pinhistory":
  $f = $x;
  break;  
  
  case "system":
  case "level_commission":
  case "trip_packages":
  $g = $x;
  break; 
  
  case "redeem":
  case "qr":
  $h = $x;
  break;
  
  case "dashboard":
  $i = $x;
  break; 
  
  case "list_m":
  case "registration_m":
  $j = $x;
  break;
  
  case "order_list";
  $m = $x;
  break;
  
  case "pay_list";
  $n = $x;
  break;
  
  case "sales_list";
  $o = $x;
  break;
  
  case "pack_list";
  case "pack_scan";
  $p = $x;
  break;
  
  case "ship_list";
  case "ship_scan";
  $q = $x;
  break;
  
  case "product_list":
  $s = $x;
  break;
  
  case "package_list":
  $t = $x;
  break;
  
}



?>
<nav class="navbar-default navbar-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav" id="main-menu">

                <!-- USER INFO -->
                <li class="text-center">
 <?php
                     if ($_SESSION['role'] != "admin") {
                                                            // Define username (optional check)
                                                            $username = isset($_SESSION['username']) ? $_SESSION['username'] : '';
                                                            
                                                            // Define image path
                                                            $userImage = "uploads/profile/{$username}.jpg";
                                                            $defaultImage = "assets/img/find_user.png";
                                                            
                                                            if (!empty($_SESSION['photo']) && file_exists($userImage)) {
                                                                $imgSrc = $userImage . "?v=" . time(); // force refresh
                                                            } else {
                                                                $imgSrc = $defaultImage;
                                                            }
                     } else {
                                                            
                                                            // Define username (optional check)
                                                            $username = isset($_SESSION['username']) ? $_SESSION['username'] : '';
                                                            
                                                            // Define image path
                                                            $userImage = "uploads/profile/{$username}.jpg";
                                                            $defaultImage = "assets/img/find_user.png";
                                                            
                                                            if (!empty($_SESSION['photo']) && file_exists($userImage)) {
                                                                $imgSrc = $userImage . "?v=" . time(); // force refresh
                                                            } else {
                                                                $imgSrc = $defaultImage;
                                                            }
                     }
 ?>
                            <img src="<?= htmlspecialchars($imgSrc) ?>" 
                                 class="user-image img-responsive" 
                                 style="width:200px; height:200px; object-fit:cover; border-radius:50%;" 
                                 alt="User profile image" />
                                 <br>
                    <code><?= $_SESSION['username'] ?></code><br>
                    <span style="color:#fff"><?= $_SESSION['namapenuh'] ?></span><br><br>
                </li>

                <!-- DASHBOARD -->
                <li><a <?= $i ?> href="dashboard.php"><i class="fa fa-dashboard"></i> Dashboard</a></li>
 
<?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier', 'finance' ])) { ?>                
 
                <!-- PROFILE -->
                <li><a <?= $k ?> href="profile.php"><i class="fa fa-user-circle"></i> My Profile</a></li>
                
<?php       }   ?>

<?php if (  $_SESSION['status'] === "active") { ?>

<?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier'])) { ?> 

                <!-- GENEALOGY -->
                <li><a <?= $a ?> href="genealogy.php"><i class="fa fa-sitemap"></i> Genealogy</a></li>
            
                <!-- BONUS -->
                <li>
                    <a <?= $c ?> href="#"><i class="fa fa-gift"></i> Bonus <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li><a href="bonus.php"><i class="fa fa-history"></i> History</a></li>
                        <?php if ($_SESSION['role'] == "admin") { ?>
                            <li><a href="add_bonus.php"><i class="fa fa-plus-circle"></i> Add Bonus</a></li>
                            <li><a href="delete_bonus.php"><i class="fa fa-trash"></i> Delete Bonus</a></li>
                        <?php } ?>
                    </ul>
                </li>
<?php       }   ?>    

<?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier', 'finance' ])) { ?> 

                <!-- WITHDRAWAL -->
                <li><a <?= $e ?> href="withdrawal.php"><i class="fa fa-money-bill-transfer"></i> Withdrawal</a></li>
<?php       }   ?>

<?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier'])) { ?> 

                <!-- MEMBERS -->
                <li>
                    <a <?= $b ?> href="#"><i class="fa fa-users"></i> Members <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <?php  if (in_array($_SESSION['role'], ['admin', 'cashier'])) { ?>
                            <li><a href="list.php"><i class="fa fa-list"></i> Members List</a></li>
                        <?php } ?>
                        <li><a href="registration.php"><i class="fa fa-user-plus"></i> New Registration</a></li>
                    </ul>
                </li>
                
 <?php      }   ?>
 
                <!-- MERCHANTS -->
                <?php if ($_SESSION['role'] == "admin") { ?>
                <li>
                    <a <?= $j ?> href="#"><i class="fa fa-store"></i> Merchants <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li><a href="list_m.php"><i class="fa fa-list"></i> Merchant List</a></li>
                        <li><a href="registration_m.php"><i class="fa fa-user-plus"></i> New Registration</a></li>
                    </ul>
                </li>
                <?php } 
                
   
    
            if ($_SESSION['role'] === "admin") {        ?> 
            
                <!-- PRODUCT -->
                <li>
                    <a <?= $s ?> href="#"><i class="fa-solid fa-tags"></i> Product <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li><a href="product_list.php"><i class="fa fa-list"></i> Product List</a></li>
                    </ul>
                </li>
                
                    <!-- PACKAGE -->
                <li>
                    <a <?= $t ?> href="#"><i class="fa-solid fa-box"></i> Package <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li><a href="package_list.php"><i class="fa fa-list"></i> Package List</a></li>
                    </ul>
                </li>

<?php       }   ?>
 
<?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier' ])) { ?> 
                <!-- STOCK -->
                <li>
                    <a <?= $l ?> href="#"><i class="fa fa-boxes-stacked"></i> Stock <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li><a href="stock_purchase.php"><i class="fa fa-cart-plus"></i> Stock Purchase</a></li>
                    </ul>
                </li>  
<?php       }   ?>

<?php       if ($_SESSION['role'] === "admin") {        ?>   

                <!-- SALES -->
                <li>
                    <a <?= $o ?> href="#"><i class="fa fa-chart-line"></i> Sales <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <li><a href="sales_list.php"><i class="fa fa-list"></i> Sales List</a></li>
                    </ul>
                </li>
<?php       }   ?>
<?php   }       ?>   

<?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier' ])) { ?> 

                <!-- ORDER -->
                <li>
                    <a <?= $m ?> href="#"><i class="fa fa-bag-shopping"></i> Order 
                        <span id="orderBadge" class="badge bg-danger ml-1" style="color:white; background-color:red; ">0</span>
                        <span class="fa arrow"></span>
                    </a>
                    <ul class="nav nav-second-level">
                        <li><a href="order_list.php"><i class="fa fa-list"></i> Order List</a></li>
                    </ul>
                </li>
<?php       }   ?>            
            
<?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier', 'finance' ])) { ?>                
                <!-- PAYMENT -->
                <li>
                    <a <?= $n ?> href="#">
                        <i class="fa-solid fa-dollar-sign"></i> Payment  
                        <span id="paymentBadge" class="badge bg-danger ml-1" style="color:white; background-color:red; margin-left:5px;"> 0</span>
                        <span class="fa arrow"></span>
                    </a>
                    <ul class="nav nav-second-level">
                        <li><a href="pay_list.php"><i class="fa fa-list"></i> Payment List</a></li>
                    </ul>
                </li>
<?php       }     

        if ($_SESSION['status'] === "active") { 
            if (in_array($_SESSION['role'], ['packaging', 'admin', 'user',  'cashier'])) { ?>        
            
                <!-- PACKING -->
                <li>
                    <a <?= $p ?> href="#">
                        <i class="fa-solid fa-boxes-packing"></i> Packaging  
                        <span id="packagingBadge" class="badge bg-danger ml-1" style="color:white; background-color:red; margin-left:5px;">0</span>
                        <span class="fa arrow"></span>
                    </a>
                    <ul class="nav nav-second-level">
                        <li><a href="pack_list.php"><i class="fa fa-list"></i> Packaging List</a></li>
            
                        <?php if (in_array($_SESSION['role'], ['packaging', 'admin', 'cashier'])) { ?>
                            <li><a href="pack_scan.php"><i class="fa-solid fa-qrcode"></i> Packaging Scan</a></li>
                        <?php } ?>
            
                    </ul>
                </li>
            
<?php       }

            if (in_array($_SESSION['role'], ['shipping', 'admin', 'user',  'cashier'])) { ?>        
 
                <!-- SHIPPING -->
                <li>
                    <a <?= $q ?> href="#">
                        <i class="fa-solid fa-truck"></i> Shipping  
                        <span id="shippingBadge" class="badge bg-danger ml-1" style="color:white; background-color:red; margin-left:5px;"> 0</span>
                        <span class="fa arrow"></span>
                    </a>
                    <ul class="nav nav-second-level">
                        <li><a href="ship_list.php"><i class="fa fa-list"></i> Shipping List</a></li>
                        <?php if (in_array($_SESSION['role'], ['shipping', 'admin', 'cashier'])) { ?>
                        <li><a href="ship_scan.php"><i class="fa-solid fa-qrcode"></i> Shipping Scan</a></li>
                        <?php } ?>
                    </ul>
                </li>
<?php       }               ?>    
<?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier' ])) { ?> 
                <!-- PIN -->
                <li>
                    <a <?= $f ?> href="#"><i class="fa fa-key"></i> PIN <span class="fa arrow"></span></a>
                    <ul class="nav nav-second-level">
                        <?php       if (in_array($_SESSION['role'], ['admin', 'cashier' ]))   { ?>
                            <li><a href="generate.php"><i class="fa fa-cogs"></i> Generate PIN</a></li>
                            <li><a href="buy_pin_fpx.php"><i class="fa fa-credit-card"></i> Buy PIN via FPX</a></li>
                        <?php       } ?>
                        <?php       if (in_array($_SESSION['role'], ['admin', 'user',  'cashier' ])) { ?> 
                            <li><a href="pin_transfer.php"><i class="fa fa-exchange"></i> Transfer PIN</a></li>
                            <li>
                                <a href="#"><i class="fa fa-clock-rotate-left"></i> PIN History <span class="fa arrow"></span></a>
                                <ul class="nav nav-third-level">
                                    <li><a href="pinhistory.php?x=unused"><i class="fa fa-circle"></i> Unused PIN</a></li>
                                    <li><a href="pinhistory.php?x=used"><i class="fa fa-check"></i> Used PIN</a></li>
                                </ul>
                            </li>
                        <?php       }   ?>
                    </ul>
                </li>
<?php       }   ?>
<?php   } ?>

<?php if ($_SESSION['role'] == "merchant") { ?>
    <!-- EWALLET -->
    <li><a <?= $q ?> href="wallet.php"><i class="fa fa-wallet"></i> eWallet</a></li>
<?php } ?>

<?php if ($_SESSION['role'] == "merchant" || $_SESSION['role'] == "admin") { ?>
    <!-- REDEEM -->
    <li>
        <a <?= $h ?> href="#"><i class="fa fa-gift"></i> Redeem <span class="fa arrow"></span></a>
        <ul class="nav nav-second-level">
            <li><a href="qr.php"><i class="fa fa-qrcode"></i> Scan QR Code</a></li>
            <li><a href="redeem.php"><i class="fa fa-history"></i> History</a></li>
        </ul>
    </li>
<?php } ?>

<?php if ($_SESSION['role'] == "user") { ?>
    <!-- MARKETING -->
    <li><a <?= $d ?> href="marketing.php"><i class="fa fa-bullhorn"></i> Marketing Tools</a></li>
<?php } ?>

<?php if ($_SESSION['role'] == "admin") { ?>
    <!-- SETTINGS -->
    <li>
        <a <?= $g ?> href="#"><i class="fa fa-cogs"></i> Settings <span class="fa arrow"></span></a>
        <ul class="nav nav-second-level">
            <li><a href="system.php"><i class="fa fa-gear"></i> MLM System</a></li>
            <!-- <li><a href="level_commission.php"><i class="fa fa-signal"></i> Level Commission</a></li>
            <li><a href="trip_packages.php"><i class="fa fa-plane"></i> Trip Packages</a></li> -->
        </ul>
    </li>
<?php } ?>

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


<style>
    /* Smooth blink effect: background highlight */
    .badge-blink {
        animation: highlightBlink 1s ease-in-out 3;
    }
    
    @keyframes highlightBlink {
        0% { background-color: red; color: white; }
        50% { background-color: yellow; color: black; }
        100% { background-color: red; color: white; }
    }
</style>

<script>
    // Track last counts
    let lastOrder = { value: null };
    let lastPayment = { value: null };
    let lastPackaging = { value: null };
    let lastShipping = { value: null };
    
    // Generic function to update badge
    async function updateBadge(badgeId, url, lastCountRef) {
        try {
            const res = await fetch(url);
            const data = await res.json();
            const badge = document.getElementById(badgeId);
    
            if (badge) {
                if (lastCountRef.value !== null && lastCountRef.value !== data.count) {
                    badge.textContent = data.count;
                    badge.classList.add("badge-blink");
    
                    // Remove blink class after animation completes
                    setTimeout(() => badge.classList.remove("badge-blink"), 3000);
                } else {
                    badge.textContent = data.count;
                }
                lastCountRef.value = data.count;
            }
        } catch (err) {
            console.error(`Error fetching ${badgeId} count:`, err);
        }
    }
        
    // Initial load
    updateBadge('orderBadge',       'order_count.php',  lastOrder);
    updateBadge('paymentBadge',     'pay_count.php',    lastPayment);
    updateBadge('packagingBadge',   'pack_count.php',   lastPackaging);
    updateBadge('shippingBadge',    'ship_count.php',   lastShipping);
    
    // Refresh every 10 seconds
    setInterval(() => {
        updateBadge('orderBadge',       'order_count.php',  lastOrder);
        updateBadge('paymentBadge',     'pay_count.php',    lastPayment);
        updateBadge('packagingBadge',   'pack_count.php',   lastPackaging);
        updateBadge('shippingBadge',    'ship_count.php',   lastShipping);
    }, 10000);
</script>
        
        
        
