<?php
session_start();
require_once '../../config/database.php';
require_once '../../helpers/quiz-recommendations.php';

// Check admin authentication
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: ../login.php');
    exit;
}

$database = new Database();
$conn = $database->getConnection();

// Get result ID
$resultId = isset($_GET['id']) ? intval($_GET['id']) : 0;

if (!$resultId) {
    header('Location: index.php');
    exit;
}

// Fetch result
$stmt = $conn->prepare("SELECT * FROM skin_quiz_results WHERE id = ?");
$stmt->execute([$resultId]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$result) {
    header('Location: index.php');
    exit;
}

// Decode JSON data
$quiz_answers = json_decode($result['quiz_answers'], true) ?? [];
$concerns = json_decode($result['skin_concerns'], true) ?? [];
$recommendedProducts = json_decode($result['recommended_products'], true) ?? [];

// Get skin care tips
$tips = getSkinCareTips($result['skin_type'], $concerns);

// Skin type info
$skinTypeInfo = [
    'oily' => ['name' => 'دهنية', 'icon' => '💧', 'color' => '#3b82f6'],
    'dry' => ['name' => 'جافة', 'icon' => '🏜️', 'color' => '#f59e0b'],
    'combination' => ['name' => 'مختلطة', 'icon' => '🌓', 'color' => '#8b5cf6'],
    'normal' => ['name' => 'عادية', 'icon' => '✨', 'color' => '#10b981'],
    'sensitive' => ['name' => 'حساسة', 'icon' => '🌸', 'color' => '#ec4899']
];

$skinInfo = $skinTypeInfo[$result['skin_type']] ?? $skinTypeInfo['normal'];

// Concern translations
$concernTranslations = [
    'acne' => 'حب الشباب',
    'dark_spots' => 'البقع الداكنة',
    'wrinkles' => 'التجاعيد',
    'dryness' => 'الجفاف',
    'large_pores' => 'المسام الواسعة',
    'dullness' => 'البشرة الباهتة',
    'dark_circles' => 'الهالات السوداء',
    'redness' => 'الاحمرار'
];
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>عرض نتيجة الاختبار - <?php echo htmlspecialchars($result['name']); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="../assets/css/admin-layout.css">
    <!-- JsBarcode for generating barcodes -->
    <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { 
            font-family: 'Tajawal', sans-serif;
            background: #f5f5f5;
        }
        .prescription-card {
            background: white;
            border-radius: 20px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.1);
            overflow: hidden;
            position: relative;
        }
        
        /* Decorative pattern background */
        .prescription-card .p-8 {
            background-image: 
                repeating-linear-gradient(
                    45deg,
                    transparent,
                    transparent 35px,
                    rgba(<?php 
                        $hex = str_replace('#', '', $skinInfo['color']);
                        $r = hexdec(substr($hex, 0, 2));
                        $g = hexdec(substr($hex, 2, 2));
                        $b = hexdec(substr($hex, 4, 2));
                        echo "$r, $g, $b";
                    ?>, 0.01) 35px,
                    rgba(<?php echo "$r, $g, $b"; ?>, 0.01) 36px
                );
        }
        .prescription-header {
            background: linear-gradient(135deg, <?php echo $skinInfo['color']; ?> 0%, <?php echo $skinInfo['color']; ?>dd 100%);
            color: white;
            padding: 40px;
            position: relative;
            overflow: hidden;
        }
        .prescription-header::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -10%;
            width: 300px;
            height: 300px;
            background: rgba(255,255,255,0.1);
            border-radius: 50%;
        }
        .prescription-header::after {
            content: '';
            position: absolute;
            bottom: -30%;
            left: -5%;
            width: 200px;
            height: 200px;
            background: rgba(255,255,255,0.08);
            border-radius: 50%;
        }
        .score-badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background: white;
            color: <?php echo $skinInfo['color']; ?>;
            font-size: 32px;
            font-weight: 800;
            box-shadow: 0 5px 20px rgba(0,0,0,0.2);
        }
        .section-title {
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 20px;
            font-weight: 700;
            color: #1f2937;
            margin-bottom: 20px;
            padding-bottom: 12px;
            border-bottom: 3px solid <?php echo $skinInfo['color']; ?>;
            position: relative;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            bottom: -3px;
            right: 0;
            width: 60px;
            height: 3px;
            background: <?php echo $skinInfo['color']; ?>;
            opacity: 0.3;
        }
        
        .section-title i {
            width: 32px;
            height: 32px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: <?php echo $skinInfo['color']; ?>15;
            border-radius: 8px;
        }
        .info-row {
            display: flex;
            padding: 12px 0;
            border-bottom: 1px solid #f3f4f6;
        }
        .info-label {
            font-weight: 600;
            color: #6b7280;
            min-width: 150px;
        }
        .info-value {
            color: #1f2937;
            flex: 1;
        }
        
        /* Medical Tables */
        .medical-table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            background: white;
        }
        
        .medical-table th {
            background: <?php echo $skinInfo['color']; ?>;
            color: white;
            padding: 12px;
            text-align: right;
            font-weight: 600;
            border: 1px solid <?php echo $skinInfo['color']; ?>;
        }
        
        .medical-table td {
            padding: 12px;
            border: 1px solid #ddd;
            text-align: right;
        }
        
        .medical-table tr:nth-child(even) {
            background: #f9fafb;
        }
        
        .medical-table tr:hover {
            background: #f3f4f6;
        }
        
        /* Print Styles - Professional Medical Prescription */
        @media print {
            @page {
                size: A4 portrait;
                margin: 15mm 20mm;
            }
            
            * {
                -webkit-print-color-adjust: exact !important;
                print-color-adjust: exact !important;
            }
            
            .no-print { 
                display: none !important; 
            }
            
            html, body { 
                background: white !important;
                margin: 0 !important;
                padding: 0 !important;
                width: 210mm;
                min-height: 297mm;
            }
            
            /* Medical Tables for Print */
            .medical-table {
                page-break-inside: avoid;
                border: 2px solid #333 !important;
            }
            
            .medical-table th {
                background: <?php echo $skinInfo['color']; ?> !important;
                color: white !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
                border: 1px solid #333 !important;
                font-size: 12pt;
            }
            
            .medical-table td {
                border: 1px solid #666 !important;
                font-size: 11pt;
                line-height: 1.4;
            }
            
            .medical-table tr:nth-child(even) {
                background: #f5f5f5 !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            
            .prescription-card { 
                box-shadow: none;
                border-radius: 0;
                max-width: 100%;
                margin: 0;
                page-break-after: avoid;
            }
            
            .prescription-header {
                background: linear-gradient(135deg, <?php echo $skinInfo['color']; ?> 0%, <?php echo $skinInfo['color']; ?>dd 100%) !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
                padding: 30px !important;
                border-bottom: 4px solid <?php echo $skinInfo['color']; ?>;
            }
            
            .prescription-header h1 {
                font-size: 28px !important;
                margin-bottom: 10px !important;
            }
            
            .score-badge {
                width: 80px !important;
                height: 80px !important;
                font-size: 28px !important;
                border: 3px solid <?php echo $skinInfo['color']; ?>;
            }
            
            .section-title {
                font-size: 18px !important;
                margin-top: 25px !important;
                margin-bottom: 15px !important;
                padding-bottom: 8px !important;
                border-bottom: 2px solid <?php echo $skinInfo['color']; ?> !important;
                page-break-after: avoid;
            }
            
            .info-row {
                padding: 8px 0 !important;
                font-size: 14px !important;
            }
            
            .bg-gray-50,
            .bg-gray-100,
            .bg-blue-50 {
                background-color: #f9fafb !important;
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            
            .grid {
                page-break-inside: avoid;
            }
            
            /* Prescription Number Box */
            .prescription-number {
                position: absolute;
                top: 20px;
                left: 20px;
                background: white;
                color: <?php echo $skinInfo['color']; ?>;
                padding: 10px 20px;
                border-radius: 8px;
                font-weight: bold;
                border: 2px solid <?php echo $skinInfo['color']; ?>;
            }
            
            /* Medical Stamp Area */
            .medical-stamp {
                margin-top: 40px;
                padding-top: 20px;
                border-top: 2px dashed #ccc;
                display: flex;
                justify-content: space-between;
                page-break-inside: avoid;
            }
            
            .stamp-box {
                width: 200px;
                height: 100px;
                border: 2px dashed #999;
                border-radius: 8px;
                display: flex;
                align-items: center;
                justify-content: center;
                color: #999;
                font-size: 12px;
            }
            
            /* Footer */
            .prescription-footer {
                margin-top: 30px;
                padding-top: 20px;
                border-top: 3px double <?php echo $skinInfo['color']; ?>;
                text-align: center;
                font-size: 11px;
                color: #666;
                page-break-inside: avoid;
                background: #f9fafb !important;
                padding: 20px !important;
                margin: 30px -40px -40px -40px !important;
            }
            
            /* Product recommendations styling for print */
            .space-y-3 > div {
                page-break-inside: avoid;
                margin-bottom: 10px !important;
            }
            
            /* Tips styling for print */
            .grid.gap-3 > div {
                page-break-inside: avoid;
            }
            
            /* Ensure proper spacing */
            .mb-8 {
                margin-bottom: 25px !important;
            }
            
            /* Border colors */
            .border-r-4 {
                border-right-width: 4px !important;
            }
            
            /* Background colors for alerts */
            .bg-blue-50 {
                background-color: #eff6ff !important;
            }
            
            .bg-yellow-50 {
                background-color: #fefce8 !important;
            }
            
            /* Watermark */
            .watermark {
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                z-index: -1;
                opacity: 1;
            }
            
            /* Decorative Corner Elements */
            .prescription-card::before {
                content: '';
                position: absolute;
                top: 0;
                right: 0;
                width: 100px;
                height: 100px;
                border-top: 4px solid <?php echo $skinInfo['color']; ?>;
                border-right: 4px solid <?php echo $skinInfo['color']; ?>;
                opacity: 0.3;
            }
            
            .prescription-card::after {
                content: '';
                position: absolute;
                bottom: 0;
                left: 0;
                width: 100px;
                height: 100px;
                border-bottom: 4px solid <?php echo $skinInfo['color']; ?>;
                border-left: 4px solid <?php echo $skinInfo['color']; ?>;
                opacity: 0.3;
            }
            
            /* Page border for print */
            @page {
                margin: 15mm;
            }
            
            body {
                border: 2px solid <?php echo $skinInfo['color']; ?>;
                padding: 10mm;
            }
            
            /* Custom checkmarks and bullets */
            .bg-blue-50 i.fas,
            .bg-yellow-50 i.fas {
                display: inline-block;
                width: 16px;
                height: 16px;
                margin-left: 5px;
            }
            
            .fa-info-circle::before {
                content: 'ℹ';
                font-style: normal;
                font-weight: bold;
            }
            
            .fa-exclamation-triangle::before {
                content: '⚠';
                font-style: normal;
            }
            
            .fa-check-circle::before {
                content: '✓';
                font-style: normal;
                font-weight: bold;
            }
            
            .fa-clock::before {
                content: '⏰';
                font-style: normal;
            }
            
            /* Professional numbering */
            .space-y-3 > div {
                counter-increment: prescription-item;
            }
            
            /* Ensure all colors print */
            * {
                -webkit-print-color-adjust: exact !important;
                print-color-adjust: exact !important;
                color-adjust: exact !important;
            }
        }
    </style>
</head>
<body class="bg-gray-50">

<?php include '../includes/sidebar.php'; ?>

<div style="margin-right: 280px; padding: 24px;" class="no-print">
    <!-- Back Button -->
    <div class="mb-6">
        <a href="index.php" class="inline-flex items-center gap-2 text-gray-600 hover:text-gray-900 transition">
            <i class="fas fa-arrow-right"></i>
            <span>العودة للقائمة</span>
        </a>
    </div>
    
    <!-- Action Buttons -->
    <div class="flex gap-3 mb-6">
        <button onclick="window.print()" class="bg-blue-600 text-white px-6 py-3 rounded-lg hover:bg-blue-700 transition flex items-center gap-2">
            <i class="fas fa-print"></i>
            <span>طباعة</span>
        </button>
        
        <button onclick="downloadPDF()" class="bg-green-600 text-white px-6 py-3 rounded-lg hover:bg-green-700 transition flex items-center gap-2">
            <i class="fas fa-file-pdf"></i>
            <span>تحميل PDF</span>
        </button>
        
        <?php if (!$result['is_contacted']): ?>
        <button onclick="markAsContacted(<?php echo $result['id']; ?>)" class="bg-purple-600 text-white px-6 py-3 rounded-lg hover:bg-purple-700 transition flex items-center gap-2">
            <i class="fas fa-check"></i>
            <span>تم التواصل</span>
        </button>
        <?php endif; ?>
        
        <a href="../../public/skin-quiz-result.php?id=<?php echo $result['id']; ?>" target="_blank" class="bg-gray-600 text-white px-6 py-3 rounded-lg hover:bg-gray-700 transition flex items-center gap-2">
            <i class="fas fa-external-link-alt"></i>
            <span>عرض للعميل</span>
        </a>
    </div>
</div>

<!-- Prescription Card -->
<div style="max-width: 900px; margin: 0 auto; padding: 0 24px 40px;">
    <div class="prescription-card" id="prescription">
        
        <!-- Header -->
        <div class="prescription-header">
            <!-- Prescription Number Badge (Print) -->
            <div class="prescription-number" style="display: none;">
                Rx #<?php echo str_pad($result['id'], 6, '0', STR_PAD_LEFT); ?>
            </div>
            
            <div style="position: relative; z-index: 2;">
                <div class="flex items-center justify-between mb-6">
                    <div>
                        <div class="text-sm text-white/80 mb-2 flex items-center gap-2">
                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
                                <polyline points="9 22 9 12 15 12 15 22"></polyline>
                            </svg>
                            مركز روز سكين للعناية بالبشرة
                        </div>
                        <h1 class="text-3xl font-bold mb-2 flex items-center gap-3">
                            <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                <path d="M9 11a3 3 0 1 0 6 0a3 3 0 0 0 -6 0"></path>
                                <path d="M17.657 16.657l-4.243 4.243a2 2 0 0 1 -2.827 0l-4.244 -4.243a8 8 0 1 1 11.314 0z"></path>
                            </svg>
                            تقرير تحليل البشرة الطبي
                        </h1>
                        <p class="text-white/90 text-sm">Skin Analysis Medical Report</p>
                    </div>
                    <div class="score-badge">
                        <div style="font-size: 12px; font-weight: 500; margin-bottom: 5px;">النتيجة</div>
                        <div><?php echo $result['score']; ?></div>
                        <div style="font-size: 10px; font-weight: 400; margin-top: 5px;">من 100</div>
                    </div>
                </div>
                
                <div class="grid grid-cols-3 gap-4 text-sm bg-white/10 rounded-lg p-4 backdrop-blur-sm">
                    <div>
                        <span class="text-white/70 block text-xs mb-1">رقم التقرير</span>
                        <span class="font-bold text-lg">#<?php echo str_pad($result['id'], 6, '0', STR_PAD_LEFT); ?></span>
                    </div>
                    <div>
                        <span class="text-white/70 block text-xs mb-1">تاريخ الفحص</span>
                        <span class="font-bold"><?php echo date('Y-m-d', strtotime($result['created_at'])); ?></span>
                    </div>
                    <div>
                        <span class="text-white/70 block text-xs mb-1">الوقت</span>
                        <span class="font-bold"><?php echo date('H:i', strtotime($result['created_at'])); ?></span>
                    </div>
                </div>
            </div>
        </div>
        
        <style>
            @media print {
                .prescription-number {
                    display: block !important;
                }
            }
        </style>
        
        <!-- Body -->
        <div class="p-8">
            
            <!-- Patient Info Table -->
            <div class="mb-8">
                <h3 style="font-size: 18px; font-weight: 700; color: #1f2937; margin-bottom: 15px; padding-bottom: 8px; border-bottom: 3px solid <?php echo $skinInfo['color']; ?>;">
                    <i class="fas fa-user-circle" style="color: <?php echo $skinInfo['color']; ?>; margin-left: 8px;"></i>
                    معلومات المريض / Patient Information
                </h3>
                
                <table class="medical-table">
                    <thead>
                        <tr>
                            <th style="width: 30%;">البيان</th>
                            <th style="width: 70%;">التفاصيل</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><strong>الاسم الكامل / Full Name</strong></td>
                            <td><?php echo htmlspecialchars($result['name']); ?></td>
                        </tr>
                        <tr>
                            <td><strong>العمر / Age</strong></td>
                            <td><?php echo $result['age']; ?> سنة</td>
                        </tr>
                        <tr>
                            <td><strong>الجنس / Gender</strong></td>
                            <td><?php echo $result['gender'] === 'female' ? 'أنثى / Female' : 'ذكر / Male'; ?></td>
                        </tr>
                        <tr>
                            <td><strong>البريد الإلكتروني / Email</strong></td>
                            <td><?php echo htmlspecialchars($result['email']); ?></td>
                        </tr>
                        <?php if ($result['phone']): ?>
                        <tr>
                            <td><strong>رقم الهاتف / Phone</strong></td>
                            <td><?php echo htmlspecialchars($result['phone']); ?></td>
                        </tr>
                        <?php endif; ?>
                        <tr>
                            <td><strong>تاريخ الفحص / Examination Date</strong></td>
                            <td><?php echo date('Y-m-d H:i', strtotime($result['created_at'])); ?></td>
                        </tr>
                    </tbody>
                </table>
            </div>
            
            <!-- Diagnosis Table -->
            <div class="mb-8">
                <h3 style="font-size: 18px; font-weight: 700; color: #1f2937; margin-bottom: 15px; padding-bottom: 8px; border-bottom: 3px solid <?php echo $skinInfo['color']; ?>;">
                    <i class="fas fa-stethoscope" style="color: <?php echo $skinInfo['color']; ?>; margin-left: 8px;"></i>
                    التشخيص الطبي / Medical Diagnosis
                </h3>
                
                <table class="medical-table">
                    <thead>
                        <tr>
                            <th style="width: 30%;">نوع الفحص</th>
                            <th style="width: 70%;">النتيجة</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><strong>نوع البشرة / Skin Type</strong></td>
                            <td>
                                <span style="font-size: 24px; margin-left: 10px;"><?php echo $skinInfo['icon']; ?></span>
                                <strong style="color: <?php echo $skinInfo['color']; ?>; font-size: 16px;">
                                    بشرة <?php echo $skinInfo['name']; ?> / <?php echo ucfirst($result['skin_type']); ?> Skin
                                </strong>
                            </td>
                        </tr>
                        <tr>
                            <td><strong>درجة التقييم / Score</strong></td>
                            <td>
                                <strong style="color: <?php echo $skinInfo['color']; ?>; font-size: 18px;">
                                    <?php echo $result['score']; ?> / 100
                                </strong>
                                <span style="margin-right: 15px;">
                                    <?php 
                                    if ($result['score'] >= 80) echo '(ممتاز / Excellent)';
                                    elseif ($result['score'] >= 60) echo '(جيد / Good)';
                                    elseif ($result['score'] >= 40) echo '(متوسط / Fair)';
                                    else echo '(يحتاج عناية / Needs Care)';
                                    ?>
                                </span>
                            </td>
                        </tr>
                        <?php if (!empty($concerns)): ?>
                        <tr>
                            <td><strong>المشاكل المكتشفة / Detected Issues</strong></td>
                            <td>
                                <?php 
                                $concernsList = array_map(function($c) use ($concernTranslations) {
                                    return $concernTranslations[$c] ?? $c;
                                }, $concerns);
                                echo implode(' • ', $concernsList);
                                ?>
                            </td>
                        </tr>
                        <?php endif; ?>
                        <?php if ($result['analysis_text']): ?>
                        <tr>
                            <td><strong>التحليل التفصيلي / Detailed Analysis</strong></td>
                            <td style="line-height: 1.8;"><?php echo nl2br(htmlspecialchars($result['analysis_text'])); ?></td>
                        </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            
            <!-- Treatment Recommendations Table -->
            <?php if (!empty($recommendedProducts)): ?>
            <div class="mb-8">
                <h3 style="font-size: 18px; font-weight: 700; color: #1f2937; margin-bottom: 15px; padding-bottom: 8px; border-bottom: 3px solid <?php echo $skinInfo['color']; ?>;">
                    <i class="fas fa-prescription-bottle" style="color: <?php echo $skinInfo['color']; ?>; margin-left: 8px;"></i>
                    العلاج الموصى به / Recommended Treatment (Rx)
                </h3>
                
                <div style="background: #eff6ff; border-right: 4px solid #3b82f6; padding: 12px; margin-bottom: 15px; border-radius: 4px;">
                    <strong>⚕️ تعليمات الاستخدام:</strong> يُنصح باستخدام المنتجات التالية حسب الترتيب المذكور للحصول على أفضل النتائج
                </div>
                
                <table class="medical-table">
                    <thead>
                        <tr>
                            <th style="width: 5%; text-align: center;">#</th>
                            <th style="width: 35%;">اسم المنتج / Product Name</th>
                            <th style="width: 40%;">الفائدة والاستخدام / Benefits & Usage</th>
                            <th style="width: 20%; text-align: center;">السعر / Price</th>
                        </tr>
                    </thead>
                    <tbody>
                
                        <?php foreach (array_slice($recommendedProducts, 0, 6) as $index => $product): ?>
                        <tr>
                            <td style="text-align: center; font-weight: bold; font-size: 16px; color: <?php echo $skinInfo['color']; ?>;">
                                <?php echo $index + 1; ?>
                            </td>
                            <td>
                                <strong style="font-size: 14px;"><?php echo htmlspecialchars($product['name']); ?></strong>
                            </td>
                            <td style="line-height: 1.6;">
                                <?php if (isset($product['reason'])): ?>
                                <div style="margin-bottom: 8px;">
                                    ✓ <strong>الفائدة:</strong> <?php echo htmlspecialchars($product['reason']); ?>
                                </div>
                                <?php endif; ?>
                                <div style="font-size: 11px; color: #666;">
                                    ⏰ <strong>الاستخدام:</strong> مرتين يومياً (صباحاً ومساءً)
                                </div>
                            </td>
                            <td style="text-align: center;">
                                <?php if ($product['discount_price']): ?>
                                <div style="text-decoration: line-through; color: #999; font-size: 11px;">
                                    <?php echo number_format($product['price'], 0); ?> ج.م
                                </div>
                                <div style="font-weight: bold; font-size: 16px; color: <?php echo $skinInfo['color']; ?>;">
                                    <?php echo number_format($product['discount_price'], 0); ?> ج.م
                                </div>
                                <?php else: ?>
                                <div style="font-weight: bold; font-size: 16px; color: <?php echo $skinInfo['color']; ?>;">
                                    <?php echo number_format($product['price'], 0); ?> ج.م
                                </div>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
                
                <div style="background: #fefce8; border-right: 4px solid #eab308; padding: 12px; margin-top: 15px; border-radius: 4px; font-size: 13px;">
                    <strong>⚠️ تحذير هام:</strong> في حالة ظهور أي تحسس أو احمرار، يُرجى التوقف عن الاستخدام فوراً واستشارة طبيب الجلدية المختص.
                </div>
            </div>
            <?php endif; ?>
            
            <!-- Care Instructions Table -->
            <?php if (!empty($tips)): ?>
            <div class="mb-8">
                <h3 style="font-size: 18px; font-weight: 700; color: #1f2937; margin-bottom: 15px; padding-bottom: 8px; border-bottom: 3px solid <?php echo $skinInfo['color']; ?>;">
                    <i class="fas fa-lightbulb" style="color: <?php echo $skinInfo['color']; ?>; margin-left: 8px;"></i>
                    تعليمات العناية / Care Instructions
                </h3>
                
                <table class="medical-table">
                    <thead>
                        <tr>
                            <th style="width: 8%; text-align: center;">#</th>
                            <th style="width: 92%;">التعليمات / Instructions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach (array_slice($tips, 0, 8) as $index => $tip): ?>
                        <tr>
                            <td style="text-align: center; font-weight: bold; color: <?php echo $skinInfo['color']; ?>;">
                                <?php echo $index + 1; ?>
                            </td>
                            <td style="line-height: 1.8;">
                                <?php echo htmlspecialchars($tip); ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
            
            <!-- Admin Notes -->
            <div class="mb-8 no-print">
                <div class="section-title">
                    <i class="fas fa-sticky-note" style="color: <?php echo $skinInfo['color']; ?>"></i>
                    <span>ملاحظات الأدمن</span>
                </div>
                
                <form onsubmit="saveNotes(event, <?php echo $result['id']; ?>)">
                    <textarea id="adminNotes" name="notes" rows="4" class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent" placeholder="أضف ملاحظاتك هنا..."><?php echo htmlspecialchars($result['admin_notes'] ?? ''); ?></textarea>
                    <button type="submit" class="mt-3 bg-purple-600 text-white px-6 py-2 rounded-lg hover:bg-purple-700 transition">
                        <i class="fas fa-save"></i> حفظ الملاحظات
                    </button>
                </form>
            </div>
            
            <!-- Medical Stamp Area (Print Only) -->
            <div class="medical-stamp" style="display: none;">
                <div>
                    <div class="text-sm font-bold mb-2">توقيع الاستشاري:</div>
                    <div class="stamp-box">
                        <span>ختم وتوقيع</span>
                    </div>
                </div>
                
                <div>
                    <div class="text-sm font-bold mb-2">التاريخ:</div>
                    <div style="padding: 10px; border-bottom: 2px solid #999; min-width: 150px;">
                        <?php echo date('Y-m-d'); ?>
                    </div>
                </div>
            </div>
            
            <!-- Footer -->
            <div class="prescription-footer border-t pt-6 text-center text-sm text-gray-500">
                <div class="mb-4">
                    <strong style="font-size: 16px; color: #1f2937;">Roz Skin Beauty & Skincare Center</strong>
                </div>
                <div class="grid grid-cols-3 gap-4 text-xs mb-4">
                    <div>
                        <i class="fas fa-phone"></i> 01234567890
                    </div>
                    <div>
                        <i class="fas fa-envelope"></i> info@rozskin.com
                    </div>
                    <div>
                        <i class="fas fa-map-marker-alt"></i> القاهرة، مصر
                    </div>
                </div>
                <div class="text-xs text-gray-400">
                    <p>هذا التقرير تم إنشاؤه بواسطة نظام تحليل البشرة الذكي</p>
                    <p class="mt-1">للاستخدام الشخصي فقط - يُنصح باستشارة طبيب الجلدية للحالات الخاصة</p>
                </div>
            </div>
            
            <!-- Barcode Section -->
            <div class="barcode-section" style="margin-top: 30px; padding: 20px; background: #f9fafb; border: 2px dashed #ddd; border-radius: 8px; text-align: center;">
                <div style="margin-bottom: 10px;">
                    <svg id="barcode"></svg>
                </div>
                <div style="font-size: 12px; color: #666; margin-top: 10px;">
                    <strong>رقم التقرير:</strong> <?php echo str_pad($result['id'], 6, '0', STR_PAD_LEFT); ?> | 
                    <strong>التاريخ:</strong> <?php echo date('Y-m-d', strtotime($result['created_at'])); ?>
                </div>
                <div style="font-size: 11px; color: #999; margin-top: 5px;">
                    للتحقق من صحة التقرير: rozskin.com/verify
                </div>
            </div>
            
            <script>
                // Generate barcode
                JsBarcode("#barcode", "<?php echo str_pad($result['id'], 6, '0', STR_PAD_LEFT); ?>", {
                    format: "CODE128",
                    width: 2,
                    height: 60,
                    displayValue: true,
                    fontSize: 14,
                    margin: 10
                });
            </script>
            
            <!-- Decorative Watermark (Print Only) -->
            <div class="watermark" style="display: none;">
                <svg width="400" height="400" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg">
                    <text x="200" y="200" 
                          font-family="Tajawal, Arial" 
                          font-size="60" 
                          font-weight="800" 
                          fill="rgba(0,0,0,0.02)" 
                          text-anchor="middle" 
                          dominant-baseline="middle"
                          transform="rotate(-45 200 200)">
                        ROZ SKIN
                    </text>
                    <text x="200" y="250" 
                          font-family="Tajawal, Arial" 
                          font-size="20" 
                          fill="rgba(0,0,0,0.015)" 
                          text-anchor="middle" 
                          dominant-baseline="middle"
                          transform="rotate(-45 200 200)">
                        Beauty & Skincare
                    </text>
                </svg>
            </div>
            
            <style>
                @media print {
                    .verification-section {
                        display: block !important;
                    }
                    
                    .watermark {
                        display: block !important;
                        position: fixed;
                        top: 50%;
                        left: 50%;
                        transform: translate(-50%, -50%);
                        z-index: -1;
                        opacity: 1;
                    }
                }
            </style>
            
        </div>
    </div>
</div>

<style>
    @media print {
        .medical-stamp,
        .watermark {
            display: block !important;
        }
        
        .prescription-footer {
            background: #f9fafb;
            padding: 20px;
            margin: 0 -40px -40px -40px;
        }
    }
</style>

<script>
function markAsContacted(id) {
    if (!confirm('هل تم التواصل مع هذا العميل؟')) return;
    
    fetch('update-status.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ id: id, contacted: 1 })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('✅ تم التحديث بنجاح');
            location.reload();
        } else {
            alert('❌ ' + data.message);
        }
    });
}

function saveNotes(event, id) {
    event.preventDefault();
    const notes = document.getElementById('adminNotes').value;
    
    fetch('save-notes.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ id: id, notes: notes })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('✅ تم حفظ الملاحظات');
        } else {
            alert('❌ ' + data.message);
        }
    });
}

function downloadPDF() {
    alert('ميزة تحميل PDF ستكون متاحة قريباً');
    // يمكن إضافة مكتبة jsPDF أو html2pdf لاحقاً
}
</script>

</body>
</html>
