<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Laporan SKM - {{ $opd->name }}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 12px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
            border-bottom: 2px solid #333;
            padding-bottom: 20px;
        }
        .header h1 {
            margin: 0;
            font-size: 24px;
        }
        .header h2 {
            margin: 10px 0;
            font-size: 18px;
            color: #666;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #0d6efd;
            color: white;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        .summary {
            margin-top: 30px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
        }
        .footer {
            margin-top: 30px;
            text-align: center;
            font-size: 10px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>LAPORAN SURVEY KEPUASAN MASYARAKAT (SKM)</h1>
        <h2>{{ $opd->name }}</h2>
        <p>Periode: {{ now()->format('d F Y') }}</p>
        @if(isset($skmData))
        <div style="margin-top: 15px;">
            <p style="font-size: 16px; font-weight: bold;">
                Nilai SKM: {{ $skmData['skm_value'] }} | 
                Kualitas Pelayanan: 
                @if($skmData['service_quality'] == 'A')
                    A (Sangat Baik)
                @elseif($skmData['service_quality'] == 'B')
                    B (Baik)
                @elseif($skmData['service_quality'] == 'C')
                    C (Kurang Baik)
                @else
                    D (Tidak Baik)
                @endif
            </p>
        </div>
        @endif
    </div>

    <table>
        <thead>
            <tr>
                <th>No</th>
                <th>Kode</th>
                <th>Elemen</th>
                <th>Pertanyaan</th>
                <th>Total Response</th>
                <th>Rata-rata Score</th>
                <th>Rata-rata Weighted</th>
                <th>Distribusi Jawaban</th>
            </tr>
        </thead>
        <tbody>
            @foreach($results as $index => $result)
                <tr>
                    <td>{{ $index + 1 }}</td>
                    <td>{{ $result['question']->code }}</td>
                    <td>{{ $result['question']->element }}</td>
                    <td>{{ $result['question']->question }}</td>
                    <td>{{ $result['stats']['total'] }}</td>
                    <td>{{ number_format($result['stats']['average_score'], 2) }}</td>
                    <td>{{ number_format($result['stats']['average_weighted_score'], 2) }}</td>
                    <td>
                        @foreach($result['distribution'] as $dist)
                            {{ $dist['option']->label }}: {{ $dist['count'] }};
                        @endforeach
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>

    @if(isset($elementStats) && count($elementStats) > 0)
    <table style="margin-top: 20px;">
        <thead>
            <tr>
                <th>Elemen</th>
                <th>Rata-rata Weighted Score</th>
                <th>Total Response</th>
                <th>Jumlah Pertanyaan</th>
            </tr>
        </thead>
        <tbody>
            @foreach($elementStats as $element => $stats)
                <tr>
                    <td><strong>{{ $element }}</strong></td>
                    <td>{{ number_format($stats['average'], 2) }}</td>
                    <td>{{ $stats['total_responses'] }}</td>
                    <td>{{ $stats['question_count'] }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
    @endif

    <div class="summary">
        <h3>Ringkasan</h3>
        <p><strong>Total Pertanyaan:</strong> {{ count($results) }}</p>
        @if(isset($skmData))
        <p><strong>Total Response:</strong> {{ $skmData['total_responses'] }}</p>
        <p><strong>Nilai SKM:</strong> {{ $skmData['skm_value'] }}</p>
        <p><strong>Kualitas Pelayanan:</strong> 
            @if($skmData['service_quality'] == 'A')
                A (Sangat Baik)
            @elseif($skmData['service_quality'] == 'B')
                B (Baik)
            @elseif($skmData['service_quality'] == 'C')
                C (Kurang Baik)
            @else
                D (Tidak Baik)
            @endif
        </p>
        @endif
    </div>

    <div class="footer">
        <p>Dicetak pada: {{ now()->format('d F Y H:i:s') }}</p>
        <p>SKM Survey System</p>
    </div>
</body>
</html>

