@extends('layouts.admin')

@section('title', 'PO '.$po->po_number)

@section('content')
<div class="tw-migrated space-y-4">
    <x-ui.page-header title="PO {{ $po->po_number }}">
        <x-slot:actions>
            <x-ui.button href="{{ route('admin.purchase-orders.index') }}" variant="outline" size="sm">
                <i class="bi bi-arrow-left"></i> Back
            </x-ui.button>
            @if($po->status==='draft')
                <form method="post" action="{{ route('admin.purchase-orders.mark-ordered', $po) }}">@csrf
                    <button type="submit" class="tw-btn-primary tw-btn-sm"><i class="bi bi-send"></i> Mark Ordered</button>
                </form>
            @endif
        </x-slot:actions>
    </x-ui.page-header>

    @if(session('success'))
        <x-ui.alert type="success">{{ session('success') }}</x-ui.alert>
    @endif
    @if($errors->any())
        <x-ui.alert type="danger">
            <ul class="mb-0">@foreach($errors->all() as $e)<li>{{ $e }}</li>@endforeach</ul>
        </x-ui.alert>
    @endif

    <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-5">
        <x-ui.stat-card label="Supplier" :value="$po->supplier?->name ?? '—'" />
        <x-ui.stat-card label="Receive Location" :value="$po->location?->name ?? '—'" />
        <x-ui.stat-card label="Status" :value="ucfirst($po->status)" />
        <x-ui.stat-card label="Expected" :value="$po->expected_date?->format('d M Y') ?? '—'" />
        <x-ui.stat-card label="PO Total" :value="'₹'.number_format($po->total_amount, 2)" />
    </div>

    @if($po->notes)
        <x-ui.card>
            <strong>Notes:</strong> {{ $po->notes }}
        </x-ui.card>
    @endif

    <x-ui.card title="Order Lines & Goods Receipt">
        <form method="post" action="{{ route('admin.purchase-orders.receive', $po) }}">
            @csrf
            <div class="tw-table-wrap">
                <table class="tw-table align-middle">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th>SKU</th>
                            <th>Ordered</th>
                            <th>Already Received</th>
                            <th>Remaining</th>
                            <th>Unit Cost</th>
                            <th>Receive Now</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($po->items as $i => $item)
                        @php $remaining = max(0, $item->quantity - $item->received_quantity); @endphp
                        <tr>
                            <td>{{ $item->product?->name }}
                                <input type="hidden" name="items[{{ $i }}][product_id]" value="{{ $item->product_id }}">
                            </td>
                            <td>{{ $item->product?->sku ?? '—' }}</td>
                            <td>{{ $item->quantity }}</td>
                            <td>{{ $item->received_quantity }}</td>
                            <td><strong>{{ $remaining }}</strong></td>
                            <td>₹{{ number_format($item->unit_cost, 2) }}</td>
                            <td style="max-width:120px;">
                                <input type="number" min="0" max="{{ $remaining }}"
                                       name="items[{{ $i }}][quantity]"
                                       class="form-control form-control-sm admin-form-control"
                                       value="{{ $remaining }}"
                                       @disabled(!in_array($po->status, ['ordered','partial']) || $remaining <= 0)>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            @if(in_array($po->status, ['ordered','partial']))
            <div class="row g-2 align-items-end mt-3">
                <div class="col-md-6">
                    <label class="admin-form-label">GRN Notes</label>
                    <input type="text" name="notes" class="form-control admin-form-control" placeholder="Optional receipt notes">
                </div>
                <div class="col-md-3">
                    <button type="submit" class="tw-btn-primary w-100"><i class="bi bi-box-seam"></i> Post Goods Receipt</button>
                </div>
            </div>
            <small class="text-muted d-block mt-2">Receiving adds stock to <strong>{{ $po->location?->name }}</strong>. You can receive partially.</small>
            @elseif($po->status==='draft')
                <x-ui.alert type="info" class="mt-3 mb-0">Mark this PO as <strong>Ordered</strong> before posting a goods receipt.</x-ui.alert>
            @elseif($po->status==='received')
                <x-ui.alert type="success" class="mt-3 mb-0">Fully received.</x-ui.alert>
            @endif
        </form>
    </x-ui.card>

    @if($po->goodsReceipts->isNotEmpty())
        <x-ui.card title="Goods Receipt History">
            <x-ui.table>
                <x-slot:thead>
                    <tr>
                        <th>GRN #</th>
                        <th>Status</th>
                        <th>Received At</th>
                        <th>Notes</th>
                    </tr>
                </x-slot:thead>
                @foreach($po->goodsReceipts as $grn)
                <tr>
                    <td>{{ $grn->grn_number }}</td>
                    <td>{{ $grn->status }}</td>
                    <td>{{ $grn->received_at?->format('d M Y H:i') }}</td>
                    <td>{{ $grn->notes ?? '—' }}</td>
                </tr>
                @endforeach
            </x-ui.table>
        </x-ui.card>
    @endif
</div>
@endsection
