<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laporan Pencabutan Akses {{ $startDate->format('d M Y') }} - {{ $endDate->format('d M Y') }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            font-size: 11px;
            line-height: 1.4;
            color: #1f2937;
            background: white;
        }
        
        .container {
            max-width: 100%;
            padding: 20px;
        }
        
        /* Header */
        .header {
            text-align: center;
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 3px solid #ef4444;
        }
        
        .header h1 {
            font-size: 22px;
            font-weight: bold;
            color: #1f2937;
            margin-bottom: 5px;
        }
        
        .header p {
            font-size: 13px;
            color: #6b7280;
        }
        
        .header .meta {
            margin-top: 10px;
            font-size: 10px;
            color: #9ca3af;
        }
        
        /* Summary Cards */
        .summary-section {
            margin-bottom: 25px;
        }
        
        .summary-grid {
            display: table;
            width: 100%;
            border-collapse: separate;
            border-spacing: 8px;
            margin-bottom: 15px;
        }
        
        .summary-card {
            display: table-cell;
            width: 25%;
            padding: 15px;
            background: #fef2f2;
            border: 1px solid #fecaca;
            border-radius: 8px;
            text-align: center;
            vertical-align: middle;
        }
        
        .summary-card.total {
            background: #fee2e2;
            border-color: #ef4444;
        }
        
        .summary-card.automatic {
            background: #dbeafe;
            border-color: #60a5fa;
        }
        
        .summary-card.manual {
            background: #fef3c7;
            border-color: #fbbf24;
        }
        
        .summary-card h3 {
            font-size: 28px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .summary-card.total h3 { color: #dc2626; }
        .summary-card.automatic h3 { color: #2563eb; }
        .summary-card.manual h3 { color: #d97706; }
        
        .summary-card p {
            font-size: 10px;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .summary-card .sub {
            font-size: 9px;
            color: #9ca3af;
            margin-top: 3px;
        }
        
        /* Period Info */
        .period-section {
            margin-bottom: 20px;
            padding: 12px 15px;
            background: #f9fafb;
            border-radius: 8px;
            border-left: 4px solid #ef4444;
        }
        
        .period-title {
            font-size: 11px;
            font-weight: 600;
            color: #374151;
            margin-bottom: 5px;
        }
        
        .period-value {
            font-size: 13px;
            color: #1f2937;
        }
        
        /* Section Title */
        .section-title {
            font-size: 14px;
            font-weight: bold;
            color: #1f2937;
            margin-bottom: 12px;
            padding-bottom: 6px;
            border-bottom: 2px solid #e5e7eb;
        }
        
        /* Table Styles */
        .table-section {
            margin-top: 25px;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 10px;
        }
        
        thead {
            background: #fef2f2;
        }
        
        th {
            padding: 12px 8px;
            text-align: left;
            font-weight: 600;
            color: #991b1b;
            text-transform: uppercase;
            font-size: 9px;
            letter-spacing: 0.5px;
            border-bottom: 2px solid #ef4444;
        }
        
        td {
            padding: 10px 8px;
            border-bottom: 1px solid #e5e7eb;
            vertical-align: middle;
        }
        
        tr:nth-child(even) {
            background: #fafafa;
        }
        
        /* Method Badges */
        .badge {
            display: inline-block;
            padding: 4px 10px;
            border-radius: 20px;
            font-size: 9px;
            font-weight: 600;
            text-transform: capitalize;
        }
        
        .badge-automatic {
            background: #dbeafe;
            color: #1e40af;
        }
        
        .badge-manual {
            background: #fef3c7;
            color: #92400e;
        }
        
        /* Employee Info */
        .employee-info {
            display: flex;
            align-items: center;
        }
        
        .avatar {
            width: 28px;
            height: 28px;
            border-radius: 50%;
            background: linear-gradient(135deg, #ef4444, #dc2626);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 10px;
            font-weight: bold;
            margin-right: 10px;
            flex-shrink: 0;
        }
        
        .employee-name {
            font-weight: 600;
            color: #1f2937;
        }
        
        .employee-nip {
            font-size: 9px;
            color: #6b7280;
        }
        
        /* Access Info */
        .access-name {
            font-weight: 500;
            color: #1f2937;
        }
        
        .access-id {
            font-size: 9px;
            color: #9ca3af;
        }
        
        /* Reason Box */
        .reason-box {
            max-width: 200px;
            padding: 5px 8px;
            background: #f3f4f6;
            border-radius: 4px;
            font-size: 9px;
            color: #4b5563;
            line-height: 1.3;
        }
        
        .reason-empty {
            color: #9ca3af;
            font-style: italic;
        }
        
        /* Revoked By */
        .revoker {
            font-size: 10px;
            color: #374151;
        }
        
        .revoker-system {
            color: #6b7280;
            font-style: italic;
        }
        
        /* Date Time */
        .datetime {
            font-size: 10px;
            color: #1f2937;
        }
        
        .datetime-time {
            font-size: 8px;
            color: #9ca3af;
        }
        
        /* Empty State */
        .empty-state {
            text-align: center;
            padding: 40px;
            color: #9ca3af;
        }
        
        /* Footer */
        .footer {
            margin-top: 30px;
            padding-top: 15px;
            border-top: 1px solid #e5e7eb;
            text-align: center;
            font-size: 9px;
            color: #9ca3af;
        }
        
        /* Stats Box */
        .stats-box {
            margin-top: 20px;
            display: table;
            width: 100%;
            border-collapse: separate;
            border-spacing: 10px;
        }
        
        .stat-item {
            display: table-cell;
            width: 50%;
            padding: 15px;
            background: #f9fafb;
            border-radius: 8px;
            border: 1px solid #e5e7eb;
        }
        
        .stat-label {
            font-size: 10px;
            color: #6b7280;
            text-transform: uppercase;
            margin-bottom: 5px;
        }
        
        .stat-value {
            font-size: 16px;
            font-weight: bold;
            color: #1f2937;
        }
        
        /* Page Break */
        .page-break {
            page-break-before: always;
        }
        
        /* Print Optimizations */
        @media print {
            body {
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            
            .container {
                padding: 0;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Header -->
        <div class="header">
            <h1>LAPORAN PENCABUTAN AKSES</h1>
            <p>Monitoring dan Audit Pencabutan Hak Akses Pegawai</p>
            <div class="meta">
                Dicetak pada: {{ now()->format('d F Y H:i') }} | 
                Total Data: {{ $summary['total'] }} Log Pencabutan
            </div>
        </div>

        <!-- Period Info -->
        <div class="period-section">
            <div class="period-title">Periode Laporan</div>
            <div class="period-value">
                {{ $startDate->format('d F Y') }} s/d {{ $endDate->format('d F Y') }}
                <span style="color: #6b7280; font-size: 10px;">
                    ({{ $startDate->diffInDays($endDate) + 1 }} hari)
                </span>
            </div>
        </div>

        <!-- Summary Section -->
        <div class="summary-section">
            <h2 class="section-title">Ringkasan Pencabutan</h2>
            <div class="summary-grid">
                <div class="summary-card total">
                    <h3>{{ $summary['total'] }}</h3>
                    <p>Total Pencabutan</p>
                </div>
                <div class="summary-card automatic">
                    <h3>{{ $summary['automatic'] }}</h3>
                    <p>Otomatis (System)</p>
                    @if($summary['total'] > 0)
                        <div class="sub">{{ round(($summary['automatic'] / $summary['total']) * 100) }}% dari total</div>
                    @endif
                </div>
                <div class="summary-card manual">
                    <h3>{{ $summary['manual'] }}</h3>
                    <p>Manual (Admin)</p>
                    @if($summary['total'] > 0)
                        <div class="sub">{{ round(($summary['manual'] / $summary['total']) * 100) }}% dari total</div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Table Section -->
        <div class="table-section page-break">
            <h2 class="section-title">Detail Log Pencabutan Akses</h2>
            
            @if(count($data) > 0)
                <table>
                    <thead>
                        <tr>
                            <th style="width: 4%">No</th>
                            <th style="width: 14%">Waktu Revoke</th>
                            <th style="width: 20%">Pegawai</th>
                            <th style="width: 18%">Akses Dicabut</th>
                            <th style="width: 10%">Method</th>
                            <th style="width: 18%">Alasan/Trigger</th>
                            <th style="width: 16%">Direvoke Oleh</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($data as $index => $log)
                            <tr>
                                <td style="text-align: center;">{{ $index + 1 }}</td>
                                <td>
                                    <div class="datetime">
                                        {{ $log->revoked_at->format('d M Y') }}
                                    </div>
                                    <div class="datetime-time">
                                        {{ $log->revoked_at->format('H:i:s') }}
                                    </div>
                                </td>
                                <td>
                                    <div class="employee-info">
                                        <div class="avatar">
                                            {{ strtoupper(substr($log->employeeAccessDetail->employee->name ?? 'UN', 0, 2)) }}
                                        </div>
                                        <div>
                                            <div class="employee-name">
                                                {{ $log->employeeAccessDetail->employee->name ?? 'Unknown' }}
                                            </div>
                                            <div class="employee-nip">
                                                NIP: {{ $log->employeeAccessDetail->employee->nip ?? '-' }}
                                            </div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div class="access-name">
                                        {{ $log->employeeAccessDetail->accessCatalog->name ?? 'Unknown Access' }}
                                    </div>
                                    <div class="access-id">
                                        ID Detail: {{ $log->employee_access_detail_id }}
                                    </div>
                                </td>
                                <td>
                                    @php
                                        $methodClass = $log->method === 'automatic' ? 'badge-automatic' : 'badge-manual';
                                    @endphp
                                    <span class="badge {{ $methodClass }}">
                                        {{ ucfirst($log->method) }}
                                    </span>
                                </td>
                                <td>
                                    @if($log->reason)
                                        <div class="reason-box">
                                            {{ $log->reason }}
                                        </div>
                                    @else
                                        <span class="reason-empty">
                                            {{ $log->method === 'automatic' ? 'Auto-revoke: Retirement/Expired' : 'No reason provided' }}
                                        </span>
                                    @endif
                                </td>
                                <td>
                                    @if($log->revoked_by && $log->revokedBy)
                                        <div class="revoker">
                                            {{ $log->revokedBy->name }}
                                        </div>
                                        <div class="datetime-time">
                                            Admin/User
                                        </div>
                                    @else
                                        <div class="revoker-system">
                                            System Automation
                                        </div>
                                        <div class="datetime-time">
                                            {{ $log->method === 'automatic' ? 'Scheduled Job' : 'Unknown' }}
                                        </div>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            @else
                <div class="empty-state">
                    <p>Tidak ada data pencabutan akses untuk periode ini</p>
                    <p style="font-size: 10px; margin-top: 5px;">
                        {{ $startDate->format('d M Y') }} - {{ $endDate->format('d M Y') }}
                    </p>
                </div>
            @endif
        </div>

        <!-- Additional Stats -->
        @if(count($data) > 0 && isset($summary['by_access']) && count($summary['by_access']) > 0)
        <div style="margin-top: 25px;">
            <h2 class="section-title">Statistik per Jenis Akses</h2>
            <div class="stats-box">
                @foreach($summary['by_access']->take(2) as $accessName => $count)
                <div class="stat-item">
                    <div class="stat-label">{{ $accessName }}</div>
                    <div class="stat-value">{{ $count }} pencabutan</div>
                </div>
                @endforeach
            </div>
            
            @if(count($summary['by_access']) > 2)
            <table style="margin-top: 15px; font-size: 10px;">
                <thead style="background: #f3f4f6;">
                    <tr>
                        <th style="padding: 8px; text-align: left;">Jenis Akses</th>
                        <th style="padding: 8px; text-align: center; width: 100px;">Jumlah</th>
                        <th style="padding: 8px; text-align: center; width: 100px;">Persentase</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($summary['by_access']->skip(2) as $accessName => $count)
                    <tr>
                        <td style="padding: 8px;">{{ $accessName }}</td>
                        <td style="padding: 8px; text-align: center;">{{ $count }}</td>
                        <td style="padding: 8px; text-align: center;">
                            {{ $summary['total'] > 0 ? round(($count / $summary['total']) * 100, 1) : 0 }}%
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
            @endif
        </div>
        @endif

        <!-- Footer -->
        <div class="footer">
            <p>Laporan ini digenerate secara otomatis oleh sistem | © {{ date('Y') }} - Dokumen Rahasia</p>
            <p style="margin-top: 5px; font-size: 8px;">
                Log ID Range: {{ $data->first()?->id ?? '-' }} - {{ $data->last()?->id ?? '-' }}
            </p>
        </div>
    </div>
</body>
</html>