@extends('layout.user')

@section('content')
@php
  $o = $vendorOrder;
  $p = $pdOrder?->product;
  $img = optional($p?->images?->first())->photo ?: ($p->banner_photo ?? null);
  $imgUrl = $img ? asset(ltrim($img, '/')) : asset('images/no-image.png');
  $canSeeCustomer = in_array($o->status, ['processing', 'delivered'], true)
    || ($o->status === 'cancelled' && (!empty($o->cancellation_requested_at) || !empty($o->cancellation_response)));
  $hasPendingCancelRequest = !empty($o->cancellation_requested_at) && empty($o->cancellation_response);
@endphp

<div class="max-w-5xl mx-auto px-3 py-3 space-y-3">
  @if(session('success'))
    <div class="rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-sm text-emerald-700">{{ session('success') }}</div>
  @endif
  @if($errors->any())
    <div class="rounded-lg border border-rose-200 bg-rose-50 px-3 py-2 text-sm text-rose-700">
      @foreach($errors->all() as $e)
        <div>{{ $e }}</div>
      @endforeach
    </div>
  @endif

  <div class="flex items-center justify-between">
    <h1 class="text-lg font-extrabold text-slate-900">Order Details</h1>
    <a href="{{ route('vendor-ship.orders') }}" class="text-sm font-bold text-blue-700">Back to Orders</a>
  </div>

  <div class="rounded-xl border border-slate-200 bg-white p-3">
    <div class="grid md:grid-cols-3 gap-3">
      <div>
        <img src="{{ $imgUrl }}" alt="Product" class="w-full max-h-64 object-cover rounded-lg border border-slate-200">
      </div>
      <div class="md:col-span-2 grid sm:grid-cols-2 gap-3 text-sm">
        <div class="rounded-lg border border-slate-200 p-3">
          <div class="text-xs text-slate-500">Vendor Order No</div>
          <div class="font-bold">{{ $o->order_no ?: ('#V' . $o->id) }}</div>
        </div>
        <div class="rounded-lg border border-slate-200 p-3">
          <div class="text-xs text-slate-500">Status</div>
          <div class="font-bold">{{ ucfirst($o->status) }}</div>
        </div>
        <div class="rounded-lg border border-slate-200 p-3">
          <div class="text-xs text-slate-500">Product</div>
          <div class="font-bold">{{ $p->title ?? 'N/A' }}</div>
        </div>
        <div class="rounded-lg border border-slate-200 p-3">
          <div class="text-xs text-slate-500">Order Date</div>
          <div class="font-bold">{{ optional($o->created_at)->format('d M Y, h:i A') }}</div>
        </div>
        <div class="rounded-lg border border-slate-200 p-3">
          <div class="text-xs text-slate-500">Quantity</div>
          <div class="font-bold">{{ (int)($pdOrder?->qty ?? 0) }}</div>
        </div>
        <div class="rounded-lg border border-slate-200 p-3">
          <div class="text-xs text-slate-500">Amount</div>
          <div class="font-bold">Tk {{ number_format((float)$o->total_amount, 2) }}</div>
        </div>
      </div>
    </div>
  </div>

  <div class="rounded-xl border border-slate-200 bg-white p-3">
    <h2 class="font-extrabold text-slate-800 mb-2">Order Item Details</h2>
    <div class="grid sm:grid-cols-2 gap-3 text-sm">
      <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Size:</span> <span class="font-semibold">{{ $pdOrder?->size ?: '-' }}</span></div>
      <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Color:</span> <span class="font-semibold">{{ $pdOrder?->color ?: '-' }}</span></div>
      <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Admin Price:</span> <span class="font-semibold">Tk {{ number_format((float)($pdOrder?->admin_price ?? 0), 2) }}</span></div>
      <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Seller Price:</span> <span class="font-semibold">Tk {{ number_format((float)($pdOrder?->sale_price ?? 0), 2) }}</span></div>
      <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Wholesale Total:</span> <span class="font-semibold">Tk {{ number_format((float)($pdOrder?->wholesale_total ?? 0), 2) }}</span></div>
      <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Grand Total:</span> <span class="font-semibold">Tk {{ number_format((float)($pdOrder?->grand_total ?? 0), 2) }}</span></div>
    </div>
  </div>

  <div class="rounded-xl border border-slate-200 bg-white p-3">
    <h2 class="font-extrabold text-slate-800 mb-2">Customer Details</h2>
    @if($canSeeCustomer && $pdOrder)
      <div class="grid sm:grid-cols-2 gap-3 text-sm">
        <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Name:</span> <span class="font-semibold">{{ $pdOrder?->customer_name ?: '-' }}</span></div>
        <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Phone:</span> <span class="font-semibold">{{ $pdOrder?->customer_phone ?: '-' }}</span></div>
        <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Alt Phone:</span> <span class="font-semibold">{{ $pdOrder?->alt_phone ?: '-' }}</span></div>
        <div class="rounded-lg border border-slate-200 p-3 sm:col-span-2"><span class="text-slate-500">Address:</span> <span class="font-semibold">{{ $pdOrder?->address ?: '-' }}</span></div>
        <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">Division:</span> <span class="font-semibold">{{ $pdOrder?->division ?: '-' }}</span></div>
        <div class="rounded-lg border border-slate-200 p-3"><span class="text-slate-500">District / Thana:</span> <span class="font-semibold">{{ $pdOrder?->district ?: '-' }} / {{ $pdOrder?->thana ?: '-' }}</span></div>
      </div>
    @else
      <div class="rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-800">
        এই অর্ডারটি এখনো processing এ নেওয়া হয়নি। Processing করার পর customer details দেখা যাবে।
      </div>
    @endif
  </div>

  <div class="rounded-xl border border-slate-200 bg-white p-3 space-y-3">
    <h2 class="font-extrabold text-slate-800">Shipping / Action</h2>

    @if(!empty($o->tracking_link))
      <div class="rounded-lg border border-blue-200 bg-blue-50 px-3 py-2 text-sm">
        <span class="text-blue-700 font-semibold">Tracking Link:</span>
        <a href="{{ $o->tracking_link }}" target="_blank" class="text-blue-700 underline break-all">{{ $o->tracking_link }}</a>
      </div>
    @endif

    @if(!empty($o->cancel_reason))
      <div class="rounded-lg border border-rose-200 bg-rose-50 px-3 py-2 text-sm">
        <span class="text-rose-700 font-semibold">Cancel Reason:</span>
        <span class="text-rose-700">{{ $o->cancel_reason }}</span>
      </div>
    @endif

    @if(!empty($o->admin_action_note))
      <div class="rounded-lg border border-slate-200 bg-slate-50 px-3 py-2 text-sm">
        <span class="text-slate-700 font-semibold">Admin Comment:</span>
        <span class="text-slate-700">{{ $o->admin_action_note }}</span>
      </div>
    @endif

    @if($o->status === 'processing' && $hasPendingCancelRequest)
      <div class="rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-800">
        Cancel request already sent to user. User response pending.
      </div>
    @endif

    @if(in_array($o->status, ['processing', 'delivered']))
      <form method="POST" action="{{ route('vendor-ship.orders.tracking', $o->id) }}" class="space-y-2">
        @csrf
        @method('PATCH')
        <label class="text-xs font-bold text-slate-600">Tracking Link (URL)</label>
        <div class="flex flex-col sm:flex-row gap-2">
          <input type="url" name="tracking_link" required value="{{ old('tracking_link', $o->tracking_link) }}"
                 class="flex-1 rounded-lg border border-slate-300 px-3 py-2 text-sm"
                 placeholder="https://courier.example/tracking/123">
          <button class="rounded-lg bg-indigo-600 px-3 py-2 text-sm font-semibold text-white hover:bg-indigo-700">Update Tracking</button>
        </div>
      </form>
    @endif

    @if($o->status === 'pending')
      <div class="flex flex-wrap gap-2">
        <form method="POST" action="{{ route('vendor-ship.orders.status', $o->id) }}">
          @csrf
          @method('PATCH')
          <input type="hidden" name="status" value="processing">
          <button class="rounded-lg bg-blue-600 px-3 py-2 text-sm font-semibold text-white hover:bg-blue-700">Move to Processing</button>
        </form>
      </div>
      <form method="POST" action="{{ route('vendor-ship.orders.status', $o->id) }}" class="space-y-2">
        @csrf
        @method('PATCH')
        <input type="hidden" name="status" value="cancelled">
        <label class="text-xs font-bold text-slate-600">Cancel Reason (Required)</label>
        <textarea name="cancel_reason" required rows="2" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm" placeholder="Why cancelled?"></textarea>
        <button class="rounded-lg bg-rose-600 px-3 py-2 text-sm font-semibold text-white hover:bg-rose-700">Cancel Order</button>
      </form>
    @endif

    @if($o->status === 'processing' && !$hasPendingCancelRequest)
      <div class="flex flex-wrap gap-2">
        <form method="POST" action="{{ route('vendor-ship.orders.status', $o->id) }}">
          @csrf
          @method('PATCH')
          <input type="hidden" name="status" value="delivered">
          <button class="rounded-lg bg-emerald-600 px-3 py-2 text-sm font-semibold text-white hover:bg-emerald-700">Mark Delivered</button>
        </form>
      </div>
      <form method="POST" action="{{ route('vendor-ship.orders.status', $o->id) }}" class="space-y-2">
        @csrf
        @method('PATCH')
        <input type="hidden" name="status" value="cancelled">
        <label class="text-xs font-bold text-slate-600">Cancel Request Reason (Required)</label>
        <textarea name="cancel_reason" required rows="2" class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm" placeholder="Why cancellation requested from user?"></textarea>
        <button class="rounded-lg bg-rose-600 px-3 py-2 text-sm font-semibold text-white hover:bg-rose-700">Request Cancel</button>
      </form>
    @endif
  </div>
</div>
@endsection

