@php
    $locale = app()->getLocale();
    $customer = $profile['customer'];
    $rental_summary = $profile['rental_summary'];
    $payment_summary = $profile['payment_summary'];
    $status_info = $profile['status_info'];
    $rating_info = $profile['rating_info'];
@endphp

<!DOCTYPE html>
<html dir="{{ $locale == 'ar' ? 'rtl' : 'ltr' }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ $customer->name }} - {{ $locale == 'ar' ? 'ملف العميل' : 'Customer Profile' }}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: Arial, sans-serif; background-color: #f5f5f5; color: #333; }
        .container { max-width: 900px; margin: 0 auto; background: white; padding: 40px; }
        .header { text-align: center; border-bottom: 3px solid #d4af37; padding-bottom: 30px; margin-bottom: 30px; }
        .header h1 { font-size: 32px; color: #111; margin-bottom: 10px; }
        .header-info { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 20px; margin-top: 20px; font-size: 13px; }
        .header-info div { padding: 10px; background: #f9f9f9; border-left: 4px solid #d4af37; }
        
        .section { margin-bottom: 40px; }
        .section h2 { font-size: 20px; color: #111; border-bottom: 2px solid #d4af37; padding-bottom: 10px; margin-bottom: 20px; }
        
        .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
        .stat-box { padding: 20px; background: #f9f9f9; border: 1px solid #ddd; border-left: 4px solid #d4af37; }
        .stat-box .label { font-size: 12px; color: #999; text-transform: uppercase; margin-bottom: 8px; }
        .stat-box .value { font-size: 28px; font-weight: bold; color: #111; }
        .stat-box .unit { font-size: 12px; color: #999; }
        
        table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        table th { background-color: #111; color: white; padding: 12px; text-align: {{ $locale == 'ar' ? 'right' : 'left' }}; font-size: 12px; font-weight: bold; }
        table td { padding: 12px; border-bottom: 1px solid #ddd; }
        table tr:nth-child(even) { background-color: #f9f9f9; }
        
        .status-badge { display: inline-block; padding: 6px 12px; border-radius: 4px; font-size: 11px; font-weight: bold; }
        .status-active { background-color: #d4f1d4; color: #2d5d2d; }
        .status-completed { background-color: #d4f1d4; color: #2d5d2d; }
        .status-pending { background-color: #fff3cd; color: #856404; }
        .status-critical { background-color: #f8d7da; color: #721c24; }
        .status-overdue { background-color: #f8d7da; color: #721c24; }
        
        .alert { padding: 15px; margin: 15px 0; border-radius: 4px; border-left: 4px solid #dc3545; background-color: #f8d7da; color: #721c24; }
        .alert-info { border-left-color: #0c5460; background-color: #d1ecf1; color: #0c5460; }
        .alert-success { border-left-color: #155724; background-color: #d4edda; color: #155724; }
        
        .footer { margin-top: 50px; padding-top: 20px; border-top: 1px solid #ddd; text-align: center; color: #999; font-size: 11px; }
        .print-date { text-align: right; color: #999; font-size: 11px; margin-bottom: 20px; }
        
        @media print {
            body { background: white; }
            .container { padding: 20px; }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Print Date -->
        <div class="print-date">
            {{ now()->format('Y-m-d H:i:s') }}
        </div>

        <!-- Header -->
        <div class="header">
            <h1>📋 {{ $locale == 'ar' ? 'ملف العميل الشامل' : 'Complete Customer Profile' }}</h1>
            <h2 style="font-size: 24px; color: #666; margin-top: 10px;">{{ $customer->name }}</h2>
            <div class="header-info">
                <div>
                    <strong>{{ $locale == 'ar' ? 'الهاتف:' : 'Phone:' }}</strong><br>{{ $customer->phone }}
                </div>
                <div>
                    <strong>{{ $locale == 'ar' ? 'البريد الإلكتروني:' : 'Email:' }}</strong><br>{{ $customer->email ?? '—' }}
                </div>
                <div>
                    <strong>{{ $locale == 'ar' ? 'تاريخ التسجيل:' : 'Registration:' }}</strong><br>{{ $customer->created_at->format('Y-m-d') }}
                </div>
            </div>
        </div>

        <!-- Status Section -->
        <div class="section">
            <h2>{{ $locale == 'ar' ? '📊 الحالة الحالية' : '📊 Current Status' }}</h2>
            <div class="alert {{ strpos($status_info['status'], 'active') !== false ? 'alert-success' : (strpos($status_info['status'], 'critical') !== false ? '' : 'alert-info') }}">
                <strong>{{ $status_info['label'] }}</strong>
                @if($status_info['status'] === 'blacklisted' && isset($status_info['reason']))
                    <br><br>{{ $locale == 'ar' ? 'سبب الحظر:' : 'Blacklist Reason:' }} {{ $status_info['reason'] }}
                    @if($status_info['until'])
                        <br>{{ $locale == 'ar' ? 'حتى:' : 'Until:' }} {{ $status_info['until']->format('Y-m-d') }}
                    @endif
                @endif
            </div>
        </div>

        <!-- Payment Summary -->
        <div class="section">
            <h2>💰 {{ $locale == 'ar' ? 'ملخص الدفع' : 'Payment Summary' }}</h2>
            <div class="grid-2">
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'إجمالي المبلغ' : 'Total Amount' }}</div>
                    <div class="value">{{ number_format($payment_summary['total_amount'], 0) }}</div>
                    <div class="unit">{{ $locale == 'ar' ? 'ر.س' : 'SAR' }}</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'المدفوع' : 'Paid' }}</div>
                    <div class="value" style="color: #2d5d2d;">{{ number_format($payment_summary['total_paid'], 0) }}</div>
                    <div class="unit">{{ $payment_summary['payment_percentage'] }}%</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'المتبقي' : 'Remaining' }}</div>
                    <div class="value" style="color: #d4a500;">{{ number_format($payment_summary['total_remaining'], 0) }}</div>
                    <div class="unit">{{ $locale == 'ar' ? 'ر.س' : 'SAR' }}</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'المبلغ المتأخر' : 'Outstanding' }}</div>
                    <div class="value" style="color: #721c24;">{{ number_format($payment_summary['overdue_amount'], 0) }}</div>
                    <div class="unit">{{ $locale == 'ar' ? 'ر.س' : 'SAR' }}</div>
                </div>
            </div>

            @if($payment_summary['total_delays'] > 0)
                <div class="alert" style="margin-top: 20px;">
                    <strong>{{ $locale == 'ar' ? '⚠️ تأخيرات قائمة' : '⚠️ Pending Delays' }}</strong><br>
                    {{ $locale == 'ar' ? 'عدد التأخيرات:' : 'Number of Delays:' }} {{ $payment_summary['pending_delays'] }} |
                    {{ $locale == 'ar' ? 'المبلغ:' : 'Amount:' }} {{ number_format($payment_summary['total_delay_amount'], 0) }} {{ $locale == 'ar' ? 'ر.س' : 'SAR' }}
                </div>
            @endif
        </div>

        <!-- Rental Summary -->
        <div class="section">
            <h2>📅 {{ $locale == 'ar' ? 'ملخص العقود' : 'Rental Summary' }}</h2>
            <div class="grid-2">
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'إجمالي العقود' : 'Total Contracts' }}</div>
                    <div class="value">{{ $rental_summary['total_contracts'] }}</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'العقود النشطة' : 'Active Contracts' }}</div>
                    <div class="value" style="color: #0c5460;">{{ $rental_summary['active_contracts'] }}</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'العقود المنتهية' : 'Completed' }}</div>
                    <div class="value" style="color: #2d5d2d;">{{ $rental_summary['completed_contracts'] }}</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'إجمالي أيام الإيجار' : 'Total Rental Days' }}</div>
                    <div class="value">{{ $rental_summary['total_rental_days'] }}</div>
                </div>
            </div>
        </div>

        <!-- Rating & Risk -->
        <div class="section">
            <h2>⭐ {{ $locale == 'ar' ? 'التقييم والمخاطر' : 'Rating & Risk Assessment' }}</h2>
            <div class="grid-2">
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'التقييم' : 'Rating' }}</div>
                    <div class="value" style="color: #d4a500;">{{ $rating_info['average_rating'] }}</div>
                    <div class="unit">{{ $locale == 'ar' ? 'من 5 نقاط' : 'out of 5 stars' }}</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'مستوى الخطر' : 'Risk Level' }}</div>
                    <div class="value" style="color: #721c24;">{{ strtoupper($rating_info['risk_level']) }}</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'عدد المخالفات' : 'Violations' }}</div>
                    <div class="value">{{ $rating_info['violation_count'] }}</div>
                </div>
                <div class="stat-box">
                    <div class="label">{{ $locale == 'ar' ? 'تأخيرات السداد' : 'Late Payments' }}</div>
                    <div class="value">{{ $rating_info['late_payment_count'] }}</div>
                </div>
            </div>
        </div>

        <!-- Contract History -->
        @if($contracts->count() > 0)
        <div class="section">
            <h2>📋 {{ $locale == 'ar' ? 'سجل العقود' : 'Contract History' }}</h2>
            <table>
                <thead>
                    <tr>
                        <th>{{ $locale == 'ar' ? 'رقم العقد' : 'Contract #' }}</th>
                        <th>{{ $locale == 'ar' ? 'السيارة' : 'Car' }}</th>
                        <th>{{ $locale == 'ar' ? 'التاريخ' : 'Date' }}</th>
                        <th>{{ $locale == 'ar' ? 'المبلغ' : 'Amount' }}</th>
                        <th>{{ $locale == 'ar' ? 'المدفوع' : 'Paid' }}</th>
                        <th>{{ $locale == 'ar' ? 'الحالة' : 'Status' }}</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($contracts as $contract)
                    <tr>
                        <td>#{{ $contract['contract_number'] }}</td>
                        <td>{{ $contract['car'] }}</td>
                        <td>{{ $contract['start_date']->format('Y-m-d') }} to {{ $contract['end_date']->format('Y-m-d') }}</td>
                        <td>{{ number_format($contract['total_amount'], 0) }}</td>
                        <td>{{ number_format($contract['paid_amount'], 0) }}</td>
                        <td>
                            <span class="status-badge {{ $contract['status'] === 'active' ? 'status-active' : ($contract['status'] === 'completed' ? 'status-completed' : 'status-pending') }}">
                                {{ $contract['status'] }}
                            </span>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @endif

        <!-- Footer -->
        <div class="footer">
            <p>{{ $locale == 'ar' ? 'تم إنشاء هذا التقرير بواسطة نظام إدارة العملاء' : 'Generated by Customer Management System' }}</p>
            <p>{{ now()->format('Y-m-d H:i:s') }}</p>
        </div>
    </div>
</body>
</html>
