<?
$a = $b = $c = $d = $e = $f = $g = $h = $i = $j = $k = $l = $m = $q= "";
$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":
  $q = $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;
  
}
?>
<nav class="navbar-default navbar-side" role="navigation">
            <div class="sidebar-collapse">
                <ul class="nav" id="main-menu">
                <li class="text-center">
                    <img src="assets/img/find_user.png" class="user-image img-responsive" style="width:100px; height:100px; object-fit:cover; border-radius:50%;" 
     /><br>
                    <code><?= $_SESSION['username'] ?></code><br>
                    <span style="color:#fff"><?= $_SESSION['namapenuh'] ?></span><br><br>
                </li>

                <li>
                        <a <?= $i ?>href="dashboard.php"><i class="fa fa-dashboard fa-1x"></i> Dashboard</a>
                </li>
            
                <li>
                        <a <?= $k ?>href="profile.php"><i class="fa fa-circle-user fa-1x"></i> My Profile</a>
                </li>
                

                    
<?php
    if (isset($_SESSION['role']) <> "merchant") {
?>
                     <li>
                        <a <?= $a ?>href="genealogy.php"><i class="fa fa-sitemap fa-1x"></i> Genealogy</a>
                    </li>
                    
<!-- BONUS -->
                    <li>
                            <a <?= $c ?> href="#"><i class="fa fa-usd fa-1x"></i>   Bonus <span class="fa arrow"></span></a>
                         
                            <ul class="nav nav-second-level">
                                    <li>
                                            <a href="bonus.php">History</a>
                                    </li>
<? if ($_SESSION['username'] == "admin") { ?>   
                                    <li>
                                            <a href="add_bonus.php">Add Bonus</a>
                                    </li>

                                    <li>
                                            <a href="delete_bonus.php">Delete Bonus</a>
                                    </li>
<? } ?>                            
                            </ul>                
                     

                    </li> 

<!-- WITHDRAWAL -->                    
                    <li>
                        <a <?= $e ?> href="withdrawal.php"><i class="fa fa-money-bill-transfer fa-1x"></i>   Withdrawal</a>
                    </li>
                    
                    <li>
                            <a <?= $b?>href="#"><i class="fa fa-users fa-1x"></i> Members<span class="fa arrow"></span></a>
                            <ul class="nav nav-second-level">
<? if ($_SESSION['username'] == "admin") { ?>
                                    <li>
                                            <a href="list.php">Members List</a>
                                    </li>
<? } ?>
                                    <li>
                                            <a href="registration.php">New Registration</a>
                                    </li>
                            </ul>
                    </li>
<? if ($_SESSION['username'] == "admin") { ?>
                    <li>
                            <a <?= $j?>href="#"><i class="fa fa-store fa-1x"></i> Merchants<span class="fa arrow"></span></a>
                            <ul class="nav nav-second-level">
                                    <li>
                                            <a href="list_m.php">Merchant List</a>
                                    </li>
                                    <li>
                                            <a href="registration_m.php">New Registration</a>
                                    </li>
                            </ul>
                    </li>
<? } ?>     
               <li>
                    <a <?= $m ?> href="#"><i class="fa-solid fa-bag-shopping fa-1x"></i> Order 
                        <span id="orderBadge" class="badge bg-danger" 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>
                <li>
                        <a <?= $l?>href="#"><i class="fa-solid fa-boxes-stacked fa-1x"></i> Stock <span class="fa arrow"></span></a>
                        <ul class="nav nav-second-level">
                            <li><a href="stock_purchase.php"> Stock Purchase</a></li>
                        </ul>
                        
                </li>
                    <li>
                            <a <?= $f?>href="#"><i class="fa fa-key fa-1x"></i> PIN<span class="fa arrow"></span></a>
                            <ul class="nav nav-second-level">
<? if ($_SESSION['username'] == "admin") { ?>                                    
                                    <li>
                                            <a href="generate.php">Generate PIN</a>
                                    </li>                                    
<? } ?>

                                    <li>
                                            <a href="buy_pin_fpx.php">Buy PIN via FPX</a>
                                    </li>

                                    <li>
                                            <a href="pin_transfer.php">Transfer PIN</a>
                                    </li>
                                    <!-- <li>
                                        <a href="#">Buy PIN<span class="fa arrow"></span></a>
                                        <ul class="nav nav-third-level">
                                            <li>
                                                <a href="#">Via FPX</a>
                                            </li>
                                            <li>
                                                <a href="zbuy_pin.php">From Bonus</a>
                                            </li>
                                        </ul>
                                    </li> -->
                                    <li>
                                        <a href="#">PIN History<span class="fa arrow"></span></a>
                                        <ul class="nav nav-third-level">
                                            <li>
                                                <a href="pinhistory.php?x=unused">Unused PIN</a>
                                            </li>
                                            <li>
                                                <a href="pinhistory.php?x=used">Used PIN</a>
                                            </li>
                                        </ul>
                                    </li>                                                                                                                             
                            </ul>
                    </li>
                   
<?php
    }
?>


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

<?php
    if (    (isset($_SESSION['role']) == "merchant") || (isset($_SESSION['role']) == "admin")  ){
?>
                    <li>
                            <a <?= $h?>href="#"><i class="fa fa-gift fa-1x"></i> Redeem <span class="fa arrow"></span></a>
                            <ul class="nav nav-second-level">
                                    <li>
                                            <a href="qr.php"><i class="fa fa-qrcode fa-1x"></i> Scan QrCode</a>
                                    </li>
                                    <li>
                                            <a href="redeem.php"><i class="fa fa-list fa-1x"></i> History</a>
                                    </li>
                            </ul>
                    </li>

<?php
    }
    if (    (isset($_SESSION['role']) <> "merchant") && (isset($_SESSION['role']) <> "admin")  ){
?>
                     <li>
                        <a <?= $d ?>href="marketing.php"><i class="fa fa-maxcdn fa-1x"></i> Marketing Tools </a>
                    </li>
<?php
    }

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

            </div>

        </nav>


                <script>

                    // GET COUNT LATEST ORDER ////////////////////////////////////////////////
                    let lastCount = null;
                    
                    async function updateOrderBadge() {
                        try {
                            let res = await fetch('order_count.php');
                            let data = await res.json();
                            let badge = document.getElementById('orderBadge');
                    
                            if (badge) {
                                // Kalau nilai bertukar, update & animasi blink
                                if (lastCount !== null && lastCount !== data.count) {
                                    badge.textContent = data.count;
                                    badge.classList.add("blink");
                    
                                    // buang class blink lepas animasi siap
                                    setTimeout(() => {
                                        badge.classList.remove("blink");
                                    }, 3000);
                                } else {
                                    badge.textContent = data.count;
                                }
                                lastCount = data.count;
                            }
                        } catch (err) {
                            console.error('Error fetching order count:', err);
                        }
                    }
                    
                    // First load
                    updateOrderBadge();
                    
                    // Refresh setiap 10s
                    setInterval(updateOrderBadge, 10000);
                </script>
        
        
        
