<x-app-layout>
    <x-slot name="header">
        <h2 class="h4 mb-0">Kelola Calon: {{ $candidate->nama_lengkap }}</h2>
    </x-slot>

    <div class="row g-3">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">Data Calon</div>
                <div class="card-body">
                    <form action="{{ route('admin-desa.candidate.update', $candidate) }}" method="POST" enctype="multipart/form-data">
                        @csrf @method('PUT')
                        @include('pilkades::admin-desa.candidate.form', ['candidate' => $candidate])
                        <button class="btn btn-primary">Simpan Perubahan</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-md-6" x-data="{ tab: 'programs' }">
            <ul class="nav nav-tabs mb-3">
                <li class="nav-item">
                    <a class="nav-link" :class="{ active: tab === 'programs' }" href="#" @click.prevent="tab = 'programs'">Program Kerja</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" :class="{ active: tab === 'gallery' }" href="#" @click.prevent="tab = 'gallery'">Galeri</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" :class="{ active: tab === 'videos' }" href="#" @click.prevent="tab = 'videos'">Video</a>
                </li>
            </ul>

            <div x-show="tab === 'programs'">
                <div class="card mb-3">
                    <div class="card-body">
                        <form action="{{ route('admin-desa.candidate.programs.store', $candidate) }}" method="POST">
                            @csrf
                            <div class="mb-2">
                                <x-text-input name="judul_program" placeholder="Judul Program" required />
                            </div>
                            <div class="mb-2">
                                <textarea name="deskripsi" class="form-control" rows="2" placeholder="Deskripsi"></textarea>
                            </div>
                            <button class="btn btn-primary btn-sm">Tambah Program</button>
                        </form>
                    </div>
                </div>
                @foreach ($candidate->programs as $program)
                    <div class="d-flex justify-content-between align-items-start border-bottom py-2">
                        <div>
                            <div class="fw-semibold">{{ $program->judul_program }}</div>
                            <div class="small text-muted">{{ $program->deskripsi }}</div>
                        </div>
                        <form action="{{ route('admin-desa.candidate.programs.destroy', $program) }}" method="POST">
                            @csrf @method('DELETE')
                            <button class="btn btn-sm btn-outline-danger">Hapus</button>
                        </form>
                    </div>
                @endforeach
            </div>

            <div x-show="tab === 'gallery'" x-cloak>
                <div class="card mb-3">
                    <div class="card-body">
                        <form action="{{ route('admin-desa.candidate.gallery.store', $candidate) }}" method="POST" enctype="multipart/form-data">
                            @csrf
                            <div class="mb-2">
                                <input type="file" name="photo" class="form-control" accept="image/*" required>
                            </div>
                            <div class="mb-2">
                                <x-text-input name="caption" placeholder="Caption (opsional)" />
                            </div>
                            <button class="btn btn-primary btn-sm">Tambah Foto</button>
                        </form>
                    </div>
                </div>
                <div class="row g-2">
                    @foreach ($candidate->gallery as $item)
                        <div class="col-4">
                            <div class="card">
                                <img src="{{ Storage::url($item->media->path) }}" class="card-img-top">
                                <div class="card-body p-2">
                                    <form action="{{ route('admin-desa.candidate.gallery.destroy', $item) }}" method="POST">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-sm btn-outline-danger w-100">Hapus</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>

            <div x-show="tab === 'videos'" x-cloak>
                <div class="card mb-3">
                    <div class="card-body">
                        <form action="{{ route('admin-desa.candidate.videos.store', $candidate) }}" method="POST">
                            @csrf
                            <div class="mb-2">
                                <x-text-input name="judul" placeholder="Judul Video" required />
                            </div>
                            <div class="mb-2">
                                <x-text-input name="url_video" placeholder="URL Video (YouTube dll)" required />
                            </div>
                            <button class="btn btn-primary btn-sm">Tambah Video</button>
                        </form>
                    </div>
                </div>
                @foreach ($candidate->videos as $video)
                    <div class="d-flex justify-content-between align-items-center border-bottom py-2">
                        <div>
                            <div class="fw-semibold">{{ $video->judul }}</div>
                            <a href="{{ $video->url_video }}" target="_blank" class="small">{{ $video->url_video }}</a>
                        </div>
                        <form action="{{ route('admin-desa.candidate.videos.destroy', $video) }}" method="POST">
                            @csrf @method('DELETE')
                            <button class="btn btn-sm btn-outline-danger">Hapus</button>
                        </form>
                    </div>
                @endforeach
            </div>
        </div>
    </div>
</x-app-layout>
