@extends('admin.layouts.layout')
@section('title')تگ‌های وبلاگ@endsection

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-1">تگ‌های وبلاگ</h4>
            <p class="text-muted mb-0">برچسب‌های مقالات</p>
        </div>
        <a href="{{ route('admin.blog.tags.create') }}" class="btn btn-primary">
            <i class="ti ti-plus me-1"></i> تگ جدید
        </a>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible mb-4">
            {{ session('success') }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="card">
        <div class="table-responsive">
            <table class="table border-top align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th>#</th>
                        <th>نام تگ</th>
                        <th>اسلاگ</th>
                        <th>تعداد مقاله</th>
                        <th style="width:130px">عملیات</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($tags as $tag)
                        <tr>
                            <td><span class="text-muted small">{{ $tag->id }}</span></td>
                            <td><span class="badge bg-label-info">{{ $tag->name }}</span></td>
                            <td><code class="small">{{ $tag->slug }}</code></td>
                            <td>{{ $tag->posts_count }}</td>
                            <td>
                                <div class="d-flex gap-1">
                                    <a href="{{ url('blog/tags/' . $tag->slug) }}" target="_blank" class="btn btn-sm btn-outline-secondary">
                                        <i class="ti ti-eye ti-xs"></i>
                                    </a>
                                    <a href="{{ route('admin.blog.tags.edit', $tag->slug) }}"
                                       class="btn btn-sm btn-outline-primary">
                                        <i class="ti ti-edit ti-xs"></i>
                                    </a>
                                    <form method="POST" action="{{ route('admin.blog.tags.destroy', $tag->slug) }}" class="delete-form">
                                        @csrf @method('DELETE')
                                        <button type="button" class="btn btn-sm btn-outline-danger btn-delete" data-name="{{ $tag->name }}">
                                            <i class="ti ti-trash ti-xs"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="text-center py-5 text-muted">تگی ثبت نشده است.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($tags->isNotEmpty())
            <div class="card-footer small text-muted d-flex align-items-center justify-content-between">
                <span>{{ $tags->total() }} تگ</span>
                {{ $tags->links() }}
            </div>
        @endif
    </div>

</div>
@endsection

@section('customjs')
<script>
document.querySelectorAll('.btn-delete').forEach(function (btn) {
    btn.addEventListener('click', function () {
        var name = this.dataset.name;
        var form = this.closest('.delete-form');
        Swal.fire({
            title: 'حذف تگ', html: 'حذف <strong>' + name + '</strong>?',
            icon: 'warning', showCancelButton: true,
            confirmButtonText: 'بله، حذف شود', cancelButtonText: 'انصراف',
            confirmButtonColor: '#d33', reverseButtons: true,
        }).then(function (r) { if (r.isConfirmed) form.submit(); });
    });
});
</script>
@endsection
