@extends('layouts.layoutMaster')

@section('title', __('Purchase Order Details'))

@section('content')
<x-breadcrumb
    :title="$purchaseOrder->order_number"
    :breadcrumbs="[
        ['name' => __('System Core'), 'url' => route('systemcore.dashboard')],
        ['name' => __('Purchase Orders'), 'url' => route('systemcore.purchase-orders.index')],
        ['name' => __('Details')]
    ]"
/>

<!-- First Row: Order Information + Summary -->
<div class="row">
    <!-- Left Column: Order Information -->
    <div class="col-lg-8 col-md-12">
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">{{ __('Order Information') }}</h5>
                <span class="badge bg-{{ $purchaseOrder->status->color() }}">{{ $purchaseOrder->status->label() }}</span>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <span class="fw-medium me-1">{{ __('Order Number:') }}</span>
                        <span>{{ $purchaseOrder->order_number }}</span>
                    </div>
                    <div class="col-md-6 mb-3">
                        <span class="fw-medium me-1">{{ __('Order Date:') }}</span>
                        <span>{{ $purchaseOrder->order_date?->format('Y-m-d') }}</span>
                    </div>
                    @if($purchaseOrder->expected_delivery_date)
                    <div class="col-md-6 mb-3">
                        <span class="fw-medium me-1">{{ __('Expected Delivery:') }}</span>
                        <span>{{ $purchaseOrder->expected_delivery_date->format('Y-m-d') }}</span>
                    </div>
                    @endif
                    <div class="col-md-6 mb-3">
                        <span class="fw-medium me-1">{{ __('Payment Status:') }}</span>
                        @php
                            $paymentColors = [
                                'pending' => 'warning',
                                'partial' => 'info',
                                'paid' => 'success',
                                'overdue' => 'danger',
                            ];
                            $paymentColor = $paymentColors[$purchaseOrder->payment_status] ?? 'secondary';
                        @endphp
                        <span class="badge bg-{{ $paymentColor }}">{{ ucfirst($purchaseOrder->payment_status) }}</span>
                    </div>
                    @if($purchaseOrder->payment_method)
                    <div class="col-md-6 mb-3">
                        <span class="fw-medium me-1">{{ __('Payment Method:') }}</span>
                        <span>{{ ucfirst($purchaseOrder->payment_method) }}</span>
                    </div>
                    @endif
                </div>

                <hr class="my-3">

                <h6 class="fw-semibold mb-2">{{ __('Supplier Information') }}</h6>
                @if($purchaseOrder->supplier)
                <div class="row">
                    <div class="col-md-6 mb-2">
                        <span class="fw-medium me-1">{{ __('Name:') }}</span>
                        <a href="{{ route('systemcore.suppliers.show', $purchaseOrder->supplier->id) }}">
                            {{ $purchaseOrder->supplier->name }}
                        </a>
                    </div>
                    <div class="col-md-6 mb-2">
                        <span class="fw-medium me-1">{{ __('Code:') }}</span>
                        <span>{{ $purchaseOrder->supplier->supplier_code }}</span>
                    </div>
                    @if($purchaseOrder->supplier->email)
                    <div class="col-md-6 mb-2">
                        <span class="fw-medium me-1">{{ __('Email:') }}</span>
                        <span>{{ $purchaseOrder->supplier->email }}</span>
                    </div>
                    @endif
                    @if($purchaseOrder->supplier->phone)
                    <div class="col-md-6 mb-2">
                        <span class="fw-medium me-1">{{ __('Phone:') }}</span>
                        <span>{{ $purchaseOrder->supplier->phone }}</span>
                    </div>
                    @endif
                </div>
                @endif

                @if($purchaseOrder->user)
                <hr class="my-3">
                <h6 class="fw-semibold mb-2">{{ __('Purchasing Agent') }}</h6>
                <p class="mb-0">{{ $purchaseOrder->user->name }}</p>
                @endif

                @if($purchaseOrder->notes)
                <hr class="my-3">
                <h6 class="fw-semibold mb-2">{{ __('Notes') }}</h6>
                <p class="text-muted mb-0">{{ $purchaseOrder->notes }}</p>
                @endif
            </div>
        </div>
    </div>

    <!-- Right Column: Order Summary & Actions -->
    <div class="col-lg-4 col-md-12">
        <!-- Order Summary -->
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bx bx-calculator me-1"></i>{{ __('Order Summary') }}
                </h5>
            </div>
            <div class="card-body">
                <table class="table table-borderless mb-0">
                    <tbody>
                        <tr>
                            <td class="text-start fw-medium">{{ __('Subtotal:') }}</td>
                            <td class="text-end">{{ number_format($purchaseOrder->subtotal, 2) }}</td>
                        </tr>
                        <tr>
                            <td class="text-start fw-medium">{{ __('Tax Amount:') }}</td>
                            <td class="text-end">{{ number_format($purchaseOrder->tax_amount, 2) }}</td>
                        </tr>
                        <tr>
                            <td class="text-start fw-medium">{{ __('Discount:') }}</td>
                            <td class="text-end text-danger">-{{ number_format($purchaseOrder->discount_amount, 2) }}</td>
                        </tr>
                        <tr>
                            <td class="text-start fw-medium">{{ __('Shipping Cost:') }}</td>
                            <td class="text-end">{{ number_format($purchaseOrder->shipping_cost, 2) }}</td>
                        </tr>
                        <tr class="border-top">
                            <td class="text-start fw-bold fs-5">{{ __('Total Amount:') }}</td>
                            <td class="text-end fw-bold fs-5 text-primary">{{ number_format($purchaseOrder->total_amount, 2) }}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Action Buttons -->
        <div class="card">
            <div class="card-body">
                <a href="{{ route('systemcore.purchase-orders.edit', $purchaseOrder->id) }}" class="btn btn-primary w-100 mb-2">
                    <i class="bx bx-edit me-1"></i>{{ __('Edit') }}
                </a>
                <a href="{{ route('systemcore.purchase-orders.index') }}" class="btn btn-label-secondary w-100">
                    <i class="bx bx-arrow-back me-1"></i>{{ __('Back') }}
                </a>
            </div>
        </div>
    </div>
</div>

<!-- Second Row: Order Items (Full Width) -->
<div class="row mt-4">
    <div class="col-12">
        <div class="card mb-4">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bx bx-list-ul me-1"></i>{{ __('Order Items') }}
                </h5>
            </div>
            <div class="card-body">
                @if($purchaseOrder->orderLines->count() > 0)
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>{{ __('Product') }}</th>
                                    <th>{{ __('SKU') }}</th>
                                    <th class="text-end">{{ __('Quantity') }}</th>
                                    <th class="text-end">{{ __('Unit Price') }}</th>
                                    <th class="text-end">{{ __('Tax') }}</th>
                                    <th class="text-end">{{ __('Discount') }}</th>
                                    <th class="text-end">{{ __('Line Total') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($purchaseOrder->orderLines as $line)
                                <tr>
                                    <td>
                                        {{ $line->product_name }}
                                        @if($line->productVariant)
                                            <br><small class="text-muted">{{ $line->productVariant->variant_name }}</small>
                                        @endif
                                    </td>
                                    <td>{{ $line->product_sku }}</td>
                                    <td class="text-end">{{ number_format($line->quantity, 2) }}</td>
                                    <td class="text-end">{{ number_format($line->unit_price, 2) }}</td>
                                    <td class="text-end">{{ number_format($line->tax_amount, 2) }}</td>
                                    <td class="text-end">{{ number_format($line->discount_amount, 2) }}</td>
                                    <td class="text-end fw-medium">{{ number_format($line->line_total, 2) }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="text-center py-4">
                        <i class="bx bx-package bx-lg text-muted"></i>
                        <p class="text-muted mt-2">{{ __('No order items found') }}</p>
                    </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
