<?php
session_start();
// include "piback/auth/config.php";

// Session variables
$username = isset($_SESSION['username']) ? $_SESSION['username'] : '';
$userid   = isset($_SESSION['userid']) ? $_SESSION['userid'] : '';

// Determine if user is admin
$isAdmin = ($username === 'admin');

// Common counts (all users)
$totalsegment = $conn->query("SELECT COUNT(*) AS total FROM segments")->fetch_assoc()['total'];
$totalcat = $conn->query("SELECT COUNT(*) AS total FROM categories")->fetch_assoc()['total'];

// Admin or User-specific counts
if ($isAdmin) {
    $totaltel       = $conn->query("SELECT COUNT(*) AS total FROM teledata")->fetch_assoc()['total'];
    $totalleads     = $conn->query("SELECT COUNT(*) AS total FROM tb_leads")->fetch_assoc()['total'];
    $totalptvleads  = $conn->query("SELECT COUNT(*) AS total FROM tb_positiveleads")->fetch_assoc()['total'];
    $totalMO        = $conn->query("SELECT COUNT(*) AS total FROM marketing_order")->fetch_assoc()['total'];
} else {
    $totaltel       = $conn->query("SELECT COUNT(*) AS total FROM teledata WHERE staff='$userid'")->fetch_assoc()['total'];
    $totalleads     = $conn->query("SELECT COUNT(*) AS total FROM tb_leads WHERE staff='$userid'")->fetch_assoc()['total'];
    $totalptvleads  = $conn->query("SELECT COUNT(*) AS total FROM tb_positiveleads WHERE staff='$userid'")->fetch_assoc()['total'];
    $totalMO        = $conn->query("SELECT COUNT(*) AS total FROM marketing_order WHERE staff='$userid'")->fetch_assoc()['total'];
}
?>

<div class="dlabnav">
   <div class="dlabnav-scroll">
      <div class="dropdown header-profile2">
         <a class="nav-link" href="javascript:void(0);" role="button" data-bs-toggle="dropdown">
            <div class="header-info2 d-flex align-items-center">
               <img src="assets/images/profile/pic1.jpg" alt="">
               <div class="d-flex align-items-center sidebar-info">
                  <div>
                     <span class="font-w400 d-block"><?php echo htmlspecialchars($username); ?></span>
                  </div>
               </div>
            </div>
         </a>
      </div>

      <ul class="metismenu" id="menu">
         <li><a href="add-segment.php">
            <i class="flaticon-025-dashboard"></i>
            <span class="nav-text">Add Segment 
               <span class="badge badge-xs style-1 new-badge"><?php echo $totalsegment; ?></span>
            </span>
         </a></li>

         <li><a href="add-category.php">
            <i class="flaticon-025-dashboard"></i>
            <span class="nav-text">Add Category  
               <span class="badge badge-xs style-1 new-badge"><?php echo $totalcat; ?></span>
            </span>
         </a></li>

         <li><a href="teledata.php">
            <i class="flaticon-041-graph"></i>
            <span class="nav-text">Teledata  
               <span class="badge badge-xs style-1 new-badge"><?php echo $totaltel; ?></span>
            </span>
         </a></li>

         <li><a href="positives.php">
            <i class="flaticon-041-graph"></i>
            <span class="nav-text">Positives  
               <span class="badge badge-xs style-1 new-badge"><?php echo $totalptvleads; ?></span>
            </span>
         </a></li>

         <li><a href="leads.php">
            <i class="flaticon-041-graph"></i>
            <span class="nav-text">Leads  
               <span class="badge badge-xs style-1 new-badge"><?php echo $totalleads; ?></span>
            </span>
         </a></li>

         <li><a href="marketingdata.php">
            <i class="flaticon-041-graph"></i>
            <span class="nav-text">Marketing Order  
               <span class="badge badge-xs style-1 new-badge"><?php echo $totalMO; ?></span>
            </span>
         </a></li>
 <?php
         if($username === 'admin'){
         ?>
         <li><a href="create-user.php">
            <i class="flaticon-041-graph"></i>
            <span class="nav-text">Manage User  
              
            </span>
         </a></li>
<?php } ?>
         <li><a href="logout.php">
            <i class="flaticon-381-user-4"></i>
            <span class="nav-text">Log Out</span>
         </a></li>
      </ul>
   </div>
</div>
