@extends('layout.admin')

@section('content')
<div class="mx-auto px-2 sm:px-3 md:px-4 max-w-6xl">

  {{-- Header --}}
  <div class="flex items-center justify-between gap-2 flex-wrap mb-2 mt-0.5">
    <h1 class="text-lg font-semibold tracking-tight">Products</h1>
    <a href="{{ route('admin.products.create') }}"
       class="inline-flex items-center gap-1.5 rounded-lg border border-indigo-600 bg-indigo-600 px-3.5 py-2 text-white hover:bg-indigo-700 text-sm">
      <i class="fa-solid fa-plus text-[12px]"></i>
      Add Product
    </a>
  </div>

  {{-- Flash --}}
  @if(session('success'))
    <div class="mb-2 rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-emerald-800 text-sm">
      <i class="fa-solid fa-circle-check mr-1.5"></i>{{ session('success') }}
    </div>
  @endif
  @if($errors->any())
    <div class="mb-2 rounded-lg border border-rose-200 bg-rose-50 px-3 py-2 text-rose-800 text-sm">
      <ul class="list-disc pl-4 space-y-0.5">
        @foreach ($errors->all() as $e)<li>{{ $e }}</li>@endforeach
      </ul>
    </div>
  @endif

  {{-- Card --}}
  <div class="rounded-xl bg-white shadow-sm border border-gray-100">
    <div class="px-3 py-2.5 border-b border-gray-100 flex items-center justify-between gap-2 flex-wrap">
      <div class="text-[13px] text-gray-500">
        Total: <span class="font-medium text-gray-700">{{ $products->total() }}</span>
      </div>

      {{-- Filters --}}
      <form method="GET" class="flex items-center gap-2">
        <div class="hidden sm:block">
          <div class="relative">
            <input type="text" name="q" value="{{ request('q') }}"
                   class="w-48 rounded-lg border border-gray-200 bg-white px-3 py-2 text-sm outline-none focus:ring-2 focus:ring-indigo-500/50"
                   placeholder="Search title/slug">
            <button class="absolute right-1.5 top-1/2 -translate-y-1/2 text-gray-500" aria-label="Search">
              <i class="fa-solid fa-magnifying-glass text-[13px]"></i>
            </button>
          </div>
        </div>
        <select name="cat" class="rounded-lg border border-gray-200 px-3 py-2 text-sm">
          <option value="">All categories</option>
          @foreach($cats as $c)
            <option value="{{ $c->id }}" @selected(request('cat')==$c->id)>{{ $c->name }}</option>
          @endforeach
        </select>
        <button class="inline-flex items-center gap-1.5 rounded-lg border border-gray-200 px-3 py-2 text-sm hover:bg-gray-50">
          <i class="fa-solid fa-filter text-[12px]"></i> Apply
        </button>
      </form>
    </div>

    <div class="overflow-auto">
      <table class="w-full border-collapse min-w-[980px]">
        <thead>
          <tr class="text-left text-sm text-gray-600 border-b border-gray-100">
            <th class="px-3 py-3 font-medium">#</th>
            <th class="px-3 py-3 font-medium">Banner</th>
            <th class="px-3 py-3 font-medium">Title</th>
            <th class="px-3 py-3 font-medium">Category</th>
            <th class="px-3 py-3 font-medium">Admin Price</th>
            <th class="px-3 py-3 font-medium">Reseller Price</th>
            <th class="px-3 py-3 font-medium">Stock</th>
            <th class="px-3 py-3 font-medium">Status</th>
            <th class="px-3 py-3 font-medium">Created</th>
            <th class="px-3 py-3 font-medium">Action</th>
          </tr>
        </thead>
        <tbody class="text-[15px]">
          @forelse($products as $i => $p)
            <tr class="border-b border-gray-50 hover:bg-gray-50/60">
              <td class="px-3 py-3 text-gray-600">
                {{ $products->firstItem() + $i }}
              </td>
              <td class="px-3 py-3">
                @if($p->banner_photo_url)
                  <img src="{{ $p->banner_photo_url }}" alt="banner" loading="lazy"
                       class="w-14 h-14 rounded-md object-cover border border-gray-200">
                @else
                  <span class="text-gray-400">—</span>
                @endif
              </td>
              <td class="px-3 py-3">
                <div class="font-semibold text-gray-800 line-clamp-1">{{ $p->title }}</div>
                <div class="text-[12px] text-gray-500">/{{ $p->slug }}</div>
              </td>
              <td class="px-3 py-3 text-gray-700">
                {{ $p->category?->name ?? '—' }}
              </td>

              <td class="px-3 py-3 text-gray-800">
                ৳{{ number_format($p->admin_price,2) }}
              </td>
              <td class="px-3 py-3 text-gray-800">
                @if($p->reseller_price > 0)
                  ৳{{ number_format($p->reseller_price,2) }}
                @else
                  <span class="text-gray-400">—</span>
                @endif
              </td>

              <td class="px-3 py-3 text-gray-700">
                {{ $p->stock }}
              </td>
              <td class="px-3 py-3">
                @if($p->status)
                  <span class="inline-flex items-center rounded-full border border-emerald-200 bg-emerald-50 px-2 py-0.5 text-[12px] text-emerald-700">Active</span>
                @else
                  <span class="inline-flex items-center rounded-full border border-rose-200 bg-rose-50 px-2 py-0.5 text-[12px] text-rose-700">Inactive</span>
                @endif
              </td>
              <td class="px-3 py-3 text-gray-600">
                {{ $p->created_at?->format('Y-m-d') }}
              </td>
              <td class="px-3 py-3">
                <div class="flex items-center gap-2">
                  <a href="{{ route('admin.products.edit',$p->id) }}"
                     class="inline-flex items-center gap-1.5 rounded-lg border border-gray-200 px-2.5 py-1.5 hover:bg-gray-50 text-sm">
                    <i class="fa-solid fa-pen-to-square text-[12px]"></i>
                    Edit
                  </a>
                  <form action="{{ route('admin.products.destroy',$p->id) }}" method="POST"
                        onsubmit="return confirm('Delete this product?');">
                    @csrf
                    <button type="submit"
                            class="inline-flex items-center gap-1.5 rounded-lg border border-rose-200 px-2.5 py-1.5 hover:bg-rose-50 text-sm text-rose-700">
                      <i class="fa-solid fa-trash text-[12px]"></i>
                      Delete
                    </button>
                  </form>
                </div>
              </td>
            </tr>
          @empty
            <tr>
              <td colspan="10" class="px-3 py-6 text-center text-gray-500">
                No products yet.
                <a href="{{ route('admin.products.create') }}" class="ml-2 inline-flex items-center gap-1.5 text-indigo-600 hover:underline">
                  <i class="fa-solid fa-plus text-[12px]"></i> Add your first product
                </a>
              </td>
            </tr>
          @endforelse
        </tbody>
      </table>
    </div>

    <div class="px-3 py-3">
      {{ $products->links() }}
    </div>
  </div>
</div>
@endsection