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

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

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

        thead th {
            border: 1px solid #000;
            padding: 5px 5px;
            text-align: center;
            font-size: 10.5px;
            font-weight: 700;
            background: #fff;
            white-space: nowrap;
            vertical-align: bottom;
            line-height: 1.3;
        }

        /* Solid side borders, dashed bottom — matches screenshot exactly */
        tbody td {
            border-left:   1px solid #000;
            border-right:  1px solid #000;
            border-top:    none;
            border-bottom: 1px dashed #777;
            padding: 4px 5px;
            vertical-align: middle;
            font-size: 10.5px;
        }

        /* Department header row */
        tr.dept-row td {
            font-weight: 700;
            font-size: 11.5px;
            color: #cc0000;
            border-left:   1px solid #000;
            border-right:  1px solid #000;
            border-bottom: none;
            padding: 6px 5px 1px;
            letter-spacing: 0.3px;
        }

        /* Sub-total row */
        tr.subtotal-row td {
            border-top:    1px dashed #777;
            border-bottom: 1px dashed #777;
            color: #cc0000;
            font-weight: 600;
            font-size: 10.5px;
            padding: 3px 5px;
        }

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

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

        .text-red { color: #cc0000; }

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

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

    {{-- ── Header ── --}}
    <div class="rpt-header">
        <div class="co-name">M.D.S</div>
        <div class="co-sub">Payroll History Report</div>
        <div class="co-range">
            From: {{ $fromDate ? \Carbon\Carbon::parse($fromDate)->format('d/m/Y') : '—' }},
            Till: {{ $toDate   ? \Carbon\Carbon::parse($toDate)->format('d/m/Y')   : '—' }}
            @if($selectedEmployee)
                &nbsp;|&nbsp; Employee: {{ $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
            /* Helper: show number or blank */
            $n  = fn($v)       => $v  ? number_format($v, 0) : '';
            /* Helper: show number or blank, but force 0 when value is explicitly 0 */
            $n0 = fn($v)       => number_format($v, 0);
            /* Helper for hours — show with 2 decimals */
            $h  = fn($v)       => $v  ? number_format($v, 2) : '';
        @endphp

        <table>
            <thead>
                <tr>
                    <th style="text-align:left">Employee</th>
                    <th>EmpID</th>
                    <th>Sal.<br>Type</th>
                    <th>From</th>
                    <th>To</th>
                    <th class="num">Salary</th>
                    <th class="num">OT<br>Hours</th>
                    <th class="num">OT<br>Amount</th>
                    <th class="num">Award</th>
                    <th class="num">Absent<br>Days</th>
                    <th class="num">Absent<br>Amt</th>
                    <th class="num">Other<br>Ded.</th>
                    <th class="num">Advance<br>Ded.</th>
                    <th class="num">Loan<br>Ded.</th>
                    <th class="num">Net Payable</th>
                </tr>
            </thead>
            <tbody>

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

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

                    @foreach($deptData['rows'] as $row)
                        <tr>
                            <td>{{ $row['employee_name'] }}</td>
                            <td class="ctr">{{ $row['employee_no'] }}</td>
                            <td class="ctr">{{ $row['salary_type'] }}</td>
                            <td class="ctr">{{ $row['from'] }}</td>
                            <td class="ctr">{{ $row['to'] }}</td>
                            <td class="num">{{ $n($row['salary']) }}</td>
                            <td class="num">{{ $h($row['ot_hours']) }}</td>
                            <td class="num">{{ $n($row['overtime']) }}</td>
                            <td class="num">{{ $n($row['award']) }}</td>
                            <td class="ctr">{{ $row['absent_days'] ?: '' }}</td>
                            <td class="num">{{ $n($row['absent_amount']) }}</td>
                            <td class="num">{{ $n($row['other_ded']) }}</td>
                            <td class="num">{{ $n($row['advance_ded']) }}</td>
                            {{-- ✅ FIXED: was rendering blank due to missing fallback --}}
                            <td class="num">{{ $n($row['loan_ded']) }}</td>
                            <td class="num">{{ $n0($row['net_payable']) }}</td>
                        </tr>
                    @endforeach

                    {{-- Sub Total --}}
                    <tr class="subtotal-row">
                        <td colspan="5" class="text-red">Sub Total =&gt;</td>
                        <td class="num text-red">{{ $n0($deptData['subSalary']) }}</td>
                        <td class="num text-red">{{ $h($deptData['subOtHours']) }}</td>
                        <td class="num text-red">{{ $n($deptData['subOvertime']) }}</td>
                        <td class="num text-red">{{ $n($deptData['subAward']) }}</td>
                        <td class="ctr text-red">{{ $deptData['subAbsentDays'] ?: '' }}</td>
                        <td class="num text-red">{{ $n($deptData['subAbsentAmount']) }}</td>
                        <td class="num text-red">{{ $n($deptData['subOtherDed']) }}</td>
                        <td class="num text-red">{{ $n($deptData['subAdvanceDed']) }}</td>
                        <td class="num text-red">{{ $n($deptData['subLoanDed']) }}</td>
                        <td class="num text-red">{{ $n0($deptData['subNet']) }}</td>
                    </tr>

                @endforeach

                {{-- Grand Total --}}
                <tr class="grand-row">
                    <td colspan="5">Grand Total =&gt;</td>
                    <td class="num">{{ $n0($reportData['grandGross']) }}</td>
                    <td class="num">{{ $h($reportData['grandOtHours']) }}</td>
                    <td class="num">{{ $n($reportData['grandOvertime']) }}</td>
                    <td class="num">{{ $n($reportData['grandAward']) }}</td>
                    <td class="ctr">{{ $reportData['grandAbsentDays'] ?: '' }}</td>
                    <td class="num">{{ $n($reportData['grandAbsentAmount']) }}</td>
                    <td class="num">{{ $n($reportData['grandOtherDed']) }}</td>
                    <td class="num">{{ $n($reportData['grandAdvanceDed']) }}</td>
                    <td class="num">{{ $n($reportData['grandLoanDed']) }}</td>
                    <td class="num">{{ $n0($reportData['grandNet']) }}</td>
                </tr>

            </tbody>
        </table>

    @endif

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

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