@extends('layouts.admin')

@section('content')
    <section class="py-4">
        <div class="container-fluid px-lg-4">
            {{-- =========================
            JUDUL HALAMAN
        ========================= --}}
            <h4 class="fw-bold mb-3">Halaman Video Kegiatan</h4>

            {{-- =========================
            BUTTON TAMBAH VIDEO
            (Trigger Modal)
        ========================= --}}
            <button class="btn btn-primary mb-3" data-bs-toggle="modal" data-bs-target="#tambahVideo">
                Tambah Video
            </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

            {{-- =========================
            ALERT ERROR VALIDATION
        ========================= --}}
            @if ($errors->any())
                <div class="alert alert-danger alert-dismissible fade show">
                    <ul class="mb-0">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            @endif

            {{-- =========================
            TABLE VIDEO (RESPONSIVE)
        ========================= --}}
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-light">
                        <tr>
                            <th width="50">#</th>
                            <th>Judul</th>
                            <th width="260">Preview Video</th>
                            <th width="150">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($videos as $no => $item)
                            <tr>
                                {{-- Nomor --}}
                                <td>{{ $no + 1 }}</td>

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

                                {{-- Preview Video YouTube --}}
                                <td>
                                    <div class="ratio ratio-16x9">
                                        <iframe src="https://www.youtube.com/embed/{{ $item->youtube_code }}"
                                            title="YouTube video" allowfullscreen>
                                        </iframe>
                                    </div>
                                </td>

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

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

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

                                        {{-- Header Modal --}}
                                        <div class="modal-header">
                                            <h5 class="modal-title">Edit Video</h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                        </div>

                                        {{-- Body Modal --}}
                                        <div class="modal-body">
                                            <form action="{{ route('video.update', $item->id) }}" method="POST">
                                                @csrf

                                                <div class="row">
                                                    {{-- Preview Video --}}
                                                    <div class="col-md-6 mb-3">
                                                        <label class="form-label">Preview</label>
                                                        <div class="ratio ratio-16x9">
                                                            <iframe
                                                                src="https://www.youtube.com/embed/{{ $item->youtube_code }}"
                                                                allowfullscreen>
                                                            </iframe>
                                                        </div>
                                                    </div>

                                                    {{-- Form Input --}}
                                                    <div class="col-md-6">
                                                        {{-- Judul --}}
                                                        <div class="mb-3">
                                                            <label class="form-label">Judul Video</label>
                                                            <input type="text" name="judul" class="form-control"
                                                                value="{{ $item->judul }}" required>
                                                        </div>

                                                        {{-- Youtube Code --}}
                                                        <div class="mb-3">
                                                            <label class="form-label">
                                                                YouTube Code
                                                                <small class="text-muted">(contoh: dQw4w9WgXcQ)</small>
                                                            </label>
                                                            <input type="text" name="youtube_code" class="form-control"
                                                                value="{{ $item->youtube_code }}" required>
                                                        </div>

                                                        {{-- Button Submit --}}
                                                        <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 TAMBAH VIDEO
========================= --}}
    <div class="modal fade" id="tambahVideo" tabindex="-1">
        <div class="modal-dialog modal-md modal-dialog-centered">
            <div class="modal-content">

                {{-- Header --}}
                <div class="modal-header">
                    <h5 class="modal-title">Tambah Video</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                {{-- Body --}}
                <div class="modal-body">
                    <form action="{{ route('video.store') }}" method="POST">
                        @csrf

                        {{-- Judul Video --}}
                        <div class="mb-3">
                            <label class="form-label">Judul Video</label>
                            <input type="text" name="judul" class="form-control" required>
                        </div>

                        {{-- Youtube Code --}}
                        <div class="mb-3">
                            <label class="form-label">
                                YouTube Code
                                <small class="text-muted">(ambil dari URL)</small>
                            </label>
                            <input type="text" name="youtube_code" class="form-control" placeholder="contoh: dQw4w9WgXcQ"
                                required>
                        </div>

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

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