<div class="container-fluid py-4">
    <x-title>
        Contact Hub
        <x-slot:buttons>
            <button wire:click="$toggle('showCategoryForm')" class="btn btn-outline-secondary me-2">Manage Categories</button>
            <a href="{{ route('templates.create') }}" wire:navigate class="btn btn-primary">+ New Template</a>
        </x-slot:buttons>
    </x-title>

    @if($showCategoryForm)
        <div class="card mb-4 shadow-sm border-info">
            <div class="card-body">
                <h5 class="card-title">Add New Category</h5>
                <div class="input-group">
                    <input type="text" wire:model="newCategoryName" class="form-control" placeholder="Category Name...">
                    <button wire:click="saveCategory" class="btn btn-info text-white">Save Category</button>
                </div>
                @error('newCategoryName') <small class="text-danger">{{ $message }}</small> @enderror
            </div>
        </div>
    @endif

    <div class="card shadow-sm">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                <tr>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Type</th>
                    <th>Last Updated</th>
                    <th class="text-end">Actions</th>
                </tr>
                </thead>
                <tbody>
                @forelse($templates as $template)
                    <tr>
                        <td>
                            <div class="fw-bold text-primary">{{ $template->title }}</div>
                            @if($template->subject) <small class="text-muted">{{ $template->subject }}</small> @endif
                        </td>
                        <td><span class="badge bg-info text-dark">{{ $template->category->name }}</span></td>
                        <td>
                                        <span class="badge {{ $template->type === 'email' ? 'bg-primary' : 'bg-warning text-dark' }}">
                                            {{ strtoupper($template->type) }}
                                        </span>
                        </td>
                        <td>{{ $template->updated_at->diffForHumans() }}</td>
                        <td class="text-end">
                            <a href="{{ route('templates.edit', $template) }}" wire:navigate class="btn btn-sm btn-outline-primary">Edit</a>
                            <button
                                    onclick="confirm('Delete this template?') || event.stopImmediatePropagation()"
                                    wire:click="deleteTemplate({{ $template->id }})"
                                    class="btn btn-sm btn-outline-danger"
                            >Delete</button>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="5" class="text-center py-5 text-muted">No templates found.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>