/home/awneajlw/public_html/codestechvista.com/pending-order-details.php
<?php
/**
 * Pending Order Details Page - View Pending Order Details
 * This page shows detailed view of a pending order with prescription info
 * Features: Order details, prescription display, mark as complete functionality
 */

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once 'config/database.php';
require_once 'includes/auth.php';
require_once 'includes/currency_helper.php';

// Check if user is logged in
if (!isLoggedIn()) {
    header('Location: welcome.php');
    exit();
}

// Get order ID from URL
$order_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$success_message = '';
$error_message = '';

// Handle Mark as Complete
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'mark_complete') {
    try {
        $database = new Database();
        $db = $database->getConnection();
        
        $user_id = $_SESSION['user_id'];
        $update_query = "UPDATE orders SET status = 'Completed' WHERE id = ? AND user_id = ?";
        $stmt = $db->prepare($update_query);
        $stmt->execute([$order_id, $user_id]);
        
        if ($stmt->rowCount() > 0) {
            // Redirect to completed orders page
            header('Location: completed-orders.php');
            exit();
        } else {
            $error_message = 'Failed to update order status.';
        }
    } catch (Exception $e) {
        $error_message = 'Database error: ' . $e->getMessage();
    }
}

// Get order details
$order_data = null;
try {
    $database = new Database();
    $db = $database->getConnection();
    
    $user_id = $_SESSION['user_id'];
    $query = "SELECT * FROM orders WHERE id = ? AND user_id = ? AND (status IS NULL OR status = 'Pending' OR status = '')";
    $stmt = $db->prepare($query);
    $stmt->execute([$order_id, $user_id]);
    $order_data = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // Get user's currency
    $user_currency = getUserCurrency($db, $user_id);
    
} catch (Exception $e) {
    $error_message = "Error loading order: " . $e->getMessage();
}

// If no order found, redirect to pending orders
if (!$order_data) {
    header('Location: pending-orders.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Details - OPTI SLIP</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Inter', sans-serif;
            background: #f5f5f5;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 500px;
            margin: 0 auto;
            padding: 0;
        }
        
        .header-section {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
        }
        
        .back-btn {
            background: none;
            border: none;
            color: black;
            font-size: 24px;
            cursor: pointer;
            padding: 10px;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            color: #169D53;
            transform: translateX(-3px);
        }
        
        .logo-section {
            text-align: center;
        }
        
        .logo-image {
            width: 80px;
            height: 80px;
            object-fit: contain;
            filter: brightness(0) saturate(100%);
        }
        
        .details-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            overflow: hidden;
            margin-bottom: 30px;
        }
        
        .detail-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 20px;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .detail-row:last-child {
            border-bottom: none;
        }
        
        .detail-label {
            font-size: 14px;
            color: #666;
            font-weight: 500;
        }
        
        .detail-value {
            font-size: 14px;
            color: #333;
            font-weight: 600;
            text-align: right;
            max-width: 200px;
            word-wrap: break-word;
        }
        
        .special-note {
            padding: 15px 20px;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .note-label {
            font-size: 14px;
            color: #666;
            font-weight: 500;
            margin-bottom: 8px;
        }
        
        .note-content {
            font-size: 13px;
            color: #333;
            line-height: 1.5;
            background: #f8f9fa;
            padding: 10px;
            border-radius: 8px;
        }
        
        .prescription-section {
            padding: 15px 20px;
        }
        
        .prescription-title {
            font-size: 14px;
            color: #666;
            font-weight: 500;
            margin-bottom: 15px;
        }
        
        .eye-section {
            margin-bottom: 15px;
        }
        
        .eye-title {
            font-size: 13px;
            color: #333;
            font-weight: 600;
            margin-bottom: 8px;
        }
        
        .prescription-values {
            display: flex;
            gap: 20px;
            font-size: 12px;
            color: #666;
            margin-bottom: 10px;
        }
        
        .prescription-value {
            display: flex;
            align-items: center;
            gap: 5px;
        }
        
        .add-section {
            margin-top: 10px;
            padding-top: 10px;
            border-top: 1px solid #f0f0f0;
        }
        
        .add-value {
            font-size: 12px;
            color: #666;
        }
        
        .action-button {
            text-align: center;
            margin-top: 20px;
        }
        
        .complete-btn {
            background: #169D53;
            color: white;
            border: none;
            padding: 15px 40px;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
        }
        
        .complete-btn:hover {
            background: #128a43;
            color: white;
            transform: translateY(-2px);
        }
        
        .alert {
            padding: 12px 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 13px;
        }
        
        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .alert-danger {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        /* Mobile Responsive */
        @media (max-width: 768px) {
            body {
                padding: 10px;
            }
            
            .container {
                max-width: 100%;
            }
            
            .header-section {
                margin-bottom: 20px;
            }
            
            .logo-image {
                width: 60px;
                height: 60px;
            }
            
            .detail-row {
                padding: 12px 15px;
            }
            
            .detail-value {
                max-width: 150px;
                font-size: 13px;
            }
            
            .detail-label {
                font-size: 13px;
            }
            
            .prescription-values {
                flex-direction: column;
                gap: 5px;
            }
            
            .complete-btn {
                padding: 12px 30px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header-section">
            <button class="back-btn" onclick="window.location.href='pending-orders.php'">
                <i class="fas fa-arrow-left"></i>
            </button>
            
            <div class="logo-section">
                <img src="assets/images/Optislipimage.png" alt="Opti Slip Logo" class="logo-image" onerror="this.style.display='none'; this.parentElement.innerHTML='<div style=\'color: black; font-size: 16px; font-weight: bold;\'>OPTI SLIP</div>'">
            </div>
            
            <div style="width: 34px;"></div> <!-- Spacer for centering -->
        </div>
        
        <!-- Success/Error Messages -->
        <?php if ($error_message): ?>
            <div class="alert alert-danger">
                <i class="fas fa-exclamation-triangle me-2"></i>
                <?php echo htmlspecialchars($error_message); ?>
            </div>
        <?php endif; ?>
        
        <div class="details-card">
            <div class="detail-row">
                <span class="detail-label">Patient Name</span>
                <span class="detail-value"><?php echo htmlspecialchars($order_data['patient_name'] ?? 'N/A'); ?></span>
            </div>
            
            <div class="detail-row">
                <span class="detail-label">WhatsApp Number</span>
                <span class="detail-value"><?php echo htmlspecialchars($order_data['whatsapp_number'] ?? 'N/A'); ?></span>
            </div>
            
            <div class="detail-row">
                <span class="detail-label">Frame Detail</span>
                <span class="detail-value"><?php echo htmlspecialchars($order_data['frame_detail'] ?? 'N/A'); ?></span>
            </div>
            
            <div class="detail-row">
                <span class="detail-label">Lens Type</span>
                <span class="detail-value"><?php echo htmlspecialchars($order_data['lens_type'] ?? 'N/A'); ?></span>
            </div>
            
            <div class="detail-row">
                <span class="detail-label">Booking Date</span>
                <span class="detail-value"><?php echo $order_data['created_at'] ? date('d/m/Y', strtotime($order_data['created_at'])) : 'N/A'; ?></span>
            </div>
            
            <div class="detail-row">
                <span class="detail-label">Total Amount</span>
                <span class="detail-value"><?php echo formatCurrency($order_data['total_amount'] ?? 0, $user_currency); ?></span>
            </div>
            
            <?php if (!empty($order_data['important_note'])): ?>
                <div class="special-note">
                    <div class="note-label">Special Note</div>
                    <div class="note-content"><?php echo nl2br(htmlspecialchars($order_data['important_note'])); ?></div>
                </div>
            <?php endif; ?>
            
            <div class="prescription-section">
                <div class="prescription-title">Prescription</div>
                
                <div class="eye-section">
                    <div class="eye-title">Right Eye :</div>
                    <div class="prescription-values">
                        <div class="prescription-value">
                            <span>SPH:</span>
                            <span><?php echo number_format($order_data['right_eye_sph'] ?? 0, 2); ?></span>
                        </div>
                        <div class="prescription-value">
                            <span>CYL:</span>
                            <span><?php echo number_format($order_data['right_eye_cyl'] ?? 0, 2); ?></span>
                        </div>
                        <div class="prescription-value">
                            <span>AXIS:</span>
                            <span><?php echo intval($order_data['right_eye_axis'] ?? 0); ?></span>
                        </div>
                    </div>
                </div>
                
                <div class="eye-section">
                    <div class="eye-title">Left Eye :</div>
                    <div class="prescription-values">
                        <div class="prescription-value">
                            <span>SPH:</span>
                            <span><?php echo number_format($order_data['left_eye_sph'] ?? 0, 2); ?></span>
                        </div>
                        <div class="prescription-value">
                            <span>CYL:</span>
                            <span><?php echo number_format($order_data['left_eye_cyl'] ?? 0, 2); ?></span>
                        </div>
                        <div class="prescription-value">
                            <span>AXIS:</span>
                            <span><?php echo intval($order_data['left_eye_axis'] ?? 0); ?></span>
                        </div>
                    </div>
                </div>
                
                <?php if (!empty($order_data['add_value'])): ?>
                    <div class="add-section">
                        <div class="add-value">Add: <?php echo htmlspecialchars($order_data['add_value']); ?></div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        
        <div class="action-button">
            <form method="POST" style="display: inline;">
                <input type="hidden" name="action" value="mark_complete">
                <button type="submit" class="complete-btn" onclick="return confirm('Are you sure you want to mark this order as complete?')">
                    Mark As Complete
                </button>
            </form>
        </div>
    </div>
</body>
</html>