@extends('layout.admin')

@section('content')
<div class="mx-auto px-2 sm:px-3 md:px-4 max-w-7xl space-y-3">
  <div class="flex items-center justify-between gap-2 flex-wrap">
    <h1 class="text-lg font-semibold tracking-tight">Vendor Details</h1>
    <div class="flex items-center gap-2">
      <a href="{{ route('admin.vendor-ship.orders', ['q' => $vendorProfile->vendor_name]) }}" class="inline-flex items-center gap-1.5 rounded-lg border border-indigo-700 bg-indigo-700 px-3.5 py-2 text-white hover:bg-indigo-800 text-sm">View Orders</a>
      <a href="{{ route('admin.vendor-ship.vendors') }}" class="inline-flex items-center gap-1.5 rounded-lg border border-slate-700 bg-slate-700 px-3.5 py-2 text-white hover:bg-slate-800 text-sm">Back</a>
    </div>
  </div>

  <div class="rounded-xl bg-white shadow-sm border border-gray-100 p-4">
    <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
      <div><img src="{{ $vendorProfile->image_url }}" alt="" class="w-24 h-24 rounded-lg object-cover border border-slate-200"></div>
      <div class="md:col-span-3 text-sm space-y-1">
        <div><span class="font-bold">Vendor Name:</span> {{ $vendorProfile->vendor_name }}</div>
        <div><span class="font-bold">Address:</span> {{ $vendorProfile->address }}</div>
        <div><span class="font-bold">User:</span> {{ $vendorProfile->user->name ?? '-' }} ({{ $vendorProfile->user->mobile ?? '-' }})</div>
        <div><span class="font-bold">Referral Code:</span> {{ $vendorProfile->user->referral_code ?? '-' }}</div>
        <div><span class="font-bold">Status:</span> {{ ucfirst($vendorProfile->status) }}</div>
        <div>
          <span class="font-bold">Service Charge:</span>
          @php
            $paidAt = $vendorProfile->service_charge_paid_at ?? null;
            $paidAmount = (float) ($vendorProfile->service_charge_amount ?? 0);
          @endphp
          @if($paidAt)
            <span class="text-emerald-700 font-semibold">Paid {{ number_format($paidAmount, 2) }}৳</span>
            <span class="text-xs text-slate-500">({{ optional($paidAt)->format('d M Y, h:i A') }})</span>
            @if(!empty($vendorProfile->service_charge_txn_id))
              <div class="text-xs text-slate-500">TXN: {{ $vendorProfile->service_charge_txn_id }}</div>
            @endif
          @elseif(($serviceCharge ?? 0) > 0)
            <span class="text-amber-700 font-semibold">Due {{ number_format((float) $serviceCharge, 2) }}৳</span>
          @else
            <span class="text-slate-500">Off</span>
          @endif
        </div>
        @if(!empty($vendorProfile->admin_note))
          <div><span class="font-bold">Admin Note:</span> {{ $vendorProfile->admin_note }}</div>
        @endif
      </div>
    </div>
  </div>

  <div class="grid grid-cols-2 md:grid-cols-5 gap-3">
    <div class="rounded-xl border border-slate-200 bg-white p-3"><div class="text-xs text-slate-500">Today</div><div class="text-xl font-extrabold">{{ $summary['today'] }}</div></div>
    <div class="rounded-xl border border-slate-200 bg-white p-3"><div class="text-xs text-slate-500">Yesterday</div><div class="text-xl font-extrabold">{{ $summary['yesterday'] }}</div></div>
    <div class="rounded-xl border border-slate-200 bg-white p-3"><div class="text-xs text-slate-500">Last 7 Days</div><div class="text-xl font-extrabold">{{ $summary['last7'] }}</div></div>
    <div class="rounded-xl border border-slate-200 bg-white p-3"><div class="text-xs text-slate-500">Last 30 Days</div><div class="text-xl font-extrabold">{{ $summary['last30'] }}</div></div>
    <div class="rounded-xl border border-slate-200 bg-white p-3"><div class="text-xs text-slate-500">Total</div><div class="text-xl font-extrabold">{{ $summary['total'] }}</div></div>
  </div>

  <div class="grid grid-cols-2 md:grid-cols-4 gap-3">
    <div class="rounded-xl border border-amber-200 bg-amber-50 p-3"><div class="text-xs text-amber-700">Pending</div><div class="text-xl font-extrabold text-amber-800">{{ (int)($statusCounts->pending ?? 0) }}</div></div>
    <div class="rounded-xl border border-blue-200 bg-blue-50 p-3"><div class="text-xs text-blue-700">Processing</div><div class="text-xl font-extrabold text-blue-800">{{ (int)($statusCounts->processing ?? 0) }}</div></div>
    <div class="rounded-xl border border-emerald-200 bg-emerald-50 p-3"><div class="text-xs text-emerald-700">Delivered</div><div class="text-xl font-extrabold text-emerald-800">{{ (int)($statusCounts->delivered ?? 0) }}</div></div>
    <div class="rounded-xl border border-rose-200 bg-rose-50 p-3"><div class="text-xs text-rose-700">Cancelled</div><div class="text-xl font-extrabold text-rose-800">{{ (int)($statusCounts->cancelled ?? 0) }}</div></div>
  </div>

  <div class="rounded-xl bg-white shadow-sm border border-gray-100 overflow-auto">
    <div class="px-4 py-3 border-b border-slate-100 font-bold">Recent Vendor Orders</div>
    <table class="w-full min-w-[760px] text-sm">
      <thead class="bg-slate-50 text-slate-600">
        <tr>
          <th class="px-3 py-2 text-left">Order No</th>
          <th class="px-3 py-2 text-left">Status</th>
          <th class="px-3 py-2 text-left">Amount</th>
          <th class="px-3 py-2 text-left">Date</th>
        </tr>
      </thead>
      <tbody>
        @forelse($vendorProfile->orders as $o)
          <tr class="border-t border-slate-100">
            <td class="px-3 py-2">{{ $o->order_no ?: ('#V' . $o->id) }}</td>
            <td class="px-3 py-2">{{ ucfirst($o->status) }}</td>
            <td class="px-3 py-2">Tk {{ number_format((float)$o->total_amount, 2) }}</td>
            <td class="px-3 py-2">{{ optional($o->created_at)->format('d M Y, h:i A') }}</td>
          </tr>
        @empty
          <tr><td colspan="4" class="px-3 py-6 text-center text-slate-500">No vendor orders found.</td></tr>
        @endforelse
      </tbody>
    </table>
  </div>

  <div class="rounded-xl bg-white shadow-sm border border-gray-100 overflow-auto">
    <div class="px-4 py-3 border-b border-slate-100 font-bold">Recent Products</div>
    <table class="w-full min-w-[980px] text-sm">
      <thead class="bg-slate-50 text-slate-600">
        <tr>
          <th class="px-3 py-2 text-left">Image</th>
          <th class="px-3 py-2 text-left">Title</th>
          <th class="px-3 py-2 text-left">Category</th>
          <th class="px-3 py-2 text-left">Admin Price</th>
          <th class="px-3 py-2 text-left">Seller Price</th>
          <th class="px-3 py-2 text-left">Sizes</th>
          <th class="px-3 py-2 text-left">Status</th>
        </tr>
      </thead>
      <tbody>
        @forelse($vendorProfile->products as $p)
          <tr class="border-t border-slate-100">
            <td class="px-3 py-2"><img src="{{ $p->main_image_url }}" class="w-12 h-12 rounded object-cover border border-slate-200" alt=""></td>
            <td class="px-3 py-2">{{ $p->title }}</td>
            <td class="px-3 py-2">{{ $p->category->name ?? '-' }}</td>
            <td class="px-3 py-2">Tk {{ number_format((float)$p->admin_price, 2) }}</td>
            <td class="px-3 py-2">Tk {{ number_format((float)$p->seller_price, 2) }}</td>
            <td class="px-3 py-2">{{ $p->sizes_text }}</td>
            <td class="px-3 py-2">{{ ucfirst($p->status) }}</td>
          </tr>
        @empty
          <tr><td colspan="7" class="px-3 py-6 text-center text-slate-500">No products found for this vendor.</td></tr>
        @endforelse
      </tbody>
    </table>
  </div>
</div>
@endsection
