based on this info database(-- phpMyAdmin SQL Dump
-- version 5.2.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Nov 06, 2024 at 01:26 PM
-- Server version: 10.11.9-MariaDB
-- PHP Version: 7.2.34

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `u406552082_kaakalia`
--

-- --------------------------------------------------------

--
-- Table structure for table `customers`
--

CREATE TABLE `customers` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `contact` varchar(50) DEFAULT NULL,
  `delivery_address` text DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `orders`
--

CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `customer_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `quantity` int(11) NOT NULL,
  `packaging_preferences` varchar(255) DEFAULT NULL,
  `status` enum('Pending','In Progress','Completed','Delivered') DEFAULT 'Pending',
  `order_date` datetime DEFAULT current_timestamp(),
  `delivery_datetime` datetime DEFAULT NULL,
  `delivery_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `products`
--

CREATE TABLE `products` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `size` varchar(50) DEFAULT NULL,
  `price` decimal(10,2) NOT NULL,
  `category` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(255) NOT NULL,
  `role` enum('admin','staff') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `customers`
--
ALTER TABLE `customers`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `orders`
--
ALTER TABLE `orders`
  ADD PRIMARY KEY (`id`),
  ADD KEY `customer_id` (`customer_id`),
  ADD KEY `product_id` (`product_id`);

--
-- Indexes for table `products`
--
ALTER TABLE `products`
  ADD PRIMARY KEY (`id`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `username` (`username`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `customers`
--
ALTER TABLE `customers`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `orders`
--
ALTER TABLE `orders`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `products`
--
ALTER TABLE `products`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `orders`
--
ALTER TABLE `orders`
  ADD CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`),
  ADD CONSTRAINT `orders_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;), in this codes (<?php
// public/orders/add_order.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
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../includes/db_connect.php';

// Initialize variables
$success = '';
$error = '';

// Fetch products for selection
$products_sql = "SELECT * FROM products ORDER BY name ASC";
$products_result = $conn->query($products_sql);

if (!$products_result) {
    die("Error fetching products: " . $conn->error);
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Sanitize and validate input
    $customer_name = trim($_POST['customer_name']);
    $contact = trim($_POST['contact']);
    $delivery_address = trim($_POST['delivery_address']);
    $product_id = intval($_POST['product_id']);
    $quantity = intval($_POST['quantity']);
    $packaging_preferences = trim($_POST['packaging_preferences']);
    $delivery_datetime = isset($_POST['delivery_datetime']) ? trim($_POST['delivery_datetime']) : null;

    // Basic validation
    if (empty($customer_name)) {
        $error = "Customer name is required.";
    } elseif (empty($product_id)) {
        $error = "Product selection is required.";
    } elseif ($quantity <= 0) {
        $error = "Quantity must be at least 1.";
    } elseif ($delivery_datetime && !preg_match("/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/", $delivery_datetime)) {
        $error = "Invalid delivery date and time format.";
    } else {
        // Optional: Ensure delivery_datetime is not in the past
        if ($delivery_datetime && strtotime(str_replace('T', ' ', $delivery_datetime)) < strtotime(date('Y-m-d H:i'))) {
            $error = "Delivery date and time cannot be in the past.";
        }
    }

    if (empty($error)) {
        // Start transaction
        $conn->begin_transaction();

        try {
            // Insert into customers table using prepared statement
            $stmt = $conn->prepare("INSERT INTO customers (name, contact, delivery_address) VALUES (?, ?, ?)");
            if (!$stmt) {
                throw new Exception("Prepare statement failed: " . $conn->error);
            }
            $stmt->bind_param("sss", $customer_name, $contact, $delivery_address);
            if (!$stmt->execute()) {
                throw new Exception("Execute failed: " . $stmt->error);
            }
            $customer_id = $stmt->insert_id;
            $stmt->close();

            // Insert into orders table using prepared statement
            $stmt = $conn->prepare("INSERT INTO orders (customer_id, product_id, quantity, packaging_preferences, delivery_datetime) VALUES (?, ?, ?, ?, ?)");
            if (!$stmt) {
                throw new Exception("Prepare statement failed: " . $conn->error);
            }

            // If delivery_datetime is provided, bind it; else, bind null
            if ($delivery_datetime) {
                $stmt->bind_param("iiiss", $customer_id, $product_id, $quantity, $packaging_preferences, $delivery_datetime);
            } else {
                // Set to NULL
                $stmt->bind_param("iiiss", $customer_id, $product_id, $quantity, $packaging_preferences, $delivery_datetime);
            }

            if (!$stmt->execute()) {
                throw new Exception("Execute failed: " . $stmt->error);
            }
            $stmt->close();

            // Commit transaction
            $conn->commit();

            $success = "Order added successfully!";

            // Reset form fields
            $_POST = [];
        } catch (Exception $e) {
            // Rollback transaction
            $conn->rollback();
            $error = "Error adding order: " . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add New Order - 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="/kaak_alia_management_system/assets/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: #515151; /* Primary text and key elements */
            --soft-brown: #b78d67; /* Secondary color for accents and buttons */
            --deep-brown: #8e6a51; /* Accent color for interactive elements */
            --white: #ffffff; /* White background and button text */
            --light-gray: #f9f9f9; /* Light gray for input backgrounds and alternate table rows */
            --divider-color: #FFD166; /* Yellow for dividers */
            --error-color: #EF476F; /* Red-pink for error messages */
            --success-color: #06D6A0; /* Green for success messages */
            --hover-shadow: rgba(0, 0, 0, 0.2);
            --transition-speed: 0.3s;
        }

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

        h3, h5, label {
            color: var(--dark-gray);
            font-weight: 600;
        }

        .form-section {
            margin-bottom: 20px;
        }

        /* ---------------------------------------------------
           3. Header Styles
        ---------------------------------------------------- */
        .header {
            padding: 20px 30px;
            background-color: var(--dark-gray);
            border-radius: 8px;
            text-align: center;
            margin-bottom: 30px;
            color: var(--white);
            font-size: 24px;
            font-weight: bold;
        }

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

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

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

        /* ---------------------------------------------------
           5. Form Container Styles
        ---------------------------------------------------- */
        .form-container {
            background-color: #ffffff;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            border-radius: 8px;
            padding: 30px;
            max-width: 700px;
            margin: auto;
        }

        .form-container h3 {
            text-align: center;
            margin-bottom: 20px;
            color: var(--soft-brown);
        }

        /* ---------------------------------------------------
           6. Form Styles
        ---------------------------------------------------- */
        .form-control {
            border-radius: 8px;
            border: 1px solid #ccc;
            background-color: var(--light-gray);
            padding: 12px 15px;
            transition: border-color var(--transition-speed), box-shadow var(--transition-speed);
            width: 70%;
            margin: 0 auto 15px auto;
            display: block;
        }

        .form-control:focus {
            border-color: var(--deep-brown);
            box-shadow: 0 0 5px rgba(142, 106, 81, 0.3);
            background-color: #ffffff;
        }

        .form-label {
            margin-bottom: 5px;
            font-size: 14px;
            display: block;
            text-align: left;
            width: 70%;
            margin: 0 auto 5px auto;
        }

        /* ---------------------------------------------------
           7. Buttons Section
        ---------------------------------------------------- */
        .submit-button {
            width: 70%;
            height: 60px;
            font-size: 18px;
            font-weight: bold;
            border-radius: 8px;
            border: none;
            cursor: pointer;
            transition: transform var(--transition-speed), box-shadow var(--transition-speed);
            padding: 10px 20px;
            display: block;
            margin: 20px auto 0 auto;
            text-align: center;
        }

        .btn-add-order {
            background-color: #FFD166;
            color: var(--white);
        }

        .btn-add-order:hover {
            background-color: #FFC043;
            transform: scale(1.02);
            box-shadow: 0 4px 6px var(--hover-shadow);
        }

        /* ---------------------------------------------------
           8. Alert Styles
        ---------------------------------------------------- */
        .alert-success {
            background-color: var(--success-color);
            color: var(--white);
            border-radius: 8px;
            padding: 15px 20px;
            margin-bottom: 20px;
            text-align: center;
            width: 70%;
            margin: 20px auto;
        }

        .alert-danger {
            background-color: var(--error-color);
            color: var(--white);
            border-radius: 8px;
            padding: 15px 20px;
            margin-bottom: 20px;
            text-align: center;
            width: 70%;
            margin: 20px auto;
        }

        /* ---------------------------------------------------
           9. Responsive Design Enhancements
        ---------------------------------------------------- */
        @media (max-width: 768px) {
            .form-control, .form-label, .submit-button, .alert-success, .alert-danger {
                width: 90%;
            }
        }

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

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

            .form-control, .form-label, .submit-button, .alert-success, .alert-danger {
                width: 100%;
            }

            .submit-button {
                height: 50px;
                font-size: 16px;
            }
        }
    </style>
</head>
<body>

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

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

    <!-- Form Container -->
    <div class="form-container">
        <h3>Add New Order</h3>
        <?php if ($success): ?>
            <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
        <?php endif; ?>
        <?php if ($error): ?>
            <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>
        <form method="POST" action="">
            <!-- Customer Details -->
            <div class="form-section">
                <h5>Customer Details</h5>
                <label class="form-label" for="customer_name">Name <span class="text-danger">*</span></label>
                <input type="text" id="customer_name" name="customer_name" class="form-control" 
                       value="<?= isset($_POST['customer_name']) ? htmlspecialchars($_POST['customer_name']) : '' ?>" 
                       required>
                
                <label class="form-label" for="contact">Contact</label>
                <input type="text" id="contact" name="contact" class="form-control" 
                       value="<?= isset($_POST['contact']) ? htmlspecialchars($_POST['contact']) : '' ?>">
                
                <label class="form-label" for="delivery_address">Delivery Address</label>
                <textarea id="delivery_address" name="delivery_address" class="form-control"><?= isset($_POST['delivery_address']) ? htmlspecialchars($_POST['delivery_address']) : '' ?></textarea>
            </div>
            <hr>
            <!-- Order Details -->
            <div class="form-section">
                <h5>Order Details</h5>
                <label class="form-label" for="product_id">Product <span class="text-danger">*</span></label>
                <select id="product_id" name="product_id" class="form-control" required>
                    <option value="">Select Product</option>
                    <?php while($product = $products_result->fetch_assoc()): ?>
                        <option value="<?= htmlspecialchars($product['id']) ?>" <?= (isset($_POST['product_id']) && $_POST['product_id'] == $product['id']) ? 'selected' : '' ?>>
                            <?= htmlspecialchars($product['name'] . " - " . $product['size'] . " (" . number_format($product['price'], 2) . " KD)") ?>
                        </option>
                    <?php endwhile; ?>
                </select>
                
                <label class="form-label" for="quantity">Quantity <span class="text-danger">*</span></label>
                <input type="number" id="quantity" name="quantity" class="form-control" min="1" 
                       value="<?= isset($_POST['quantity']) ? intval($_POST['quantity']) : '1' ?>" required>
                
                <label class="form-label" for="packaging_preferences">Packaging Preferences</label>
                <input type="text" id="packaging_preferences" name="packaging_preferences" class="form-control" 
                       value="<?= isset($_POST['packaging_preferences']) ? htmlspecialchars($_POST['packaging_preferences']) : '' ?>">
                
                <!-- Delivery DateTime Field -->
                <label class="form-label" for="delivery_datetime">Delivery Date and Time</label>
                <input type="datetime-local" id="delivery_datetime" name="delivery_datetime" class="form-control" 
                       value="<?= isset($_POST['delivery_datetime']) ? htmlspecialchars($_POST['delivery_datetime']) : '' ?>">
            </div>
            <!-- Submit Button -->
            <button type="submit" class="submit-button btn-add-order">Add Order</button>
        </form>
    </div>

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

    <!-- Bootstrap JS Bundle -->
    <script src="/kaak_alia_management_system/assets/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>) i want to add other product in one order, because maybe customer wants more differnet products so update add order and change these codes (<?php
// public/orders/edit_order.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
require_once '../../includes/auth.php';
require_once '../../includes/db_connect.php';

// Initialize success and error variables
$success = '';
$error = '';

// Retrieve and sanitize the order ID from GET parameters
$order_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// Redirect if no valid order ID is provided
if ($order_id <= 0) {
    header("Location: view_orders.php");
    exit();
}

// Fetch order details using prepared statements
$order_sql = "SELECT * FROM orders WHERE id = ? LIMIT 1";
$order_stmt = $conn->prepare($order_sql);
$order_stmt->bind_param("i", $order_id);
$order_stmt->execute();
$order_result = $order_stmt->get_result();

if ($order_result->num_rows != 1) {
    header("Location: view_orders.php");
    exit();
}

$order = $order_result->fetch_assoc();
$order_stmt->close();

// Fetch products for selection using prepared statements
$products_sql = "SELECT * FROM products ORDER BY name ASC";
$products_result = $conn->query($products_sql);

// Handle form submission for updating the order
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Retrieve and sanitize form inputs
    $product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;
    $quantity = isset($_POST['quantity']) ? intval($_POST['quantity']) : 1;
    $packaging_preferences = isset($_POST['packaging_preferences']) ? trim($_POST['packaging_preferences']) : '';
    $status = isset($_POST['status']) ? trim($_POST['status']) : 'Pending';
    $delivery_datetime = isset($_POST['delivery_datetime']) ? trim($_POST['delivery_datetime']) : null;

    // Validate inputs
    if ($product_id <= 0 || $quantity <= 0 || empty($status)) {
        $error = "Please fill in all required fields with valid data.";
    } elseif ($delivery_datetime && !preg_match("/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/", $delivery_datetime)) {
        $error = "Invalid delivery date and time format.";
    } else {
        // Optional: Ensure delivery_datetime is not in the past
        if ($delivery_datetime && strtotime(str_replace('T', ' ', $delivery_datetime)) < strtotime(date('Y-m-d H:i'))) {
            $error = "Delivery date and time cannot be in the past.";
        }
    }

    if (empty($error)) {
        // Update order using prepared statements
        $update_order_sql = "UPDATE orders 
                             SET product_id = ?, quantity = ?, packaging_preferences = ?, status = ?, delivery_datetime = ? 
                             WHERE id = ?";
        $update_stmt = $conn->prepare($update_order_sql);
        if ($update_stmt) {
            $update_stmt->bind_param("iisssi", $product_id, $quantity, $packaging_preferences, $status, $delivery_datetime, $order_id);
            if ($update_stmt->execute()) {
                $success = "Order updated successfully!";
                // Refresh order data
                $order['product_id'] = $product_id;
                $order['quantity'] = $quantity;
                $order['packaging_preferences'] = $packaging_preferences;
                $order['status'] = $status;
                $order['delivery_datetime'] = $delivery_datetime;
            } else {
                $error = "Error updating order: " . $update_stmt->error;
            }
            $update_stmt->close();
        } else {
            $error = "Failed to prepare the update statement: " . $conn->error;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Order #<?= htmlspecialchars($order_id) ?> - 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="../../public/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 */
            --primary-color: #b78d67; /* Buttons and Highlights */
            --secondary-color: #515151; /* Text and Icons */
            --success-color: #06D6A0; /* Success Messages */
            --error-color: #EF476F; /* Error Messages */
            --white: #FFFFFF;
            --background-gradient: linear-gradient(to bottom, #f5f7fa, #c3cfe2);
            --divider-color: #FFD166;
            --input-background: #f9f9f9;
            --border-color: #ccc;
            --hover-shadow: rgba(0, 0, 0, 0.1);
            --transition-speed: 0.3s;
        }

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

        h1, h3, h4, 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. Container Styles
        ---------------------------------------------------- */
        .container-custom {
            max-width: 800px;
            margin: auto;
            background-color: var(--white);
            padding: 30px;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            box-shadow: 0 4px 6px var(--hover-shadow);
        }

        /* ---------------------------------------------------
           5. Success and Error Alerts
        ---------------------------------------------------- */
        .alert-custom {
            border-radius: 8px;
            padding: 15px 20px;
            margin-bottom: 20px;
            text-align: center;
        }

        .alert-success-custom {
            background-color: var(--success-color);
            color: var(--white);
        }

        .alert-danger-custom {
            background-color: var(--error-color);
            color: var(--white);
        }

        /* ---------------------------------------------------
           6. Form Styles
        ---------------------------------------------------- */
        .form-label {
            font-weight: 600;
            color: var(--secondary-color);
        }

        .form-control {
            background-color: var(--input-background);
            border: 1px solid var(--border-color);
            border-radius: 6px;
            padding: 10px 15px;
            transition: border-color var(--transition-speed), box-shadow var(--transition-speed);
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 5px rgba(182, 141, 103, 0.5);
            background-color: var(--white);
        }

        .btn-primary-custom {
            background-color: var(--primary-color);
            border: none;
            transition: background-color var(--transition-speed), transform var(--transition-speed);
            width: 48%;
        }

        .btn-primary-custom:hover {
            background-color: #a77c55;
            transform: scale(1.05);
        }

        .btn-secondary-custom {
            background-color: #6c757d;
            border: none;
            transition: background-color var(--transition-speed), transform var(--transition-speed);
            width: 48%;
        }

        .btn-secondary-custom:hover {
            background-color: #5a6268;
            transform: scale(1.05);
        }

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

        /* ---------------------------------------------------
           8. Responsive Design Enhancements
        ---------------------------------------------------- */
        @media (max-width: 768px) {
            .container-custom {
                padding: 20px;
            }

            .btn {
                width: 100%;
                margin-bottom: 10px;
            }
        }

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

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

            .form-label {
                font-size: 14px;
            }

            .form-control {
                padding: 8px 12px;
            }

            .btn {
                font-size: 14px;
                padding: 10px 0;
            }

            .footer {
                font-size: 12px;
            }
        }
    </style>
</head>
<body>

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

    <!-- Container -->
    <div class="container-custom">
        <h3 class="mb-4">Edit Order #<?= htmlspecialchars($order_id) ?></h3>
        
        <!-- Display Success or Error Messages -->
        <?php if (!empty($success)): ?>
            <div class="alert alert-custom alert-success-custom"><?= htmlspecialchars($success) ?></div>
        <?php endif; ?>

        <?php if (!empty($error)): ?>
            <div class="alert alert-custom alert-danger-custom"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>

        <!-- Edit Order Form -->
        <form method="POST" action="">
            <div class="row mb-3">
                <!-- Product Selection -->
                <div class="col-md-6 mb-3">
                    <label for="product_id" class="form-label">Product <span class="text-danger">*</span></label>
                    <select name="product_id" id="product_id" class="form-control" required>
                        <option value="">Select Product</option>
                        <?php while($product = $products_result->fetch_assoc()): ?>
                            <option value="<?= htmlspecialchars($product['id']) ?>" <?= ($product['id'] == $order['product_id']) ? 'selected' : '' ?>>
                                <?= htmlspecialchars($product['name'] . " - " . $product['size'] . " (" . number_format($product['price'], 2) . " KD)") ?>
                            </option>
                        <?php endwhile; ?>
                    </select>
                </div>
                <!-- Quantity Input -->
                <div class="col-md-6 mb-3">
                    <label for="quantity" class="form-label">Quantity <span class="text-danger">*</span></label>
                    <input type="number" name="quantity" id="quantity" class="form-control" min="1" value="<?= htmlspecialchars($order['quantity']) ?>" required>
                </div>
            </div>
            <!-- Packaging Preferences -->
            <div class="mb-3">
                <label for="packaging_preferences" class="form-label">Packaging Preferences</label>
                <input type="text" name="packaging_preferences" id="packaging_preferences" class="form-control" value="<?= htmlspecialchars($order['packaging_preferences']) ?>" placeholder="e.g., Gift Wrap">
            </div>
            <!-- Delivery DateTime -->
            <div class="mb-3">
                <label for="delivery_datetime" class="form-label">Delivery Date and Time</label>
                <input type="datetime-local" name="delivery_datetime" id="delivery_datetime" class="form-control" 
                       value="<?= isset($_POST['delivery_datetime']) ? htmlspecialchars($_POST['delivery_datetime']) : ($order['delivery_datetime'] ? date('Y-m-d\TH:i', strtotime($order['delivery_datetime'])) : '') ?>">
            </div>
            <!-- Status Selection -->
            <div class="mb-4">
                <label for="status" class="form-label">Status <span class="text-danger">*</span></label>
                <select name="status" id="status" class="form-control" required>
                    <option value="">Select Status</option>
                    <option value="Pending" <?= ($order['status'] == 'Pending') ? 'selected' : '' ?>>Pending</option>
                    <option value="In Progress" <?= ($order['status'] == 'In Progress') ? 'selected' : '' ?>>In Progress</option>
                    <option value="Completed" <?= ($order['status'] == 'Completed') ? 'selected' : '' ?>>Completed</option>
                    <option value="Delivered" <?= ($order['status'] == 'Delivered') ? 'selected' : '' ?>>Delivered</option>
                </select>
            </div>
            <!-- Form Buttons -->
            <div class="d-flex justify-content-between">
                <button type="submit" class="btn btn-primary-custom">Update Order</button>
                <a href="view_orders.php" class="btn btn-secondary-custom">Cancel</a>
            </div>
        </form>
    </div>

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

    <!-- Bootstrap JS Bundle -->
    <script src="../../public/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>) and (<?php
// public/orders/view_orders.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
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../includes/db_connect.php';

// Handle status update
if (isset($_GET['action']) && $_GET['action'] === 'update_status') {
    $order_id = isset($_GET['order_id']) ? intval($_GET['order_id']) : 0;
    $new_status = isset($_GET['new_status']) ? trim($_GET['new_status']) : '';

    // Validate new_status
    $valid_statuses = ['Pending', 'In Progress', 'Completed', 'Delivered'];
    if ($order_id > 0 && in_array($new_status, $valid_statuses)) {
        // Optional: If setting to 'Delivered', ensure delivery_datetime is set
        if ($new_status === 'Delivered') {
            $check_sql = "SELECT delivery_datetime FROM orders WHERE id = ?";
            $check_stmt = $conn->prepare($check_sql);
            $check_stmt->bind_param("i", $order_id);
            $check_stmt->execute();
            $check_result = $check_stmt->get_result();
            if ($check_result->num_rows == 1) {
                $order = $check_result->fetch_assoc();
                if (empty($order['delivery_datetime'])) {
                    header("Location: view_orders.php?error=3"); // Delivery datetime not set
                    exit();
                }
            }
            $check_stmt->close();
        }

        // Update status using prepared statement
        $update_sql = "UPDATE orders SET status = ? WHERE id = ?";
        $stmt = $conn->prepare($update_sql);
        if ($stmt) {
            $stmt->bind_param("si", $new_status, $order_id);
            if ($stmt->execute()) {
                $stmt->close();
                header("Location: view_orders.php?success=1");
                exit();
            } else {
                $stmt->close();
                header("Location: view_orders.php?error=1");
                exit();
            }
        } else {
            header("Location: view_orders.php?error=2");
            exit();
        }
    } else {
        header("Location: view_orders.php?error=2");
        exit();
    }
}

// Fetch orders with customer and product details
$orders_sql = "SELECT orders.*, customers.name as customer_name, products.name as product_name, products.size as product_size 
               FROM orders 
               JOIN customers ON orders.customer_id = customers.id 
               JOIN products ON orders.product_id = products.id 
               ORDER BY orders.order_date DESC";
$orders_result = $conn->query($orders_sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>View Orders - 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="/kaak_alia_management_system/assets/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 */
            --soft-brown: #FFD166; /* Add New Order Button */
            --blue: #118AB2; /* View Orders Button */
            --green: #06D6A0; /* Manage Products Button */
            --orange: #F4A261; /* Manage Users Button */
            --red-pink: #EF476F; /* Generate Reports Button */

            --main-text: #333333;
            --secondary-text: #666666;
            --white: #FFFFFF;
            --light-gray: #F9F9F9;
            --divider-color: #FFD166;

            --transition-speed: 0.3s;
            --hover-shadow: rgba(0, 0, 0, 0.2);
        }

        /* ---------------------------------------------------
           2. Global Styles
        ---------------------------------------------------- */
        body {
            font-family: 'Cairo', sans-serif;
            background: linear-gradient(to bottom, #f5f7fa, #c3cfe2);
            margin: 0;
            padding: 30px;
            box-sizing: border-box;
        }

        /* ---------------------------------------------------
           3. Header Styles
        ---------------------------------------------------- */
        .header {
            padding: 20px 30px;
            background-color: var(--dark-gray);
            border-radius: 8px;
            text-align: center;
            margin-bottom: 30px;
            color: var(--white);
            font-size: 24px;
            font-weight: bold;
        }

        /* ---------------------------------------------------
           4. Success and Error Alerts
        ---------------------------------------------------- */
        .alert-success-custom {
            background-color: var(--green);
            color: var(--white);
            border-radius: 8px;
            padding: 15px 20px;
            margin-bottom: 20px;
            text-align: center;
            width: 70%;
            margin: 20px auto;
        }

        .alert-error-custom {
            background-color: var(--red-pink);
            color: var(--white);
            border-radius: 8px;
            padding: 15px 20px;
            margin-bottom: 20px;
            text-align: center;
            width: 70%;
            margin: 20px auto;
        }

        /* ---------------------------------------------------
           5. Recent Orders Table
        ---------------------------------------------------- */
        .recent-orders-section {
            background-color: #ffffff;
            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;
        }

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

        .recent-orders-section tr:nth-child(even) {
            background-color: var(--light-gray);
        }

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

        .recent-orders-section td {
            font-size: 14px;
            color: var(--main-text);
        }

        .recent-orders-section .view-all {
            display: block;
            text-align: right;
            margin-top: 10px;
            font-size: 14px;
            color: var(--blue);
            font-weight: bold;
            text-decoration: none;
            transition: color 0.2s;
        }

        .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;
        }

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

        /* ---------------------------------------------------
           7. Responsive Design Enhancements
        ---------------------------------------------------- */
        @media (max-width: 768px) {
            /* Align form elements for tablets */
            .form-label, .form-control, .submit-button, .alert-success-custom, .alert-error-custom {
                width: 90%;
                margin: 0 auto 15px auto;
            }
        }

        @media (max-width: 480px) {
            /* Align form elements for mobile devices */
            .header {
                padding: 15px 20px;
                font-size: 20px;
            }

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

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

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

    <!-- Header -->
    <div class="header">
        View Orders
    </div>

    <!-- Display Success or Error Messages -->
    <?php if (isset($_GET['success']) && $_GET['success'] == 1): ?>
        <div class="alert-success-custom">Order status updated successfully!</div>
    <?php endif; ?>
    
    <?php if (isset($_GET['error'])): ?>
        <?php if ($_GET['error'] == 1): ?>
            <div class="alert-error-custom">Failed to update order status.</div>
        <?php elseif ($_GET['error'] == 2): ?>
            <div class="alert-error-custom">Invalid status selected.</div>
        <?php elseif ($_GET['error'] == 3): ?>
            <div class="alert-error-custom">Cannot set status to Delivered without a delivery date and time.</div>
        <?php endif; ?>
    <?php endif; ?>

    <!-- Container -->
    <div class="recent-orders-section">
        <h4>All Orders</h4>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Customer</th>
                    <th>Product</th>
                    <th>Size</th>
                    <th>Quantity</th>
                    <th>Packaging</th>
                    <th>Status</th>
                    <th>Order Date</th>
                    <th>Delivery DateTime</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($orders_result->num_rows > 0): ?>
                    <?php while($order = $orders_result->fetch_assoc()): ?>
                        <tr>
                            <td><?= htmlspecialchars($order['id']) ?></td>
                            <td><?= htmlspecialchars($order['customer_name']) ?></td>
                            <td><?= htmlspecialchars($order['product_name']) ?></td>
                            <td><?= htmlspecialchars($order['product_size']) ?></td>
                            <td><?= htmlspecialchars($order['quantity']) ?></td>
                            <td><?= htmlspecialchars($order['packaging_preferences']) ?></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><?= date("d M Y, h:i A", strtotime($order['order_date'])) ?></td>
                            <td><?= $order['delivery_datetime'] ? date("d M Y, h:i A", strtotime($order['delivery_datetime'])) : 'N/A' ?></td>
                            <td>
                                <!-- Edit Button -->
                                <a href="edit_order.php?id=<?= htmlspecialchars($order['id']) ?>" class="btn btn-sm btn-outline-secondary" title="Edit Order">
                                    <i class="bi bi-pencil-fill"></i>
                                </a>
                                <!-- Change Status Dropdown -->
                                <div class="btn-group">
                                    <button type="button" class="btn btn-sm btn-outline-info dropdown-toggle" data-bs-toggle="dropdown" title="Change Status">
                                        <i class="bi bi-arrow-repeat"></i>
                                    </button>
                                    <ul class="dropdown-menu">
                                        <li><a class="dropdown-item" href="?action=update_status&order_id=<?= htmlspecialchars($order['id']) ?>&new_status=Pending">Pending</a></li>
                                        <li><a class="dropdown-item" href="?action=update_status&order_id=<?= htmlspecialchars($order['id']) ?>&new_status=In Progress">In Progress</a></li>
                                        <li><a class="dropdown-item" href="?action=update_status&order_id=<?= htmlspecialchars($order['id']) ?>&new_status=Completed">Completed</a></li>
                                        <li><a class="dropdown-item" href="?action=update_status&order_id=<?= htmlspecialchars($order['id']) ?>&new_status=Delivered">Delivered</a></li>
                                    </ul>
                                </div>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="10" class="text-center">No orders found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
        <!-- <a href="#" class="view-all">View All Orders</a> --> <!-- Removed since all orders are already displayed -->
    </div>
    
    <!-- Footer -->
    <div class="footer">
        &copy; 2024 Kaak Alia Bakery. All rights reserved.
    </div>
    
    <!-- Bootstrap JS Bundle -->
    <script src="/kaak_alia_management_system/assets/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Initialize Bootstrap Tooltip (Optional) -->
    <script>
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[title]'))
        var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl)
        })
    </script>
</body>
</html>)