@extends('layouts.app')

@section('title', 'Product Catalog')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">Product Catalog</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                    <li class="breadcrumb-item active">Products</li>
                </ol>
            </nav>
        </div>
        <a href="{{ route('products.create') }}" class="btn btn-primary px-4">
            <i class="bi bi-box-seam me-2"></i> Add Product
        </a>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Product / SKU</th>
                            <th>Category</th>
                            <th>Unit</th>
                            <th>Sale Price</th>
                            <th>BOM Status</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($products as $product)
                            <tr>
                                <td class="ps-4">
                                    <div class="fw-bold">{{ $product->name }}</div>
                                    <span class="text-muted small badge bg-light border">{{ $product->sku }}</span>
                                </td>
                                <td>
                                    <span class="badge bg-secondary-subtle text-secondary border">
                                        {{ str_replace('_', ' ', ucfirst($product->category)) }}
                                    </span>
                                </td>
                                <td>{{ $product->unit_of_measure }}</td>
                                <td class="fw-semibold text-primary">${{ number_format($product->sale_price, 2) }}</td>
                                <td>
                                    @if($product->category == 'raw_material')
                                        <span class="text-muted small italic">Not Applicable</span>
                                    @elseif($product->activeBom)
                                        <span class="badge bg-success-subtle text-success border border-success-subtle">
                                            <i class="bi bi-check2-circle me-1"></i> Active v{{ $product->activeBom->version }}
                                        </span>
                                    @else
                                        <span class="badge bg-warning-subtle text-warning border border-warning-subtle text-wrap"
                                            style="max-width: 100px;">
                                            No Active BOM
                                        </span>
                                    @endif
                                </td>
                                <td class="text-end pe-4">
                                    <div class="d-flex justify-content-end gap-2">
                                        @if($product->category != 'raw_material')
                                            <a href="{{ route('boms.create', ['product_id' => $product->id]) }}"
                                                class="btn btn-outline-primary btn-sm" title="Configure BOM">
                                                <i class="bi bi-diagram-3"></i>
                                            </a>
                                        @endif
                                        <a href="{{ route('products.edit', $product->id) }}" class="btn btn-light btn-sm">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">Initialize your catalog to start production.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="mt-4">
        {{ $products->links() }}
    </div>
@endsection