@extends('layouts.app')

@section('title', 'Laporan Kehadiran Karyawan')
@section('page-title', 'Laporan Presensi & Kehadiran')
@section('page-subtitle', 'Laporan log presensi komprehensif dengan filter kustom tanggal dan departemen.')

@section('page-actions')
<div class="d-flex justify-content-md-end gap-2 mt-2 mt-md-0">
    <button type="button" class="btn btn-outline-secondary" onclick="window.print()">
        <i class="bi bi-printer me-1"></i> Cetak Laporan
    </button>
    <a href="{{ route('admin.attendance.export', request()->query()) }}" class="btn btn-success">
        <i class="bi bi-file-earmark-excel me-1"></i> Export Excel (.xls)
    </a>
</div>
@endsection

@section('content')
{{-- Filter Preset Card --}}
<div class="card shadow-sm border-0 mb-4 d-print-none">
    <div class="card-body p-3">
        <form method="GET" action="{{ route('admin.reports.index') }}" class="row g-2 align-items-center">
            <div class="col-12 col-md-3">
                <label class="form-label text-xs text-muted mb-1">Periode Preset</label>
                <select name="preset" class="form-select bg-light border-0" onchange="this.form.submit()">
                    <option value="today" {{ $preset == 'today' ? 'selected' : '' }}>Hari Ini</option>
                    <option value="this_week" {{ $preset == 'this_week' ? 'selected' : '' }}>Minggu Ini</option>
                    <option value="this_month" {{ $preset == 'this_month' ? 'selected' : '' }}>Bulan Ini</option>
                    <option value="custom" {{ $preset == 'custom' ? 'selected' : '' }}>Kustom Rentang Tanggal</option>
                </select>
            </div>

            @if($preset == 'custom')
                <div class="col-6 col-md-2">
                    <label class="form-label text-xs text-muted mb-1">Dari Tanggal</label>
                    <input type="date" name="start_date" class="form-control bg-light border-0" value="{{ $startDate }}">
                </div>
                <div class="col-6 col-md-2">
                    <label class="form-label text-xs text-muted mb-1">Sampai Tanggal</label>
                    <input type="date" name="end_date" class="form-control bg-light border-0" value="{{ $endDate }}">
                </div>
            @endif

            <div class="col-6 col-md-3">
                <label class="form-label text-xs text-muted mb-1">Departemen</label>
                <select name="department_id" class="form-select bg-light border-0">
                    <option value="">Semua Departemen</option>
                    @foreach($departments as $dept)
                        <option value="{{ $dept->id }}" {{ request('department_id') == $dept->id ? 'selected' : '' }}>{{ $dept->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-6 col-md-2 d-flex align-items-end">
                <button type="submit" class="btn btn-primary w-100"><i class="bi bi-funnel me-1"></i> Terapkan</button>
            </div>
        </form>
    </div>
</div>

{{-- Main Report Table --}}
<div class="card shadow-sm border-0">
    <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
        <h5 class="card-title mb-0 fw-bold">
            <i class="bi bi-file-earmark-text me-2 text-primary"></i> 
            Log Presensi ({{ \Carbon\Carbon::parse($startDate)->translatedFormat('d M Y') }} s/d {{ \Carbon\Carbon::parse($endDate)->translatedFormat('d M Y') }})
        </h5>
        <span class="badge bg-light-primary text-primary">Total: {{ $attendances->total() }} Record</span>
    </div>
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="bg-light">
                    <tr>
                        <th class="ps-4">Tanggal</th>
                        <th>Karyawan</th>
                        <th>Departemen</th>
                        <th>Jam Masuk</th>
                        <th>Jam Pulang</th>
                        <th>Status</th>
                        <th>Terlambat</th>
                        <th class="pe-4">Jarak Geofence</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($attendances as $att)
                        <tr>
                            <td class="ps-4">
                                <strong>{{ \Carbon\Carbon::parse($att->attendance_date)->translatedFormat('d M Y') }}</strong>
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div class="avatar avatar-sm bg-light-primary text-primary me-2 flex-shrink-0">
                                        @if($att->employee?->photo)
                                            <img src="{{ asset($att->employee->photo) }}" alt="Avatar" style="width: 32px; height: 32px; object-fit: cover; border-radius: 50%;">
                                        @else
                                            <span class="avatar-content">{{ strtoupper(substr($att->employee?->name ?? 'K', 0, 2)) }}</span>
                                        @endif
                                    </div>
                                    <div>
                                        <h6 class="mb-0 fw-bold">{{ $att->employee?->name ?? 'Karyawan' }}</h6>
                                        <small class="text-muted">{{ $att->employee?->nik }}</small>
                                    </div>
                                </div>
                            </td>
                            <td>{{ $att->employee?->department?->name ?? '-' }}</td>
                            <td>
                                @if($att->check_in)
                                    <span class="fw-bold text-success">{{ \Carbon\Carbon::parse($att->check_in)->format('H:i') }}</span>
                                @else
                                    <span class="text-muted">-</span>
                                @endif
                            </td>
                            <td>
                                @if($att->check_out)
                                    <span class="fw-bold text-danger">{{ \Carbon\Carbon::parse($att->check_out)->format('H:i') }}</span>
                                @else
                                    <span class="text-muted">-</span>
                                @endif
                            </td>
                            <td>
                                <span class="badge {{ $att->status_badge }}">{{ $att->status_label }}</span>
                            </td>
                            <td>
                                @if($att->late_minutes > 0)
                                    <span class="text-danger fw-bold">+{{ $att->late_minutes }}m</span>
                                @else
                                    <span class="text-success">-</span>
                                @endif
                            </td>
                            <td class="pe-4">
                                @if($att->distance_in !== null)
                                    <span class="badge bg-light-primary text-primary">{{ round($att->distance_in) }} Meter</span>
                                @else
                                    <span class="text-muted">-</span>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="text-center py-5 text-muted">
                                Tidak ada log presensi pada periode yang dipilih.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($attendances->hasPages())
            <div class="p-3 border-top d-flex justify-content-end">
                {{ $attendances->links('pagination::bootstrap-5') }}
            </div>
        @endif
    </div>
</div>
@endsection
