@extends('layouts.admin')

@section('title', 'Products')

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Catalog</span>
            <h1>Products</h1>
        </div>
        <a class="button primary" href="{{ route('admin.products.create') }}">Add product</a>
    </section>

    @if (session('status'))
        <p class="form-status">{{ session('status') }}</p>
    @endif

    <section class="panel">
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Product</th>
                        <th>Category</th>
                        <th>Price</th>
                        <th>Purchase</th>
                        <th>Stock</th>
                        <th>Media</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($products as $product)
                        <tr>
                            <td>
                                <strong>{{ $product->name }}</strong>
                                @if ($product->is_featured)
                                    <small>Featured</small>
                                @endif
                            </td>
                            <td>{{ $product->category->name }}</td>
                            <td>
                                {{ \App\Models\SiteSetting::formatPrice($product->sellingPrice()) }}
                                @if ($product->compareAtPrice())
                                    <small><s>{{ \App\Models\SiteSetting::formatPrice($product->compareAtPrice()) }}</s></small>
                                @endif
                            </td>
                            <td>{{ \App\Models\SiteSetting::formatPrice($product->purchase_price) }}</td>
                            <td>{{ $product->stock }}</td>
                            <td>{{ $product->images_count }} images{{ $product->youtube_url ? ' + video' : '' }}{{ $product->variants_count ? ' + '.$product->variants_count.' variants' : '' }}</td>
                            <td><span class="status {{ $product->is_active ? 'paid' : 'refunded' }}">{{ $product->is_active ? 'Active' : 'Inactive' }}</span></td>
                            <td>
                                <div class="table-actions">
                                    <a class="text-button" href="{{ route('admin.products.edit', $product) }}">Edit</a>
                                    <a class="text-button" href="{{ route('admin.inventory.index', ['product_id' => $product->id]) }}">Inventory</a>
                                    <form method="POST" action="{{ route('admin.products.toggle', $product) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button class="text-button" type="submit">{{ $product->is_active ? 'Disable' : 'Enable' }}</button>
                                    </form>
                                    <form method="POST" action="{{ route('admin.products.destroy', $product) }}" onsubmit="return confirm('Delete this product?')">
                                        @csrf
                                        @method('DELETE')
                                        <button class="text-button danger" type="submit">Delete</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8">No products yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $products->links() }}
    </section>
@endsection
