@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.brands.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 fade show mb-4">
            {{ session('success') }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show mb-4">
            {{ session('error') }}<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>تعداد محصول</th>
                        <th>وضعیت</th>
                        <th style="width:130px">عملیات</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($brands as $brand)
                        <tr>
                            <td><span class="text-muted small">{{ $brand->id }}</span></td>
                            <td>
                                @if($brand->image)
                                    <img src="{{ asset('uploads/brands/' . $brand->image) }}"
                                         class="rounded" alt="{{ $brand->name }}"
                                         style="width:48px;height:48px;object-fit:contain;background:#f8f8f8">
                                @else
                                    <div class="rounded bg-light d-flex align-items-center justify-content-center"
                                         style="width:48px;height:48px;">
                                        <i class="ti ti-brand-nike text-muted"></i>
                                    </div>
                                @endif
                            </td>
                            <td><span class="fw-medium">{{ $brand->name }}</span></td>
                            <td><code class="small">{{ $brand->slug }}</code></td>
                            <td>{{ number_format($brand->products_count) }}</td>
                            <td>
                                @if($brand->is_active)
                                    <span class="badge bg-label-success">فعال</span>
                                @else
                                    <span class="badge bg-label-secondary">غیرفعال</span>
                                @endif
                            </td>
                            <td>
                                <div class="d-flex gap-1">
                                    <a href="{{ route('admin.brands.edit', $brand->slug) }}"
                                       class="btn btn-sm btn-outline-primary">
                                        <i class="ti ti-edit ti-xs"></i>
                                    </a>
                                    <form method="POST" action="{{ route('admin.brands.destroy', $brand->slug) }}" class="delete-form">
                                        @csrf @method('DELETE')
                                        <button type="button" class="btn btn-sm btn-outline-danger btn-delete"
                                                data-name="{{ $brand->name }}"
                                                @if($brand->products_count > 0) disabled title="این برند به محصولات وصل است" @endif>
                                            <i class="ti ti-trash ti-xs"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center py-5 text-muted">
                                <i class="ti ti-brand-nike fs-3 d-block mb-2"></i>
                                هیچ برندی ثبت نشده است.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($brands->isNotEmpty())
            <div class="card-footer text-muted small d-flex align-items-center justify-content-between">
                <span>{{ $brands->total() }} برند</span>
                {{ $brands->links() }}
            </div>
        @endif
    </div>

</div>
@endsection

@section('customjs')
<script>
document.querySelectorAll('.btn-delete').forEach(function (btn) {
    btn.addEventListener('click', function () {
        if (btn.disabled) return;
        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
