@extends('navbar')
@section('content')
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Hasil Foto Anda - Skyrent Studio</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="{{ asset('css/photo.css') }}">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">

    <style>
        .container {
            margin-top: 30px;
        }
        .card {
            border-radius: 10px;
            overflow: hidden;
        }
        .card img {
            height: 250px;
            object-fit: cover;
        }
        .download-btn {
            width: 100%;
        }
        .alert {
            margin-top: 20px;
        }
        footer {
            background: #222;
            color: white;
            text-align: center;
            padding: 15px;
            margin-top: 30px;
        }
    </style>
</head>
<body>

<div class="text-center mt-4">
    <div class="alert alert-warning d-inline-block" role="alert" style="max-width: 600px;">
        <strong>Catatan:</strong> Token dan foto Anda akan <u>terhapus secara otomatis dalam waktu 7 hari</u> setelah diunggah. Pastikan Anda mengunduh foto sebelum batas waktu tersebut.
    </div>
</div>

<div class="container">
    <h2 class="mb-4 text-center">Hasil Foto Anda</h2>

    <!-- Form Input Token -->
    <div class="text-center mb-4">
        <form method="GET" action="{{ route('photos.verify') }}">
            <div class="input-group">
                <input type="text" name="token" class="form-control" placeholder="Masukkan Token Anda" required>
                <button type="submit" class="btn btn-primary">Cek Foto</button>
            </div>
        </form>
    </div>

    <!-- Menampilkan Token dan Tanggal Booking yang Dimiliki Customer -->
    @if(isset($bookings) && !$bookings->isEmpty())
        <div class="text-center mb-4">
            <h5>Token dan Tanggal Pemotretan Anda:</h5>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Token</th>
                        <th>Tanggal Pemotretan</th>
                        <th>Tanggal Upload</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($bookings as $booking)
                        @php $seenDates = []; @endphp
                        @foreach($booking->photos as $photo)
                            @if (!in_array($booking->tanggal_pemotretan, $seenDates))
                                <tr>
                                    <td>{{ $photo->token }}</td>
                                    <td>{{ \Carbon\Carbon::parse($booking->tanggal_pemotretan)->format('d-m-Y') }}</td>
                                    <td>{{ \Carbon\Carbon::parse($photo->created_at)->format('d-m-Y H:i') }}</td>
                                </tr>
                                @php $seenDates[] = $booking->tanggal_pemotretan; @endphp
                            @endif
                        @endforeach
                    @endforeach
                </tbody>

            </table>
        </div>
    @endif

    <!-- Menampilkan Foto berdasarkan Token -->
    @if(session('error'))
        <div class="alert alert-danger text-center">
            <p>{{ session('error') }}</p>
        </div>
    @endif

    @if(isset($photos) && !$photos->isEmpty())
        <!-- Tombol Download Semua Foto -->
        <div class="text-end mb-3">
            <a href="{{ route('photos.downloadAll', ['token' => request('token')]) }}" class="btn btn-success">
                <i class="fas fa-file-archive"></i> Download Semua Foto
            </a>
        </div>

        <div class="masonry-gallery">
            @foreach($photos as $photo)
                <div class="masonry-photo">
                    <a href="{{ asset('/' . $photo->file_path) }}" download class="download-btn" title="Download Foto">
                        <i class="fas fa-download"></i>
                    </a>
                    <img src="{{ asset('/' . $photo->file_path) }}" alt="Foto ID {{ $photo->id }}">
                </div>
            @endforeach
        </div>
    @else
        <p>Token tidak ditemukan atau foto belum diupload.</p>
    @endif
</div>

</body>
</html>
@endsection
