@extends('layouts.admin')

@section('content')
    <section class="py-4">
        <div class="container-fluid">

            <h4 class="fw-bold mb-3">Halaman Photo Kegiatan</h4>

            <!-- Button Upload -->
            <button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#uploadModal">
                Upload Photo
            </button>

            {{-- Alert Success --}}
            @if (session()->has('success'))
                <div class="alert alert-success alert-dismissible fade show">
                    <strong>Informasi :</strong> {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            <!-- TABLE -->
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-light">
                        <tr>
                            <th width="50">#</th>
                            <th width="180">Image</th>
                            <th>Nama Kegiatan</th>
                            <th width="150">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($photos as $no => $item)
                            <tr>
                                <td>{{ $no + 1 }}</td>

                                <td>
                                    <img src="{{ asset('storage/photo/' . $item->image) }}" class="img-fluid rounded"
                                        style="max-height:120px; object-fit:cover;">
                                </td>

                                <td>{{ $item->judul }}</td>

                                <td>
                                    <button class="btn btn-warning btn-sm w-100 mb-1" data-bs-toggle="modal"
                                        data-bs-target="#editModal{{ $item->id }}">
                                        Edit
                                    </button>

                                    <form action="{{ route('photo.destroy', $item->id) }}" method="POST">
                                        @csrf
                                        <button onclick="return confirm('Yakin dihapus?')"
                                            class="btn btn-danger btn-sm w-100">
                                            Hapus
                                        </button>
                                    </form>
                                </td>
                            </tr>

                            <!-- MODAL EDIT -->
                            <div class="modal fade" id="editModal{{ $item->id }}" tabindex="-1">
                                <div class="modal-dialog modal-lg modal-dialog-centered">
                                    <div class="modal-content">

                                        <div class="modal-header">
                                            <h5 class="modal-title">Edit Photo</h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                        </div>

                                        <div class="modal-body">
                                            <form action="{{ route('photo.update', $item->id) }}" method="POST"
                                                enctype="multipart/form-data">
                                                @csrf

                                                <div class="row">
                                                    <div class="col-md-5 text-center mb-3">
                                                        <img src="{{ asset('storage/photo/' . $item->image) }}"
                                                            class="img-fluid rounded mb-2"
                                                            style="max-height:200px; object-fit:cover;">

                                                        <input type="hidden" name="old_image" value="{{ $item->image }}">
                                                        <input type="file" name="image" class="form-control">
                                                    </div>

                                                    <div class="col-md-7">
                                                        <div class="mb-3">
                                                            <label class="form-label">Nama Kegiatan</label>
                                                            <input type="text" name="judul" class="form-control"
                                                                value="{{ $item->judul }}" required>
                                                        </div>

                                                        <button type="submit" class="btn btn-primary w-100">
                                                            Update
                                                        </button>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>

                                    </div>
                                </div>
                            </div>
                            <!-- END MODAL EDIT -->
                        @endforeach
                    </tbody>
                </table>
            </div>

        </div>
    </section>

    <!-- MODAL UPLOAD -->
    <div class="modal fade" id="uploadModal" tabindex="-1">
        <div class="modal-dialog modal-md modal-dialog-centered">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title">Upload Photo</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <div class="modal-body">
                    <form action="{{ route('photo.store') }}" method="POST" enctype="multipart/form-data">
                        @csrf

                        <div class="mb-3">
                            <label class="form-label">Pilih Photo</label>
                            <input type="file" name="image" class="form-control" required>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Nama Kegiatan</label>
                            <input type="text" name="judul" class="form-control" required>
                        </div>

                        <button type="submit" class="btn btn-primary w-100">
                            Simpan
                        </button>
                    </form>
                </div>

            </div>
        </div>
    </div>
@endsection
