<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cetak Daftar Instansi / Pelanggan</title>
    <style>
        @media print {
            @page {
                size: A4 portrait;
                margin: 15mm;
            }

            .no-print {
                display: none !important;
            }
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            font-size: 10px;
            line-height: 1.4;
            background: white;
            padding: 20px;
            color: #111827;
        }

        .header {
            text-align: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #333;
        }

        .header h1 {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .header h2 {
            font-size: 14px;
            font-weight: normal;
            color: #666;
            text-transform: uppercase;
        }

        .header p {
            font-size: 10px;
            color: #666;
            margin-bottom: 5px;
        }

        .header .period {
            font-size: 12px;
            margin-top: 10px;
        }

        .summary-section {
            display: flex;
            justify-content: space-around;
            margin-bottom: 20px;
            padding: 15px;
            background: #f9f9f9;
            border-radius: 5px;
        }

        .summary-box {
            text-align: center;
        }

        .summary-box .label {
            font-size: 10px;
            color: #666;
            text-transform: uppercase;
        }

        .summary-box .value {
            font-size: 16px;
            font-weight: bold;
            color: #333;
        }

        .detail-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        .detail-table th,
        .detail-table td {
            border: 1px solid #ddd;
            padding: 6px 8px;
            text-align: left;
            font-size: 9px;
            vertical-align: top;
        }

        .detail-table th {
            background: #f5f5f5;
            font-weight: bold;
            text-transform: uppercase;
        }

        .text-right {
            text-align: right;
        }

        .text-center {
            text-align: center;
        }

        .member-badge {
            display: inline-block;
            padding: 2px 6px;
            border-radius: 10px;
            font-size: 8px;
            font-weight: bold;
            background: #f3f4f6;
            color: #374151;
        }

        .member-studio {
            background: #dbeafe;
            color: #1d4ed8;
        }

        .member-umum {
            background: #f3f4f6;
            color: #374151;
        }

        .footer {
            margin-top: 30px;
            padding-top: 15px;
            border-top: 1px solid #ddd;
            display: flex;
            justify-content: space-between;
            font-size: 9px;
            color: #666;
        }

        .print-btn {
            background: #1a1a1a;
            color: white;
            padding: 10px 30px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            margin-top: 20px;
        }

        .print-btn:hover {
            background: #333;
        }

        .no-print {
            text-align: center;
        }

        @media screen {
            body {
                max-width: 210mm;
                margin: 20px auto;
                border: 1px solid #ccc;
                box-shadow: 0 0 10px rgba(0,0,0,0.1);
            }
        }
    </style>
</head>
<body>
    @php
        $totalCustomers = $customers->count();
        $totalUmum = $customers->where('member_type', 'umum')->count();
        $totalInstansi = $customers->where('member_type', 'studio')->count();

        $memberTypeLabel = 'Semua';
        if (request('member_type') === 'umum') {
            $memberTypeLabel = 'Umum';
        } elseif (request('member_type') === 'studio') {
            $memberTypeLabel = 'Instansi';
        }

        $shopName = $shopName ?? \App\Models\Setting::getShopName();
        $shopAddress = $shopAddress ?? \App\Models\Setting::getShopAddress();
        $shopLogo = $shopLogo ?? \App\Models\Setting::getShopLogo();
    @endphp

    <div class="header">
        @if($shopLogo ?? false)
            <img src="{{ $shopLogo }}" alt="{{ $shopName }}" style="max-height: 50px; margin-bottom: 10px;">
        @endif

        <h1>{{ $shopName }}</h1>

        @if($shopAddress)
            <p>{{ $shopAddress }}</p>
        @endif

        <h2>DAFTAR INSTANSI / PELANGGAN</h2>

        <div class="period">
            Pencarian: {{ request('search') ?: 'Semua' }} |
            Jenis: {{ $memberTypeLabel }}
        </div>
    </div>

    <div class="summary-section">
        <div class="summary-box">
            <div class="label">Total Data</div>
            <div class="value">{{ number_format($totalCustomers) }}</div>
        </div>

        <div class="summary-box">
            <div class="label">Umum</div>
            <div class="value">{{ number_format($totalUmum) }}</div>
        </div>

        <div class="summary-box">
            <div class="label">Instansi</div>
            <div class="value">{{ number_format($totalInstansi) }}</div>
        </div>
    </div>

    <table class="detail-table">
        <thead>
            <tr>
                <th class="text-center" style="width: 35px;">No</th>
                <th>Instansi / Pelanggan</th>
                <th style="width: 95px;">Jenis</th>
                <th style="width: 95px;">No. HP</th>
                <th style="width: 110px;">Kontak / PIC</th>
                <th style="width: 110px;">NPWP</th>
                <th style="width: 145px;">Email</th>
                <th>Alamat</th>
            </tr>
        </thead>
        <tbody>
            @forelse($customers as $index => $customer)
                <tr>
                    <td class="text-center">{{ $index + 1 }}</td>
                    <td>{{ $customer->name }}</td>
                    <td>
                        @if($customer->member_type === 'studio')
                            <span class="member-badge member-studio">Instansi</span>
                        @else
                            <span class="member-badge member-umum">Umum</span>
                        @endif
                    </td>
                    <td>{{ $customer->phone ?? '-' }}</td>
                    <td>{{ $customer->contact_person ?? '-' }}</td>
                    <td>{{ $customer->npwp ?? '-' }}</td>
                    <td>{{ $customer->email ?? '-' }}</td>
                    <td>{{ $customer->address ?? '-' }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="8" class="text-center">Tidak ada data pelanggan</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <div class="footer">
        <div>Dicetak pada: {{ now()->format('d M Y H:i:s') }}</div>
        <div>Halaman 1 dari 1</div>
    </div>

    <div class="no-print">
        <button class="print-btn" onclick="window.print()">🖨️ Cetak</button>
    </div>

    <script>
        window.addEventListener('load', function () {
            window.print();
        });
    </script>
</body>
</html>