@extends('layouts.app')
@section('title','Attendance Summary')
@section('page-title','Attendance')

@section('content')
<div class="page-header">
    <div class="d-flex align-items-center justify-content-between flex-wrap gap-2">
        <div class="d-flex align-items-center gap-2">
            <a href="{{ route('attendance.index') }}" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left"></i></a>
            <div>
                <h1>Monthly Attendance Summary</h1>
                <nav aria-label="breadcrumb"><ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('attendance.index') }}">Attendance</a></li>
                    <li class="breadcrumb-item active">Summary — {{ \Carbon\Carbon::parse($month.'-01')->format('F Y') }}</li>
                </ol></nav>
            </div>
        </div>
    </div>
</div>

<div class="card mb-3">
    <div class="card-body py-3">
        <form method="GET" class="d-flex gap-2 align-items-end flex-wrap">
            <div>
                <label class="form-label">Month</label>
                <input type="month" name="month" class="form-control" value="{{ $month }}">
            </div>
            <div style="align-self:flex-end;">
                <button type="submit" class="btn btn-primary"><i class="bi bi-search me-1"></i>View</button>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <span>{{ \Carbon\Carbon::parse($month.'-01')->format('F Y') }} — All Active Employees</span>
        <button class="btn btn-sm btn-outline-success"><i class="bi bi-file-earmark-excel me-1"></i>Export</button>
    </div>
    <div class="table-responsive">
        <table class="table mb-0">
            <thead>
                <tr>
                    <th>Employee</th>
                    <th class="d-none d-md-table-cell">Department</th>
                    <th class="text-center" style="color:#059669;">Present</th>
                    <th class="text-center" style="color:#dc2626;">Absent</th>
                    <th class="text-center" style="color:#f59e0b;">Late</th>
                    <th class="text-center" style="color:#0284c7;">On Leave</th>
                    <th class="text-center">Attendance %</th>
                </tr>
            </thead>
            <tbody>
            @forelse($summary as $emp)
            @php
                $total  = ($emp->present_days + $emp->absent_days + $emp->late_days + $emp->leave_days);
                $worked = $emp->present_days + $emp->late_days;
                $pct    = $total > 0 ? round(($worked / $total) * 100) : 0;
                $pctCol = $pct >= 90 ? '#059669' : ($pct >= 75 ? '#f59e0b' : '#dc2626');
            @endphp
            <tr>
                <td>
                    <div class="d-flex align-items-center gap-2">
                        <div class="avatar avatar-sm" style="background:linear-gradient(135deg,var(--ul-primary),var(--ul-accent));color:#fff;flex-shrink:0;">
                            {{ strtoupper(substr($emp->first_name??'?',0,1)) }}
                        </div>
                        <div>
                            <div class="fw-semibold" style="font-size:.83rem;">{{ $emp->full_name }}</div>
                            <div style="font-size:.71rem;color:var(--text-2);">{{ $emp->employee_number }}</div>
                        </div>
                    </div>
                </td>
                <td class="d-none d-md-table-cell" style="font-size:.82rem;color:var(--text-2);">{{ $emp->department->name ?? '—' }}</td>
                <td class="text-center fw-bold" style="color:#059669;">{{ $emp->present_days }}</td>
                <td class="text-center fw-bold" style="color:#dc2626;">{{ $emp->absent_days }}</td>
                <td class="text-center fw-bold" style="color:#f59e0b;">{{ $emp->late_days }}</td>
                <td class="text-center fw-bold" style="color:#0284c7;">{{ $emp->leave_days }}</td>
                <td class="text-center">
                    <div class="d-flex align-items-center gap-2">
                        <div class="progress flex-fill" style="height:8px;">
                            <div class="progress-bar" style="width:{{ $pct }}%;background:{{ $pctCol }};"></div>
                        </div>
                        <span class="fw-bold" style="color:{{ $pctCol }};min-width:38px;font-size:.82rem;">{{ $pct }}%</span>
                    </div>
                </td>
            </tr>
            @empty
            <tr><td colspan="7" class="text-center py-4" style="color:var(--text-2);">No records found for this period</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
    @if($summary->hasPages())
    <div class="card-body pt-0">{{ $summary->links('pagination::bootstrap-5') }}</div>
    @endif
</div>
@endsection
