@extends('admin.layouts.app')

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

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

<div class="page-shell">
    <div class="page-head">
        <div>
            <h1 class="page-title">Suppliers</h1>
            <div class="page-subtitle">Manage supplier records, contact details, and purchase source data.</div>
        </div>
        @if(Route::has('admin.suppliers.create'))
            <a href="{{ route('admin.suppliers.create') }}" class="btn primary">Add Supplier</a>
        @endif
    </div>

    <div class="filter-card">
        <form method="GET" action="{{ route('admin.suppliers.index') }}">
            <div class="grid-4">
                <div>
                    <label for="q">Search</label>
                    <input type="text" id="q" name="q" value="{{ request('q') }}" placeholder="Search supplier, phone, email...">
                </div>

                <div class="form-actions" style="align-items:end;">
                    <button class="btn primary" type="submit">Filter</button>
                    <a href="{{ route('admin.suppliers.index') }}" class="btn btn-light">Reset</a>
                </div>
            </div>
        </form>
    </div>

    <div class="card">
        <div class="table-wrap">
            <table class="table">
                <thead>
                    <tr>
                        <th style="width:80px;">#</th>
                        <th>Name</th>
                        <th>Phone</th>
                        <th>Email</th>
                        <th>Company</th>
                        <th>Address</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($items as $index => $item)
                        <tr>
                            <td>{{ method_exists($items, 'firstItem') ? $items->firstItem() + $index : $index + 1 }}</td>
                            <td>
                                <div style="font-weight:700">{{ $item->name ?? '—' }}</div>
                                @if(!empty($item->notes))
                                    <div class="muted" style="font-size:12px;">{{ \Illuminate\Support\Str::limit($item->notes, 40) }}</div>
                                @endif
                            </td>
                            <td>{{ $item->phone ?? '—' }}</td>
                            <td>{{ $item->email ?? '—' }}</td>
                            <td>{{ $item->company ?? $item->company_name ?? '—' }}</td>
                            <td>{{ $item->address ?? '—' }}</td>
                            <td class="text-end" style="white-space:nowrap;">
                                @if(Route::has('admin.suppliers.edit'))
                                    <a href="{{ route('admin.suppliers.edit', $item) }}" class="btn btn-light">Edit</a>
                                @endif

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

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