<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/menu_config.php';

// Set timezone to East Africa Time
date_default_timezone_set('Africa/Nairobi');

// Check if user is logged in and has Super Admin role
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Get user details and role verification
$user_id = $_SESSION['user_id'];
$sql_user_details = "SELECT u.*, ur.role_name 
                     FROM users u 
                     INNER JOIN user_roles ur ON u.role_id = ur.role_id 
                     WHERE u.user_id = ?";
$stmt_user = $conn->prepare($sql_user_details);
$stmt_user->bind_param("i", $user_id);
$stmt_user->execute();
$user_details = $stmt_user->get_result()->fetch_assoc();

if (!$user_details || $user_details['role_name'] !== 'Super Admin') {
    header('Location: dashboard.php');
    exit();
}

$full_name = $user_details['full_name'];
$role_name = $user_details['role_name'];

// Handle search and filter parameters
$search_query = isset($_GET['search']) ? trim($_GET['search']) : '';
$status_filter = isset($_GET['status']) ? $_GET['status'] : '';
$date_from = isset($_GET['date_from']) ? $_GET['date_from'] : '';
$date_to = isset($_GET['date_to']) ? $_GET['date_to'] : '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 20;
$offset = ($page - 1) * $limit;

// Build WHERE clause for filtering
$where_conditions = [];
$params = [];
$param_types = '';

if (!empty($search_query)) {
    $where_conditions[] = "(o.order_number LIKE ? OR c.first_name LIKE ? OR c.last_name LIKE ? OR c.phone LIKE ?)";
    $search_param = '%' . $search_query . '%';
    $params = array_merge($params, [$search_param, $search_param, $search_param, $search_param]);
    $param_types .= 'ssss';
}

if (!empty($status_filter)) {
    $where_conditions[] = "o.status = ?";
    $params[] = $status_filter;
    $param_types .= 's';
}

if (!empty($date_from)) {
    $where_conditions[] = "DATE(o.order_date) >= ?";
    $params[] = $date_from;
    $param_types .= 's';
}

if (!empty($date_to)) {
    $where_conditions[] = "DATE(o.order_date) <= ?";
    $params[] = $date_to;
    $param_types .= 's';
}

$where_clause = !empty($where_conditions) ? 'WHERE ' . implode(' AND ', $where_conditions) : '';

// Get total count for pagination
$count_sql = "SELECT COUNT(*) as total 
              FROM orders o 
              INNER JOIN customers c ON o.customer_id = c.customer_id 
              $where_clause";
$count_stmt = $conn->prepare($count_sql);
if (!empty($params)) {
    $count_stmt->bind_param($param_types, ...$params);
}
$count_stmt->execute();
$total_records = $count_stmt->get_result()->fetch_assoc()['total'];
$total_pages = ceil($total_records / $limit);

// Get orders with customer and staff details
$sql_orders = "SELECT 
    o.order_id,
    o.order_number,
    o.order_date,
    o.pickup_date,
    o.delivery_date,
    o.estimated_delivery,
    o.status as order_status,
    o.priority,
    o.total_pieces,
    o.total_amount,
    o.paid_amount,
    o.balance_amount,
    o.payment_status,
    c.first_name,
    c.last_name,
    c.customer_code,
    c.phone,
    c.customer_type,
    creator.full_name as created_by_name,
    assignee.full_name as assigned_to_name
FROM orders o
INNER JOIN customers c ON o.customer_id = c.customer_id
LEFT JOIN users creator ON o.created_by = creator.user_id
LEFT JOIN users assignee ON o.assigned_to = assignee.user_id
$where_clause
ORDER BY o.order_date DESC, o.order_id DESC
LIMIT ? OFFSET ?";

$final_params = array_merge($params, [$limit, $offset]);
$final_param_types = $param_types . 'ii';

$stmt_orders = $conn->prepare($sql_orders);
$stmt_orders->bind_param($final_param_types, ...$final_params);
$stmt_orders->execute();
$result_orders = $stmt_orders->get_result();

// Get summary statistics
$sql_summary = "SELECT 
    COUNT(*) as total_orders,
    SUM(CASE WHEN o.status = 'PENDING' THEN 1 ELSE 0 END) as pending_orders,
    SUM(CASE WHEN o.status = 'IN_PROGRESS' THEN 1 ELSE 0 END) as in_progress_orders,
    SUM(CASE WHEN o.status = 'READY' THEN 1 ELSE 0 END) as ready_orders,
    SUM(CASE WHEN o.status = 'DELIVERED' THEN 1 ELSE 0 END) as delivered_orders,
    SUM(CASE WHEN o.status = 'CANCELLED' THEN 1 ELSE 0 END) as cancelled_orders,
    COALESCE(SUM(o.total_amount), 0) as total_revenue,
    COALESCE(SUM(o.paid_amount), 0) as total_paid,
    COALESCE(SUM(o.balance_amount), 0) as total_outstanding,
    COALESCE(SUM(o.total_pieces), 0) as total_pieces
FROM orders o 
INNER JOIN customers c ON o.customer_id = c.customer_id 
$where_clause";

$stmt_summary = $conn->prepare($sql_summary);
if (!empty($params)) {
    $stmt_summary->bind_param($param_types, ...$params);
}
$stmt_summary->execute();
$summary = $stmt_summary->get_result()->fetch_assoc();

$conn->close();
?>

<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'All Orders - Laundry Management')); ?>
    <?php include 'layouts/head-css.php'; ?>
    
    <style>
        body {
            background-color: #f8fafc;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }
        
        .page-content {
            padding: 1.5rem;
        }
        
        /* Header Section */
        .page-header {
            background: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
            color: white;
            border-radius: 12px;
            padding: 2rem;
            margin-bottom: 2rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
        
        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 1rem;
        }
        
        .header-title h1 {
            font-size: 1.875rem;
            font-weight: 700;
            margin-bottom: 0.5rem;
        }
        
        .header-subtitle {
            opacity: 0.9;
            font-size: 1rem;
        }
        
        .header-actions {
            display: flex;
            gap: 1rem;
            align-items: center;
        }
        
        .btn-primary {
            background: rgba(255, 255, 255, 0.2);
            border: 1px solid rgba(255, 255, 255, 0.3);
            color: white;
            padding: 0.75rem 1.5rem;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.2s ease;
        }
        
        .btn-primary:hover {
            background: rgba(255, 255, 255, 0.3);
            color: white;
            transform: translateY(-1px);
        }
        
        /* Statistics Cards */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1.5rem;
            margin-bottom: 2rem;
        }
        
        .stat-card {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            transition: all 0.2s ease;
        }
        
        .stat-card:hover {
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
            transform: translateY(-1px);
        }
        
        .stat-title {
            font-size: 0.875rem;
            font-weight: 500;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            margin-bottom: 0.5rem;
        }
        
        .stat-value {
            font-size: 2rem;
            font-weight: 700;
            color: #1e293b;
            margin-bottom: 0.25rem;
        }
        
        .stat-change {
            font-size: 0.875rem;
            color: #64748b;
        }
        
        /* Filters Section */
        .filters-section {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 2rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
        }
        
        .filters-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1rem;
            align-items: end;
        }
        
        .form-group {
            margin-bottom: 0;
        }
        
        .form-label {
            font-size: 0.875rem;
            font-weight: 600;
            color: #374151;
            margin-bottom: 0.5rem;
        }
        
        .form-control {
            border: 1px solid #d1d5db;
            border-radius: 8px;
            padding: 0.75rem;
            font-size: 0.875rem;
            transition: border-color 0.2s ease;
        }
        
        .form-control:focus {
            border-color: #3b82f6;
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
            outline: none;
        }
        
        .btn-secondary, .btn-outline-secondary {
            padding: 0.75rem 1.5rem;
            font-weight: 600;
            border-radius: 8px;
            font-size: 0.875rem;
            transition: all 0.2s ease;
        }
        
        .btn-secondary {
            background: #3b82f6;
            border: 1px solid #3b82f6;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #2563eb;
            border-color: #2563eb;
            transform: translateY(-1px);
        }
        
        .btn-outline-secondary {
            background: transparent;
            border: 1px solid #d1d5db;
            color: #374151;
        }
        
        .btn-outline-secondary:hover {
            background: #f3f4f6;
            border-color: #9ca3af;
        }
        
        /* Orders Table */
        .orders-table-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            overflow: hidden;
        }
        
        .table-header {
            padding: 1.5rem;
            border-bottom: 1px solid #e2e8f0;
            background: #f8fafc;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .table-title {
            font-size: 1.125rem;
            font-weight: 700;
            color: #1e293b;
            margin: 0;
        }
        
        .results-info {
            font-size: 0.875rem;
            color: #64748b;
        }
        
        .orders-table {
            width: 100%;
            border-collapse: collapse;
            margin: 0;
        }
        
        .orders-table th {
            background: #f8fafc;
            padding: 1rem;
            text-align: left;
            font-weight: 600;
            font-size: 0.875rem;
            color: #374151;
            border-bottom: 1px solid #e2e8f0;
            white-space: nowrap;
        }
        
        .orders-table td {
            padding: 1rem;
            border-bottom: 1px solid #f1f5f9;
            font-size: 0.875rem;
            vertical-align: middle;
        }
        
        .orders-table tbody tr:hover {
            background: #f8fafc;
        }
        
        /* Status Badges */
        .status-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.025em;
        }
        
        .status-pending { background: #fef3c7; color: #92400e; }
        .status-in_progress { background: #dbeafe; color: #1d4ed8; }
        .status-ready { background: #d1fae5; color: #065f46; }
        .status-delivered { background: #dcfce7; color: #166534; }
        .status-cancelled { background: #fee2e2; color: #991b1b; }
        
        .priority-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
        }
        
        .priority-normal { background: #f3f4f6; color: #374151; }
        .priority-high { background: #fef3c7; color: #92400e; }
        .priority-urgent { background: #fee2e2; color: #991b1b; }
        
        .payment-badge {
            padding: 0.25rem 0.75rem;
            border-radius: 9999px;
            font-size: 0.75rem;
            font-weight: 600;
            text-transform: uppercase;
        }
        
        .payment-pending { background: #fef3c7; color: #92400e; }
        .payment-partial { background: #dbeafe; color: #1d4ed8; }
        .payment-paid { background: #dcfce7; color: #166534; }
        .payment-refunded { background: #fee2e2; color: #991b1b; }
        
        /* Customer Info */
        .customer-info {
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }
        
        .customer-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #3b82f6;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 0.875rem;
            flex-shrink: 0;
        }
        
        .customer-details {
            min-width: 0;
        }
        
        .customer-name {
            font-weight: 600;
            color: #1e293b;
            line-height: 1.25;
        }
        
        .customer-meta {
            color: #64748b;
            font-size: 0.75rem;
            line-height: 1.25;
        }
        
        /* Order Links */
        .order-link {
            color: #3b82f6;
            text-decoration: none;
            font-weight: 600;
        }
        
        .order-link:hover {
            color: #1d4ed8;
            text-decoration: underline;
        }
        
        /* Amount Formatting */
        .amount {
            font-weight: 600;
            font-family: 'Courier New', monospace;
        }
        
        .amount-positive { color: #059669; }
        .amount-negative { color: #dc2626; }
        .amount-neutral { color: #64748b; }
        
        /* Pagination */
        .pagination-section {
            background: white;
            border-radius: 12px;
            padding: 1.5rem;
            box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
            border: 1px solid #e2e8f0;
            margin-top: 2rem;
        }
        
        .pagination-info {
            color: #64748b;
            font-size: 0.875rem;
            margin-bottom: 1rem;
        }
        
        .pagination {
            display: flex;
            gap: 0.5rem;
            justify-content: center;
            flex-wrap: wrap;
        }
        
        .pagination a, .pagination span {
            padding: 0.5rem 0.75rem;
            border: 1px solid #d1d5db;
            border-radius: 8px;
            text-decoration: none;
            color: #374151;
            font-weight: 500;
            transition: all 0.2s ease;
        }
        
        .pagination a:hover {
            background: #f3f4f6;
            border-color: #9ca3af;
        }
        
        .pagination .current {
            background: #3b82f6;
            border-color: #3b82f6;
            color: white;
        }
        
        /* Responsive Design */
        @media (max-width: 768px) {
            .page-content {
                padding: 1rem;
            }
            
            .header-content {
                flex-direction: column;
                text-align: center;
            }
            
            .stats-grid {
                grid-template-columns: 1fr;
            }
            
            .filters-grid {
                grid-template-columns: 1fr;
            }
            
            .orders-table-container {
                overflow-x: auto;
            }
            
            .orders-table {
                min-width: 900px;
            }
        }
    </style>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <!-- Page Header -->
                    <div class="page-header">
                        <div class="header-content">
                            <div class="header-title">
                                <h1>All Orders Management</h1>
                                <div class="header-subtitle">Complete order tracking and management system</div>
                            </div>
                            <div class="header-actions">
                                <a href="new_order.php" class="btn-primary">
                                    <i class="ph-plus"></i> New Order
                                </a>
                                <a href="order_reports.php" class="btn-primary">
                                    <i class="ph-chart-bar"></i> Reports
                                </a>
                            </div>
                        </div>
                    </div>

                    <!-- Statistics Cards -->
                    <div class="stats-grid">
                        <div class="stat-card">
                            <div class="stat-title">Total Orders</div>
                            <div class="stat-value"><?php echo number_format($summary['total_orders']); ?></div>
                            <div class="stat-change">All time orders</div>
                        </div>
                        
                        <div class="stat-card">
                            <div class="stat-title">Pending Orders</div>
                            <div class="stat-value"><?php echo number_format($summary['pending_orders']); ?></div>
                            <div class="stat-change">Awaiting processing</div>
                        </div>
                        
                        <div class="stat-card">
                            <div class="stat-title">In Progress</div>
                            <div class="stat-value"><?php echo number_format($summary['in_progress_orders']); ?></div>
                            <div class="stat-change">Currently processing</div>
                        </div>
                        
                        <div class="stat-card">
                            <div class="stat-title">Ready Orders</div>
                            <div class="stat-value"><?php echo number_format($summary['ready_orders']); ?></div>
                            <div class="stat-change">Ready for pickup</div>
                        </div>
                        
                        <div class="stat-card">
                            <div class="stat-title">Total Revenue</div>
                            <div class="stat-value">KES <?php echo number_format($summary['total_revenue']); ?></div>
                            <div class="stat-change">Total order value</div>
                        </div>
                        
                        <div class="stat-card">
                            <div class="stat-title">Outstanding Amount</div>
                            <div class="stat-value">KES <?php echo number_format($summary['total_outstanding']); ?></div>
                            <div class="stat-change">Pending payments</div>
                        </div>
                    </div>

                    <!-- Filters Section -->
                    <div class="filters-section">
                        <form method="GET" class="filters-form">
                            <div class="filters-grid">
                                <div class="form-group">
                                    <label class="form-label">Search Orders</label>
                                    <input type="text" name="search" class="form-control" 
                                           placeholder="Order number, customer name, phone..." 
                                           value="<?php echo htmlspecialchars($search_query); ?>">
                                </div>
                                
                                <div class="form-group">
                                    <label class="form-label">Status</label>
                                    <select name="status" class="form-control">
                                        <option value="">All Statuses</option>
                                        <option value="PENDING" <?php echo $status_filter === 'PENDING' ? 'selected' : ''; ?>>Pending</option>
                                        <option value="IN_PROGRESS" <?php echo $status_filter === 'IN_PROGRESS' ? 'selected' : ''; ?>>In Progress</option>
                                        <option value="READY" <?php echo $status_filter === 'READY' ? 'selected' : ''; ?>>Ready</option>
                                        <option value="DELIVERED" <?php echo $status_filter === 'DELIVERED' ? 'selected' : ''; ?>>Delivered</option>
                                        <option value="CANCELLED" <?php echo $status_filter === 'CANCELLED' ? 'selected' : ''; ?>>Cancelled</option>
                                    </select>
                                </div>
                                
                                <div class="form-group">
                                    <label class="form-label">From Date</label>
                                    <input type="date" name="date_from" class="form-control" 
                                           value="<?php echo htmlspecialchars($date_from); ?>">
                                </div>
                                
                                <div class="form-group">
                                    <label class="form-label">To Date</label>
                                    <input type="date" name="date_to" class="form-control" 
                                           value="<?php echo htmlspecialchars($date_to); ?>">
                                </div>
                                
                                <div class="form-group">
                                    <button type="submit" class="btn-secondary">
                                        <i class="ph-funnel"></i> Filter
                                    </button>
                                    <a href="all_orders.php" class="btn-outline-secondary">
                                        <i class="ph-x"></i> Clear
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>

                    <!-- Orders Table -->
                    <div class="orders-table-container">
                        <div class="table-header">
                            <h3 class="table-title">Orders List</h3>
                            <div class="results-info">
                                Showing <?php echo number_format(min($limit, $total_records)); ?> of <?php echo number_format($total_records); ?> orders
                            </div>
                        </div>
                        
                        <table class="orders-table">
                            <thead>
                                <tr>
                                    <th>Order Details</th>
                                    <th>Customer</th>
                                    <th>Status</th>
                                    <th>Priority</th>
                                    <th>Items</th>
                                    <th>Amount</th>
                                    <th>Payment</th>
                                    <th>Dates</th>
                                    <th>Staff</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($result_orders->num_rows > 0): ?>
                                    <?php while($order = $result_orders->fetch_assoc()): ?>
                                    <tr>
                                        <td>
                                            <div>
                                                <a href="view_order.php?id=<?php echo $order['order_id']; ?>" class="order-link">
                                                    <?php echo $order['order_number']; ?>
                                                </a>
                                                <div class="customer-meta">
                                                    ID: <?php echo $order['order_id']; ?>
                                                </div>
                                            </div>
                                        </td>
                                        
                                        <td>
                                            <div class="customer-info">
                                                <div class="customer-avatar">
                                                    <?php echo strtoupper(substr($order['first_name'], 0, 1) . substr($order['last_name'], 0, 1)); ?>
                                                </div>
                                                <div class="customer-details">
                                                    <div class="customer-name"><?php echo $order['first_name'] . ' ' . $order['last_name']; ?></div>
                                                    <div class="customer-meta">
                                                        <?php echo $order['customer_code']; ?> | <?php echo $order['phone']; ?>
                                                        <br><span class="status-badge status-<?php echo strtolower($order['customer_type']); ?>">
                                                            <?php echo $order['customer_type']; ?>
                                                        </span>
                                                    </div>
                                                </div>
                                            </div>
                                        </td>
                                        
                                        <td>
                                            <span class="status-badge status-<?php echo strtolower($order['order_status']); ?>">
                                                <?php echo str_replace('_', ' ', $order['order_status']); ?>
                                            </span>
                                        </td>
                                        
                                        <td>
                                            <span class="priority-badge priority-<?php echo strtolower($order['priority']); ?>">
                                                <?php echo $order['priority']; ?>
                                            </span>
                                        </td>
                                        
                                        <td>
                                            <span style="font-weight: 600;"><?php echo $order['total_pieces']; ?></span>
                                            <div class="customer-meta">pieces</div>
                                        </td>
                                        
                                        <td>
                                            <div class="amount amount-positive">KES <?php echo number_format($order['total_amount'], 2); ?></div>
                                            <?php if ($order['balance_amount'] > 0): ?>
                                                <div class="customer-meta amount-negative">
                                                    Bal: KES <?php echo number_format($order['balance_amount'], 2); ?>
                                                </div>
                                            <?php endif; ?>
                                        </td>
                                        
                                        <td>
                                            <span class="payment-badge payment-<?php echo strtolower($order['payment_status']); ?>">
                                                <?php echo $order['payment_status']; ?>
                                            </span>
                                        </td>
                                        
                                        <td>
                                            <div style="font-weight: 500;">
                                                <?php echo date('M j, Y', strtotime($order['order_date'])); ?>
                                            </div>
                                            <div class="customer-meta">
                                                <?php echo date('g:i A', strtotime($order['order_date'])); ?>
                                            </div>
                                            <?php if ($order['estimated_delivery']): ?>
                                                <div class="customer-meta" style="color: #d97706;">
                                                    Due: <?php echo date('M j', strtotime($order['estimated_delivery'])); ?>
                                                </div>
                                            <?php endif; ?>
                                        </td>
                                        
                                        <td>
                                            <div style="font-weight: 500;"><?php echo $order['created_by_name'] ?: 'N/A'; ?></div>
                                            <?php if ($order['assigned_to_name']): ?>
                                                <div class="customer-meta">
                                                    Assigned: <?php echo $order['assigned_to_name']; ?>
                                                </div>
                                            <?php endif; ?>
                                        </td>
                                        
                                        <td>
                                            <div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
                                                <a href="view_order.php?id=<?php echo $order['order_id']; ?>" 
                                                   style="color: #3b82f6; text-decoration: none; font-size: 0.75rem; padding: 0.25rem 0.5rem; border: 1px solid #3b82f6; border-radius: 4px;"
                                                   title="View Order">
                                                    <i class="ph-eye"></i>
                                                </a>
                                                <a href="edit_order.php?id=<?php echo $order['order_id']; ?>" 
                                                   style="color: #d97706; text-decoration: none; font-size: 0.75rem; padding: 0.25rem 0.5rem; border: 1px solid #d97706; border-radius: 4px;"
                                                   title="Edit Order">
                                                    <i class="ph-pencil"></i>
                                                </a>
                                                <a href="print_order.php?id=<?php echo $order['order_id']; ?>" 
                                                   style="color: #059669; text-decoration: none; font-size: 0.75rem; padding: 0.25rem 0.5rem; border: 1px solid #059669; border-radius: 4px;"
                                                   title="Print Order">
                                                    <i class="ph-printer"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="10" style="text-align: center; padding: 3rem; color: #64748b;">
                                            <div style="font-size: 3rem; margin-bottom: 1rem;">📋</div>
                                            <div style="font-size: 1.125rem; font-weight: 600; margin-bottom: 0.5rem;">No Orders Found</div>
                                            <div>Try adjusting your search criteria or create a new order.</div>
                                            <a href="new_order.php" style="color: #3b82f6; text-decoration: none; font-weight: 600; margin-top: 1rem; display: inline-block;">
                                                Create New Order →
                                            </a>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                    <div class="pagination-section">
                        <div class="pagination-info">
                            Showing <?php echo (($page - 1) * $limit) + 1; ?> to <?php echo min($page * $limit, $total_records); ?> 
                            of <?php echo number_format($total_records); ?> orders
                        </div>
                        
                        <div class="pagination">
                            <?php if ($page > 1): ?>
                                <a href="?page=1<?php echo !empty($_GET) ? '&' . http_build_query(array_diff_key($_GET, ['page' => ''])) : ''; ?>">First</a>
                                <a href="?page=<?php echo $page - 1; ?><?php echo !empty($_GET) ? '&' . http_build_query(array_diff_key($_GET, ['page' => ''])) : ''; ?>">Previous</a>
                            <?php endif; ?>
                            
                            <?php
                            $start_page = max(1, $page - 2);
                            $end_page = min($total_pages, $page + 2);
                            
                            for ($i = $start_page; $i <= $end_page; $i++):
                            ?>
                                <?php if ($i == $page): ?>
                                    <span class="current"><?php echo $i; ?></span>
                                <?php else: ?>
                                    <a href="?page=<?php echo $i; ?><?php echo !empty($_GET) ? '&' . http_build_query(array_diff_key($_GET, ['page' => ''])) : ''; ?>"><?php echo $i; ?></a>
                                <?php endif; ?>
                            <?php endfor; ?>
                            
                            <?php if ($page < $total_pages): ?>
                                <a href="?page=<?php echo $page + 1; ?><?php echo !empty($_GET) ? '&' . http_build_query(array_diff_key($_GET, ['page' => ''])) : ''; ?>">Next</a>
                                <a href="?page=<?php echo $total_pages; ?><?php echo !empty($_GET) ? '&' . http_build_query(array_diff_key($_GET, ['page' => ''])) : ''; ?>">Last</a>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
    <script src="assets/js/app.js"></script>
</body>
</html>