<?php
$statusBadge = ['ORDERED' => 'badge-blue', 'PARTIALLY_RECEIVED' => 'badge-gold', 'RECEIVED' => 'badge-green', 'CANCELLED' => 'badge-red'];
$due = (float) $po['subtotal'] - (float) $po['paid_amount'];
?>
<a href="<?= Router::url('purchasing') ?>" style="font-size:13px;color:var(--color-text-muted);"><i class="fa-solid fa-arrow-left"></i> Back to Purchasing</a>

<div style="display:grid;grid-template-columns:1fr 320px;gap:18px;margin-top:14px;align-items:start;">
    <div>
        <div class="panel">
            <div class="panel-head">
                <h3><?= e($po['po_number']) ?></h3>
                <span class="badge <?= $statusBadge[$po['status']] ?? 'badge-gray' ?>"><?= e(str_replace('_', ' ', $po['status'])) ?></span>
            </div>
            <div style="display:grid;grid-template-columns:repeat(2,1fr);gap:8px;font-size:13px;color:var(--color-text-muted);">
                <div><i class="fa-solid fa-truck"></i> <?= e($po['supplier_name']) ?><?= $po['supplier_phone'] ? ' · ' . e($po['supplier_phone']) : '' ?></div>
                <div><i class="fa-regular fa-calendar"></i> Ordered <?= date('d M Y', strtotime($po['order_date'])) ?></div>
                <?php if ($po['expected_date']): ?><div><i class="fa-solid fa-truck-fast"></i> Expected <?= date('d M Y', strtotime($po['expected_date'])) ?></div><?php endif; ?>
                <?php if ($po['notes']): ?><div><i class="fa-solid fa-note-sticky"></i> <?= e($po['notes']) ?></div><?php endif; ?>
            </div>
        </div>

        <div class="panel">
            <h3 style="margin-bottom:12px;">Items</h3>
            <?php if ($po['status'] !== 'CANCELLED' && $po['status'] !== 'RECEIVED' && Auth::can('purchasing', 'edit')): ?>
            <form method="POST" action="<?= Router::url('purchasing/receive') ?>">
                <?= $csrf ?>
                <input type="hidden" name="id" value="<?= $po['id'] ?>">
                <table class="data-table">
                <thead><tr><th>Material</th><th>Ordered</th><th>Received</th><th>Remaining</th><th>Receive Now</th></tr></thead>
                <tbody>
                <?php foreach ($po['items'] as $it): $remaining = (float)$it['quantity_ordered'] - (float)$it['quantity_received']; ?>
                <tr>
                    <td><?= e($it['material_name']) ?></td>
                    <td><?= number_format((float)$it['quantity_ordered'],2) ?> <?= e($it['unit']) ?></td>
                    <td><?= number_format((float)$it['quantity_received'],2) ?> <?= e($it['unit']) ?></td>
                    <td><?= number_format($remaining,2) ?> <?= e($it['unit']) ?></td>
                    <td>
                        <?php if ($remaining > 0): ?>
                        <input type="number" step="0.001" name="receive[<?= $it['id'] ?>]" class="form-control" style="max-width:110px;" placeholder="0" max="<?= $remaining ?>">
                        <?php else: ?>
                        <span class="badge badge-green">Complete</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
                </table>
                <button type="submit" class="btn btn-primary" style="margin-top:14px;"><i class="fa-solid fa-box"></i> Confirm Receipt</button>
            </form>
            <?php else: ?>
            <table class="data-table">
            <thead><tr><th>Material</th><th>Ordered</th><th>Received</th><th>Unit Cost</th><th>Line Total</th></tr></thead>
            <tbody>
            <?php foreach ($po['items'] as $it): ?>
            <tr>
                <td><?= e($it['material_name']) ?></td>
                <td><?= number_format((float)$it['quantity_ordered'],2) ?> <?= e($it['unit']) ?></td>
                <td><?= number_format((float)$it['quantity_received'],2) ?> <?= e($it['unit']) ?></td>
                <td><?= formatMoney((float)$it['unit_cost']) ?></td>
                <td><?= formatMoney((float)$it['line_total']) ?></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>

    <div>
        <?php if ($po['status'] !== 'CANCELLED' && Auth::can('purchasing', 'edit')): ?>
        <div class="panel">
            <h3 style="margin-bottom:12px;">Payment</h3>
            <div style="font-size:13px;margin-bottom:10px;">
                <div style="display:flex;justify-content:space-between;"><span>Subtotal</span><strong><?= formatMoney((float)$po['subtotal']) ?></strong></div>
                <div style="display:flex;justify-content:space-between;"><span>Paid</span><strong><?= formatMoney((float)$po['paid_amount']) ?></strong></div>
                <div style="display:flex;justify-content:space-between;color:<?= $due > 0 ? 'var(--color-danger)' : 'var(--color-success)' ?>;"><span>Due</span><strong><?= formatMoney($due) ?></strong></div>
            </div>
            <?php if ($due > 0): ?>
            <form method="POST" action="<?= Router::url('purchasing/pay') ?>">
                <?= $csrf ?>
                <input type="hidden" name="id" value="<?= $po['id'] ?>">
                <div class="form-group">
                    <label>Pay From</label>
                    <select name="finance_account_id" class="form-control" required>
                        <?php foreach ($financeAccounts as $fa): ?>
                        <option value="<?= $fa['id'] ?>"><?= e($fa['account_name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>Amount</label>
                    <input type="number" step="0.01" name="amount" class="form-control" value="<?= number_format($due,2,'.','') ?>" required>
                </div>
                <button type="submit" class="btn btn-primary" style="width:100%;"><i class="fa-solid fa-money-bill"></i> Record Payment</button>
            </form>
            <?php endif; ?>

            <?php if ($po['status'] === 'ORDERED'): ?>
            <form method="POST" action="<?= Router::url('purchasing/cancel') ?>" style="margin-top:10px;" onsubmit="return confirm('Cancel this purchase order?');">
                <?= $csrf ?>
                <input type="hidden" name="id" value="<?= $po['id'] ?>">
                <button type="submit" class="btn btn-danger" style="width:100%;"><i class="fa-solid fa-ban"></i> Cancel PO</button>
            </form>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <div class="panel">
            <h3 style="margin-bottom:12px;">Payment History</h3>
            <?php foreach ($po['payments'] as $p): ?>
            <div style="font-size:12.5px;padding:6px 0;border-bottom:1px solid var(--color-border);display:flex;justify-content:space-between;">
                <span><?= date('d M, h:i A', strtotime($p['paid_at'])) ?> · <?= e($p['account_name']) ?></span>
                <strong><?= formatMoney((float)$p['amount']) ?></strong>
            </div>
            <?php endforeach; ?>
            <?php if (empty($po['payments'])): ?>
            <div style="text-align:center;color:var(--color-text-muted);font-size:12px;padding:10px 0;">No payments recorded yet.</div>
            <?php endif; ?>
        </div>
    </div>
</div>