@extends('layouts.siswa')

@section('title', 'Riwayat Kehadiran - MagangTime')
@section('page-title', 'Riwayat Kehadiran')

@section('content')
<div class="card">
    <div class="card-header bg-white">
        <div class="row align-items-center mb-3">
            <div class="col-md-6">
                <h5 class="mb-0"><i class="fas fa-history me-2"></i>Riwayat Data Kehadiran</h5>
            </div>
            <div class="col-md-6 text-end">
                <small class="text-muted">
                    Menampilkan {{ $attendance->count() }} dari {{ $attendance->total() }} data
                </small>
            </div>
        </div>

        <!-- Form Filter dan Pencarian -->
        <form method="GET" action="{{ route('siswa.riwayat') }}">
            <div class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Tanggal Mulai</label>
                    <input type="date" name="start_date" class="form-control" value="{{ request('start_date') }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Tanggal Akhir</label>
                    <input type="date" name="end_date" class="form-control" value="{{ request('end_date') }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Status</label>
                    <select name="status" class="form-select">
                        <option value="">Semua Status</option>
                        <option value="hadir" {{ request('status') == 'hadir' ? 'selected' : '' }}>Hadir</option>
                        <option value="izin" {{ request('status') == 'izin' ? 'selected' : '' }}>Izin</option>
                        <option value="alfa" {{ request('status') == 'alfa' ? 'selected' : '' }}>Alfa</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <div class="btn-group w-100" role="group">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search"></i>
                        </button>
                        @if(request()->hasAny(['start_date', 'end_date', 'status']))
                            <a href="{{ route('siswa.riwayat') }}" class="btn btn-outline-secondary">
                                <i class="fas fa-times"></i>
                            </a>
                        @endif
                    </div>
                </div>
            </div>
        </form>
    </div>
    
    <div class="card-body">
        {{-- Desktop Table (visible on md and up) --}}
        <div class="table-responsive d-none d-md-block">
            <table class="table table-striped table-hover">
                <thead class="table-dark">
                    <tr>
                        <th width="5%">No</th>
                        <th>Tanggal</th>
                        <th class="text-center">Status</th>
                        <th class="text-center">Jam Masuk</th>
                        <th class="text-center">Jam Pulang</th>
                        <th class="text-center" width="40%">Keterangan</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($attendance as $index => $att)
                    <tr>
                        <td>{{ $attendance->firstItem() + $index }}</td>
                        <td>{{ $att->date->isoFormat('DD/MM/Y') }}</td>
                        <td class="text-center">
                            <span class="badge bg-{{ $att->status_badge }}">
                                {{ ucfirst($att->status) }}
                            </span>
                        </td>
                        <td class="text-center">{{ $att->check_in_time ? $att->check_in_time->format('H:i') : '–' }}</td>
                        <td class="text-center">{{ $att->check_out_time ? $att->check_out_time->format('H:i') : '–' }}</td>
                        <td class="text-center">
                            <span class="d-inline-block text-truncate" style="max-width: 150px;" title="{{ $att->notes }}">
                                {{ $att->notes ?: '–' }}
                            </span>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">
                            @if(request()->hasAny(['start_date', 'end_date', 'status']))
                                <i class="fas fa-search fa-3x mb-3"></i>
                                <p>Tidak ditemukan data kehadiran dengan kriteria yang dipilih</p>
                                <a href="{{ route('siswa.riwayat') }}" class="btn btn-primary">Reset Filter</a>
                            @else
                                <i class="fas fa-calendar-times fa-3x mb-3"></i>
                                <p>Belum ada data kehadiran</p>
                            @endif
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Mobile Cards (visible on small screens) --}}
        <div class="d-block d-md-none">
            @forelse($attendance as $index => $att)
            <div class="card mb-3 border-start border-primary border-4">
                <div class="card-body py-3">
                    <div class="d-flex justify-content-between align-items-start mb-2">
                        <h6 class="fw-bold mb-1">{{ $att->user->name ?? auth()->user()->name }}</h6>
                        <small class="text-muted">#{{ $attendance->firstItem() + $index }}</small>
                    </div>

                    <div class="row mb-2">
                        <div class="col-6">
                            <small class="text-muted">Tanggal:</small><br>
                            <small class="fw-semibold">{{ $att->date->isoFormat('DD/MM/Y') }}</small>
                        </div>
                        <div class="col-6">
                            <small class="text-muted">Status:</small><br>
                            <span class="badge bg-{{ $att->status_badge }}">{{ ucfirst($att->status) }}</span>
                        </div>
                    </div>

                    <div class="row mb-2">
                        <div class="col-6">
                            <small class="text-muted">Jam Masuk:</small><br>
                            <small class="fw-semibold">{{ $att->check_in_time ? $att->check_in_time->format('H:i') : '–' }}</small>
                        </div>
                        <div class="col-6">
                            <small class="text-muted">Jam Pulang:</small><br>
                            <small class="fw-semibold">{{ $att->check_out_time ? $att->check_out_time->format('H:i') : '–' }}</small>
                        </div>
                    </div>

                    @if($att->notes)
                    <div class="mb-2">
                        <small class="text-muted">Keterangan:</small><br>
                        <small>{{ $att->notes }}</small>
                    </div>
                    @endif
                </div>
            </div>
            @empty
            <div class="text-center text-muted py-5">
                @if(request()->hasAny(['start_date', 'end_date', 'status']))
                    <i class="fas fa-search fa-3x mb-3"></i>
                    <p>Tidak ditemukan data kehadiran dengan kriteria yang dipilih</p>
                    <a href="{{ route('siswa.riwayat') }}" class="btn btn-primary">Reset Filter</a>
                @else
                    <i class="fas fa-calendar-times fa-3x mb-3"></i>
                    <p>Belum ada data kehadiran</p>
                @endif
            </div>
            @endforelse
        </div>
        
        <!-- Enhanced Bootstrap Pagination -->
        @if($attendance->lastPage() > 1)
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <nav aria-label="Page navigation">
                    <ul class="pagination pagination-sm mb-0">
                        {{-- Previous Page Link --}}
                        @if($attendance->onFirstPage())
                            <li class="page-item disabled">
                                <span class="page-link"><i class="fas fa-chevron-left"></i></span>
                            </li>
                        @else
                            <li class="page-item">
                                <a class="page-link" href="{{ $attendance->appends(request()->query())->previousPageUrl() }}">
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                        @endif

                        {{-- Pagination Elements --}}
                        @php
                            $start = max($attendance->currentPage() - 2, 1);
                            $end = min($start + 4, $attendance->lastPage());
                            $start = max($end - 4, 1);
                        @endphp

                        @if($start > 1)
                            <li class="page-item">
                                <a class="page-link" href="{{ $attendance->appends(request()->query())->url(1) }}">1</a>
                            </li>
                            @if($start > 2)
                                <li class="page-item disabled"><span class="page-link">...</span></li>
                            @endif
                        @endif

                        @for($i = $start; $i <= $end; $i++)
                            @if($i == $attendance->currentPage())
                                <li class="page-item active">
                                    <span class="page-link">{{ $i }}</span>
                                </li>
                            @else
                                <li class="page-item">
                                    <a class="page-link" href="{{ $attendance->appends(request()->query())->url($i) }}">{{ $i }}</a>
                                </li>
                            @endif
                        @endfor

                        @if($end < $attendance->lastPage())
                            @if($end < $attendance->lastPage() - 1)
                                <li class="page-item disabled"><span class="page-link">...</span></li>
                            @endif
                            <li class="page-item">
                                <a class="page-link" href="{{ $attendance->appends(request()->query())->url($attendance->lastPage()) }}">{{ $attendance->lastPage() }}</a>
                            </li>
                        @endif

                        {{-- Next Page Link --}}
                        @if($attendance->hasMorePages())
                            <li class="page-item">
                                <a class="page-link" href="{{ $attendance->appends(request()->query())->nextPageUrl() }}">
                                    <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                        @else
                            <li class="page-item disabled">
                                <span class="page-link"><i class="fas fa-chevron-right"></i></span>
                            </li>
                        @endif
                    </ul>
                </nav>
            </div>
            <div>
                <small class="text-muted">
                    Halaman {{ $attendance->currentPage() }} dari {{ $attendance->lastPage() }} 
                    <span class="mx-1">•</span>
                    {{ number_format($attendance->total()) }} total data
                </small>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
