<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Loan / Advance Summary Report — Print</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: Arial, sans-serif; font-size: 11.5px; background: #fff; color: #000; }
        .container { width: 97%; margin: 16px auto; }

        /* ── Header ── */
        .rpt-header { text-align: center; margin-bottom: 12px; line-height: 1.6; }
        .rpt-header .co-name  { font-size: 20px; font-weight: 700; letter-spacing: 1px; }
        .rpt-header .co-sub   { font-size: 14px; font-weight: 600; }
        .rpt-header .co-range { font-size: 11px; color: #222; margin-top: 3px; }

        /* ── Table ── */
        table { width: 100%; border-collapse: collapse; margin-top: 8px; }

        thead th {
            border-top:    2px solid #000;
            border-bottom: 2px solid #000;
            border-left:   none;
            border-right:  none;
            padding: 7px 8px;
            text-align: left;
            font-size: 11.5px;
            font-weight: 700;
            background: #fff;
            white-space: nowrap;
        }
        thead th.num { text-align: right; }
        thead th.ctr { text-align: center; }

        /* Data rows */
        tbody td {
            border-left:   none;
            border-right:  none;
            border-top:    none;
            border-bottom: 1px dashed #999;
            padding: 6px 8px;
            vertical-align: middle;
            font-size: 11px;
        }

        /* Department header row */
        tr.dept-row td {
            font-weight: 700;
            font-size: 12px;
            color: #cc0000;
            border-bottom: none;
            padding: 8px 8px 1px;
            letter-spacing: 0.3px;
        }

        /* Dept totals row */
        tr.dept-total-row td {
            border-top:    1px dashed #888;
            border-bottom: 1px dashed #888;
            font-size: 11px;
            padding: 5px 8px;
        }

        /* Grand total row */
        tr.grand-row td {
            border-top:    2px solid #000 !important;
            border-bottom: 2px solid #000 !important;
            font-weight: 700;
            font-size: 11.5px;
            background: #f5f5f5;
            padding: 7px 8px;
        }

        td.num { text-align: right;  white-space: nowrap; }
        td.ctr { text-align: center; white-space: nowrap; }
        td.lft { text-align: left; }

        /* Print button */
        .no-print {
            margin-top: 18px;
            display: inline-block;
            padding: 8px 22px;
            background: #007bff;
            color: #fff;
            border: none;
            cursor: pointer;
            font-size: 13px;
            border-radius: 3px;
        }

        @media print {
            .no-print { display: none; }
            thead     { display: table-header-group; }
            tr        { page-break-inside: avoid; }
            body      { font-size: 10.5px; }
        }
    </style>
</head>
<body>
<div class="container">

    {{-- ── Header ── --}}
    <div class="rpt-header">
        <div class="co-name">M.D.S</div>
        <div class="co-sub">Loan / Advance Summary Report</div>
        <div class="co-range">
            @if($selectedEmployee)
                Employee: &nbsp; {{ $selectedEmployee->first_name }} {{ $selectedEmployee->last_name }}
            @endif
            @if($selectedDepartment)
                &nbsp;|&nbsp; Dept: {{ $selectedDepartment->name }}
            @endif
        </div>
    </div>

    @if(empty($reportData['departments']))
        <p style="text-align:center;padding:30px;color:#888;">No records found.</p>
    @else

        @php
            $n  = fn($v) => $v  ? number_format($v, 0) : '-';
            $n0 = fn($v) => number_format($v, 0);
        @endphp

        <table>
            <thead>
                <tr>
                    <th style="width:26%">Name</th>
                    <th class="ctr" style="width:16%">Designation</th>
                    <th class="ctr" style="width:12%">Card No</th>
                    <th class="num" style="width:115px">Opening</th>
                    <th class="num" style="width:115px">Payments</th>
                    <th class="num" style="width:115px">Deductions</th>
                    <th class="num" style="width:115px">Balance</th>
                </tr>
            </thead>
            <tbody>

                @foreach($reportData['departments'] as $deptName => $deptData)

                    {{-- Department Header --}}
                    <tr class="dept-row">
                        <td colspan="7">{{ $deptName }}</td>
                    </tr>

                    @foreach($deptData['rows'] as $row)
                        <tr>
                            <td class="lft">{{ $row['employee_name'] }}</td>
                            <td class="ctr">{{ $row['designation'] }}</td>
                            <td class="ctr">{{ $row['card_no'] }}</td>
                            <td class="num">{{ $n($row['opening']) }}</td>
                            <td class="num">{{ $n($row['payments']) }}</td>
                            <td class="num">{{ $n($row['deductions']) }}</td>
                            <td class="num">{{ $n($row['balance']) }}</td>
                        </tr>
                    @endforeach

                    {{-- Dept Totals --}}
                    <tr class="dept-total-row">
                        <td class="lft">Dept. Totals ==&gt;</td>
                        <td></td>
                        <td></td>
                        <td class="num">{{ $n($deptData['totals']['opening']) }}</td>
                        <td class="num">{{ $n0($deptData['totals']['payments']) }}</td>
                        <td class="num">{{ $n0($deptData['totals']['deductions']) }}</td>
                        <td class="num">{{ $n0($deptData['totals']['balance']) }}</td>
                    </tr>

                @endforeach

                {{-- Grand Totals --}}
                <tr class="grand-row">
                    <td class="lft">GRAND TOTALS ==&gt;</td>
                    <td></td>
                    <td></td>
                    <td class="num">{{ $n($reportData['grand']['opening']) }}</td>
                    <td class="num">{{ $n0($reportData['grand']['payments']) }}</td>
                    <td class="num">{{ $n0($reportData['grand']['deductions']) }}</td>
                    <td class="num">{{ $n0($reportData['grand']['balance']) }}</td>
                </tr>

            </tbody>
        </table>

    @endif

    <button class="no-print" onclick="window.print()">🖨 Print Report</button>

</div>
</body>
</html>