<?php
// session_start();

// ROLE FIX (case-insensitive)
$role = ($_SESSION['role'] ?? '');

?>

<div class="layout-wrap">

    <div class="overplay-menu d-lg-none"></div>

    <div class="section-menu-left">
        <div class="box-logo">
            <a href="index.html" id="site-logo-inner">
                <img class="" id="logo_header" alt="image" src="img/farm_logo.png">
            </a>
            <div class="button-show-hide">
                <i class="icon-chevron-left"></i>
            </div>
        </div>

        <div class="section-menu-left-wrap">
            <div class="center">
                <div class="center-item">
                    <ul class="">

                        <?php if ($role == 'Super-Admin') { ?>

                        <!-- ================= SUPER ADMIN ================= -->

                        <li class="menu-item has-children">
                            <a href="#" class="menu-item-button">
                                <div class="icon"><i class="icon-layers"></i></div>
                                <div class="text">Crop</div>
                            </a>
                            <ul class="sub-menu">
                                <li class="sub-menu-item"><a href="category_list.php"><div class="text">All Crop</div></a></li>
                                <li class="sub-menu-item"><a href="add_category.php"><div class="text">Add Crop</div></a></li>
                            </ul>
                        </li>

                        <li class="menu-item has-children">
                            <a href="#" class="menu-item-button">
                                <div class="icon"><i class="icon-layers"></i></div>
                                <div class="text">Variety</div>
                            </a>
                            <ul class="sub-menu">
                                <li class="sub-menu-item"><a href="lifeline_list.php"><div class="text">All Variety</div></a></li>
                                <li class="sub-menu-item"><a href="add_lifeline_config.php"><div class="text">Add Variety</div></a></li>
                            </ul>
                        </li>

                        <li class="menu-item has-children">
                            <a href="#" class="menu-item-button">
                                <div class="icon"><i class="icon-layers"></i></div>
                                <div class="text">Brand</div>
                            </a>
                            <ul class="sub-menu">
                                <li class="sub-menu-item"><a href="brand_list.php"><div class="text">All Brand</div></a></li>
                                <li class="sub-menu-item"><a href="add_brand.php"><div class="text">Add Brand</div></a></li>
                            </ul>
                        </li>

                        <li class="menu-item has-children">
                            <a href="#" class="menu-item-button">
                                <div class="icon"><i class="icon-layers"></i></div>
                                <div class="text">Nursery</div>
                            </a>
                            <ul class="sub-menu">
                                <li class="sub-menu-item"><a href="distributor_list.php"><div class="text">All Nursery</div></a></li>
                                <li class="sub-menu-item"><a href="add_distributor.php"><div class="text">Add Nursery</div></a></li>
                                <li class="sub-menu-item"><a href="add_sub_distributor.php"><div class="text">Add Sub Nursery</div></a></li>
                            </ul>
                        </li>

                        <li class="menu-item has-children">
                            <a href="#" class="menu-item-button">
                                <div class="icon"><i class="icon-layers"></i></div>
                                <div class="text">Inventory Management</div>
                            </a>
                            <ul class="sub-menu">
                                <!--<li class="sub-menu-item"><a href="inventory_list.php"><div class="text">All Inventory</div></a></li>-->
                                <li class="sub-menu-item"><a href="add_inventory.php"><div class="text">Add Inventory</div></a></li>
                                <li class="sub-menu-item"><a href="inventory_stat_calc.php"><div class="text">Sapling Inventory</div></a></li>
                            </ul>
                        </li>
                        
                        <li class="menu-item">
                            <a href="products.php">
                                <div class="icon"><i class="icon-pie-chart"></i></div>
                                <div class="text">Orders</div>
                            </a>
                        </li>
                        
                        <?php
                            if(session_status() === PHP_SESSION_NONE){
                                session_start();
                            }
                            
                            include 'includes/connect.php';
                            
                            $user_id = $_SESSION['user_id'] ?? $_SESSION['distributor_id'] ?? 0;
                            
                            $cart_count = 0;
                            
                            if($user_id){
                                $q = mysqli_query($conn, "SELECT SUM(quantity) as total FROM cart WHERE user_id='$user_id'");
                                if($q){
                                    $r = mysqli_fetch_assoc($q);
                                    $cart_count = $r['total'] ?? 0;
                                }
                            }
                            ?>
                            
                            <style>
                            .menu-item a{
                                position: relative;
                                display: flex;
                                align-items: center;
                                gap: 10px;
                            }
                            
                            .cart-badge{
                                position: absolute;
                                top: 16px;
                                left: 95px;
                                background: #ff3b30;
                                color: #fff;
                                font-size: 11px;
                                font-weight: 700;
                                min-width: 18px;
                                height: 18px;
                                padding: 0 5px;
                                border-radius: 999px;
                                display: flex;
                                align-items: center;
                                justify-content: center;
                                box-shadow: 0 3px 8px rgba(0,0,0,0.2);
                            }
                            </style>
                            
                            <li class="menu-item">
                                <a href="cart.php">
                            
                                    <div class="icon">
                                        <i class="icon-pie-chart"></i>
                                    </div>
                            
                                    <div class="text">Cart</div>
                            
                                    <?php if($cart_count > 0){ ?>
                                        <span class="cart-badge">
                                            <?php echo $cart_count; ?>
                                        </span>
                                    <?php } ?>
                            
                                </a>
                            </li>
                            
                            <li class="menu-item">
                            <a href="user_orders.php">
                                <div class="icon"><i class="icon-pie-chart"></i></div>
                                <div class="text">User Order Details</div>
                            </a>
                        </li>

                        <?php } elseif ($role == 'nursery') { ?>

                        <!-- ================= NURSERY ================= -->

                        <li class="menu-item has-children">
                            <a href="#" class="menu-item-button">
                                <div class="icon"><i class="icon-layers"></i></div>
                                <div class="text">Inventory Management</div>
                            </a>
                            <ul class="sub-menu">
                                <li class="sub-menu-item"><a href="inventory_list.php"><div class="text">All Inventory</div></a></li>
                                <li class="sub-menu-item"><a href="add_inventory.php"><div class="text">Add Inventory</div></a></li>
                                <li class="sub-menu-item"><a href="inventory_stat_calc.php"><div class="text">Sapling Inventory</div></a></li>
                            </ul>
                        </li>

                        <li class="menu-item has-children">
                            <a href="#"><div class="text">Crop</div></a>
                            <ul class="sub-menu">
                                <li><a href="category_list.php">All Crop</a></li>
                                <li><a href="add_category.php">Add Crop</a></li>
                            </ul>
                        </li>

                        <li class="menu-item has-children">
                            <a href="#"><div class="text">Variety</div></a>
                            <ul class="sub-menu">
                                <li><a href="lifeline_list.php">All Variety</a></li>
                                <li><a href="add_lifeline_config.php">Add Variety</a></li>
                            </ul>
                        </li>

                        <li class="menu-item has-children">
                            <a href="#"><div class="text">Brand</div></a>
                            <ul class="sub-menu">
                                <li><a href="brand_list.php">All Brand</a></li>
                                <li><a href="add_brand.php">Add Brand</a></li>
                            </ul>
                        </li>

                        <?php } elseif ($role == 'distributor') { ?>

                        <!-- ================= DISTRIBUTOR ================= -->

                        <li class="menu-item has-children">
                            <a href="#"><div class="text">Nursery</div></a>
                            <ul class="sub-menu">
                                <li><a href="distributor_list.php">All Nursery</a></li>
                                <li><a href="add_distributor.php">Add Nursery</a></li>
                            </ul>
                        </li>

                        <?php } ?>

                        <!-- ================= COMMON ================= -->

                        <li class="menu-item">
                            <a href="report.html">
                                <div class="icon"><i class="icon-pie-chart"></i></div>
                                <div class="text">Report</div>
                            </a>
                        </li>

                        <li class="menu-item">
                            <a href="setting.html">
                                <div class="icon"><i class="icon-settings"></i></div>
                                <div class="text">Setting</div>
                            </a>
                        </li>

                        <li class="menu-item">
                            <a href="logout.php">
                                <div class="icon"><i class="icon-log-out"></i></div>
                                <div class="text">Log out</div>
                            </a>
                        </li>

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

    <div class="section-content-right">
        <div class="header-dashboard">
            <div class="wrap">
                <div class="header-left">
                    <a href="index.html">
                        <img class="" id="logo_header_mobile" alt="image" src="images/logo/logo.svg">
                    </a>
                </div>

                <div class="header-grid">

                    <div class="header-item button-dark-light">
                        <i class="icon-moon"></i>
                    </div>

                    <div class="popup-wrap user type-header">
                        <div class="dropdown">
                            <button class="btn btn-secondary dropdown-toggle" type="button"
                                data-bs-toggle="dropdown">

                                <span class="header-user wg-user">
                                    <span class="image">
                                        <img src="images/avatar/user-1.png" alt="image">
                                    </span>
                                    <span class="text flex flex-column">
                                        <span class="name text-main-dark">Shafeen Shaikh</span>
                                        <span class="sub text-tiny"><?php echo ucfirst($role); ?></span>
                                    </span>
                                </span>

                            </button>

                            <div class="dropdown-menu dropdown-menu-end has-content">

                                <a href="setting.html" class="user-item">
                                    <div class="icon"><i class="icon-settings"></i></div>
                                    <div class="body-title-2">Setting</div>
                                </a>

                                <a href="faqs.html" class="user-item">
                                    <div class="icon"><i class="icon-headphones"></i></div>
                                    <div class="body-title-2">Support</div>
                                </a>

                                <a href="logout.php" class="user-item">
                                    <div class="icon"><i class="icon-log-out"></i></div>
                                    <div class="body-title-2">Log out</div>
                                </a>

                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>

        <div class="main-content">