@extends('web_user.layouts.app')
@section('title', 'Daftar Verifikasi Audit')

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

    <style>
        [x-cloak] {
            display: none !important;
        }

        .select-clean {
            appearance: none !important;
            -webkit-appearance: none !important;
            -moz-appearance: none !important;
            background-image: none !important;
        }

        .select-clean::-ms-expand {
            display: none !important;
        }
    </style>

    {{-- 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-emerald-50 text-emerald-600">
                <i class="fas fa-clipboard-check text-xs"></i>
            </div>

            <div>
                <h2 class="text-base font-bold text-slate-800">Hasil Verifikasi</h2>
                <p class="mt-0.5 text-xs text-slate-500">
                    Kelola data audit yang sudah melalui proses verifikasi.
                </p>
            </div>
        </div>
    </div>

    {{-- Sudah Verifikasi --}}
    <div class="mb-4 overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm">

        {{-- Header Tabel --}}
        <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-check text-xs"></i>
                    </div>

                    <div>
                        <h3 class="text-sm font-bold text-slate-800">Daftar Audit Sudah Diverifikasi</h3>
                        <p class="mt-0.5 text-xs text-slate-500">
                            Data audit yang telah dilengkapi hasil verifikasi oleh anda namun belum diverifikasi akhir oleh admin.
                        </p>
                    </div>
                </div>
            </div>
        </div>

        {{-- Filter Form --}}
        <div class="border-b border-slate-100 bg-slate-50/70 px-4 py-3">
            <form action="{{ route('audit.verifikasi') }}" method="GET">
                <div class="rounded-xl border border-slate-200 bg-white px-3 py-3 shadow-sm">
                    <div class="grid grid-cols-1 gap-2 md:grid-cols-5">

                        {{-- Filter Tema --}}
                        <div x-data="{ open: false }" class="relative">
                            <select name="sudah_tema"
                                @focus="open = true"
                                @blur="open = false"
                                @change="open = false"
                                class="select-clean h-9 w-full rounded-lg border border-slate-200 bg-slate-50 px-3 pr-9 text-xs font-medium text-slate-600 outline-none transition focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-100">
                                <option value="">-- Pilih Tema --</option>

                                @foreach ($temaList as $id => $nama)
                                    <option value="{{ $id }}" {{ $sudahTema == $id ? 'selected' : '' }}>
                                        {{ $nama }}
                                    </option>
                                @endforeach
                            </select>

                            <span class="pointer-events-none absolute inset-y-0 right-3 flex items-center text-slate-500">
                                <i class="fas fa-chevron-down text-[10px] transition-transform duration-300"
                                    :class="open ? 'rotate-180 text-blue-500' : 'rotate-0'"></i>
                            </span>
                        </div>

                        {{-- Filter Bulan --}}
                        <div x-data="{ open: false }" class="relative">
                            <select name="sudah_bulan"
                                @focus="open = true"
                                @blur="open = false"
                                @change="open = false"
                                class="select-clean h-9 w-full rounded-lg border border-slate-200 bg-slate-50 px-3 pr-9 text-xs font-medium text-slate-600 outline-none transition focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-100">
                                <option value="">-- Pilih Bulan --</option>

                                @for ($i = 1; $i <= 12; $i++)
                                    <option value="{{ $i }}" {{ $sudahBulan == $i ? 'selected' : '' }}>
                                        {{ \Carbon\Carbon::create()->month($i)->translatedFormat('F') }}
                                    </option>
                                @endfor
                            </select>

                            <span class="pointer-events-none absolute inset-y-0 right-3 flex items-center text-slate-500">
                                <i class="fas fa-chevron-down text-[10px] transition-transform duration-300"
                                    :class="open ? 'rotate-180 text-blue-500' : 'rotate-0'"></i>
                            </span>
                        </div>

                        {{-- Filter Tahun --}}
                        <div x-data="{ open: false }" class="relative">
                            <select name="sudah_tahun"
                                @focus="open = true"
                                @blur="open = false"
                                @change="open = false"
                                class="select-clean h-9 w-full rounded-lg border border-slate-200 bg-slate-50 px-3 pr-9 text-xs font-medium text-slate-600 outline-none transition focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-100">
                                <option value="">-- Pilih Tahun --</option>

                                @foreach ($tahunList as $tahun)
                                    <option value="{{ $tahun }}" {{ $sudahTahun == $tahun ? 'selected' : '' }}>
                                        {{ $tahun }}
                                    </option>
                                @endforeach
                            </select>

                            <span class="pointer-events-none absolute inset-y-0 right-3 flex items-center text-slate-500">
                                <i class="fas fa-chevron-down text-[10px] transition-transform duration-300"
                                    :class="open ? 'rotate-180 text-blue-500' : 'rotate-0'"></i>
                            </span>
                        </div>

                        {{-- Tombol Filter & Reset --}}
                        <div class="grid grid-cols-2 gap-2 md:col-span-2">
                            <button type="submit"
                                class="inline-flex h-9 items-center justify-center gap-1.5 rounded-lg bg-blue-500 px-3 text-xs font-medium text-white transition hover:bg-blue-600">
                                <i class="fas fa-check text-[11px]"></i>
                                Filter
                            </button>

                            <a href="{{ route('audit.verifikasi', ['reset' => 1]) }}"
                                class="inline-flex h-9 items-center justify-center gap-1.5 rounded-lg bg-gray-200 px-3 text-xs font-medium text-slate-600 transition hover:bg-gray-300">
                                <i class="fas fa-rotate-left text-[11px]"></i>
                                Reset
                            </a>
                        </div>

                    </div>
                </div>
            </form>
        </div>

        {{-- Table Controls --}}
        <div class="flex flex-col gap-3 px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
            <div class="flex items-center gap-2 text-xs text-slate-600">
                <span>Tampilkan</span>

                <div x-data="{ open: false }" class="relative">
                    <select id="entriesSudah"
                        @focus="open = true"
                        @blur="open = false"
                        @change="open = false"
                        class="select-clean h-9 w-20 rounded-lg border border-slate-200 bg-slate-50 px-3 pr-8 text-xs font-medium text-slate-700 shadow-sm outline-none transition focus:border-blue-500 focus:bg-white focus:ring-2 focus:ring-blue-100">
                        <option value="5">5</option>
                        <option value="10" selected>10</option>
                        <option value="25">25</option>
                        <option value="50">50</option>
                    </select>

                    <span class="pointer-events-none absolute inset-y-0 right-3 flex items-center text-slate-500">
                        <i class="fas fa-chevron-down text-[10px] transition-transform duration-300"
                            :class="open ? 'rotate-180 text-blue-500' : 'rotate-0'"></i>
                    </span>
                </div>

                <span>entri</span>
            </div>

            <div class="relative w-full sm:w-64">
                <input type="text"
                    id="searchSudah"
                    placeholder="Cari data verifikasi..."
                    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>

        {{-- Table --}}
        <div class="overflow-x-auto px-4 pb-4">
            <div class="overflow-hidden rounded-lg border border-slate-200">
                <table class="w-full min-w-[900px] 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">Tema</th>
                            <th class="border-r border-slate-200 px-3 py-2 text-center font-bold">Divisi</th>
                            <th class="w-24 border-r border-slate-200 px-3 py-2 text-center font-bold">Status</th>
                            <th class="border-r border-slate-200 px-3 py-2 text-center font-bold">Tanggal Audit</th>
                            <th class="border-r border-slate-200 px-3 py-2 text-center font-bold">Tanggal Verifikasi</th>
                            <th class="border-r border-slate-200 px-3 py-2 text-center font-bold">Keterangan Sesudah</th>
                            <th class="w-32 px-3 py-2 text-center font-bold">Aksi</th>
                        </tr>
                    </thead>

                    <tbody id="bodySudah" class="divide-y divide-slate-100">
                        @forelse ($auditSudahDilengkapi as $index => $audit)
                            <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 font-semibold text-slate-700">
                                    {{ $audit->tema->nama ?? '-' }}
                                </td>

                                <td class="border-r border-slate-100 px-3 py-2 text-left text-slate-600">
                                    {{ $audit->bagian->nama_bagian ?? '-' }}
                                </td>

                                <td class="border-r border-slate-100 px-3 py-2 text-center">
                                    <span class="inline-flex rounded-full bg-red-100 px-3 py-1 text-xs font-semibold text-red-500">
                                        Open
                                    </span>
                                </td>

                                <td class="border-r border-slate-100 px-3 py-2 text-center text-slate-600">
                                    {{ \Carbon\Carbon::parse($audit->tanggal_audit)->translatedFormat('d F Y') }}
                                </td>

                                <td class="border-r border-slate-100 px-3 py-2 text-center text-slate-600">
                                    {{ $audit->tanggal_verifikasi ? \Carbon\Carbon::parse($audit->tanggal_verifikasi)->translatedFormat('d F Y') : '-' }}
                                </td>

                                <td class="border-r border-slate-100 px-3 py-2 text-left text-slate-600">
                                    {{ $audit->keterangan_sesudah ?? '-' }}
                                </td>

                                <td class="px-3 py-2 text-center">
                                    <a href="{{ route('audit.show', $audit->id) }}"
                                        class="inline-flex h-8 items-center justify-center gap-1 rounded-lg bg-green-500  px-3 text-xs font-medium text-white transition hover:bg-green-600">
                                        <i class="fas fa-eye text-[11px]"></i>
                                        Lihat Detail
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="px-3 py-6 text-center text-xs italic text-slate-500">
                                    Tidak ada audit yang sudah diverifikasi.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

    </div>

</div>
@endsection

@push('scripts')
<script>
document.addEventListener("DOMContentLoaded", function () {
    const searchInput = document.getElementById("searchSudah");
    const entriesSelect = document.getElementById("entriesSudah");
    const tableBody = document.getElementById("bodySudah");

    if (!searchInput || !entriesSelect || !tableBody) return;

    const rows = tableBody.querySelectorAll("tr");

    function filterRows() {
        const searchTerm = searchInput.value.toLowerCase();
        const perPage = parseInt(entriesSelect.value);
        let visible = 0;

        rows.forEach(row => {
            const text = row.innerText.toLowerCase();
            const match = text.includes(searchTerm);

            if (match && visible < perPage) {
                row.style.display = "";
                visible++;
            } else {
                row.style.display = "none";
            }
        });
    }

    searchInput.addEventListener("input", filterRows);
    entriesSelect.addEventListener("change", filterRows);

    filterRows();

    if (window.lucide) {
        window.lucide.createIcons();
    }
});
</script>
@endpush