@extends('admin.layouts.app')
@section('title', 'Manajemen Tema')

@section('content')
<div class="min-h-screen bg-slate-50 p-3">

    {{-- Popup sukses --}}
    @if(session('success'))
        <div id="popup-success"
            class="fixed top-4 left-1/2 z-50 w-[90%] max-w-sm -translate-x-1/2 rounded-xl bg-green-500 px-4 py-3 text-sm text-white shadow-xl"
            style="transform: translate(-50%, -120%); opacity: 0; transition: all .5s ease;">
            <div class="flex items-center gap-3">
                <div class="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-white/15">
                    <i class="fas fa-check text-xs"></i>
                </div>

                <div>
                    <p class="text-sm font-semibold">Berhasil</p>
                    <p class="text-xs text-white/90">{{ session('success') }}</p>
                </div>
            </div>
        </div>

        <script>
            document.addEventListener('DOMContentLoaded', () => {
                const popup = document.getElementById('popup-success');

                popup.style.transform = 'translate(-50%, 0)';
                popup.style.opacity = '1';

                setTimeout(() => {
                    popup.style.transform = 'translate(-50%, -120%)';
                    popup.style.opacity = '0';

                    setTimeout(() => popup.remove(), 500);
                }, 4000);
            });
        </script>
    @endif

    {{-- Header Judul --}}
    <div class="mb-4 overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm">
        <div class="flex items-center gap-3 px-4 py-3">
            <div class="flex h-9 w-9 items-center justify-center rounded-lg bg-blue-50 text-blue-600">
                <i class="fas fa-tags text-xs"></i>
            </div>

            <div>
                <h2 class="text-base font-bold text-slate-800">Manajemen Tema</h2>
                <p class="mt-0.5 text-xs text-slate-500">
                    Menambah, mengedit, dan menghapus tema audit.
                </p>
            </div>
        </div>
    </div>

    <div class="grid grid-cols-1 gap-4 lg:grid-cols-[300px_1fr]">

        {{-- Form Tambah Tema --}}
        <div class="h-fit overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm">
            <div class="border-b border-slate-100 bg-white px-4 py-3">
                <div class="flex items-center gap-3">
                    <div class="flex h-9 w-9 items-center justify-center rounded-lg bg-blue-50 text-blue-600">
                        <i class="fas fa-plus text-xs"></i>
                    </div>

                    <div>
                        <h3 class="text-sm font-bold text-slate-800">Tambah Tema</h3>
                        <p class="mt-0.5 text-xs text-slate-500">
                            Masukkan tema baru.
                        </p>
                    </div>
                </div>
            </div>

            <form action="{{ route('admin.tema.store') }}" method="POST" class="p-4">
                @csrf

                <div class="mb-4">
                    <label class="mb-1 block text-xs font-semibold text-slate-600">
                        Nama Tema <span class="text-red-500">*</span>
                    </label>

                    <input type="text"
                        name="nama"
                        required
                        placeholder="Masukkan nama tema"
                        class="h-9 w-full rounded-lg border border-slate-200 bg-slate-50 px-3 text-xs text-slate-700 outline-none transition placeholder:text-slate-400 focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-100">
                </div>

                <div class="flex items-center justify-end gap-2">
                    <button type="reset"
                        class="inline-flex h-8 w-20 items-center justify-center gap-1 rounded-lg bg-gray-200 px-2 text-xs font-medium text-slate-600 transition hover:bg-gray-300">
                        <i class="fas fa-rotate-left text-[10px]"></i>
                        Batal
                    </button>

                    <button type="submit"
                        class="inline-flex h-8 w-20 items-center justify-center gap-1 rounded-lg bg-blue-500 px-2 text-xs font-medium text-white transition hover:bg-blue-600">
                        <i class="fas fa-check text-[10px]"></i>
                        Tambah
                    </button>
                </div>
            </form>
        </div>

        {{-- Tabel Tema --}}
        <div class="overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm">
            <div class="border-b border-slate-100 bg-white px-4 py-3">
                <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
                    <div class="flex items-center gap-3">
                        <div class="flex h-9 w-9 items-center justify-center rounded-lg bg-emerald-50 text-emerald-600">
                            <i class="fas fa-list text-xs"></i>
                        </div>

                        <div>
                            <h3 class="text-sm font-bold text-slate-800">Daftar Tema</h3>
                            <p class="mt-0.5 text-xs text-slate-500">
                                Data tema audit yang tersimpan.
                            </p>
                        </div>
                    </div>

                    <div class="relative w-full sm:w-64">
                        <input type="text"
                            id="searchTema"
                            placeholder="Cari tema..."
                            class="h-9 w-full rounded-lg border border-slate-200 bg-slate-50 pl-3 pr-9 text-xs text-slate-700 shadow-sm outline-none transition placeholder:text-slate-400 focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-100">

                        <span class="pointer-events-none absolute inset-y-0 right-3 flex items-center text-slate-400">
                            <svg class="h-3.5 w-3.5"
                                fill="none"
                                stroke="currentColor"
                                stroke-width="2"
                                viewBox="0 0 24 24">
                                <path stroke-linecap="round"
                                    stroke-linejoin="round"
                                    d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                            </svg>
                        </span>
                    </div>
                </div>
            </div>

            <div class="overflow-x-auto px-4 py-4">
                <div class="overflow-hidden rounded-lg border border-slate-200">
                    <table id="tableTema" class="w-full min-w-[600px] border-collapse text-xs">
                        <thead>
                            <tr class="border-b border-slate-200 bg-slate-50 uppercase tracking-wide text-slate-500">
                                <th class="w-14 border-r border-slate-200 px-3 py-2 text-center font-bold">No</th>
                                <th class="border-r border-slate-200 px-3 py-2 text-center font-bold">Nama Tema</th>
                                <th class="w-32 px-3 py-2 text-center font-bold">Aksi</th>
                            </tr>
                        </thead>

                        <tbody class="divide-y divide-slate-100">
                            @forelse($tema as $index => $t)
                                <tr class="transition hover:bg-slate-50">
                                    <td class="border-r border-slate-100 px-3 py-2 text-center font-medium text-slate-600">
                                        {{ $index + 1 }}
                                    </td>

                                    <td class="border-r border-slate-100 px-3 py-2 text-left">
                                        <span class="text-display font-semibold text-slate-700">
                                            {{ $t->nama }}
                                        </span>

                                        <form action="{{ route('admin.tema.update', $t->id) }}"
                                            method="POST"
                                            id="form-tema-{{ $t->id }}"
                                            class="hidden edit-form">
                                            @csrf
                                            @method('PUT')

                                            <input type="text"
                                                name="nama"
                                                value="{{ $t->nama }}"
                                                class="h-8 w-full rounded-lg border border-slate-200 bg-white px-2 text-xs text-slate-700 outline-none transition focus:border-blue-500 focus:ring-2 focus:ring-blue-100">
                                        </form>
                                    </td>

                                    <td class="px-3 py-2 text-center">
                                        <div class="flex items-center justify-center gap-1.5">

                                            {{-- Tombol Edit --}}
                                            <button type="button"
                                                class="btn-edit inline-flex h-7 w-7 items-center justify-center rounded-md bg-blue-50 text-blue-600 transition hover:bg-blue-600 hover:text-white"
                                                title="Edit Tema">
                                                <i class="fas fa-edit text-[11px]"></i>
                                            </button>

                                            {{-- Tombol Simpan --}}
                                            <button type="submit"
                                                form=""
                                                class="btn-save hidden h-7 w-7 items-center justify-center rounded-md bg-emerald-50 text-emerald-600 transition hover:bg-emerald-600 hover:text-white"
                                                title="Simpan Perubahan">
                                                <i class="fas fa-check text-[11px]"></i>
                                            </button>

                                            {{-- Tombol Batal --}}
                                            <button type="button"
                                                class="btn-cancel hidden h-7 w-7 items-center justify-center rounded-md bg-gray-100 text-gray-500 transition hover:bg-gray-500 hover:text-white"
                                                title="Batal Edit">
                                                <i class="fas fa-times text-[11px]"></i>
                                            </button>

                                            {{-- Tombol Hapus --}}
                                            <form action="{{ route('admin.tema.destroy', $t->id) }}"
                                                method="POST"
                                                class="inline form-delete"
                                                onsubmit="return confirm('Yakin ingin menghapus tema ini?');">
                                                @csrf
                                                @method('DELETE')

                                                <button type="submit"
                                                    class="inline-flex h-7 w-7 items-center justify-center rounded-md bg-red-50 text-red-500 transition hover:bg-red-500 hover:text-white"
                                                    title="Hapus Tema">
                                                    <i class="fas fa-trash text-[11px]"></i>
                                                </button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="3" class="px-3 py-6 text-center text-xs italic text-slate-500">
                                        Belum ada data tema.
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </div>
</div>

{{-- Script inline edit --}}
<script>
document.addEventListener('DOMContentLoaded', () => {
    document.querySelectorAll('.btn-edit').forEach(editBtn => {
        editBtn.addEventListener('click', e => {
            const row = e.target.closest('tr');
            const textDisplay = row.querySelector('.text-display');
            const editForm = row.querySelector('.edit-form');
            const saveBtn = row.querySelector('.btn-save');
            const cancelBtn = row.querySelector('.btn-cancel');
            const deleteForm = row.querySelector('.form-delete');
            const editButton = row.querySelector('.btn-edit');

            textDisplay.classList.add('hidden');
            editForm.classList.remove('hidden');

            saveBtn.classList.remove('hidden');
            saveBtn.classList.add('inline-flex');

            cancelBtn.classList.remove('hidden');
            cancelBtn.classList.add('inline-flex');

            editButton.classList.add('hidden');
            deleteForm.classList.add('hidden');

            saveBtn.setAttribute('form', editForm.getAttribute('id'));
            editForm.querySelector('input[name="nama"]').focus();
        });
    });

    document.querySelectorAll('.btn-cancel').forEach(cancelBtn => {
        cancelBtn.addEventListener('click', e => {
            const row = e.target.closest('tr');
            const textDisplay = row.querySelector('.text-display');
            const editForm = row.querySelector('.edit-form');
            const saveBtn = row.querySelector('.btn-save');
            const editButton = row.querySelector('.btn-edit');
            const deleteForm = row.querySelector('.form-delete');

            textDisplay.classList.remove('hidden');
            editForm.classList.add('hidden');

            saveBtn.classList.add('hidden');
            saveBtn.classList.remove('inline-flex');

            cancelBtn.classList.add('hidden');
            cancelBtn.classList.remove('inline-flex');

            editButton.classList.remove('hidden');
            deleteForm.classList.remove('hidden');
        });
    });

    const searchInput = document.getElementById('searchTema');
    const table = document.getElementById('tableTema');

    if (searchInput && table) {
        const rows = table.querySelectorAll('tbody tr');

        searchInput.addEventListener('keyup', function () {
            const keyword = searchInput.value.toLowerCase();

            rows.forEach(row => {
                const text = row.textContent.toLowerCase();
                row.style.display = text.includes(keyword) ? '' : 'none';
            });
        });
    }
});
</script>

@endsection