<?php
use Livewire\Component;
use App\Models\Faq;
?>

<div class="max-content">
    <header class="flex justify-between items-center mb-10">
        <div>
            <h1 class="text-2xl font-semibold tracking-tight">Essential Inquiries (FAQs)</h1>
            <p class="text-sm text-gray-500 mt-1">Manage the frequently asked questions shown on the about page.</p>
        </div>
        <a href="{{ route('admin.faqs.create') }}" wire:navigate class="btn-primary" style="background-color: #99BC85; color: white; padding: 8px 16px; border-radius: 6px; font-size: 13px; font-weight: 500; display: inline-flex; align-items: center; gap: 8px;">
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path></svg>
            New FAQ
        </a>
    </header>

    @if (session()->has('message'))
        <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
            <span class="block sm:inline">{{ session('message') }}</span>
        </div>
    @endif

    <div class="card">
        <div class="overflow-x-auto">
            <table>
                <thead>
                    <tr>
                        <th>Question</th>
                        <th width="150">Order</th>
                        <th class="text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($faqs as $faq)
                        <tr>
                            <td class="px-6 py-4 font-medium max-w-[500px]">
                                <div class="text-sm text-gray-900">{{ $faq->question }}</div>
                                <div class="text-[11px] text-gray-400 mt-1 truncate">{{ Str::limit($faq->answer, 100) }}</div>
                            </td>
                            <td class="px-6 py-4">
                                <input type="number" wire:change="updateOrder({{ $faq->id }}, $event.target.value)" class="input-field w-20 py-1" value="{{ $faq->sort_order }}">
                            </td>
                            <td class="px-6 py-4 text-right">
                                <a href="{{ route('admin.faqs.edit', $faq->id) }}" wire:navigate class="text-sage font-semibold text-xs px-2" style="color: #99BC85;">Edit</a>
                                <button wire:click="delete({{ $faq->id }})" wire:confirm="Are you sure?" wire:loading.attr="disabled" class="text-red-500 font-semibold text-xs px-2">Delete</button>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="3" class="px-6 py-4 text-center text-gray-500 text-sm">No FAQs found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>