@extends('layouts.admin')

@section('title', 'Purchase Order '.$purchaseOrder->order_number)

@section('page-title', 'Purchase Order '.$purchaseOrder->order_number)

@section('breadcrumbs')
    <a href="{{ route('admin.purchase-orders.index') }}" class="hover:text-primary">Purchase Orders</a>
    <span class="material-symbols-outlined text-xs">chevron_right</span>
    <span class="text-primary font-medium">{{ $purchaseOrder->order_number }}</span>
@endsection

@section('content')
<div class="max-w-[1400px] mx-auto space-y-6">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">{{ $purchaseOrder->order_number }}</h1>
            <p class="text-sm text-gray-500 mt-1">Created {{ $purchaseOrder->created_at?->format('M d, Y g:i A') ?? '—' }}</p>
        </div>
        <div class="flex flex-wrap items-center gap-2">
            @if(Route::has('admin.purchase-orders.edit'))
                <a href="{{ route('admin.purchase-orders.edit', $purchaseOrder) }}" class="inline-flex items-center gap-2 px-4 py-2 bg-primary text-white rounded-lg text-sm font-medium hover:bg-blue-600 transition-colors">
                    <span class="material-symbols-outlined text-lg">edit</span>
                    Edit
                </a>
            @endif
            <a href="{{ route('admin.purchase-orders.index') }}" class="inline-flex items-center gap-2 px-4 py-2 border border-gray-300 text-gray-700 rounded-lg text-sm font-medium hover:bg-gray-50 transition-colors">
                <span class="material-symbols-outlined text-lg">arrow_back</span>
                Back to list
            </a>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <div class="lg:col-span-2 space-y-6">
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                <h2 class="text-lg font-semibold text-gray-900 mb-4">Details</h2>
                <dl class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-sm">
                    <div>
                        <dt class="text-gray-500">Order date</dt>
                        <dd class="font-medium text-gray-900">{{ $purchaseOrder->order_date?->format('M d, Y') ?? '—' }}</dd>
                    </div>
                    <div>
                        <dt class="text-gray-500">Status</dt>
                        <dd class="font-medium text-gray-900 capitalize">{{ str_replace('_', ' ', $purchaseOrder->status ?? '—') }}</dd>
                    </div>
                    <div>
                        <dt class="text-gray-500">Payment</dt>
                        <dd class="font-medium text-gray-900 capitalize">{{ str_replace('_', ' ', $purchaseOrder->payment_method ?? '—') }}</dd>
                    </div>
                    @if($purchaseOrder->signature_type)
                        <div>
                            <dt class="text-gray-500">Signature</dt>
                            <dd class="font-medium text-gray-900 capitalize">{{ $purchaseOrder->signature_type === 'esignature' ? 'eSignature' : 'Manual' }}</dd>
                        </div>
                    @endif
                </dl>
            </div>

            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-200">
                    <h2 class="text-lg font-semibold text-gray-900">Line items</h2>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full text-left text-sm">
                        <thead class="bg-gray-50 text-xs uppercase text-gray-500">
                            <tr>
                                <th class="py-3 px-6">Description</th>
                                <th class="py-3 px-6 text-right">Qty</th>
                                <th class="py-3 px-6 text-right">Rate</th>
                                <th class="py-3 px-6 text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-100">
                            @forelse($purchaseOrder->items as $item)
                                <tr>
                                    <td class="py-3 px-6 text-gray-900">{{ $item->description }}</td>
                                    <td class="py-3 px-6 text-right text-gray-700">{{ $item->quantity }}</td>
                                    <td class="py-3 px-6 text-right text-gray-700">{{ \App\Models\Currency::formatAmount($item->rate, null) }}</td>
                                    <td class="py-3 px-6 text-right font-medium text-gray-900">{{ \App\Models\Currency::formatAmount($item->amount, null) }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="4" class="py-8 px-6 text-center text-gray-500">No line items</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>

            @if($purchaseOrder->notes || $purchaseOrder->terms || $purchaseOrder->bank_details)
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 space-y-4">
                    @if($purchaseOrder->notes)
                        <div>
                            <h3 class="text-sm font-semibold text-gray-700 mb-2">Notes</h3>
                            <p class="text-sm text-gray-600 whitespace-pre-wrap">{{ $purchaseOrder->notes }}</p>
                        </div>
                    @endif
                    @if($purchaseOrder->terms)
                        <div>
                            <h3 class="text-sm font-semibold text-gray-700 mb-2">Terms</h3>
                            <p class="text-sm text-gray-600 whitespace-pre-wrap">{{ $purchaseOrder->terms }}</p>
                        </div>
                    @endif
                    @if($purchaseOrder->bank_details)
                        <div>
                            <h3 class="text-sm font-semibold text-gray-700 mb-2">Bank details</h3>
                            <p class="text-sm text-gray-600 whitespace-pre-wrap">{{ $purchaseOrder->bank_details }}</p>
                        </div>
                    @endif
                </div>
            @endif
        </div>

        <div class="space-y-6">
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                <h2 class="text-lg font-semibold text-gray-900 mb-4">Summary</h2>
                <dl class="space-y-3 text-sm">
                    <div class="flex justify-between">
                        <dt class="text-gray-600">Subtotal</dt>
                        <dd class="font-medium text-gray-900">{{ \App\Models\Currency::formatAmount($purchaseOrder->amount, null) }}</dd>
                    </div>
                    <div class="flex justify-between">
                        <dt class="text-gray-600">Tax</dt>
                        <dd class="font-medium text-gray-900">{{ \App\Models\Currency::formatAmount($purchaseOrder->tax_amount, null) }}</dd>
                    </div>
                    <div class="flex justify-between">
                        <dt class="text-gray-600">Discount</dt>
                        <dd class="font-medium text-gray-900">− {{ \App\Models\Currency::formatAmount($purchaseOrder->discount_amount, null) }}</dd>
                    </div>
                    <div class="pt-3 border-t border-gray-200 flex justify-between items-center">
                        <dt class="text-base font-semibold text-gray-900">Total</dt>
                        <dd class="text-lg font-bold text-primary">{{ \App\Models\Currency::formatAmount($purchaseOrder->total, null) }}</dd>
                    </div>
                </dl>
            </div>

            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                <h2 class="text-lg font-semibold text-gray-900 mb-4">Supplier</h2>
                @if($purchaseOrder->supplier)
                    <p class="font-medium text-gray-900">{{ $purchaseOrder->supplier->name }}</p>
                    @if($purchaseOrder->supplier->email)
                        <p class="text-sm text-gray-600 mt-1">{{ $purchaseOrder->supplier->email }}</p>
                    @endif
                    @if($purchaseOrder->supplier->phone)
                        <p class="text-sm text-gray-600">{{ $purchaseOrder->supplier->phone }}</p>
                    @endif
                @else
                    <p class="text-sm text-gray-500">—</p>
                @endif
            </div>

            @if($purchaseOrder->company)
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                    <h2 class="text-lg font-semibold text-gray-900 mb-4">Bill from (company)</h2>
                    <p class="font-medium text-gray-900">{{ $purchaseOrder->company->name }}</p>
                    @if($purchaseOrder->company->email)
                        <p class="text-sm text-gray-600 mt-1">{{ $purchaseOrder->company->email }}</p>
                    @endif
                </div>
            @endif

            @if($purchaseOrder->signature)
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
                    <h2 class="text-lg font-semibold text-gray-900 mb-4">Signature</h2>
                    <p class="text-sm text-gray-900">{{ $purchaseOrder->signature->name }}</p>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
