@extends('layouts.admin')

@section('title', 'Laporan')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="mb-0">Laporan</h2>
    <div>
        <a href="{{ route('admin.reports.export.excel', request()->query()) }}" class="btn btn-success btn-sm">Eksport Excel</a>
        <a href="{{ route('admin.reports.export.pdf', request()->query()) }}" class="btn btn-danger btn-sm">Eksport PDF</a>
        <button onclick="window.print()" class="btn btn-outline-secondary btn-sm">Cetak Laporan</button>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-2"><div class="card p-3 text-center"><small>Jumlah Peserta</small><h5>{{ $stats['total_participants'] }}</h5></div></div>
    <div class="col-md-2"><div class="card p-3 text-center"><small>Telah Muat Turun</small><h5 class="text-success">{{ $stats['downloaded_participants'] }}</h5></div></div>
    <div class="col-md-2"><div class="card p-3 text-center"><small>Belum Muat Turun</small><h5 class="text-warning">{{ $stats['not_downloaded_participants'] }}</h5></div></div>
    <div class="col-md-2"><div class="card p-3 text-center"><small>Peratus</small><h5>{{ $stats['download_percentage'] }}%</h5></div></div>
    <div class="col-md-2"><div class="card p-3 text-center"><small>Transaksi</small><h5>{{ $stats['total_transactions'] }}</h5></div></div>
</div>

<ul class="nav nav-tabs mb-3">
    <li class="nav-item"><a class="nav-link {{ $type === 'overall' ? 'active' : '' }}" href="?type=overall">Keseluruhan</a></li>
    <li class="nav-item"><a class="nav-link {{ $type === 'downloaded' ? 'active' : '' }}" href="?type=downloaded">Telah Muat Turun</a></li>
    <li class="nav-item"><a class="nav-link {{ $type === 'not_downloaded' ? 'active' : '' }}" href="?type=not_downloaded">Belum Muat Turun</a></li>
    <li class="nav-item"><a class="nav-link {{ $type === 'history' ? 'active' : '' }}" href="?type=history">Sejarah Muat Turun</a></li>
</ul>

@if($data)
<div class="card"><div class="card-body p-0">
    <div class="table-responsive">
        <table class="table table-hover mb-0">
            <thead class="table-light">
                @if($type === 'downloaded')
                <tr><th>Nama</th><th>No. Peserta</th><th>Kategori</th><th>Pertama</th><th>Terakhir</th><th>Bilangan</th></tr>
                @elseif($type === 'not_downloaded')
                <tr><th>Nama</th><th>No. Peserta</th><th>Kategori</th><th>Tarikh Import</th></tr>
                @elseif($type === 'history')
                <tr><th>Nama</th><th>Tarikh</th><th>IP</th><th>Peranti</th><th>Browser</th><th>Status</th><th>Jenis</th></tr>
                @endif
            </thead>
            <tbody>
                @foreach($data as $row)
                <tr>
                    @if($type === 'downloaded')
                    <td>{{ $row->full_name }}</td><td>{{ $row->participant_number }}</td><td>{{ $row->category }}</td>
                    <td>{{ $row->first_downloaded_at?->format('d/m/Y H:i') }}</td><td>{{ $row->last_downloaded_at?->format('d/m/Y H:i') }}</td><td>{{ $row->download_count }}</td>
                    @elseif($type === 'not_downloaded')
                    <td>{{ $row->full_name }}</td><td>{{ $row->participant_number }}</td><td>{{ $row->category }}</td><td>{{ $row->created_at->format('d/m/Y H:i') }}</td>
                    @elseif($type === 'history')
                    <td>{{ $row->participant?->full_name }}</td><td>{{ $row->downloaded_at->format('d/m/Y H:i') }}</td><td>{{ $row->ip_address }}</td>
                    <td>{{ $row->device_type }}</td><td>{{ $row->browser }}</td><td>{{ $row->download_status }}</td><td>{{ $row->download_type }}</td>
                    @endif
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    <div class="p-3">{{ $data->appends(request()->query())->links() }}</div>
</div></div>
@endif
@endsection
