<div>
    <div class="print-header">
        <h1 style="margin: 0; font-size: 24px; color: #333;">Vehicle Inspection Report</h1>
        <p style="margin: 5px 0 0 0; color: #666; font-size: 14px;">
            {{ app(\App\Settings\GeneralSettings::class)->shop_name ?? 'Car Repairs Shop' }}
        </p>
        <p style="margin: 0; color: #666; font-size: 12px;">
            {{ app(\App\Settings\GeneralSettings::class)->shop_address ?? '' }}
        </p>
        <p style="margin: 0; color: #666; font-size: 12px;">
            Phone: {{ app(\App\Settings\GeneralSettings::class)->shop_phone ?? '' }} | 
            Email: {{ app(\App\Settings\GeneralSettings::class)->shop_email ?? '' }}
        </p>
    </div>

    <!-- Inspection Summary -->
    @php
        $checklist = $inspection->inspection_checklist ?? [];
        $totalItems = 0;
        $passedItems = 0;
        $failedItems = 0;
        
        foreach($checklist as $section => $items) {
            if(is_array($items)) {
                foreach($items as $key => $value) {
                    $totalItems++;
                    $lowerValue = strtolower($value ?? '');
                    if(in_array($lowerValue, ['pass', 'good', 'excellent', 'satisfactory', 'yes'])) {
                        $passedItems++;
                    } elseif(in_array($lowerValue, ['fail', 'poor', 'unsatisfactory', 'no', 'damaged'])) {
                        $failedItems++;
                    }
                }
            }
        }
        
        $passRate = $totalItems > 0 ? round(($passedItems / $totalItems) * 100) : 0;
    @endphp
    
    @if($totalItems > 0)
    <div style="background: #f8f9fa; border: 1px solid #dee2e6; padding: 15px; margin-bottom: 25px; border-radius: 5px;">
        <h3 style="margin: 0 0 10px 0; font-size: 16px; color: #333;">Inspection Summary</h3>
        <div style="display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 15px; text-align: center;">
            <div>
                <strong style="font-size: 18px; color: #333;">{{ $totalItems }}</strong><br>
                <span style="font-size: 11px; color: #666;">Total Items</span>
            </div>
            <div>
                <strong style="font-size: 18px; color: #28a745;">{{ $passedItems }}</strong><br>
                <span style="font-size: 11px; color: #666;">Passed</span>
            </div>
            <div>
                <strong style="font-size: 18px; color: #dc3545;">{{ $failedItems }}</strong><br>
                <span style="font-size: 11px; color: #666;">Failed</span>
            </div>
            <div>
                <strong style="font-size: 18px; color: {{ $passRate >= 80 ? '#28a745' : ($passRate >= 60 ? '#ffc107' : '#dc3545') }};">{{ $passRate }}%</strong><br>
                <span style="font-size: 11px; color: #666;">Pass Rate</span>
            </div>
        </div>
    </div>
    @endif

    <div class="info-grid">
        <!-- Job Card Information -->
        <div class="info-section">
            <h3>Job Card Information</h3>
            <div class="info-row">
                <span class="label">Job Card No:</span>
                <span class="value">{{ $jobCard->job_card_number }}</span>
            </div>
            <div class="info-row">
                <span class="label">Date Created:</span>
                <span class="value">{{ $jobCard->created_at->format('d M Y') }}</span>
            </div>
            <div class="info-row">
                <span class="label">Status:</span>
                <span class="value">{{ ucwords(str_replace('_', ' ', $jobCard->status)) }}</span>
            </div>
            <div class="info-row">
                <span class="label">Branch:</span>
                <span class="value">{{ $jobCard->branch->name ?? 'N/A' }}</span>
            </div>
        </div>

        <!-- Customer Information -->
        <div class="info-section">
            <h3>Customer Information</h3>
            <div class="info-row">
                <span class="label">Name:</span>
                <span class="value">{{ $jobCard->customer->name }}</span>
            </div>
            <div class="info-row">
                <span class="label">Email:</span>
                <span class="value">{{ $jobCard->customer->email }}</span>
            </div>
            <div class="info-row">
                <span class="label">Phone:</span>
                <span class="value">{{ $jobCard->customer->phone }}</span>
            </div>
            <div class="info-row">
                <span class="label">Address:</span>
                <span class="value">{{ $jobCard->customer->address ?: 'N/A' }}</span>
            </div>
        </div>

        <!-- Vehicle Information -->
        <div class="info-section">
            <h3>Vehicle Information</h3>
            <div class="info-row">
                <span class="label">Make & Model:</span>
                <span class="value">{{ $jobCard->vehicle->make }} {{ $jobCard->vehicle->model }}</span>
            </div>
            <div class="info-row">
                <span class="label">Year:</span>
                <span class="value">{{ $jobCard->vehicle->year }}</span>
            </div>
            <div class="info-row">
                <span class="label">License Plate:</span>
                <span class="value">{{ $jobCard->vehicle->license_plate }}</span>
            </div>
            <div class="info-row">
                <span class="label">VIN:</span>
                <span class="value">{{ $jobCard->vehicle->vin ?: 'N/A' }}</span>
            </div>
        </div>
    </div>

    <!-- Inspection Details -->
    <div class="info-grid" style="grid-template-columns: 1fr 1fr;">
        <div class="info-section">
            <h3>Inspection Details</h3>
            <div class="info-row">
                <span class="label">Type:</span>
                <span class="value">{{ ucwords(str_replace('_', ' ', $inspection->inspection_type)) }}</span>
            </div>
            <div class="info-row">
                <span class="label">Date:</span>
                <span class="value">{{ $inspection->created_at->format('d M Y H:i') }}</span>
            </div>
            <div class="info-row">
                <span class="label">Inspector:</span>
                <span class="value">{{ $inspection->inspector->name ?? 'N/A' }}</span>
            </div>
            <div class="info-row">
                <span class="label">Current Mileage:</span>
                <span class="value">{{ number_format($inspection->current_mileage) }} km</span>
            </div>
        </div>

        <div class="info-section">
            <h3>Vehicle Condition</h3>
            <div class="info-row">
                <span class="label">Fuel Level:</span>
                <span class="value">{{ $inspection->fuel_level }}%</span>
            </div>
            <div class="info-row">
                <span class="label">Overall Condition:</span>
                <span class="value">{{ ucfirst($inspection->overall_condition ?? 'Good') }}</span>
            </div>
            <div class="info-row">
                <span class="label">Cleanliness:</span>
                <span class="value">{{ ucfirst($inspection->cleanliness_rating ?? 'Clean') }}</span>
            </div>
            @if($inspection->quality_rating)
            <div class="info-row">
                <span class="label">Quality Rating:</span>
                <span class="value">{{ $inspection->quality_rating }}/10</span>
            </div>
            @endif
        </div>
    </div>

    <!-- Inspection Checklist -->
    <div style="margin-bottom: 30px;">
        <h3 style="margin-bottom: 20px; font-size: 18px; color: #333; border-bottom: 2px solid #333; padding-bottom: 10px;">
            Inspection Checklist
        </h3>
        
        @php
            $checklist = $inspection->inspection_checklist ?? [];
        @endphp
        
        <div class="checklist-grid">
            <!-- Documentation Section -->
            @if(isset($checklist['documentation']))
            <div class="checklist-section">
                <h4>📋 Documentation</h4>
                @foreach($checklist['documentation'] as $key => $value)
                <div class="checklist-item">
                    <span>{{ ucwords(str_replace('_', ' ', $key)) }}</span>
                    <span class="status-badge status-{{ strtolower($value ?: 'na') }}">{{ strtoupper($value ?: 'N/A') }}</span>
                </div>
                @endforeach
            </div>
            @endif

            <!-- Exterior Section -->
            @if(isset($checklist['exterior']))
            <div class="checklist-section">
                <h4>🚗 Exterior</h4>
                @foreach($checklist['exterior'] as $key => $value)
                <div class="checklist-item">
                    <span>{{ ucwords(str_replace('_', ' ', $key)) }}</span>
                    <span class="status-badge status-{{ strtolower($value ?: 'na') }}">{{ strtoupper($value ?: 'N/A') }}</span>
                </div>
                @endforeach
            </div>
            @endif

            <!-- Interior Section -->
            @if(isset($checklist['interior']))
            <div class="checklist-section">
                <h4>🏠 Interior</h4>
                @foreach($checklist['interior'] as $key => $value)
                <div class="checklist-item">
                    <span>{{ ucwords(str_replace('_', ' ', $key)) }}</span>
                    <span class="status-badge status-{{ strtolower($value ?: 'na') }}">{{ strtoupper($value ?: 'N/A') }}</span>
                </div>
                @endforeach
            </div>
            @endif

            <!-- Engine Section -->
            @if(isset($checklist['engine']))
            <div class="checklist-section">
                <h4>� Engine & Mechanical</h4>
                @foreach($checklist['engine'] as $key => $value)
                <div class="checklist-item">
                    <span>{{ ucwords(str_replace('_', ' ', $key)) }}</span>
                    <span class="status-badge status-{{ strtolower($value ?: 'na') }}">{{ strtoupper($value ?: 'N/A') }}</span>
                </div>
                @endforeach
            </div>
            @endif

            <!-- Under Hood Section -->
            @if(isset($checklist['under_hood']))
            <div class="checklist-section">
                <h4>⚡ Under Hood</h4>
                @foreach($checklist['under_hood'] as $key => $value)
                <div class="checklist-item">
                    <span>{{ ucwords(str_replace('_', ' ', $key)) }}</span>
                    <span class="status-badge status-{{ strtolower($value ?: 'na') }}">{{ strtoupper($value ?: 'N/A') }}</span>
                </div>
                @endforeach
            </div>
            @endif

            <!-- Under Vehicle Section -->
            @if(isset($checklist['under_vehicle']))
            <div class="checklist-section">
                <h4>🔍 Under Vehicle</h4>
                @foreach($checklist['under_vehicle'] as $key => $value)
                <div class="checklist-item">
                    <span>{{ ucwords(str_replace('_', ' ', $key)) }}</span>
                    <span class="status-badge status-{{ strtolower($value ?: 'na') }}">{{ strtoupper($value ?: 'N/A') }}</span>
                </div>
                @endforeach
            </div>
            @endif
        </div>
    </div>

    <!-- Damage Report -->
    @if($inspection->damage_diagram_data)
    <div class="damage-report page-break">
        <h3 style="margin-bottom: 20px; font-size: 18px; color: #333; border-bottom: 2px solid #333; padding-bottom: 10px;">
            Vehicle Damage Report
        </h3>
        
        @php
            $damageData = is_string($inspection->damage_diagram_data) 
                ? json_decode($inspection->damage_diagram_data, true) 
                : $inspection->damage_diagram_data;
        @endphp
        
        @if($damageData && is_array($damageData) && count($damageData) > 0)
        <div class="damage-grid">
            @foreach($damageData as $index => $damage)
            <div class="damage-item">
                <strong>Damage {{ $index + 1 }}</strong><br>
                <span style="font-size: 11px; color: #666;">
                    Position: {{ $damage['x'] ?? 'N/A' }}, {{ $damage['y'] ?? 'N/A' }}
                </span><br>
                <span style="font-size: 11px; color: #888;">
                    Type: {{ $damage['type'] ?? 'Unknown' }}
                </span>
            </div>
            @endforeach
        </div>
        @else
        <p style="color: #666; font-style: italic;">No damage recorded during inspection.</p>
        @endif
    </div>
    @endif

    <!-- Signature Section -->
    <div style="margin-top: 50px; border-top: 2px solid #ddd; padding-top: 30px;">
        <div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 40px;">
            <div style="text-align: center;">
                <div style="border-bottom: 1px solid #333; margin-bottom: 10px; height: 40px;"></div>
                <strong>Inspector Signature</strong><br>
                <span style="font-size: 11px; color: #666;">{{ $inspection->inspector->name ?? 'N/A' }}</span><br>
                <span style="font-size: 11px; color: #666;">Date: {{ $inspection->created_at->format('d/m/Y') }}</span>
            </div>
            <div style="text-align: center;">
                <div style="border-bottom: 1px solid #333; margin-bottom: 10px; height: 40px;"></div>
                <strong>Customer Signature</strong><br>
                <span style="font-size: 11px; color: #666;">{{ $jobCard->customer->name }}</span><br>
                <span style="font-size: 11px; color: #666;">Date: ________________</span>
            </div>
            <div style="text-align: center;">
                <div style="border-bottom: 1px solid #333; margin-bottom: 10px; height: 40px;"></div>
                <strong>Service Manager</strong><br>
                <span style="font-size: 11px; color: #666;">Name: ________________</span><br>
                <span style="font-size: 11px; color: #666;">Date: ________________</span>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <div style="margin-top: 40px; text-align: center; border-top: 1px solid #ddd; padding-top: 20px; color: #666; font-size: 10px;">
        <p style="margin: 5px 0;">This report was generated on {{ now()->format('d M Y \a\t H:i') }}</p>
        <p style="margin: 5px 0;">{{ app(\App\Settings\GeneralSettings::class)->shop_name ?? 'Car Repairs Shop' }} - Professional Vehicle Inspection Services</p>
        <p style="margin: 5px 0;">Report ID: {{ $inspection->id }} | Job Card: {{ $jobCard->job_card_number }}</p>
    </div>
</div>
