@extends('layouts.app')

@section('title', 'Purchase Orders')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">Purchase Orders</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0 small">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                    <li class="breadcrumb-item active">Procurement</li>
                </ol>
            </nav>
        </div>
        <a href="{{ route('purchase-orders.create') }}" class="btn btn-primary px-4 shadow-sm">
            <i class="bi bi-cart-plus me-2"></i> Create New PO
        </a>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table align-middle mb-0 table-hover">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4">Order #</th>
                            <th>Supplier</th>
                            <th>Date</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th class="text-end pe-4">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($purchaseOrders as $po)
                            <tr>
                                <td class="ps-4">
                                    <div class="fw-bold">{{ $po->po_number }}</div>
                                    <small class="text-muted">{{ $po->items_count ?? $po->items->count() }} line items</small>
                                </td>
                                <td>
                                    <div class="fw-bold">{{ $po->partner->name }}</div>
                                    <code class="smallest">{{ $po->partner->code }}</code>
                                </td>
                                <td>{{ $po->order_date->format('M d, Y') }}</td>
                                <td><span class="fw-bold text-dark">${{ number_format($po->total_amount, 2) }}</span></td>
                                <td>
                                    @php
                                        $statusClass = [
                                            'draft' => 'secondary',
                                            'sent' => 'info',
                                            'received' => 'success',
                                            'partially_received' => 'warning',
                                            'cancelled' => 'danger'
                                        ][$po->status] ?? 'primary';
                                    @endphp
                                    <span
                                        class="badge bg-{{ $statusClass }}-subtle text-{{ $statusClass }} border border-{{ $statusClass }}-subtle text-uppercase">
                                        {{ str_replace('_', ' ', $po->status) }}
                                    </span>
                                </td>
                                <td class="text-end pe-4">
                                    <a href="{{ route('purchase-orders.show', $po->id) }}"
                                        class="btn btn-outline-primary btn-sm px-3">
                                        Manage
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-5 text-muted">No purchase orders found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($purchaseOrders->hasPages())
            <div class="card-footer bg-white border-0">{{ $purchaseOrders->links() }}</div>
        @endif
    </div>
@endsection