@extends('admin.layouts.app')
@section('title', 'Products')
@section('content')
    <div class="product-list-container p-4">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2 class="h4">Product List</h2>
            <a href="{{ route('admin.product.create') }}" class="btn btn-success">
                <i class="fas fa-plus-circle me-2"></i> Add Product
            </a>
        </div>

        <div class="table-responsive bg-white p-3">
            <table id="productTable" class="table table-hover">
                <thead>
                    <tr>
                        <th>Image</th>
                        <th>Product Name</th>
                        <th>Product cate</th>
                        <th>Price</th>
                        <th>Stock</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {{-- Loop through the products passed from the controller --}}
                    @foreach ($products as $product)
                        <tr>
                            <td>
                                {{-- Display the product image --}}
                                @if($product->image)
                                    <img src="{{ asset($product->image) }}" alt="{{ $product->name }}" class="product-image-small">
                                @else
                                    <img src="[https://via.placeholder.com/50](https://via.placeholder.com/50)" alt="No Image" class="product-image-small">
                                @endif
                            </td>
                            <td>{{ $product->name }}</td>
                            <td>{{ $product->category->name ?? 'N/A' }}</td> 
                            <td>₹ {{ number_format($product->price, 2) }}</td>
                            <td>{{ $product->stock }}</td>
                            <td class="">
                                <a href="{{ route('admin.product.edit', $product->id) }}" class="btn btn-primary btn-sm me-2">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <form action="{{ route('admin.product.delete', $product->id) }}" method="POST" class="d-inline">
                                   @csrf
                                    <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this product?');">
                                        <i class="fas fa-trash-alt"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
@endsection