<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Presenteeism Details Report</title>
    <style>
        @page {
            margin: 130px 30px 70px 30px;
        }
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 7px;
            margin: 0;
            padding: 0;
        }
        .header {
            position: fixed;
            top: -110px;
            left: 0;
            right: 0;
            height: 100px;
        }
        .header-left  { float: left;  width: 70%; }
        .header-right { float: right; width: 30%; text-align: right; }
        .header-right img { max-height: 60px; }
        .report-name  { font-size: 13px; font-weight: bold; color: #2c3e50; }
        .company-name { font-size: 10px; font-weight: bold; }
        .small-text   { font-size: 8px; color: #555; margin-top: 2px; }
        .footer {
            position: fixed;
            bottom: -50px;
            left: 0;
            right: 0;
            height: 40px;
            font-size: 8px;
            border-top: 1px solid #ccc;
            padding-top: 3px;
        }
        .footer-left   { float: left;  width: 33%; }
        .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 {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid #ccc;
            padding: 2px 3px;
            font-size: 6.5px;
            white-space: nowrap;
            overflow: hidden;
        }
        th {
            background-color: #2c3e50;
            color: #ffffff;
            font-weight: bold;
            text-align: left;
        }
        .tc { text-align: center; }
        .date-row { background: #2c3e50; color: #fff; font-weight: bold; font-size: 7px; }
    </style>
</head>
<body>

<div class="header">
    <div class="header-left">
        <div class="report-name">Presenteeism Details Report</div>
        <div class="company-name">{{ $company->legal_name ?? $company->display_name ?? '' }}</div>
        <div class="small-text">Branch: {{ $branch->name ?? 'All Branches' }}</div>
        <div class="small-text">
            @if(!empty($filters['from_date']) && !empty($filters['to_date']))
                Period: {{ $filters['from_date'] }} to {{ $filters['to_date'] }}
            @endif
            &nbsp; Printed: {{ now()->format('d M Y, H:i') }}
        </div>
    </div>
    <div class="header-right">
        @if(!empty($company->logo))
            <img src="{{ public_path($company->logo) }}">
        @endif
    </div>
</div>

<div class="footer">
    <div class="footer-left">LaraOffice</div>
    <div class="footer-center"><span class="page-number"></span></div>
    <div class="footer-right">{{ now()->format('Y-m-d H:i') }}</div>
</div>

<table>
    <thead>
    <tr>
        <th style="width:3%">#</th>
        <th style="width:9%">Emp Code</th>
        <th style="width:20%">Name</th>
        <th style="width:16%">Department</th>
        <th style="width:16%">Designation</th>
        <th style="width:12%">Shift</th>
        <th style="width:8%">Status</th>
        <th style="width:8%">In Time</th>
        <th style="width:8%">Out Time</th>
    </tr>
    </thead>
    <tbody>
    @forelse($grouped as $date => $rows)
        <tr>
            <td colspan="9" class="date-row">
                {{ \Carbon\Carbon::parse($date)->format('d M Y, l') }} &nbsp; ({{ $rows->count() }} present)
            </td>
        </tr>
        @foreach($rows as $row)
        @php
            $name = trim($row->employee->first_name . ' ' . ($row->employee->middle_name ?? '') . ' ' . $row->employee->last_name);
        @endphp
        <tr>
            <td class="tc">{{ $loop->iteration }}</td>
            <td>{{ $row->employee->employee_code }}</td>
            <td>{{ $name }}</td>
            <td>{{ optional($row->employee->department)->name }}</td>
            <td>{{ optional($row->employee->designation)->name }}</td>
            <td>{{ optional($row->shift)->name }}</td>
            <td class="tc">{{ $row->attendance_status }}</td>
            <td class="tc">{{ $row->in_time ? $row->in_time->format('H:i') : '—' }}</td>
            <td class="tc">{{ $row->out_time ? $row->out_time->format('H:i') : '—' }}</td>
        </tr>
        @endforeach
    @empty
        <tr>
            <td colspan="9" class="tc" style="padding:10px; color:#888; font-style:italic;">No records found.</td>
        </tr>
    @endforelse
        <tr>
            <td colspan="9" style="background:#ecf0f1; font-weight:bold; font-size:7px; padding:3px;">
                Total Records: {{ $records->count() }}
            </td>
        </tr>
    </tbody>
</table>
</body>
</html>
