@extends('admin.layouts.app')

@section('title', 'Brands')
@section('page_title', 'Brands')

@section('content')
@php
    $items = $brands ?? $items ?? collect();
@endphp

<div class="page-shell">
    <div class="page-head">
        <div>
            <h1 class="page-title">Brands</h1>
            <div class="page-subtitle">Manage brand names for products, filters, and reporting accuracy.</div>
        </div>
    </div>

    <div class="grid-3">
        <div class="card">
            <h3 style="margin-top:0;">Add Brand</h3>

            @if(Route::has('admin.brands.store'))
                <form method="POST" action="{{ route('admin.brands.store') }}">
                    @csrf
                    <label for="name">Brand Name</label>
                    <input type="text" id="name" name="name" value="{{ old('name') }}" placeholder="Enter brand name" required>

                    @if(Route::has('admin.brands.index'))
                        <div class="form-actions">
                            <button type="submit" class="btn primary">Save Brand</button>
                            <a href="{{ route('admin.brands.index') }}" class="btn btn-light">Reset</a>
                        </div>
                    @else
                        <div class="form-actions">
                            <button type="submit" class="btn primary">Save Brand</button>
                        </div>
                    @endif
                </form>
            @else
                <div class="alert alert-info">Brand store route is not available yet.</div>
            @endif
        </div>

        <div class="card" style="grid-column:span 2;">
            <div class="page-head" style="margin-bottom:10px;">
                <div>
                    <h3 style="margin:0;">Brand List</h3>
                    <div class="page-subtitle">All saved brands are shown here.</div>
                </div>
            </div>

            <div class="table-wrap">
                <table class="table">
                    <thead>
                        <tr>
                            <th style="width:80px;">#</th>
                            <th>Name</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($items as $index => $item)
                            @php
                                $name = $item->name ?? $item->brand_name ?? '—';
                            @endphp
                            <tr>
                                <td>{{ method_exists($items, 'firstItem') ? $items->firstItem() + $index : $index + 1 }}</td>
                                <td>{{ $name }}</td>
                                <td class="text-end" style="white-space:nowrap;">
                                    @if(Route::has('admin.brands.edit'))
                                        <a href="{{ route('admin.brands.edit', $item) }}" class="btn btn-light">Edit</a>
                                    @endif

                                    @if(Route::has('admin.brands.destroy'))
                                        <form method="POST" action="{{ route('admin.brands.destroy', $item) }}" style="display:inline-block;" onsubmit="return confirm('Delete this brand?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger">Delete</button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="3">No brands found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if(method_exists($items, 'links'))
                <div style="margin-top:14px;">
                    {{ $items->links() }}
                </div>
            @endif
        </div>
    </div>
</div>
@endsection