@extends('pdf.layout')

@section('title', __('Purchase Order') . ' - ' . $order->po_number)
@section('doc_name', __('PURCHASE ORDER'))
@section('doc_number', __('Order #') . ': ' . $order->po_number)

@section('content')
    <div style="margin-bottom: 20px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 50%; vertical-align: top;">
                    <div class="fw-bold text-uppercase" style="margin-bottom: 5px; color: #666;">{{ __('Supplier') }}:</div>
                    <div class="fw-bold" style="font-size: 14px;">{{ $order->partner->name ?? __('Unknown Supplier') }}</div>
                    @if($order->partner)
                        <p style="margin: 2px 0;">{{ $order->partner->address }}</p>
                        <p style="margin: 2px 0;">{{ __('Phone') }}: {{ $order->partner->phone }}</p>
                        @if($order->partner->email)
                            <p style="margin: 2px 0;">{{ __('Email') }}: {{ $order->partner->email }}</p>
                        @endif
                    @endif
                </td>
                <td style="width: 50%; vertical-align: top; text-align: right;">
                    <div class="fw-bold text-uppercase" style="margin-bottom: 5px; color: #666;">{{ __('Order Details') }}:</div>
                    <p style="margin: 2px 0;">{{ __('Order Date') }}: {{ $order->order_date?->format('Y-m-d') ?? $order->created_at->format('Y-m-d') }}</p>
                    @if($order->expected_date)
                        <p style="margin: 2px 0;">{{ __('Expected Date') }}: {{ $order->expected_date->format('Y-m-d') }}</p>
                    @endif
                    <p style="margin: 2px 0;">{{ __('Status') }}: 
                        <span class="badge {{ in_array($order->status, ['completed', 'received']) ? 'badge-success' : ($order->status === 'draft' ? 'badge-secondary' : 'badge-primary') }}">
                            {{ ucfirst($order->status) }}
                        </span>
                    </p>
                    <p style="margin: 2px 0;">{{ __('Authorized By') }}: {{ $order->user->name ?? 'System' }}</p>
                    @if($order->branch)
                        <p style="margin: 2px 0;">{{ __('Delivery to') }}: {{ $order->branch->name }}</p>
                    @endif
                </td>
            </tr>
        </table>
    </div>

    <table class="data-table">
        <thead>
            <tr>
                <th style="width: 50px;">#</th>
                <th>{{ __('Description') }}</th>
                <th class="text-center" style="width: 80px;">{{ __('Qty') }}</th>
                <th class="text-right" style="width: 100px;">{{ __('Unit Cost') }}</th>
                <th class="text-right" style="width: 100px;">{{ __('Total') }}</th>
            </tr>
        </thead>
        <tbody>
            @foreach($order->items as $index => $item)
                <tr>
                    <td>{{ $index + 1 }}</td>
                    <td>
                        <div class="fw-bold">{{ $item->product->localized_name ?? $item->product->name }}</div>
                        @if($item->product->sku)<small style="color: #666;">SKU: {{ $item->product->sku }}</small>@endif
                    </td>
                    <td class="text-center">{{ $item->quantity }}</td>
                    <td class="text-right">{{ number_format($item->cost_price, 2) }}</td>
                    <td class="text-right">{{ number_format($item->total, 2) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>

    <div class="totals">
        <table>
            <tr class="totals-row totals-grand">
                <td class="text-right">{{ __('Grand Total') }}:</td>
                <td class="text-right">{{ number_format($order->total, 2) }} {{ $currency ?? 'EGP' }}</td>
            </tr>
            @php
                $totalPaid = $order->payments->sum('amount');
                $balance = $order->total - $totalPaid;
            @endphp
            @if($totalPaid > 0)
                <tr class="totals-row">
                    <td class="text-right">{{ __('Amount Paid') }}:</td>
                    <td class="text-right">{{ number_format($totalPaid, 2) }}</td>
                </tr>
                <tr class="totals-row">
                    <td class="text-right fw-bold">{{ __('Balance Due') }}:</td>
                    <td class="text-right fw-bold">{{ number_format($balance, 2) }}</td>
                </tr>
            @endif
        </table>
        <div style="clear: both;"></div>
    </div>

    @if($order->notes)
        <div style="margin-top: 30px; padding: 10px; border: 1px solid #eee; background-color: #fcfcfc;">
            <div class="fw-bold" style="margin-bottom: 5px;">{{ __('Special Instructions / Notes') }}:</div>
            <div style="font-style: italic;">{{ $order->notes }}</div>
        </div>
    @endif

    <div style="margin-top: 50px;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 33%; text-align: center;">
                    <div style="border-top: 1px solid #ccc; width: 120px; margin: 0 auto; margin-top: 40px;"></div>
                    <p>{{ __('Prepared By') }}</p>
                </td>
                <td style="width: 33%; text-align: center;">
                    <div style="border-top: 1px solid #ccc; width: 120px; margin: 0 auto; margin-top: 40px;"></div>
                    <p>{{ __('Authorized By') }}</p>
                </td>
                <td style="width: 33%; text-align: center;">
                    <div style="border-top: 1px solid #ccc; width: 120px; margin: 0 auto; margin-top: 40px;"></div>
                    <p>{{ __('Supplier Acknowledgment') }}</p>
                </td>
            </tr>
        </table>
    </div>
@endsection
