@extends('admin.layouts.app')

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

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

<div class="page-shell">
    <div class="page-head">
        <div>
            <h1 class="page-title">Colors</h1>
            <div class="page-subtitle">Manage color options for products and variants.</div>
        </div>
    </div>

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

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

                    <div class="form-actions">
                        <button type="submit" class="btn primary">Save Color</button>
                        <a href="{{ route('admin.colors.index') }}" class="btn btn-light">Reset</a>
                    </div>
                </form>
            @else
                <div class="alert alert-info">Color 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;">Color List</h3>
                    <div class="page-subtitle">All saved colors 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->color_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.colors.edit'))
                                        <a href="{{ route('admin.colors.edit', $item) }}" class="btn btn-light">Edit</a>
                                    @endif

                                    @if(Route::has('admin.colors.destroy'))
                                        <form method="POST" action="{{ route('admin.colors.destroy', $item) }}" style="display:inline-block;" onsubmit="return confirm('Delete this color?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger">Delete</button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="3">No colors 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