<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Cash Denominations Report</title>

    <style>
        @page {
            margin: 140px 20px 80px 20px;
        }

        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 12px;
            margin: 0;
        }

        /* =========================
           HEADER
        ========================== */
        .header {
            position: fixed;
            top: -120px;
            left: 0;
            right: 0;
            height: 100px;
        }

        .header-left {
            float: left;
            width: 70%;
        }

        .header-right {
            float: right;
            width: 30%;
            text-align: right;
        }

        .report-name {
            font-size: 15px;
            font-weight: bold;
        }

        .company-name {
            font-size: 13px;
            font-weight: bold;
        }

        .small-text {
            font-size: 11px;
        }

       .footer {
            position: fixed;
            bottom: -60px;
            left: 0;
            right: 0;
            height: 50px;
            font-size: 10px;
        }

        .footer-left {
            float: left;
            width: 33%;
            text-align: left;
        }

        .footer-center {
            float: left;
            width: 34%;
            text-align: center;
        }

        .footer-right {
            float: right;
            width: 33%;
            text-align: right;
        }

        .page-number:before {
            content: "Page " counter(page) " / " counter(pages);
        }

        /* =========================
           TABLE
        ========================== */
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 7px;
        }

        th, td {
            border: 1px solid #000;
            padding: 3px 2px;
        }

        th {
            background: #f2f2f2;
            font-weight: bold;
        }

        td.number {
            text-align: right;
        }

        .clearfix::after {
            content: "";
            display: table;
            clear: both;
        }
    </style>
</head>
<body>

<!-- ================= HEADER ================= -->
<div class="header">
    <div class="header-left">
        <div class="report-name">
            Cash Denominations Report
        </div>
        <div class="company-name">
            {{ $company->legal_name ?? $company->display_name ?? '' }}
        </div>
        <div class="small-text">
            {{ trim(implode(', ', array_filter([$company->address_line1 ?? null, $company->address_line2 ?? null]))) }}
        </div>
        <div class="small-text">
            @if(!empty($filters['from_date']) && !empty($filters['to_date']))
                Period: {{ \Carbon\Carbon::parse($filters['from_date'])->format('d M Y') }} to {{ \Carbon\Carbon::parse($filters['to_date'])->format('d M Y') }}
            @endif
        </div>
    </div>

    <div class="header-right">
        @if(!empty($company->logo))
            <img src="{{ public_path($company->logo) }}" height="70">
        @endif
    </div>
</div>

<!-- ================= FOOTER ================= -->
<div class="footer">
    <div class="footer-left">
        LaraOffice
    </div>

    <div class="footer-center">
        <span class="page-number"></span>
    </div>

    <div class="footer-right">
        Printed: {{ now()->format('Y-m-d H:i') }}
    </div>
</div>

<!-- ================= CONTENT ================= -->
@php
    $currencySymbol = optional($currency)->symbol ?? 'Rs.';
@endphp

<table>
    <thead>
        <tr>
            <th rowspan="2">Emp Code</th>
            <th rowspan="2">EPF No</th>
            <th rowspan="2">Employee Name</th>
            <th rowspan="2">Dept</th>
            <th rowspan="2">Net Salary<br>({{ $currencySymbol }})</th>
            @if(!empty($denominations['notes']))
                <th colspan="{{ count($denominations['notes']) }}">Notes</th>
            @endif
            @if(!empty($denominations['coins']))
                <th colspan="{{ count($denominations['coins']) }}">Coins</th>
            @endif
        </tr>
        <tr>
            @if(!empty($denominations['notes']))
                @foreach($denominations['notes'] as $denomination)
                    <th>{{ number_format($denomination) }}</th>
                @endforeach
            @endif
            @if(!empty($denominations['coins']))
                @foreach($denominations['coins'] as $denomination)
                    <th>{{ number_format($denomination, 2) }}</th>
                @endforeach
            @endif
        </tr>
    </thead>
    <tbody>
        @foreach($cash_breakdown as $item)
        @php
            $employee = $item['employee'];
            $breakdown = $item['breakdown'];
        @endphp
        <tr>
            <td>{{ $employee->employee_code }}</td>
            <td>{{ $employee->epf_no }}</td>
            <td>{{ trim($employee->first_name . ' ' . $employee->middle_name . ' ' . $employee->last_name) }}</td>
            <td>{{ optional($employee->department)->name }}</td>
            <td class="number">{{ number_format($item['net_salary'], 2) }}</td>

            @if(!empty($denominations['notes']))
                @foreach($denominations['notes'] as $denomination)
                    @php
                        $count = 0;
                        foreach ($breakdown as $b) {
                            if ($b['type'] === 'note' && $b['denomination'] == $denomination) {
                                $count = $b['count'];
                                break;
                            }
                        }
                    @endphp
                    <td class="number">{{ $count > 0 ? $count : '-' }}</td>
                @endforeach
            @endif

            @if(!empty($denominations['coins']))
                @foreach($denominations['coins'] as $denomination)
                    @php
                        $count = 0;
                        foreach ($breakdown as $b) {
                            if ($b['type'] === 'coin' && $b['denomination'] == $denomination) {
                                $count = $b['count'];
                                break;
                            }
                        }
                    @endphp
                    <td class="number">{{ $count > 0 ? $count : '-' }}</td>
                @endforeach
            @endif
        </tr>
        @endforeach
    </tbody>
</table>

@if(count($cash_breakdown) == 0)
    <div style="text-align: center; padding: 50px; font-size: 14px; color: #999;">
        No payroll records found for the selected filters.
    </div>
@endif

</body>
</html>
