<?php 
include "config.php";
session_start();

if (!isset($_SESSION['log']) || $_SESSION['log'] == "") {
    header("location:index.php");
    exit();
}

$user = $_SESSION['log'];

// 1. Fetch all permissions once into an array to optimize performance
$permissions = [];
$perm_query = mysqli_query($connect, "SELECT filename, datashow FROM userpermission WHERE username = '$user'");
while($row = mysqli_fetch_assoc($perm_query)) {
    $permissions[$row['filename']] = $row['datashow'];
}

// 2. Helper function to check permissions easily
function can_view($file, $permissions) {
    return isset($permissions[$file]) && $permissions[$file] == '1';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>VSI</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bootstrap/min.css">
  <link rel="stylesheet" href="fontawesome-free-5.15.1-web/css/all.css">
  <!-- <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> -->
  <script src="bootstrap/popper.min.js"></script>
  <script src="bootstrap/bootstrap.js"></script>
  <script src="javascript/selectt2.js"></script>

  <style>
    .navbar { background-color: #8F4A95 !important; }
    .nav-link, .navbar-brand { color: white !important; }
    .nav-link:hover { color: #FFD700 !important; }
    .dropdown-item:hover { background-color: #8F4A95; color: white !important; }
    
    /* Ensure the content doesn't hide under the fixed navbar */
    body { padding-top: 70px; }
    
    /* Optional: Custom scrollbar for dropdowns */
    .dropdown-menu {
        max-height: 400px;
        overflow-y: auto;
		
    }
      input[type="text"],select,.required {
   display: block;
    height: calc(1.5em + .75rem + 2px);
    padding: .375rem .75rem;
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    color: #495057;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #ced4da;
    border-radius: .25rem;
    transition: border-color .15s 
ease-in-out, box-shadow .15s 
ease-in-out
  }
  input[type="text"]:focus,select:focus,.required:focus{
        color: #495057;
    background-color: #fff;
    border-color: #80bdff;
    outline: 0;
    box-shadow: 0 0 0 .2rem rgba(0, 123, 255, .25);
  }
      .requiredvalue
{
    border-color: red !important;
}
  </style>
      <script>
  $(document).on('focus', '.select2-selection.select2-selection--single', function (e) {
  $(this).closest(".select2-container").siblings('select:enabled').select2('open');
});
</script>
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-dark fixed-top shadow">
  <a class="navbar-brand" href="#">YSS</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNav">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="mainNav">
    <ul class="navbar-nav mr-auto">
      
      <li class="nav-item">
        <a class="nav-link" href="home.php"><i class="fas fa-chart-line"></i> Dashboard</a>
      </li>

      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown"><i class="fas fa-graduation-cap"></i> Master</a>
        <div class="dropdown-menu">
          <?php if(can_view('productmaster', $permissions)) echo '<a class="dropdown-item" href="itemmaster.php">Product</a>'; ?>
          <?php if(can_view('customermaster', $permissions)) echo '<a class="dropdown-item" href="mastercustomer.php">Customer</a>'; ?>
         
          <?php if(can_view('vehiclemaster', $permissions)) echo '<a class="dropdown-item" href="vehiclemaster.php">Vehicle</a>'; ?>
          <?php if(can_view('customerratemaster', $permissions)) echo '<a class="dropdown-item" href="ratemaster.php">Customer Rate</a>'; ?>
         <?php if(can_view('Deliverysite', $permissions)) echo '<a class="dropdown-item" href="delivery_site.php">Delivery Site</a>'; ?>
           <?php if(can_view('employeemaster', $permissions)) echo '<a class="dropdown-item" href="masteremployee.php" >Employee Details</a>'; ?>
          <?php if(can_view('cftdetails', $permissions)) echo '<a class="dropdown-item" href="master_cft.php" >CFT Calculation</a>'; ?>
		   <?php if(can_view('creditdetails', $permissions)) echo '<a class="dropdown-item" href="expensemaster.php">Expense Title</a>'; ?>
        </div>
      </li>
       <?php if(can_view('ticket', $permissions)): ?>
      <li class="nav-item">
       <a class="nav-link" href="ticket.php"><i class="fas fa-bus"></i>Vehicle&nbsp;In</a>
      </li>
       <?php endif; ?>
       
        <?php if(can_view('secondtransaction', $permissions)): ?>
      <li class="nav-item">
      <a class="nav-link" href="secondtransaction.php"><i class="fas fa-truck"></i>Vehicle&nbsp;Out</a>
      </li>
       <?php endif; ?>
       
       <?php if(can_view('singleentry', $permissions)): ?>
      <li class="nav-item">
      <a class="nav-link" href="singleentry.php"><i class="fas fa-truck"></i>Own&nbsp;Vehicle</a>
      </li>
       <?php endif; ?>
       
       
     

      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown"><i class="fas fa-university"></i> Transaction</a>
        <div class="dropdown-menu">
          <?php if(can_view('creditdetails', $permissions)) echo '<a class="dropdown-item" href="credit.php">Credit</a>'; ?>
          <?php if(can_view('debitdetails', $permissions)) echo '<a class="dropdown-item" href="debit.php">Debit</a>'; ?>
          <?php if(can_view('advanceentry', $permissions)) echo '<a class="dropdown-item" href="advance.php">Advance</a>'; ?>
		   <?php if(can_view('creditdetails', $permissions)) echo '<a class="dropdown-item" href="cash_handling.php">Cash&nbsp;Handling</a>'; ?>
		    <?php if(can_view('creditdetails', $permissions)) echo '<a class="dropdown-item" href="expense_handling.php">Expense&nbsp;Handling</a>'; ?>
		   <?php if(can_view('machinereading', $permissions)) echo '<a class="dropdown-item" href="machine_units.php">Machine/EB&nbsp;Units</a>'; ?>
          
        </div>
      </li>

      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown"><i class="fas fa-file-alt"></i> Report</a>
        <div class="dropdown-menu">
          <?php if(can_view('emptyreport', $permissions)) echo '<a class="dropdown-item" href="emptyinreport.php">Empty-In</a>'; ?>
		  <?php if(can_view('loadoutreport', $permissions)) echo '<a class="dropdown-item" href="dayreport.php">Dayreport</a>'; ?>
		  <?php if(can_view('loadoutreport', $permissions)) echo '<a class="dropdown-item" href="abstractreport.php">Purchase/Sale Abstract</a>'; ?>
		   <?php if(can_view('machinereading', $permissions)) echo '<a class="dropdown-item" href="machinereport.php">Machinereport</a>'; ?>
          <?php if(can_view('loadoutreport', $permissions)) echo '<a class="dropdown-item" href="loadoutreport.php">Load-Out</a>'; ?>
          
          <?php if(can_view('dailyreport', $permissions)) echo '<a class="dropdown-item" href="dailyreport.php">Daily Report</a>'; ?>
		  
		   <?php if(can_view('loadoutreport', $permissions)) echo '<a class="dropdown-item" href="fullreport.php" >Full Report</a>'; ?>
	  <?php if(can_view('loadoutreport', $permissions)) echo '<a class="dropdown-item" href="splitupreport.php" >Overall Report</a>'; ?>
		  
          <?php if(can_view('materialreport', $permissions)) echo '<a class="dropdown-item" href="materialreport.php">Material Report</a>'; ?>
		  <?php if(can_view('materialreport', $permissions)) echo '<a class="dropdown-item" href="Purchasereport.php" >Purchase-Report</a>'; ?>
		  <?php if(can_view('salesreport', $permissions)) echo '<a class="dropdown-item" href="salesreport.php">Invoice Report</a>'; ?>
		  <?php if(can_view('salesreport', $permissions)) echo '<a class="dropdown-item" href="gstreport.php" >Tax-Report</a>'; ?>
		   <?php if(can_view('salesreport', $permissions)) echo '<a class="dropdown-item" href="customer_report.php" >Customer-Report</a>'; ?>
		   <?php if(can_view('salespending', $permissions)) echo '<a class="dropdown-item" href="salespending_report.php">Salespending</a>'; ?>
           <?php if(can_view('purchasepending', $permissions)) echo '<a class="dropdown-item" href="purchasepending_report.php">Purchasepending</a>'; ?>
		   <?php if(can_view('customerstatement', $permissions)) echo '<a class="dropdown-item" href="customerstatement.php">Customerstatement</a>'; ?>
           <?php if(can_view('supplierstatement', $permissions)) echo '<a class="dropdown-item" href="supplierstatement.php">Supplierstatement</a>'; ?>
          
            <?php if(can_view('creditdetails', $permissions)) echo '<a class="dropdown-item" href="expense_report.php">Expense&nbsp;Report</a>'; ?>
           
		   <?php if(can_view('deletedreport', $permissions)) echo '<a class="dropdown-item" href="deletereport.php">Deleted-Report</a>'; ?>
           
           
           
        </div>
      </li>

    </ul>

    <ul class="navbar-nav">
      <?php if(can_view('backup', $permissions)): ?>
        <li class="nav-item"><a class="nav-link" href="dbbackup.php"><i class="fas fa-cog"></i> Backup</a></li>
      <?php endif; ?>
       <?php if(can_view('setting', $permissions)): ?>
        <li class="nav-item"><a class="nav-link" href="userpermission.php"><i class="fas fa-cog"></i> Settings</a></li>
      <?php endif; ?>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">
          <i class="fas fa-user-circle"></i> <?php echo htmlspecialchars($user); ?>
        </a>
        <div class="dropdown-menu dropdown-menu-right">
          <a class="dropdown-item" href="logout.php">Logout</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

<div class="container-fluid">
    </div>

</body>
</html>