<?php
$stmt = $conn->query("SELECT * FROM companies ORDER BY id DESC LIMIT 1");
$company = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<style> 
@media (max-width: 768px) {
   
    .iq-sidebar {
        position: fixed;
        top: 0;
        left: -250px; 
        width: 250px;
        height: 100%;
        overflow-y: auto;
        transition: left 0.3s ease;
        z-index: 1000;
    }

    .iq-sidebar.active {
        left: 0;
    }

    .iq-menu-bt-sidebar {
        position: fixed;
        top: 15px;
        left: 15px;
        z-index: 1100;
        cursor: pointer;
    }

    body.sidebar-open::before {
        content: "";
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        background: rgba(0, 0, 0, 0.5);
        z-index: 900;
    }
}

</style>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const sidebar = document.querySelector('.iq-sidebar');
    const toggleBtn = document.querySelector('.iq-menu-bt-sidebar .iq-menu-bt');

    if (toggleBtn && sidebar) {
        toggleBtn.addEventListener('click', () => {
            sidebar.classList.toggle('active');
            document.body.classList.toggle('sidebar-open');
        });
    }
});
</script>

<div class="iq-sidebar" style="background:rgb(5, 37, 78); ">
   <div class="iq-sidebar-logo d-flex justify-content-between">
      <a href="index.php">
         <div class="iq-light-logo">
            <!--<img src="images/logo.gif" class="img-fluid" alt=""style="height: 100px; width: auto;">-->
         </div>
         <div class="iq-dark-logo">
            <img src="images/logo-dark.gif" class="img-fluid" alt="" style="height: 100px; width: auto;">
         </div>
         <?php if (!empty($company['name'])): ?>
            <span style="text-transform: capitalize; color:white;font-weight:800">
               <?= htmlspecialchars($company['name']) ?></span>
         <?php endif; ?>

         <!-- <span style="color:white; font-size:16px; text-transform:capitalize; font-weight:700">Afghan Dry Fruit</span> -->

      </a>
      <div class="iq-menu-bt-sidebar">
         <div class="iq-menu-bt align-self-center">
            <div class="wrapper-menu">
               <div class="main-circle"><i class="ri-arrow-left-s-line"></i></div>
               <div class="hover-circle"><i class="ri-arrow-right-s-line"></i></div>
            </div>
         </div>
      </div>

   </div>

   <?php
   include 'includes/config.php';
   $email = $_SESSION['email'];

   $query = "SELECT id FROM users WHERE email = :email";
   $stmt = $conn->prepare($query);
   $stmt->bindParam(':email', $email);
   $stmt->execute();
   $user = $stmt->fetch(PDO::FETCH_ASSOC);
   $userID = $user['id'];

   $userRightsQuery = "SELECT user_auth FROM user_rights WHERE user_id = :userID";
   $userRightsStmt = $conn->prepare($userRightsQuery);
   $userRightsStmt->bindParam(':userID', $userID);
   $userRightsStmt->execute();
   $userRights = $userRightsStmt->fetchAll(PDO::FETCH_COLUMN);

   ?>

   <div id="sidebar-scrollbar">
      <nav class="iq-sidebar-menu">
         <ul id="iq-sidebar-toggle" class="iq-menu">
            <li class="iq-menu-title"><i class="ri-subtract-line"></i><span style="color:yellow"></span></li>
            <?php if (in_array(1, $userRights)) : ?>

               <li class="">
                  <a href="#menu-design" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-menu-3-line"></i><span>Contacts</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="menu-design" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <li><a href="list_supplier.php"><i class="ri-git-commit-line"></i>Suppliers</a></li>
                     <li><a href="list_customers.php"><i class="ri-text-spacing"></i>Customers</a></li>
                     <!-- <li><a href="list_customer_group.php"><i class="ri-indent-decrease"></i>Customer Group</a></li>
                           <li><a href="list_import_contact.php"><i class="ri-line-height"></i>Import Contact</a></li> -->
                  </ul>
               </li>
            <?php endif; ?>

            <?php if (in_array(2, $userRights)) : ?>
               <li>

                  <a href="#ecommerce" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-shopping-cart-line"></i><span>Products</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="ecommerce" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <?php if (in_array(2, $userRights)) : ?>

                        <li><a href="list_products.php"><i class="ri-git-commit-line"></i>List Products</a></li>
                     <?php endif; ?>
                     <?php if (in_array(3, $userRights)) : ?>

                        <li><a href="add_products.php"><i class="ri-text-spacing"></i>Add Product</a></li>
                     <?php endif; ?>
                     <?php if (in_array(4, $userRights)) : ?>
                        <li><a href="print_label.php"><i class="ri-indent-decrease"></i>Print Labels</a></li>
                     <?php endif; ?>

                     <!-- <li><a href="variations.php"><i class="ri-line-height"></i>Variations</a></li>
                           <li><a href="horizontal-menu.html"><i class="ri-git-commit-line"></i>Import Products</a></li>
                           <li><a href="horizontal-top-menu.html"><i class="ri-text-spacing"></i>Import Opening Stocks</a></li>
                           <li><a href="selling_price_group.php"><i class="ri-indent-decrease"></i>Selling Price Group</a></li> -->
                     <?php if (in_array(5, $userRights)) : ?>
                        <li><a href="units.php"><i class="ri-line-height"></i>Units</a></li>
                     <?php endif; ?>
                     <?php if (in_array(6, $userRights)) : ?>
                        <li><a href="categories.php"><i class="ri-git-commit-line"></i>Categroies</a></li>
                     <?php endif; ?>
                     <?php if (in_array(7, $userRights)) : ?>
                        <li><a href="brand.php"><i class="ri-text-spacing"></i>Brands</a></li>
                        <!-- <li><a href="add_color.php"><i class="ri-text-spacing"></i>Color</a></li>
                     <li><a href="sizes.php"><i class="ri-text-spacing"></i>Size</a></li> -->
                     <?php endif; ?>
                     <!-- <li><a href="two-sidebar.html"><i class="ri-indent-decrease"></i>Warranties</a></li> -->
                  </ul>
               </li>
            <?php endif; ?>

            <?php if (in_array(8, $userRights)) : ?>
               <li>
                  <a href="#ui-elements" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-pencil-ruler-line"></i><span>Purchases</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="ui-elements" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <?php if (in_array(8, $userRights)) : ?>
                        <li><a href="list_purchase.php"><i class="ri-git-commit-line"></i>List Purchases</a></li>
                     <?php endif; ?>
                     <?php if (in_array(9, $userRights)) : ?>
                        <li><a href="add_purchase.php"><i class="ri-text-spacing"></i>Add Purchase</a></li>
                     <?php endif; ?>
                     <!-- <li><a href="list_purchase_return.php"><i class="ri-indent-decrease"></i>List Purchase Return</a></li> -->
                  </ul>
               </li>
            <?php endif; ?>

            <?php if (in_array(10, $userRights)) : ?>
               <li>
                  <a href="#forms" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-profile-line"></i><span>Sell</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="forms" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <?php if (in_array(10, $userRights)) : ?>
                        <li style="display: none;"><a href="list_pos_manually.php"><i class="ri-git-commit-line"></i>All Sales</a></li>
                     <?php endif; ?>

                     <?php if (in_array(11, $userRights)) : ?>
                        <li style="display: none;"><a href="add_sale.php"><i class="ri-text-spacing"></i>Add Sale</a></li>
                     <?php endif; ?>

                     <?php if (in_array(12, $userRights)) : ?>
                        <li><a href="list_pos.php"><i class="ri-git-commit-line"></i>List POS</a></li>
                     <?php endif; ?>
                     <?php if (in_array(13, $userRights)) : ?>
                        <li><a href="pos.php"><i class="ri-indent-decrease"></i>POS</a></li>
                     <?php endif; ?>
                     <?php if (in_array(13, $userRights)) : ?>
                        <li><a href="return_sale.php"><i class="ri-indent-decrease"></i>Return Sale</a></li>
                     <?php endif; ?>
                     <?php if (in_array(13, $userRights)) : ?>
                        <li><a href="list_return_pos.php"><i class="ri-indent-decrease"></i>List Return Sale</a></li>
                        <!-- <li><a href="list_damage.php"><i class="ri-indent-decrease"></i>List Damage</a></li> -->
                     <?php endif; ?>
                     <!-- <li><a href="list_draft.php"><i class="ri-git-commit-line"></i>List Drafts</a></li>
                        <li><a href="list_qutation.php"><i class="ri-git-commit-line"></i>List Qutations</a></li>
                        <li><a href="list_sell_return.php"><i class="ri-git-commit-line"></i>List Sell Return</a></li>
                           <li><a href="list_shipment.php"><i class="ri-line-height"></i>Shipments</a></li>
                           <li><a href="list_discount.php"><i class="ri-git-commit-line"></i>Discounts</a></li> -->
                  </ul>
               </li>
            <?php endif; ?>

            <?php if (in_array(14, $userRights)) : ?>
               <!-- <li>
                  <a href="#mailbox" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-mail-line"></i><span>Stock Transfer</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="mailbox" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <?php if (in_array(14, $userRights)) : ?>
                        <li><a href="list_stock_receive.php"><i class="ri-inbox-line"></i>List Stock Receive</a></li>
                     <?php endif; ?>
                     <?php if (in_array(15, $userRights)) : ?>
                        <li><a href="list_stock_transfer.php"><i class="ri-inbox-line"></i>List Stock Transfer</a></li>
                     <?php endif; ?>
                     <?php if (in_array(16, $userRights)) : ?>
                        <li><a href="add_stock_transfer.php"><i class="ri-edit-line"></i>Add Stock Transfer</a></li>
                     <?php endif; ?>
                  </ul>
               </li> -->
            <?php endif; ?>
            <!--<li>-->
            <!--   <a href="#wizard-form" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-archive-drawer-line"></i><span>Stock Adjustment</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>-->
            <!--   <ul id="wizard-form" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">-->
            <!--   <li><a href="list_stock_adjustment.php"><i class="ri-inbox-line"></i>List Stock Adjuments</a></li>-->
            <!--   <li><a href="add_stock_adjustment.php"><i class="ri-edit-line"></i>Add Stock Adjuments</a></li>-->
            <!--   </ul>-->
            <!--</li>-->
            <?php if (in_array(17, $userRights)) : ?>
               <li>
                  <a href="#tables" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-table-line"></i><span>Expenses</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="tables" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <?php if (in_array(17, $userRights)) : ?>
                        <li><a href="list_expense.php"><i class="ri-inbox-line"></i>List Expenses</a></li>
                     <?php endif; ?>
                     <?php if (in_array(18, $userRights)) : ?>
                        <li><a href="add_expense.php"><i class="ri-edit-line"></i>Add Expenses</a></li>
                     <?php endif; ?>
                     <?php if (in_array(19, $userRights)) : ?>
                        <li><a href="list_expense_category.php"><i class="ri-edit-line"></i>Expense Categroies</a></li>
                     <?php endif; ?>
                  </ul>
               </li>
            <?php endif; ?>

            <?php if (in_array(20, $userRights)) : ?>
               <li>
                  <a href="#charts" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-pie-chart-box-line"></i><span>Reports</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="charts" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <li><a href="profit_loss_report.php"><i class="ri-file-chart-line"></i>Profit/Loss Report</a></li>
                     <!-- <li><a href="purchase_season_report.php"><i class="ri-bar-chart-line"></i>Purchase Seasons Report</a></li> -->
                     <!-- <li><a href="purchase_sale_report.php"><i class="ri-bar-chart-line"></i>Purchase & Sale</a></li> -->
                     <!-- <li><a href="tax_report.php"><i class="ri-folder-chart-line"></i>Tax Report</a></li> -->
                     <!--<li><a href="supplier_customer_report.php"><i class="ri-folder-chart-2-line"></i>Suplier & Customer Report</a></li>-->
                     <li><a href="audit_report.php"><i class="ri-folder-chart-2-line"></i>Audit Report</a></li>
                     <li><a href="stock_report.php"><i class="ri-folder-chart-2-line"></i>Stock Report</a></li>
                     <!-- <li><a href="stock_adjustment_report.php"><i class="ri-folder-chart-2-line"></i>Stock Adjustment Report</a></li> -->
                     <!-- <li><a href="trending_products.php"><i class="ri-folder-chart-2-line"></i>Trending Products</a></li> -->
                     <li><a href="item_report.php"><i class="ri-folder-chart-2-line"></i>Sale Items Reports</a></li>
                     <li><a href="purchase_season_report.php"><i class="ri-folder-chart-2-line"></i>Purchase Items Reports</a></li>
                     <!--<li><a href="product_purchase_report.php"><i class="ri-folder-chart-2-line"></i>Product Purchase Reports</a></li>-->
                     <li><a href="product_sell_report.php"><i class="ri-folder-chart-2-line"></i>Product Sell Reports</a></li>
                     <!-- <li><a href="product_damage_report.php"><i class="ri-folder-chart-2-line"></i>Product Damage Reports</a></li> -->
                     <!-- <li><a href="purchase_payment_report.php"><i class="ri-folder-chart-2-line"></i>Purchase Payment Reports</a></li> -->
                     <!-- <li><a href="sell_payment_report.php"><i class="ri-folder-chart-2-line"></i>Sell Payment Reports</a></li> -->
                     <!-- <li><a href="expense_report.php"><i class="ri-folder-chart-2-line"></i>Expense Reports</a></li> -->
                     <!-- <li><a href="register_report.php"><i class="ri-folder-chart-2-line"></i>Register Reports</a></li> -->
                     <!-- <li><a href="Sale_representative_report.php"><i class="ri-folder-chart-2-line"></i>Sale Representative Report Reports</a></li> -->
                  </ul>
               </li>
            <?php endif; ?>
            <?php if (in_array(21, $userRights)) : ?>

               <li>
                  <a href="#accounts" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-table-line"></i><span>Accounting</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="accounts" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <!-- <li><a href="list_recovery.php"><i class="ri-inbox-line"></i>Installment Recovery</a></li> -->
                     <li><a href="list_banks.php"><i class="ri-inbox-line"></i>Bank & Cash Accounts</a></li>
                     <li><a href="list_transaction.php"><i class="ri-edit-line"></i>Transactions</a></li>
                     <li><a href="list_customer_ledger.php"><i class="ri-edit-line"></i>Customer Ledger</a></li>
                     <li><a href="list_supplier_ledger.php"><i class="ri-edit-line"></i>Supplier Ledger</a></li>
                     <li><a href="list_cheque.php"><i class="ri-edit-line"></i>Cheques</a></li>
                     <li><a href="list_cheque_payable.php"><i class="ri-edit-line"></i>Payable Cheques</a></li>
                  </ul>
               </li>
            <?php endif; ?>
            <?php if (in_array(22, $userRights)) : ?>
               <li>
                  <a href="#userinfo" class="iq-waves-effect collapsed" data-toggle="collapse" aria-expanded="false"><i class="ri-user-line"></i><span>User Management</span><i class="ri-arrow-right-s-line iq-arrow-right"></i></a>
                  <ul id="userinfo" class="iq-submenu collapse" data-parent="#iq-sidebar-toggle">
                     <li><a href="list_users.php"><i class="ri-profile-line"></i>User</a></li>
                     <li><a href="list_company.php"><i class="ri-profile-line"></i>Company Details</a></li>
                     <li>
                        <!-- <a href="backup.php" target="_blank">
    <i class="ri-profile-line"></i> Backup Download
  </a> -->
                     </li>

                     <!-- <li><a href="profile-edit.html"><i class="ri-file-edit-line"></i>Roles</a></li> -->
                  </ul>
               </li>
            <?php endif; ?>
         </ul>
      </nav>
      <div class="p-3"></div>
   </div>
</div>