<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">

    <style>
        @page {
            size: A4 landscape;
            margin: 10mm;
        }

        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 10px;
        }

        h1, h3 {
            text-align: center;
            margin: 5px 0;
        }

        /* Added specific style for Group Headers */
        .group-header {
            text-align: left;
            margin-top: 20px;
            margin-bottom: 5px;
            font-size: 12px;
            font-weight: bold;
            text-transform: uppercase;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 5px; /* Reduced margin to keep totals close */
        }

        th, td {
            border: 1px solid #000;
            padding: 6px;
            text-align: center;
        }

        th {
            background-color: #f4b638;
            font-weight: bold;
        }

        .sub-title {
            font-weight: bold;
            background-color: #ffe9be;
        }

        .logo-wrapper {
            text-align: center;
            margin-bottom: 10px;
        }

        .logo-wrapper img {
            width: 120px;
        }

        /* Box for Group Totals */
        .group-total-box {
            width: 100%;
            margin-bottom: 20px;
            text-align: right;
            border: 1px solid #000; /* Optional: remove if you just want text */
            padding: 10px;
            background-color: #f8f9fa;
        }

        /* Ensure tables don't break awkwardly */
        .kp-section {
            page-break-inside: avoid;
        }

        .gift-section {
            margin-top: 24px;
            page-break-inside: avoid;
        }

        .gift-title {
            text-align: left;
            margin: 0 0 6px 0;
            font-size: 12px;
            font-weight: bold;
            text-transform: uppercase;
        }

        .gift-table {
            margin-bottom: 8px;
        }

        .gift-note {
            margin: 6px 0 0 0;
            font-size: 9px;
            font-style: italic;
            line-height: 1.4;
        }
    </style>

</head>

<body>

    <div class="logo-wrapper">
        <img src="data:image/png;base64,{{ $base64_image }}" alt="GTBZ Logo">
    </div>

    <h1>Ringkasan Pelan GTBZ</h1>
    <p style="text-align:center;">Leader Bonda Zaimas</p>

    <table>
        <tr>
            <th colspan="2">Maklumat Ahli</th>
            <th colspan="2">Maklumat Waris</th>
        </tr>
        <tr>
            <td class="sub-title">Nama Ahli</td>
            <td>{{ $user->name }}</td>
            <td class="sub-title">Nama Waris</td>
            <td>{{ $user->nok_name }}</td>
        </tr>
        <tr>
            <td class="sub-title">IC</td>
            <td>{{ $user->ic_no }}</td>
            <td class="sub-title">IC</td>
            <td>{{ $user->nok_ic_no }}</td>
        </tr>
        <tr>
            <td class="sub-title">Email Address</td>
            <td>{{ $user->email }}</td>
            <td class="sub-title">Email Address</td>
            <td>-</td>
        </tr>
        <tr>
            <td class="sub-title">No Telefon</td>
            <td>{{ $user->phone_no }}</td>
            <td class="sub-title">No Telefon</td>
            <td>{{ $user->nok_phone_no }}</td>
        </tr>
        {{-- <tr>
            <td class="sub-title">No Akaun</td>
            <td>{{ $user->bank_account_no }}</td>
            <td class="sub-title">No Akaun</td>
            <td>{{ $user->nok_bank_account_no }}</td>
        </tr> --}}
        {{-- <tr>
            <td class="sub-title">Nama Bank</td>
            <td>{{ $user->bank_name }}</td>
            <td class="sub-title">Nama Bank</td>
            <td>{{ $user->nok_bank_name }}</td>
        </tr> --}}
        <tr>
            <td class="sub-title" colspan="2">Alamat</td>
            <td colspan="2">{{ $user->address }}</td>
        </tr>
    </table>

    <br>

    @php
        // 1. Group the plans by KP Name
        $groupedPlans = $plans->groupBy('kp_name');

        // 2. Initialize Grand Totals for the entire document
        $grandTotalPO = 0;
        $grandTotalModal = 0;
    @endphp

    @foreach ($groupedPlans as $kpName => $plansInGroup)
        @php
            // Initialize totals for THIS specific KP
            $groupModal = 0;
            $groupPO = 0;
        @endphp

        <div class="kp-section">
            <div class="group-header">
                SENARAI PLAN (KP: {{ $kpName }})
            </div>

            <table>
                <thead>
                    <tr>
                        <th>Nama Pelan</th>
                        <th>Nama KP</th>
                        <th>Modal</th>
                        <th>PO KP</th>
                        <th>PO Bonda</th>
                        <th>Jumlah PO</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($plansInGroup as $plan)
                        @php
                            $currentPO = $plan->total_po_kp + $plan->total_po_bonda;

                            // Add to Group Totals
                            $groupModal += $plan->total_modal;
                            $groupPO += $currentPO;

                            // Add to Grand Totals
                            $grandTotalModal += $plan->total_modal;
                            $grandTotalPO += $currentPO;
                        @endphp
                        <tr>
                            <td class="sub-title">{{ $plan->name }}</td>
                            <td>{{ $plan->kp_name }}</td>
                            <td>RM {{ number_format($plan->total_modal / 100, 2) }}</td>
                            <td>RM {{ number_format($plan->total_po_kp / 100, 2) }}</td>
                            <td>RM {{ number_format($plan->total_po_bonda / 100, 2) }}</td>
                            <td>RM {{ number_format($currentPO / 100, 2) }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>

            <div class="group-total-box">
                <b>JUMLAH MODAL ({{ $kpName }}):</b> RM {{ number_format($groupModal / 100, 2) }} &nbsp;&nbsp;&nbsp;&nbsp;<br>
                <b>JUMLAH PO ({{ $kpName }}):</b> RM {{ number_format($groupPO / 100, 2) }} &nbsp;&nbsp;&nbsp;&nbsp;<br>
                @php
                    $bank = $bankAccount[$plan->kp_id] ?? null;
                @endphp
                <b>AKAUN BANK ({{ $bank['name'] ?? '—' }}):</b> {{ $bank['account_no'] ?? '—' }} &nbsp;&nbsp;&nbsp;&nbsp;
            </div>
        </div>
    @endforeach

    <div style="margin-top: 30px; border-top: 2px solid #000; padding-top: 10px;">
        <h3>
            JUMLAH KESELURUHAN MODAL (SEMUA): RM {{ number_format($grandTotalModal / 100, 2) }}
        </h3>
        <h3>
            JUMLAH KESELURUHAN PO (SEMUA): RM {{ number_format($grandTotalPO / 100, 2) }}
        </h3>
    </div>

    @php
        $eligibleGifts = $nonCashGifts ?? collect();
    @endphp

    @if ($eligibleGifts->isNotEmpty())
        <div class="gift-section">
            <div class="gift-title">SENARAI HADIAH PLAN</div>
            <table class="gift-table">
                <thead>
                    <tr>
                        <th>Nama Pelan</th>
                        <th>Hadiah</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($eligibleGifts as $gift)
                        <tr>
                            <td class="sub-title">{{ $gift->plan_name }}</td>
                            <td>{{ $gift->gift_name }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
            <p class="gift-note">
                <strong>*Nota:</strong> Hadiah-hadiah ini mungkin ditukarkan dengan nilai RM yang setara,
                bergantung kepada keputusan dan budi bicara pengurusan dan perbincangan bersama ahli.
            </p>
        </div>
    @endif

</body>

</html>
