in this codes (<?php
// public/dashboard.php

// Enable error reporting for debugging (disable in production)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Include authentication and database connection first
require_once '../includes/auth.php';
require_once '../includes/db_connect.php';

// Now include the header
require_once '../includes/header.php';

/**
 * Helper Function to Fetch Product Price by ID
 *
 * @param mysqli $conn
 * @param int $product_id
 * @return float
 */
function get_product_price($conn, $product_id) {
    $price_sql = "SELECT price FROM products WHERE id = ? LIMIT 1";
    $price_stmt = $conn->prepare($price_sql);
    if (!$price_stmt) {
        return 0.00;
    }
    $price_stmt->bind_param("i", $product_id);
    $price_stmt->execute();
    $price_result = $price_stmt->get_result();
    if ($price_result && $price_result->num_rows === 1) {
        $price = (float)$price_result->fetch_assoc()['price'];
    } else {
        $price = 0.00;
    }
    $price_stmt->close();
    return $price;
}

/**
 * Fetch Quick Stats
 */

// Total Orders
$totalOrdersSql = "SELECT COUNT(*) AS total FROM orders";
$totalOrdersResult = $conn->query($totalOrdersSql);
$totalOrders = ($totalOrdersResult && $row = $totalOrdersResult->fetch_assoc()) ? (int)$row['total'] : 0;

// Pending Orders
$pendingOrdersSql = "SELECT COUNT(*) AS total FROM orders WHERE status = 'Pending'";
$pendingOrdersResult = $conn->query($pendingOrdersSql);
$pendingOrders = ($pendingOrdersResult && $row = $pendingOrdersResult->fetch_assoc()) ? (int)$row['total'] : 0;

// Completed Orders
$completedOrdersSql = "SELECT COUNT(*) AS total FROM orders WHERE status = 'Completed'";
$completedOrdersResult = $conn->query($completedOrdersSql);
$completedOrders = ($completedOrdersResult && $row = $completedOrdersResult->fetch_assoc()) ? (int)$row['total'] : 0;

// Delivered Orders
$deliveredOrdersSql = "SELECT COUNT(*) AS total FROM orders WHERE status = 'Delivered'";
$deliveredOrdersResult = $conn->query($deliveredOrdersSql);
$deliveredOrders = ($deliveredOrdersResult && $row = $deliveredOrdersResult->fetch_assoc()) ? (int)$row['total'] : 0;

// Total Sales from Orders (Primary Products)
$totalSalesOrdersSql = "
    SELECT SUM(products.price * orders.quantity) AS total
    FROM orders
    JOIN products ON orders.product_id = products.id
    WHERE orders.status IN ('Completed', 'Delivered')
";
$totalSalesOrdersResult = $conn->query($totalSalesOrdersSql);
$totalSalesOrders = ($totalSalesOrdersResult && $row = $totalSalesOrdersResult->fetch_assoc()) ? (float)$row['total'] : 0.00;

// Total Sales from Order Items (Additional Products)
$totalSalesOrderItemsSql = "
    SELECT SUM(products.price * order_items.quantity) AS total
    FROM order_items
    JOIN products ON order_items.product_id = products.id
    JOIN orders ON order_items.order_id = orders.id
    WHERE orders.status IN ('Completed', 'Delivered')
";
$totalSalesOrderItemsResult = $conn->query($totalSalesOrderItemsSql);
$totalSalesOrderItems = ($totalSalesOrderItemsResult && $row = $totalSalesOrderItemsResult->fetch_assoc()) ? (float)$row['total'] : 0.00;

// Total Sales (Combined)
$totalSales = $totalSalesOrders + $totalSalesOrderItems;

// Fetch Recent Orders (Primary and Additional Products)
$recentOrdersSql = "
    SELECT 
        orders.id AS order_id,
        customers.name AS customer_name,
        customers.contact,
        customers.delivery_address,
        orders.status,
        orders.order_date,
        orders.delivery_datetime,
        products.id AS primary_product_id,
        products.name AS primary_product_name,
        products.size AS primary_product_size,
        orders.quantity AS primary_quantity,
        orders.packaging_preferences AS primary_packaging_preferences
    FROM orders
    JOIN customers ON orders.customer_id = customers.id
    JOIN products ON orders.product_id = products.id
    ORDER BY orders.order_date DESC
    LIMIT 5
";
$recentOrdersResult = $conn->query($recentOrdersSql);
$recentOrders = ($recentOrdersResult && $recentOrdersResult->num_rows > 0) ? $recentOrdersResult : false;

// Fetch Order Items for Recent Orders
$recentOrderIds = [];
if ($recentOrders) {
    while ($order = $recentOrders->fetch_assoc()) {
        $recentOrderIds[] = (int)$order['order_id'];
    }
}

// Reset the pointer of recentOrdersResult for later use
if ($recentOrders) {
    $recentOrdersResult->data_seek(0);
}

// Fetch all additional products for recent orders
$additionalProducts = [];
if (!empty($recentOrderIds)) {
    $placeholders = implode(',', array_fill(0, count($recentOrderIds), '?'));
    $types = str_repeat('i', count($recentOrderIds));
    $additionalProductsSql = "
        SELECT 
            order_items.order_id,
            products.name AS product_name,
            products.size AS product_size,
            order_items.quantity,
            order_items.packaging_preferences,
            products.price
        FROM order_items
        JOIN products ON order_items.product_id = products.id
        WHERE order_items.order_id IN ($placeholders)
    ";
    $additionalProductsStmt = $conn->prepare($additionalProductsSql);
    if ($additionalProductsStmt) {
        $additionalProductsStmt->bind_param($types, ...$recentOrderIds);
        $additionalProductsStmt->execute();
        $additionalProductsResult = $additionalProductsStmt->get_result();
        while ($item = $additionalProductsResult->fetch_assoc()) {
            $additionalProducts[$item['order_id']][] = $item;
        }
        $additionalProductsStmt->close();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dashboard - Kaak Alia Management System</title>
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700&display=swap" rel="stylesheet">
    
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
    
    <!-- Embedded Custom Styles -->
    <style>
        /* ---------------------------------------------------
           1. CSS Variables for Consistent Design
        ---------------------------------------------------- */
        :root {
            --dark-gray: #333333; /* Header and Footer Background */
            --button-add-order: #e2a20c; /* Light Golden-Yellow */
            --button-view-orders: #118AB2; /* Blue */
            --button-manage-products: #06D6A0; /* Green */
            --button-manage-users: #F4A261; /* Orange */
            --button-generate-reports: #EF476F; /* Red-Pink */

            --main-text: #333333;
            --secondary-text: #666666;
            --white: #FFFFFF;
            --background-gradient: linear-gradient(to bottom, #f5f7fa, #c3cfe2);
            --divider-color: #FFD166;

            --hover-shadow: rgba(0, 0, 0, 0.2);
        }

        /* ---------------------------------------------------
           2. Global Styles
        ---------------------------------------------------- */
        body {
            font-family: 'Cairo', sans-serif;
            background-color: var(--white);
            margin: 0;
            padding: 30px;
            box-sizing: border-box;
        }

        h2, h5, p {
            margin: 0;
        }

        /* ---------------------------------------------------
           3. Header Styles
        ---------------------------------------------------- */
        .header {
            padding: 20px 30px;
            background-color: var(--dark-gray);
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
        }

        .header h1 {
            color: var(--white);
            font-size: 24px;
            font-weight: bold;
        }

        /* ---------------------------------------------------
           4. Welcome Section
        ---------------------------------------------------- */
        .welcome-section {
            background-color: var(--white);
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            padding: 20px;
            text-align: center;
            margin-bottom: 20px;
        }

        .welcome-section h3 {
            font-size: 20px;
            color: var(--main-text);
            margin-bottom: 10px;
        }

        .welcome-section hr {
            width: 50px;
            height: 3px;
            background-color: var(--button-add-order);
            border: none;
            margin: 10px auto;
        }

        /* ---------------------------------------------------
           5. Statistics Section
        ---------------------------------------------------- */
        .stats-section {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }

        .stat-card {
            background-color: var(--white);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            padding: 15px;
            display: flex;
            align-items: center;
            transition: transform 0.2s;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-icon {
            font-size: 24px;
            margin-right: 10px;
        }

        .stat-icon.total-orders { color: var(--secondary-text); }
        .stat-icon.pending-orders { color: var(--button-add-order); }
        .stat-icon.completed-orders { color: var(--button-view-orders); }
        .stat-icon.delivered-orders { color: var(--button-generate-reports); }
        .stat-icon.total-sales { color: var(--button-manage-products); }

        .stat-info h5 {
            font-size: 16px;
            color: var(--main-text);
            margin-bottom: 5px;
        }

        .stat-info p {
            font-size: 20px;
            font-weight: bold;
            color: var(--main-text);
        }

        /* ---------------------------------------------------
           6. Main Buttons Section
        ---------------------------------------------------- */
        .buttons-section {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 20px;
        }

        .action-button {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 100%;
            max-width: 200px;
            height: 60px;
            font-size: 18px;
            font-weight: bold;
            color: var(--white);
            border: none;
            border-radius: 8px;
            padding: 10px 20px;
            cursor: pointer;
            transition: transform 0.2s, box-shadow 0.2s;
            text-decoration: none;
            text-align: center;
        }

        .action-button.add-order { background-color: var(--button-add-order); }
        .action-button.view-orders { background-color: var(--button-view-orders); }
        .action-button.manage-products { background-color: var(--button-manage-products); }
        .action-button.manage-users { background-color: var(--button-manage-users); }
        .action-button.generate-reports { background-color: var(--button-generate-reports); }

        .action-button:hover {
            transform: scale(1.02);
            box-shadow: 0 6px 10px var(--hover-shadow);
        }

        /* ---------------------------------------------------
           7. Recent Orders Section
        ---------------------------------------------------- */
        .recent-orders-section {
            background-color: var(--white);
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }

        .recent-orders-section h4 {
            font-size: 16px;
            font-weight: bold;
            color: var(--main-text);
            margin-bottom: 15px;
        }

        .recent-orders-section table {
            width: 100%;
            border-collapse: collapse;
        }

        .recent-orders-section th, 
        .recent-orders-section td {
            padding: 12px;
            text-align: left;
            font-size: 16px;
            vertical-align: top;
        }

        .recent-orders-section th {
            background-color: #333333;
            color: var(--white);
            font-weight: bold;
        }

        .recent-orders-section tr:nth-child(even) {
            background-color: #F9F9F9;
        }

        .recent-orders-section tr:hover {
            background-color: #e6f7ff;
        }

        .recent-orders-section td {
            font-size: 14px;
            color: #333333;
        }

        .recent-orders-section .view-all {
            display: block;
            text-align: right;
            margin-top: 10px;
            font-size: 14px;
            color: #118AB2;
            font-weight: bold;
            text-decoration: none;
        }

        .recent-orders-section .view-all:hover {
            color: #075985;
        }

        /* Badge Styles */
        .badge-status-pending {
            background-color: #FFD166;
            color: var(--white);
            padding: 5px 10px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }

        .badge-status-in-progress {
            background-color: #118AB2;
            color: var(--white);
            padding: 5px 10px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }

        .badge-status-completed {
            background-color: #06D6A0;
            color: var(--white);
            padding: 5px 10px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }

        .badge-status-delivered {
            background-color: #EF476F;
            color: var(--white);
            padding: 5px 10px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }

        /* ---------------------------------------------------
           8. Footer Styles
        ---------------------------------------------------- */
        .footer {
            background-color: #333333;
            color: var(--white);
            padding: 15px;
            text-align: center;
            border-radius: 8px;
            font-size: 14px;
            line-height: 20px;
            margin-top: 40px;
        }

        /* ---------------------------------------------------
           9. Responsive Design Enhancements
        ---------------------------------------------------- */
        @media (min-width: 768px) {
            .buttons-section .action-button {
                width: 48%;
            }
        }

        @media (max-width: 767px) {
            .buttons-section .action-button {
                width: 100%;
            }

            .recent-orders-section table, 
            .recent-orders-section th, 
            .recent-orders-section td {
                font-size: 12px;
            }
        }

        @media (max-width: 480px) {
            .header {
                padding: 15px 20px;
            }

            .header h1 {
                font-size: 20px;
            }

            .welcome-section h3 {
                font-size: 18px;
            }

            .action-button {
                font-size: 16px;
                height: 50px;
            }

            .recent-orders-section th, 
            .recent-orders-section td {
                padding: 10px;
                font-size: 12px;
            }

            .recent-orders-section h4 {
                font-size: 14px;
            }
        }
    </style>
</head>
<body>

    <!-- Header -->
    <div class="header">
        <h1>Kaak Alia Dashboard</h1>
    </div>

    <!-- Welcome Section -->
    <div class="welcome-section">
        <h3>Welcome to the Kaak Alia Management System!</h3>
        <hr>
    </div>

    <!-- Statistics Section -->
    <div class="stats-section">
        <!-- Total Orders -->
        <div class="stat-card">
            <div class="stat-icon total-orders">
                <i class="bi bi-cart-fill"></i>
            </div>
            <div class="stat-info">
                <h5>Total Orders</h5>
                <p><?= number_format($totalOrders) ?></p>
            </div>
        </div>
        <!-- Pending Orders -->
        <div class="stat-card">
            <div class="stat-icon pending-orders">
                <i class="bi bi-hourglass-top"></i>
            </div>
            <div class="stat-info">
                <h5>Pending Orders</h5>
                <p><?= number_format($pendingOrders) ?></p>
            </div>
        </div>
        <!-- Completed Orders -->
        <div class="stat-card">
            <div class="stat-icon completed-orders">
                <i class="bi bi-check-circle-fill"></i>
            </div>
            <div class="stat-info">
                <h5>Completed Orders</h5>
                <p><?= number_format($completedOrders) ?></p>
            </div>
        </div>
        <!-- Delivered Orders -->
        <div class="stat-card">
            <div class="stat-icon delivered-orders">
                <i class="bi bi-truck"></i>
            </div>
            <div class="stat-info">
                <h5>Delivered Orders</h5>
                <p><?= number_format($deliveredOrders) ?></p>
            </div>
        </div>
        <!-- Total Sales -->
        <div class="stat-card">
            <div class="stat-icon total-sales">
                <i class="bi bi-cash-stack"></i>
            </div>
            <div class="stat-info">
                <h5>Total Sales (KD)</h5>
                <p><?= number_format($totalSales, 2) ?></p>
            </div>
        </div>
    </div>

    <!-- Main Buttons Section -->
    <div class="buttons-section">
        <a href="orders/add_order.php" class="action-button add-order">
            <i class="bi bi-plus-circle-fill me-2"></i>
            Add New Order
        </a>
        <a href="orders/view_orders.php" class="action-button view-orders">
            <i class="bi bi-eye-fill me-2"></i>
            View Orders
        </a>
        <a href="products/manage_products.php" class="action-button manage-products">
            <i class="bi bi-box-seam-fill me-2"></i>
            Manage Products
        </a>
        <a href="users/manage_users.php" class="action-button manage-users">
            <i class="bi bi-people-fill me-2"></i>
            Manage Users
        </a>
        <a href="reports/generate_reports.php" class="action-button generate-reports">
            <i class="bi bi-bar-chart-fill me-2"></i>
            Generate Reports
        </a>
    </div>

    <!-- Recent Orders Section -->
    <div class="recent-orders-section">
        <h4>Recent Orders</h4>
        <table>
            <thead>
                <tr>
                    <th>#ID</th>
                    <th>Customer Name</th>
                    <th>Contact</th>
                    <th>Delivery Address</th>
                    <th>Primary Product</th>
                    <th>Additional Products</th>
                    <th>Status</th>
                    <th>Order Date</th>
                    <th>Delivery DateTime</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($recentOrders && $recentOrders->num_rows > 0): ?>
                    <?php while($order = $recentOrders->fetch_assoc()): ?>
                        <tr>
                            <td><?= htmlspecialchars($order['order_id']) ?></td>
                            <td><?= htmlspecialchars($order['customer_name']) ?></td>
                            <td><?= htmlspecialchars($order['contact']) ?></td>
                            <td><?= nl2br(htmlspecialchars($order['delivery_address'])) ?></td>
                            <td>
                                <?= htmlspecialchars($order['primary_product_name'] . " - " . $order['primary_product_size'] . " (" . number_format(get_product_price($conn, $order['primary_product_id']), 2) . " KD)") ?><br>
                                Quantity: <?= htmlspecialchars($order['primary_quantity']) ?><br>
                                Packaging: <?= htmlspecialchars($order['primary_packaging_preferences'] ?? 'None') ?>
                            </td>
                            <td>
                                <?php
                                    $orderId = (int)$order['order_id'];
                                    if (isset($additionalProducts[$orderId])) {
                                        foreach ($additionalProducts[$orderId] as $item) {
                                            echo htmlspecialchars($item['product_name'] . " - " . $item['product_size'] . " (" . number_format($item['price'], 2) . " KD)") . "<br>";
                                            echo "Quantity: " . htmlspecialchars($item['quantity']) . "<br>";
                                            echo "Packaging: " . htmlspecialchars($item['packaging_preferences'] ?? 'None') . "<br><br>";
                                        }
                                    } else {
                                        echo "None";
                                    }
                                ?>
                            </td>
                            <td>
                                <?php
                                    switch ($order['status']) {
                                        case 'Pending':
                                            echo '<span class="badge-status-pending">Pending</span>';
                                            break;
                                        case 'In Progress':
                                            echo '<span class="badge-status-in-progress">In Progress</span>';
                                            break;
                                        case 'Completed':
                                            echo '<span class="badge-status-completed">Completed</span>';
                                            break;
                                        case 'Delivered':
                                            echo '<span class="badge-status-delivered">Delivered</span>';
                                            break;
                                        default:
                                            echo '<span class="badge bg-light text-dark">Unknown</span>';
                                    }
                                ?>
                            </td>
                            <td><?= htmlspecialchars(date("d M Y, h:i A", strtotime($order['order_date']))) ?></td>
                            <td><?= $order['delivery_datetime'] ? htmlspecialchars(date("d M Y, h:i A", strtotime($order['delivery_datetime']))) : 'N/A' ?></td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="9" class="text-center">No recent orders.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
        <a href="orders/view_orders.php" class="view-all">View All Orders</a>
    </div>

    <!-- Footer -->
    <div class="footer">
        &copy; 2024 Kaak Alia Bakery. All rights reserved.
    </div>

    <!-- Embedded Bootstrap JS -->
    <script src="bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>
) make the table respovines with all devices and maek it clear and easy to use in mobile version and add more codes to make it better and type full codes