@extends('admin.layouts.app')
@section('title', ucfirst($type) . 's')
@section('page-title', ucfirst($type) . 's')

@section('content')
<div x-data="postsManager('{{ $type }}')" x-init="init()">
    {{-- Toolbar --}}
    <div class="flex flex-col sm:flex-row sm:items-center gap-3 mb-6">
        <div class="flex items-center gap-3 flex-1 min-w-0">
            <div class="relative flex-1">
                <svg class="absolute left-3 top-2.5 w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                </svg>
                <input type="text" x-model="search" @input="onSearch()" placeholder="Search..."
                       class="pl-9 pr-4 py-2 text-sm border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-indigo-500 outline-none w-full">
            </div>
            <div class="flex gap-1 p-1 bg-gray-100 dark:bg-gray-700 rounded-lg flex-shrink-0">
                @foreach(['' => 'All', 'draft' => 'Draft', 'published' => 'Published'] as $val => $label)
                <button type="button" @click="status = '{{ $val }}'; fetch(1)"
                        :class="status === '{{ $val }}' ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 dark:text-gray-400 hover:text-gray-700'"
                        class="px-2.5 py-1.5 rounded-md text-xs font-medium transition-all whitespace-nowrap">{{ $label }}</button>
                @endforeach
            </div>
        </div>
        <a href="{{ route('admin.posts.create', $type) }}"
           class="flex items-center justify-center gap-2 px-4 py-2 bg-indigo-600 hover:bg-indigo-700 text-white rounded-lg text-sm font-medium transition-colors flex-shrink-0">
            <x-admin-icon name="plus" class="w-4 h-4" /> New {{ ucfirst($type) }}
        </a>
    </div>

    {{-- Table --}}
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm overflow-x-auto">
        <table class="w-full text-sm">
            <thead class="bg-gray-50 dark:bg-gray-700/50">
                <tr>
                    <th class="text-left px-4 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Title</th>
                    <th class="hidden sm:table-cell text-left px-4 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Author</th>
                    <th class="text-left px-4 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Status</th>
                    <th class="hidden md:table-cell text-left px-4 py-3 text-xs font-medium text-gray-500 dark:text-gray-400 uppercase">Date</th>
                    <th class="px-4 py-3"></th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200 dark:divide-gray-700">
                {{-- Loading --}}
                <template x-if="loading">
                    <tr><td colspan="5" class="px-6 py-12 text-center">
                        <div class="flex items-center justify-center gap-2 text-gray-400">
                            <svg class="w-5 h-5 animate-spin" fill="none" viewBox="0 0 24 24">
                                <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
                                <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/>
                            </svg>
                            Loading...
                        </div>
                    </td></tr>
                </template>

                {{-- Rows --}}
                <template x-if="!loading">
                    <template x-for="post in items" :key="post.id">
                        <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/30 transition-colors">
                            <td class="px-4 py-3">
                                <p class="font-medium text-gray-900 dark:text-white" x-text="post.title"></p>
                                <p class="text-xs text-gray-400" x-text="'/' + post.slug"></p>
                            </td>
                            <td class="hidden sm:table-cell px-4 py-3 text-gray-600 dark:text-gray-300" x-text="post.author"></td>
                            <td class="px-4 py-3">
                                <span :class="post.status === 'published'
                                    ? 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400'
                                    : 'bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400'"
                                    class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium capitalize"
                                    x-text="post.status">
                                </span>
                            </td>
                            <td class="hidden md:table-cell px-4 py-3 text-gray-500 dark:text-gray-400" x-text="post.created_at"></td>
                            <td class="px-4 py-3">
                                <div class="flex items-center justify-end gap-1">
                                <a :href="post.edit_url" class="p-1.5 text-gray-400 hover:text-indigo-600 dark:hover:text-indigo-400 rounded">
                                    <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="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"/></svg>
                                </a>
                                <button type="button" @click="deletPost(post.id)" :disabled="deletingId === post.id"
                                        class="p-1.5 text-gray-400 hover:text-red-600 dark:hover:text-red-400 rounded disabled:opacity-50">
                                    <svg x-show="deletingId === post.id" class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
                                    <svg x-show="deletingId !== post.id" 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="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                </button>
                                </div>
                            </td>
                        </tr>
                    </template>
                </template>

                {{-- Empty --}}
                <template x-if="!loading && items.length === 0">
                    <tr><td colspan="5" class="px-6 py-12 text-center text-gray-400">No {{ $type }}s found.</td></tr>
                </template>
            </tbody>
        </table>

        {{-- Pagination --}}
        <div x-show="meta.last_page > 1" class="px-4 py-4 border-t border-gray-200 dark:border-gray-700 flex flex-wrap items-center justify-between gap-3">
            <p class="text-sm text-gray-500 dark:text-gray-400">
                Total: <span x-text="meta.total"></span>
            </p>
            <div class="flex items-center gap-1">
                <button @click="fetch(meta.current_page - 1)" :disabled="meta.current_page === 1"
                        class="px-3 py-1.5 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-40 hover:bg-gray-50 dark:hover:bg-gray-700">←</button>
                <template x-for="p in pages()" :key="p">
                    <button @click="fetch(p)"
                            :class="p === meta.current_page ? 'bg-indigo-600 text-white border-indigo-600' : 'border-gray-300 dark:border-gray-600 hover:bg-gray-50 dark:hover:bg-gray-700 text-gray-700 dark:text-gray-300'"
                            class="px-3 py-1.5 text-sm rounded border" x-text="p"></button>
                </template>
                <button @click="fetch(meta.current_page + 1)" :disabled="meta.current_page === meta.last_page"
                        class="px-3 py-1.5 text-sm rounded border border-gray-300 dark:border-gray-600 disabled:opacity-40 hover:bg-gray-50 dark:hover:bg-gray-700">→</button>
            </div>
        </div>
    </div>
</div>
@endsection
