@extends('backend.layout.app')

@section('content')
<div class="container">
    <div class="container-header">
        <h1>{{ $title }}</h1>
        <a href="{{ route('backend.products.create') }}" class="btn btn-primary">Add product</a>
    </div>

    <div class="card table-card">
        <div class="table-header">
            <form method="get" action="{{ route('backend.products.index') }}" class="table-search-form">
                <div class="table-search-form__group">
                    <div class="table-search table-search--narrow">
                        <input type="text" name="search" class="search-input" placeholder="Search products..." value="{{ $search ?? '' }}">
                        <i class="las la-search"></i>
                    </div>
                    <button type="submit" class="btn btn-secondary search-btn">Search</button>
                    @if(request()->filled('search'))
                        <a href="{{ route('backend.products.index') }}" class="btn btn-outline">Clear</a>
                    @endif
                </div>
            </form>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Price</th>
                        <th>In stock</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($products as $p)
                    <tr>
                        <td>{{ $products->firstItem() + $loop->index }}</td>
                        <td>
                            @if($p->image)
                                <img src="{{ asset($p->image) }}" alt="" style="max-height:28px; margin-right:6px;" class="img-thumbnail">
                            @endif
                            {{ $p->name }}
                        </td>
                        <td>{{ $p->productCategory->name ?? '-' }}</td>
                        <td>{{ number_format($p->price, 2) }}</td>
                        <td>{{ $p->in_stock }}</td>
                        <td>{{ $p->status ? 'On' : 'Off' }}</td>
                        <td>
                            <div class="table-actions">
                                <a href="{{ route('backend.products.accounts', $p->id) }}" class="table-btn" title="Accounts"><i class="las la-list"></i></a>
                                <a href="{{ route('backend.products.edit', $p->id) }}" class="table-btn" title="Edit"><i class="las la-pen"></i></a>
                                <form action="{{ route('backend.products.destroy', $p->id) }}" method="post" class="d-inline" onsubmit="return confirm('Delete this product?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="table-btn" title="Delete"><i class="las la-trash"></i></button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="7" class="text-center">No products yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="pagination-wrapper">
            {{ $products->links('vendor.pagination.default') }}
        </div>
    </div>
</div>
@endsection
