/home/awneajlw/www/codestechvista.com/search-results.php
<?php
/**
 * Search Record Page - Search and Display Patient Orders
 * This page allows users to search orders by patient name, tracking ID, or phone number
 * Features: Real-time search, filter options, responsive design
 */

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();
}

$search_query = '';
$search_results = [];
$total_results = 0;
$error_message = '';

// Handle search
if (isset($_GET['q']) && !empty(trim($_GET['q']))) {
    $search_query = trim($_GET['q']);
    
    try {
        $database = new Database();
        $db = $database->getConnection();
        
        $user_id = $_SESSION['user_id'];
        $search_term = '%' . $search_query . '%';
        
        // Get user's currency
        $user_currency = getUserCurrency($db, $user_id);
        
        // Search in orders table
        $orders_sql = "SELECT o.*, s.shop_name, 'orders' as source_table 
                      FROM orders o 
                      LEFT JOIN shops s ON o.user_id = s.user_id 
                      WHERE o.user_id = ? 
                      AND (o.tracking_id LIKE ? 
                            OR o.patient_name LIKE ? 
                            OR o.whatsapp_number LIKE ?)";
        
        $stmt1 = $db->prepare($orders_sql);
        $stmt1->execute([$user_id, $search_term, $search_term, $search_term]);
        $orders_results = $stmt1->fetchAll(PDO::FETCH_ASSOC);
        
        // Search in add_record table
        $add_record_sql = "SELECT a.*, s.shop_name, 'add_record' as source_table 
                          FROM add_record a 
                          LEFT JOIN shops s ON a.user_id = s.user_id 
                          WHERE a.user_id = ? 
                          AND (a.tracking_id LIKE ? 
                                OR a.patient_name LIKE ? 
                                OR a.whatsapp_number LIKE ?)";
        
        $stmt2 = $db->prepare($add_record_sql);
        $stmt2->execute([$user_id, $search_term, $search_term, $search_term]);
        $add_record_results = $stmt2->fetchAll(PDO::FETCH_ASSOC);
        
        // Combine results
        $search_results = array_merge($orders_results, $add_record_results);
        
        // Sort by created_at descending
        usort($search_results, function($a, $b) {
            return strtotime($b['created_at']) - strtotime($a['created_at']);
        });
        
        $total_results = count($search_results);
        
    } catch (Exception $e) {
        $error_message = "Search error: " . $e->getMessage();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search Record - 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', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f5f5;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .back-btn {
            background: none;
            border: none;
            color: #169D53;
            font-size: 24px;
            cursor: pointer;
            margin-bottom: 20px;
            padding: 10px;
            transition: all 0.3s ease;
            border-radius: 50%;
        }
        
        .back-btn:hover {
            background: rgba(22, 157, 83, 0.1);
            transform: translateX(-3px);
        }
        
        .search-header {
            background: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            text-align: center;
        }
        
        .search-title {
            color: #169D53;
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 10px;
        }
        
        .search-subtitle {
            color: #666;
            font-size: 16px;
            margin-bottom: 30px;
        }
        
        .search-form {
            max-width: 600px;
            margin: 0 auto;
            position: relative;
        }
        
        .search-input-group {
            position: relative;
            display: flex;
            align-items: center;
        }
        
        .search-input {
            width: 100%;
            padding: 15px 20px 15px 50px;
            border: 2px solid #169D53;
            border-radius: 25px;
            font-size: 16px;
            outline: none;
            transition: all 0.3s ease;
        }
        
        .search-input:focus {
            box-shadow: 0 0 0 4px rgba(22, 157, 83, 0.1);
            border-color: #128a43;
        }
        
        .search-icon {
            position: absolute;
            left: 18px;
            top: 50%;
            transform: translateY(-50%);
            color: #169D53;
            font-size: 18px;
            z-index: 10;
        }
        
        .search-btn {
            background: #169D53;
            color: white;
            width: 200px;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            margin-left: 10px;
            transition: all 0.3s ease;
        }
        
        .search-btn:hover {
            background: #128a43;
            transform: translateY(-2px);
        }
        
        .search-tips {
            margin-top: 20px;
            color: #666;
            font-size: 14px;
        }
        
        .results-section {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        
        .results-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }
        
        .results-title {
            color: #169D53;
            font-size: 20px;
            font-weight: 700;
        }
        
        .results-count {
            background: #169D53;
            color: white;
            padding: 5px 12px;
            border-radius: 15px;
            font-size: 14px;
            font-weight: 600;
        }
        
        .no-results {
            text-align: center;
            padding: 60px 20px;
            color: #666;
        }
        
        .no-results i {
            font-size: 48px;
            color: #ddd;
            margin-bottom: 20px;
        }
        
        .no-results h3 {
            color: #999;
            margin-bottom: 10px;
        }
        
        .search-result-card {
            background: white;
            border-radius: 20px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
            overflow: hidden;
            margin-bottom: 30px;
            max-width: 400px;
            margin-left: auto;
            margin-right: auto;
        }
        
        .result-header {
            text-align: center;
            padding: 40px 20px 20px 20px;
            position: relative;
            z-index: 2;
        }
        
        .result-title {
            color: #169D53;
            font-size: 22px;
            font-weight: 700;
            margin-bottom: 0;
        }
        
        .result-content {
            padding: 25px;
            position: relative;
        }
        
        .result-row {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            padding: 8px 0;
            min-height: 35px;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .result-row:last-child {
            border-bottom: none;
        }
        
        .result-label {
            font-size: 14px;
            color: #666;
            font-weight: 500;
            flex: 0 0 auto;
            margin-right: 10px;
            line-height: 1.4;
        }
        
        .result-value {
            font-size: 14px;
            color: #333;
            font-weight: 500;
            text-align: left;
            flex: 1;
            word-wrap: break-word;
            line-height: 1.4;
        }
        
        .special-note-section {
            margin: 30px 0;
        }
        
        .section-title {
            font-size: 18px;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 15px;
        }
        
        .note-text {
            color: #6b7280;
            line-height: 1.6;
            font-size: 14px;
        }
        
        .prescription-section {
            margin: 30px 0;
        }
        
        .prescription-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-bottom: 20px;
        }
        
        .eye-section {
            background: #f9fafb;
            padding: 20px;
            border-radius: 12px;
            border: 1px solid #e5e7eb;
        }
        
        .eye-title {
            font-size: 16px;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 15px;
            text-align: center;
        }
        
        .prescription-row {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 10px;
            margin-bottom: 10px;
        }
        
        .prescription-item {
            text-align: center;
        }
        
        .prescription-label {
            font-size: 12px;
            color: #6b7280;
            margin-bottom: 5px;
        }
        
        .prescription-value {
            font-size: 16px;
            font-weight: 600;
            color: #1f2937;
        }
        
        .add-section {
            text-align: center;
            margin-top: 20px;
        }
        
        .add-value {
            font-size: 18px;
            font-weight: 700;
            color: #10b981;
        }
        
        .action-buttons {
            display: flex;
            gap: 20px;
            justify-content: center;
            margin-top: 40px;
        }
        
        .action-btn {
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 12px;
            font-weight: 600;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(16, 185, 129, 0.3);
        }
        
        .error-message {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #f5c6cb;
        }
        
        /* Mobile Responsive */
        @media (max-width: 768px) {
            body {
                padding: 15px;
                background: #f5f5f5;
            }
            
            .container {
                max-width: 100%;
                padding: 0 10px;
            }
            
            .back-btn {
                font-size: 20px;
                padding: 8px;
                margin-bottom: 15px;
            }
            
            .search-result-card {
                margin: 0 auto 20px auto;
                border-radius: 15px;
                overflow: hidden;
                box-shadow: 0 4px 15px rgba(0,0,0,0.1);
                max-width: 100%;
            }
            
            .result-header {
                padding: 30px 15px 15px 15px;
            }
            
            .result-content {
                padding: 20px;
            }
            
            .result-row {
                padding: 8px 0;
                display: flex;
                flex-direction: column;
                gap: 4px;
                min-height: auto;
                border-bottom: 1px solid #f0f0f0;
            }
            
            .result-label {
                font-size: 13px;
                color: #666;
                font-weight: 500;
                margin-right: 0;
                margin-bottom: 2px;
            }
            
            .result-value {
                font-size: 14px;
                font-weight: 600;
                color: #333;
                text-align: left;
                word-break: break-word;
            }
            
            .result-header h2 {
                font-size: 18px;
                margin-bottom: 10px;
            }
            
            .action-buttons {
                flex-direction: column;
                gap: 10px;
                margin-top: 20px;
                padding: 0 20px 20px 20px;
            }
            
            .action-btn {
                width: 100%;
                justify-content: center;
                padding: 12px 20px;
                font-size: 14px;
                border-radius: 8px;
            }
            
            .prescription-grid {
                grid-template-columns: 1fr;
                gap: 20px;
            }
            
            .search-header {
                padding: 20px;
                margin-bottom: 20px;
            }
            
            .search-title {
                font-size: 24px;
            }
            
            .search-subtitle {
                font-size: 14px;
                margin-bottom: 20px;
            }
            
            .search-input-group {
                flex-direction: column;
                gap: 10px;
            }
            
            .search-input {
                padding: 12px 15px 12px 40px;
                font-size: 14px;
            }
            
            .search-btn {
                width: 100%;
                margin-left: 0;
                padding: 12px 20px;
                font-size: 14px;
            }
            
            .search-tips {
                font-size: 12px;
                text-align: center;
            }
            
            .results-section {
                padding: 15px;
            }
            
            .results-header {
                flex-direction: column;
                gap: 10px;
                align-items: flex-start;
            }
            
            .results-title {
                font-size: 18px;
            }
        }
        
        @media (max-width: 480px) {
            body {
                padding: 10px;
            }
            
            .container {
                padding: 0 5px;
            }
            
            .search-result-card {
                margin: 0 auto 15px auto;
                border-radius: 12px;
                max-width: 100%;
            }
            
            .result-content {
                padding: 15px;
            }
            
            .result-row {
                padding: 6px 0;
                flex-direction: column;
                align-items: stretch;
                gap: 2px;
                min-height: auto;
            }
            
            .result-label {
                font-size: 12px;
                margin-right: 0;
                text-align: left;
                margin-bottom: 2px;
            }
            
            .result-value {
                font-size: 13px;
                text-align: left;
                font-weight: 700;
                color: #000;
            }
            
            .action-buttons {
                padding: 0 15px 15px 15px;
            }
            
            .action-btn {
                padding: 10px 15px;
                font-size: 13px;
            }
            
            .result-header h2 {
                font-size: 16px;
            }
            
            .search-header {
                padding: 15px;
                margin-bottom: 15px;
                border-radius: 10px;
            }
            
            .search-title {
                font-size: 20px;
                margin-bottom: 8px;
            }
            
            .search-subtitle {
                font-size: 13px;
                margin-bottom: 20px;
            }
            
            .search-input {
                padding: 10px 12px 10px 35px;
                font-size: 13px;
                border-radius: 20px;
            }
            
            .search-icon {
                left: 12px;
                font-size: 14px;
            }
            
            .search-btn {
                padding: 10px 15px;
                font-size: 13px;
                border-radius: 20px;
            }
            
            .search-tips {
                font-size: 11px;
                margin-top: 15px;
            }
            
            .results-section {
                padding: 12px;
                border-radius: 10px;
            }
            
            .results-title {
                font-size: 16px;
            }
            
            .results-count {
                font-size: 12px;
                padding: 4px 8px;
            }
            
            .prescription-grid {
                grid-template-columns: 1fr;
                gap: 15px;
            }
            
            .eye-section {
                padding: 15px;
            }
            
            .eye-title {
                font-size: 14px;
                margin-bottom: 10px;
            }
            
            .prescription-row {
                gap: 8px;
            }
            
            .prescription-label {
                font-size: 11px;
            }
            
            .prescription-value {
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <button class="back-btn" onclick="window.location.href='home.php'">
            <i class="fas fa-arrow-left"></i>
        </button>
        
        <div class="search-header">
            <h1 class="search-title">Search Records</h1>
            <p class="search-subtitle">Search patient orders by name, tracking ID, or WhatsApp number</p>
            
            <form class="search-form" method="GET">
                <div class="search-input-group">
                    <i class="fas fa-search search-icon"></i>
                    <input 
                        type="text" 
                        name="q" 
                        class="search-input" 
                        placeholder="Enter patient name, tracking ID, or phone number..." 
                        value="<?php echo htmlspecialchars($search_query); ?>"
                        required
                    >
                    <button type="submit" class="search-btn">
                        <i class="fas fa-search"></i> Search
                    </button>
                </div>
                <div class="search-tips">
                    <strong>Search Tips:</strong> You can search by patient name, tracking ID (e.g., ORD20241225_1234), or WhatsApp number
                </div>
            </form>
        </div>
        
        <?php if ($error_message): ?>
            <div class="error-message">
                <i class="fas fa-exclamation-triangle"></i>
                <?php echo htmlspecialchars($error_message); ?>
            </div>
        <?php endif; ?>
        
        <?php if ($search_query): ?>
            <div class="results-section">
                <div class="results-header">
                    <h2 class="results-title">Search Results</h2>
                    <span class="results-count"><?php echo $total_results; ?> Found</span>
                </div>
                
                <?php if (empty($search_results)): ?>
                    <div class="no-results">
                        <i class="fas fa-search"></i>
                        <h3>No Records Found</h3>
                        <p>No orders found matching "<?php echo htmlspecialchars($search_query); ?>"</p>
                        <p>Try searching with different keywords or check your spelling.</p>
                    </div>
                <?php else: ?>
                    <?php foreach ($search_results as $order): ?>
                        <div class="search-result-card">
                            <div class="result-header">
                                <h2 class="result-title">Patient Slip</h2>
                            </div>
                            
                            <div class="result-content">
                                <div class="result-row">
                                    <span class="result-label">Patient Name :</span>
                                    <span class="result-value"><?php echo htmlspecialchars($order['patient_name'] ?? 'N/A'); ?></span>
                                </div>
                                
                                <div class="result-row">
                                    <span class="result-label">WhatsApp Number :</span>
                                    <span class="result-value"><?php echo htmlspecialchars($order['whatsapp_number'] ?? 'N/A'); ?></span>
                                </div>
                                
                                <div class="result-row">
                                    <span class="result-label">Frame Detail :</span>
                                    <span class="result-value"><?php echo htmlspecialchars($order['frame_detail'] ?? 'N/A'); ?></span>
                                </div>
                                
                                <div class="result-row">
                                    <span class="result-label">Lens Type :</span>
                                    <span class="result-value"><?php echo htmlspecialchars($order['lens_type'] ?? 'N/A'); ?></span>
                                </div>
                                
                                <div class="result-row">
                                    <span class="result-label">Booking Date :</span>
                                    <span class="result-value"><?php echo $order['created_at'] ? date('d/m/Y', strtotime($order['created_at'])) : 'N/A'; ?></span>
                                </div>
                                
                                <div class="result-row">
                                    <span class="result-label">Delivery Date :</span>
                                    <span class="result-value"><?php echo $order['delivery_date'] ? date('d/m/Y', strtotime($order['delivery_date'])) : 'N/A'; ?></span>
                                </div>
                                
                                <div class="result-row">
                                    <span class="result-label">Total Amount :</span>
                                    <span class="result-value"><?php echo formatCurrency($order['total_amount'] ?? 0, $user_currency); ?></span>
                                </div>
                                
                                <div class="result-row">
                                    <span class="result-label">Advance Payment :</span>
                                    <span class="result-value"><?php echo formatCurrency($order['advance'] ?? 0, $user_currency); ?></span>
                                </div>
                                
                                <div class="result-row">
                                    <span class="result-label">Balance Payment :</span>
                                    <span class="result-value"><?php echo formatCurrency($order['balance'] ?? 0, $user_currency); ?></span>
                                </div>
                            </div>
                            
                            <!-- Special Note Section -->
                            <?php if (!empty($order['special_note'])): ?>
                            <div class="special-note-section">
                                <h3 class="section-title">Special Note</h3>
                                <p class="note-text"><?php echo htmlspecialchars($order['special_note']); ?></p>
                            </div>
                            <?php endif; ?>
                            
                            <!-- Prescription Section -->
                            <?php if (isset($order['right_eye_sph']) || isset($order['right_sph']) || isset($order['right_eye_cyl']) || isset($order['right_cyl'])): ?>
                            <div class="prescription-section">
                                <h3 class="section-title">Prescription</h3>
                                
                                <div class="prescription-grid">
                                    <!-- Right Eye -->
                                    <div class="eye-section">
                                        <div class="eye-title">Right Eye</div>
                                        <div class="prescription-row">
                                            <div class="prescription-item">
                                                <div class="prescription-label">SPH</div>
                                                <div class="prescription-value"><?php echo number_format($order['right_eye_sph'] ?? $order['right_sph'] ?? 0, 2); ?></div>
                                            </div>
                                            <div class="prescription-item">
                                                <div class="prescription-label">CYL</div>
                                                <div class="prescription-value"><?php echo number_format($order['right_eye_cyl'] ?? $order['right_cyl'] ?? 0, 2); ?></div>
                                            </div>
                                            <div class="prescription-item">
                                                <div class="prescription-label">AXIS</div>
                                                <div class="prescription-value"><?php echo $order['right_eye_axis'] ?? $order['right_axis'] ?? 0; ?></div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <!-- Left Eye -->
                                    <div class="eye-section">
                                        <div class="eye-title">Left Eye</div>
                                        <div class="prescription-row">
                                            <div class="prescription-item">
                                                <div class="prescription-label">SPH</div>
                                                <div class="prescription-value"><?php echo number_format($order['left_eye_sph'] ?? $order['left_sph'] ?? 0, 2); ?></div>
                                            </div>
                                            <div class="prescription-item">
                                                <div class="prescription-label">CYL</div>
                                                <div class="prescription-value"><?php echo number_format($order['left_eye_cyl'] ?? $order['left_cyl'] ?? 0, 2); ?></div>
                                            </div>
                                            <div class="prescription-item">
                                                <div class="prescription-label">AXIS</div>
                                                <div class="prescription-value"><?php echo $order['left_eye_axis'] ?? $order['left_axis'] ?? 0; ?></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                
                                <!-- Add Section -->
                                <?php if (isset($order['add_value']) || isset($order['near_add']) || isset($order['add_power'])): ?>
                                <div class="add-section">
                                    <div class="prescription-label">Add</div>
                                    <div class="add-value"><?php echo number_format($order['add_value'] ?? $order['near_add'] ?? $order['add_power'] ?? 0, 2); ?></div>
                                </div>
                                <?php endif; ?>
                            </div>
                            <?php endif; ?>
                        </div>
                        
                        <!-- Action Buttons Outside Card -->
                        <div class="action-buttons">
                            <button class="action-btn" onclick="shareOrder(<?php echo $order['id']; ?>)">
                                <i class="fas fa-share-alt"></i>
                                Share
                            </button>
                            <button class="action-btn" onclick="printOrder(<?php echo $order['id']; ?>)">
                                <i class="fas fa-print"></i>
                                Print
                            </button>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
    
    <script>
        function shareOrder(orderId) {
            if (navigator.share) {
                navigator.share({
                    title: 'OPTI SLIP Order',
                    text: 'Check out this order from OPTI SLIP',
                    url: window.location.href
                });
            } else {
                // Fallback: copy to clipboard
                navigator.clipboard.writeText(window.location.href).then(() => {
                    alert('Order link copied to clipboard!');
                });
            }
        }
        
        function printOrder(orderId) {
            window.print();
        }
        
        // Auto-focus search input on page load
        document.addEventListener('DOMContentLoaded', function() {
            const searchInput = document.querySelector('.search-input');
            if (searchInput && !searchInput.value) {
                searchInput.focus();
            }
        });
        
        // Add enter key support for search
        document.querySelector('.search-input').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                document.querySelector('.search-form').submit();
            }
        });
    </script>
</body>
</html>