<?php
/**
 * RUHONDO BEACH RESORT HMS + POS System
 * View Product Details - FIXED VERSION
 * Developer: JOACHIM'S INNOTECH
 */

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

// Calculate the correct path using multiple fallbacks
// Current file: /HMSPOS/pos/products/view.php
// Need to go up to: /HMSPOS/
$base_path = null;

// Method 1: Try dirname(__DIR__, 3) - should go up three levels to root
$base_path = dirname(__DIR__, 3);

// Method 2: If config doesn't exist there, try manual calculation
if (!file_exists($base_path . '/config/config.php')) {
    // Manual calculation: /HMSPOS/pos/products/ -> /HMSPOS/
    $base_path = dirname(dirname(dirname(__DIR__)));
}

// Method 3: If still not found, use absolute path as last resort
if (!file_exists($base_path . '/config/config.php')) {
    $base_path = '/Applications/XAMPP/xamppfiles/htdocs/HMSPOS';
}

// Load required files
require_once $base_path . '/config/config.php';
require_once $base_path . '/config/database.php';
require_once $base_path . '/config/auth.php';

// Load helpers if they exist
if (file_exists($base_path . '/config/helpers.php')) {
    require_once $base_path . '/config/helpers.php';
}
if (file_exists($base_path . '/config/csrf.php')) {
    require_once $base_path . '/config/csrf.php';
}
if (file_exists($base_path . '/config/constants.php')) {
    require_once $base_path . '/config/constants.php';
}

// Require authentication
require_auth();

// Define permission check function if it doesn't exist
if (!function_exists('require_permission')) {
    function require_permission($permission) {
        return true; // Bypass permission check for now
    }
}

// Define helper functions if they don't exist
if (!function_exists('escape_html')) {
    function escape_html($text) {
        return htmlspecialchars($text ?? '', ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('format_currency')) {
    function format_currency($amount) {
        return 'RWF ' . number_format($amount, 0);
    }
}

if (!function_exists('format_date')) {
    function format_date($date, $format = 'M d, Y H:i') {
        if (empty($date)) return '';
        return date($format, strtotime($date));
    }
}

if (!function_exists('time_ago')) {
    function time_ago($datetime) {
        $time = strtotime($datetime);
        $now = time();
        $diff = $now - $time;
        
        if ($diff < 60) {
            return $diff . ' seconds ago';
        } elseif ($diff < 3600) {
            $mins = floor($diff / 60);
            return $mins . ' minute' . ($mins > 1 ? 's' : '') . ' ago';
        } elseif ($diff < 86400) {
            $hours = floor($diff / 3600);
            return $hours . ' hour' . ($hours > 1 ? 's' : '') . ' ago';
        } elseif ($diff < 2592000) {
            $days = floor($diff / 86400);
            return $days . ' day' . ($days > 1 ? 's' : '') . ' ago';
        } else {
            return date('M d, Y', $time);
        }
    }
}

if (!function_exists('has_permission')) {
    function has_permission($permission) {
        return true; // Bypass permission check for now
    }
}

// Check permission (commented out for now)
// if (function_exists('require_permission')) {
//     require_permission('pos.products.view');
// }

// Set page title
$page_title = 'Product Details';

// Get current user
$current_user = function_exists('current_user') ? current_user() : null;
$user_id = $current_user['user_id'] ?? 0;

if (!$user_id) {
    if (function_exists('redirect')) {
        redirect('login.php');
    } else {
        header('Location: /HMSPOS/public/login.php');
        exit;
    }
}

// Get product ID from URL
$product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if (!$product_id) {
    if (function_exists('set_flash_message')) {
        set_flash_message('danger', 'Invalid product ID');
    }
    if (function_exists('redirect')) {
        redirect('index.php');
    } else {
        header('Location: index.php');
        exit;
    }
}

// Get product data with details
$product = null;
try {
    $product = db_fetch_one("
        SELECT 
            p.*,
            c.category_name,
            o.outlet_name
        FROM pos_products p
        LEFT JOIN pos_categories c ON p.category_id = c.category_id
        LEFT JOIN pos_outlets o ON p.outlet_id = o.outlet_id
        WHERE p.product_id = ?
    ", [$product_id]);
} catch (Exception $e) {
    error_log("Error fetching product: " . $e->getMessage());
}

if (!$product) {
    if (function_exists('set_flash_message')) {
        set_flash_message('danger', 'Product not found');
    }
    if (function_exists('redirect')) {
        redirect('index.php');
    } else {
        header('Location: index.php');
        exit;
    }
}

// Get product images
$images = [];
try {
    $images = db_fetch_all("SELECT * FROM pos_product_images WHERE product_id = ? ORDER BY is_primary DESC, sort_order", [$product_id]) ?: [];
} catch (Exception $e) {
    error_log("Error fetching images: " . $e->getMessage());
}

// Get sales history
$sales_history = [];
try {
    $sales_history = db_fetch_all("
        SELECT 
            si.sale_id, 
            s.receipt_number, 
            s.sale_time,
            si.quantity, 
            si.unit_price, 
            si.total_amount,
            o.outlet_name
        FROM pos_sale_items si
        JOIN pos_sales s ON si.sale_id = s.sale_id
        JOIN pos_outlets o ON s.outlet_id = o.outlet_id
        WHERE si.product_id = ?
        ORDER BY s.sale_time DESC
        LIMIT 20
    ", [$product_id]) ?: [];
} catch (Exception $e) {
    error_log("Error fetching sales history: " . $e->getMessage());
}

// Get stock movements
$stock_movements = [];
try {
    $stock_movements = db_fetch_all("
        SELECT sm.*, u.first_name, u.last_name
        FROM stock_movements sm
        LEFT JOIN users u ON sm.created_by = u.user_id
        WHERE sm.product_id = ?
        ORDER BY sm.created_at DESC
        LIMIT 20
    ", [$product_id]) ?: [];
} catch (Exception $e) {
    error_log("Error fetching stock movements: " . $e->getMessage());
}

// Calculate statistics
$stats = [
    'total_sold' => 0,
    'total_revenue' => 0,
    'avg_price' => 0,
    'last_sold' => null
];

try {
    $stats['total_sold'] = (float)db_fetch_value("SELECT COALESCE(SUM(quantity), 0) FROM pos_sale_items WHERE product_id = ?", [$product_id]) ?: 0;
    $stats['total_revenue'] = (float)db_fetch_value("SELECT COALESCE(SUM(total_amount), 0) FROM pos_sale_items WHERE product_id = ?", [$product_id]) ?: 0;
    $stats['avg_price'] = $stats['total_sold'] > 0 ? $stats['total_revenue'] / $stats['total_sold'] : 0;
    $stats['last_sold'] = db_fetch_value("SELECT MAX(s.sale_time) FROM pos_sale_items si JOIN pos_sales s ON si.sale_id = s.sale_id WHERE si.product_id = ?", [$product_id]);
} catch (Exception $e) {
    error_log("Error calculating stats: " . $e->getMessage());
}

// Include header
$header_path = $base_path . '/views/partials/header.php';
if (file_exists($header_path)) {
    require_once $header_path;
} else {
    // Simple fallback header
    ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Product Details</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
        <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
        <style>
            body { background-color: #f4f6f9; font-family: 'Segoe UI', sans-serif; }
            .content-wrapper { padding: 20px; }
            .card { border-radius: 0.5rem; box-shadow: 0 0 10px rgba(0,0,0,0.1); margin-bottom: 20px; }
            .card-header { background: #fff; border-bottom: 1px solid #dee2e6; padding: 15px 20px; font-weight: 600; }
        </style>
    </head>
    <body>
        <div class="wrapper">
            <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
                <div class="container-fluid">
                    <a class="navbar-brand" href="#">Product Details</a>
                    <div class="navbar-nav ms-auto">
                        <span class="nav-item nav-link text-white">
                            <i class="fas fa-user"></i> 
                            <?php echo isset($current_user['username']) ? htmlspecialchars($current_user['username']) : 'User'; ?>
                        </span>
                        <a class="nav-item nav-link" href="/HMSPOS/public/logout.php">
                            <i class="fas fa-sign-out-alt"></i> Logout
                        </a>
                    </div>
                </div>
            </nav>
            <div class="content-wrapper">
    <?php
}
?>

<!-- Content Header -->
<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1 class="m-0">Product: <?php echo escape_html($product['product_name'] ?? ''); ?></h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="/HMSPOS/public/dashboard.php">Home</a></li>
                    <li class="breadcrumb-item"><a href="/HMSPOS/pos/dashboard.php">POS</a></li>
                    <li class="breadcrumb-item"><a href="index.php">Products</a></li>
                    <li class="breadcrumb-item active">View</li>
                </ol>
            </div>
        </div>
    </div>
</div>

<!-- Main content -->
<section class="content">
    <div class="container-fluid">
        <!-- Action Buttons -->
        <div class="row mb-3">
            <div class="col-12">
                <div class="btn-group">
                    <a href="index.php" class="btn btn-secondary">
                        <i class="fas fa-arrow-left me-1"></i> Back to List
                    </a>
                    
                    <?php if (function_exists('has_permission') && has_permission('pos.products.manage')): ?>
                        <a href="edit.php?id=<?php echo $product_id; ?>" class="btn btn-warning">
                            <i class="fas fa-edit me-1"></i> Edit Product
                        </a>
                        
                        <a href="images.php?id=<?php echo $product_id; ?>" class="btn btn-primary">
                            <i class="fas fa-images me-1"></i> Manage Images
                        </a>
                        
                        <?php if (!empty($product['barcode'])): ?>
                            <a href="barcode.php?id=<?php echo $product_id; ?>" class="btn btn-info" target="_blank">
                                <i class="fas fa-barcode me-1"></i> Print Barcode
                            </a>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Stock Alert -->
        <?php if (!empty($product['stock_item']) && ($product['current_stock'] ?? 0) <= ($product['min_stock'] ?? 0)): ?>
            <div class="alert alert-warning alert-dismissible fade show">
                <i class="fas fa-exclamation-triangle me-2"></i>
                <strong>Low Stock Alert!</strong> Current stock (<?php echo $product['current_stock'] ?? 0; ?>) is below minimum level (<?php echo $product['min_stock'] ?? 0; ?>).
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <div class="row">
            <!-- Product Information -->
            <div class="col-md-4">
                <div class="card card-primary card-outline">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-info-circle me-1"></i>
                            Product Information
                        </h3>
                    </div>
                    <div class="card-body">
                        <table class="table table-sm">
                            <tr>
                                <th style="width: 120px;">Code:</th>
                                <td><strong><?php echo escape_html($product['product_code'] ?? ''); ?></strong></td>
                            </tr>
                            <?php if (!empty($product['barcode'])): ?>
                            <tr>
                                <th>Barcode:</th>
                                <td><?php echo escape_html($product['barcode']); ?></td>
                            </tr>
                            <?php endif; ?>
                            <tr>
                                <th>Name:</th>
                                <td><?php echo escape_html($product['product_name'] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <th>Category:</th>
                                <td><?php echo escape_html($product['category_name'] ?? ''); ?></td>
                            </tr>
                            <tr>
                                <th>Outlet:</th>
                                <td><?php echo !empty($product['outlet_name']) ? escape_html($product['outlet_name']) : 'All Outlets'; ?></td>
                            </tr>
                            <tr>
                                <th>Status:</th>
                                <td>
                                    <?php if (!empty($product['is_active'])): ?>
                                        <span class="badge bg-success">Active</span>
                                    <?php else: ?>
                                        <span class="badge bg-danger">Inactive</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        </table>
                        
                        <?php if (!empty($product['description'])): ?>
                            <hr>
                            <h6>Description</h6>
                            <p class="text-muted"><?php echo nl2br(escape_html($product['description'])); ?></p>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Pricing Information -->
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-dollar-sign me-1"></i>
                            Pricing Information
                        </h3>
                    </div>
                    <div class="card-body">
                        <table class="table table-sm">
                            <tr>
                                <th>Unit Price:</th>
                                <td><h4><?php echo format_currency($product['unit_price'] ?? 0); ?></h4></td>
                            </tr>
                            <?php if (!empty($product['cost_price'])): ?>
                            <tr>
                                <th>Cost Price:</th>
                                <td><?php echo format_currency($product['cost_price']); ?></td>
                            </tr>
                            <tr>
                                <th>Margin:</th>
                                <td>
                                    <?php 
                                    $cost = $product['cost_price'] ?? 0;
                                    $price = $product['unit_price'] ?? 1;
                                    $margin = $cost > 0 ? (($price - $cost) / $price) * 100 : 100;
                                    echo round($margin, 1) . '%';
                                    ?>
                                </td>
                            </tr>
                            <?php endif; ?>
                            <tr>
                                <th>Tax:</th>
                                <td>
                                    <?php if (!empty($product['tax_id'])): ?>
                                        <?php echo escape_html($product['tax_name'] ?? 'Tax'); ?> (<?php echo $product['tax_rate'] ?? 0; ?>%)
                                        <?php if (!empty($product['tax_inclusive'])): ?>
                                            <br><small class="text-muted">Inclusive</small>
                                        <?php endif; ?>
                                    <?php else: ?>
                                        <span class="text-muted">No tax</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

                <!-- Product Images -->
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-images me-1"></i>
                            Product Images
                        </h3>
                        <div class="card-tools">
                            <a href="images.php?id=<?php echo $product_id; ?>" class="btn btn-sm btn-primary">
                                Manage
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <?php if (empty($images)): ?>
                            <div class="text-center text-muted py-3">
                                <i class="fas fa-image fa-3x mb-2"></i>
                                <p>No images uploaded</p>
                            </div>
                        <?php else: ?>
                            <div class="row">
                                <?php foreach (array_slice($images, 0, 4) as $image): ?>
                                    <div class="col-6 mb-2">
                                        <img src="/HMSPOS/uploads/product_images/<?php echo urlencode($image['image_path'] ?? ''); ?>" 
                                             class="img-fluid rounded" alt="Product Image"
                                             style="height: 100px; width: 100%; object-fit: cover;">
                                        <?php if (!empty($image['is_primary'])): ?>
                                            <span class="badge bg-success">Primary</span>
                                        <?php endif; ?>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                            <?php if (count($images) > 4): ?>
                                <div class="text-center mt-2">
                                    <small class="text-muted">+<?php echo count($images) - 4; ?> more images</small>
                                </div>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- Inventory & Statistics -->
            <div class="col-md-4">
                <!-- Inventory Information -->
                <div class="card card-<?php echo !empty($product['stock_item']) ? 'success' : 'secondary'; ?>">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-boxes me-1"></i>
                            Inventory Information
                        </h3>
                    </div>
                    <div class="card-body">
                        <?php if (!empty($product['stock_item'])): ?>
                            <div class="info-box bg-light">
                                <span class="info-box-icon bg-info">
                                    <i class="fas fa-cubes"></i>
                                </span>
                                <div class="info-box-content">
                                    <span class="info-box-text">Current Stock</span>
                                    <span class="info-box-number"><?php echo $product['current_stock'] ?? 0; ?> <?php echo $product['unit_of_measure'] ?? ''; ?></span>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-4 text-center">
                                    <h5><?php echo $product['min_stock'] ?? 0; ?></h5>
                                    <small class="text-muted">Min Stock</small>
                                </div>
                                <div class="col-4 text-center">
                                    <h5><?php echo !empty($product['max_stock']) ? $product['max_stock'] : '∞'; ?></h5>
                                    <small class="text-muted">Max Stock</small>
                                </div>
                                <div class="col-4 text-center">
                                    <h5><?php echo !empty($product['unit_of_measure']) ? $product['unit_of_measure'] : '-'; ?></h5>
                                    <small class="text-muted">Unit</small>
                                </div>
                            </div>
                            
                            <hr>
                            
                            <div class="d-flex justify-content-between">
                                <span>Stock Status:</span>
                                <span class="badge bg-<?php 
                                    $current = $product['current_stock'] ?? 0;
                                    $min = $product['min_stock'] ?? 0;
                                    echo $current <= 0 ? 'danger' : ($current <= $min ? 'warning' : 'success');
                                ?>">
                                    <?php 
                                    echo $current <= 0 ? 'Out of Stock' : ($current <= $min ? 'Low Stock' : 'Normal');
                                    ?>
                                </span>
                            </div>
                            
                            <div class="progress mt-2">
                                <?php 
                                $current = $product['current_stock'] ?? 0;
                                $max = $product['max_stock'] ?? $current;
                                $stock_level = $max > 0 ? ($current / $max) * 100 : ($current > 0 ? 100 : 0);
                                ?>
                                <div class="progress-bar bg-success" style="width: <?php echo min(100, $stock_level); ?>%"></div>
                            </div>
                            
                            <div class="mt-3">
                                <strong>Track Serial Numbers:</strong>
                                <?php echo !empty($product['track_serial']) ? 'Yes' : 'No'; ?>
                            </div>
                        <?php else: ?>
                            <div class="text-center text-muted py-3">
                                <i class="fas fa-box-open fa-3x mb-2"></i>
                                <p>Inventory tracking is disabled for this product</p>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>

                <!-- Sales Statistics -->
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-chart-bar me-1"></i>
                            Sales Statistics
                        </h3>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-6">
                                <div class="info-box bg-light">
                                    <span class="info-box-icon bg-success">
                                        <i class="fas fa-shopping-cart"></i>
                                    </span>
                                    <div class="info-box-content">
                                        <span class="info-box-text">Total Sold</span>
                                        <span class="info-box-number"><?php echo $stats['total_sold']; ?></span>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="col-6">
                                <div class="info-box bg-light">
                                    <span class="info-box-icon bg-warning">
                                        <i class="fas fa-dollar-sign"></i>
                                    </span>
                                    <div class="info-box-content">
                                        <span class="info-box-text">Revenue</span>
                                        <span class="info-box-number"><?php echo format_currency($stats['total_revenue']); ?></span>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="col-6">
                                <div class="info-box bg-light">
                                    <span class="info-box-icon bg-info">
                                        <i class="fas fa-chart-line"></i>
                                    </span>
                                    <div class="info-box-content">
                                        <span class="info-box-text">Avg. Price</span>
                                        <span class="info-box-number"><?php echo format_currency($stats['avg_price']); ?></span>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="col-6">
                                <div class="info-box bg-light">
                                    <span class="info-box-icon bg-danger">
                                        <i class="fas fa-clock"></i>
                                    </span>
                                    <div class="info-box-content">
                                        <span class="info-box-text">Last Sold</span>
                                        <span class="info-box-number">
                                            <?php echo $stats['last_sold'] ? time_ago($stats['last_sold']) : 'Never'; ?>
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Recent Stock Movements -->
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-exchange-alt me-1"></i>
                            Recent Stock Movements
                        </h3>
                    </div>
                    <div class="card-body p-0">
                        <?php if (empty($stock_movements)): ?>
                            <div class="text-center text-muted py-3">
                                <p>No stock movements recorded</p>
                            </div>
                        <?php else: ?>
                            <div class="list-group list-group-flush">
                                <?php foreach ($stock_movements as $movement): ?>
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between">
                                        <div>
                                            <span class="badge bg-<?php 
                                                echo ($movement['movement_type'] ?? '') == 'purchase' ? 'success' : 
                                                    (($movement['movement_type'] ?? '') == 'sale' ? 'info' : 
                                                    (($movement['movement_type'] ?? '') == 'adjustment' ? 'warning' : 'secondary'));
                                            ?>">
                                                <?php echo ucfirst($movement['movement_type'] ?? 'unknown'); ?>
                                            </span>
                                            <br>
                                            <small class="text-muted">
                                                <?php echo time_ago($movement['created_at'] ?? date('Y-m-d H:i:s')); ?>
                                            </small>
                                        </div>
                                        <div class="text-end">
                                            <span class="badge bg-<?php echo ($movement['quantity'] ?? 0) > 0 ? 'success' : 'danger'; ?>">
                                                <?php echo ($movement['quantity'] ?? 0) > 0 ? '+' : ''; ?><?php echo $movement['quantity'] ?? 0; ?>
                                            </span>
                                            <br>
                                            <small>by <?php echo escape_html($movement['first_name'] ?? 'System'); ?></small>
                                        </div>
                                    </div>
                                    <?php if (!empty($movement['notes'])): ?>
                                        <small class="text-muted"><?php echo escape_html($movement['notes']); ?></small>
                                    <?php endif; ?>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- Sales History -->
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">
                            <i class="fas fa-history me-1"></i>
                            Recent Sales
                        </h3>
                    </div>
                    <div class="card-body p-0">
                        <?php if (empty($sales_history)): ?>
                            <div class="text-center text-muted py-4">
                                <i class="fas fa-receipt fa-3x mb-3"></i>
                                <p>No sales history</p>
                            </div>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-striped">
                                    <thead>
                                        <tr>
                                            <th>Date</th>
                                            <th>Receipt</th>
                                            <th class="text-center">Qty</th>
                                            <th class="text-end">Amount</th>
                                            <th>Outlet</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($sales_history as $sale): ?>
                                        <tr>
                                            <td>
                                                <?php echo format_date($sale['sale_time'] ?? '', 'M d, H:i'); ?>
                                            </td>
                                            <td>
                                                <a href="/HMSPOS/pos/sales/receipt.php?id=<?php echo $sale['sale_id'] ?? 0; ?>">
                                                    <?php echo escape_html($sale['receipt_number'] ?? ''); ?>
                                                </a>
                                            </td>
                                            <td class="text-center"><?php echo $sale['quantity'] ?? 0; ?></td>
                                            <td class="text-end"><?php echo format_currency($sale['total_amount'] ?? 0); ?></td>
                                            <td><?php echo escape_html($sale['outlet_name'] ?? ''); ?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<style>
.info-box {
    min-height: 70px;
    margin-bottom: 10px;
}
.info-box .info-box-icon {
    width: 50px;
    height: 50px;
    line-height: 50px;
    font-size: 1.5rem;
}
.info-box .info-box-content {
    margin-left: 60px;
}
.me-1 { margin-right: 0.25rem; }
.me-2 { margin-right: 0.5rem; }
.ms-2 { margin-left: 0.5rem; }
.mb-2 { margin-bottom: 0.5rem; }
.mt-2 { margin-top: 0.5rem; }
.text-end { text-align: right; }
.badge.bg-success { background-color: #28a745; color: #fff; }
.badge.bg-warning { background-color: #ffc107; color: #000; }
.badge.bg-danger { background-color: #dc3545; color: #fff; }
.badge.bg-info { background-color: #17a2b8; color: #fff; }
</style>

<?php
// Include footer
$footer_path = $base_path . '/views/partials/footer.php';
if (file_exists($footer_path)) {
    require_once $footer_path;
} else {
    echo '</div></div></body></html>';
}
?>