<?php
/**
 * Pay Purchase Due Form
 * Uses $purchase from PurchaseController::pay()
 */
$dueAmount = $purchase['total'] - $purchase['amount_paid'];
?>
<div class="page-header">
    <div>
        <a href="<?= APP_URL ?>/purchases" class="btn btn-ghost btn-sm mb-4" style="padding-left:0;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="19" y1="12" x2="5" y2="12"></line><polyline points="12 19 5 12 12 5"></polyline></svg>
            Back to Purchases
        </a>
    </div>
    <h1>Pay Purchase: <?= e($purchase['purchase_no']) ?></h1>
    <div class="page-subtitle">Process payment for outstanding balance.</div>
</div>

<div class="grid-3 mb-6" style="grid-template-columns: 1fr 1fr;">
    <div class="card">
        <div class="card-header"><h3 class="card-title">Payment Details</h3></div>
        
        <form action="<?= APP_URL ?>/purchases/<?= $purchase['id'] ?>/process-payment" method="POST">
            <?= Security::csrfField() ?>

            <div class="flex justify-between py-3 border-b border-soft">
                <span class="text-muted">Purchase No</span>
                <span class="font-bold"><?= e($purchase['purchase_no']) ?></span>
            </div>

            <div class="flex justify-between py-3 border-b border-soft">
                <span class="text-muted">Total Amount</span>
                <span class="font-bold"><?= money($purchase['total']) ?></span>
            </div>

            <div class="flex justify-between py-3 border-b border-soft">
                <span class="text-muted">Amount Paid</span>
                <span class="text-success font-bold"><?= money($purchase['amount_paid']) ?></span>
            </div>

            <div class="flex justify-between py-3 border-t-2 border-primary" style="font-size: 1.2rem; font-weight: 800;">
                <span>Due Amount</span>
                <span class="text-danger"><?= money($dueAmount) ?></span>
            </div>

            <div class="form-group mt-6">
                <label class="form-label">Payment Amount <span class="required">*</span></label>
                <div class="input-group">
                    <span class="input-group-text">$</span>
                    <input type="number" name="payment_amount" class="form-control" value="<?= $dueAmount ?>" step="0.01" max="<?= $dueAmount ?>" required>
                </div>
                <div class="text-xs text-muted mt-1">Maximum: <?= money($dueAmount) ?></div>
            </div>

            <div class="form-group mt-4">
                <label class="form-label">Payment Method <span class="required">*</span></label>
                <select name="payment_method" class="form-control" required>
                    <option value="cash">Cash</option>
                    <option value="card">Card</option>
                    <option value="transfer">Bank Transfer</option>
                    <option value="cheque">Cheque</option>
                    <option value="credit">Credit</option>
                    <option value="other">Other</option>
                </select>
            </div>

            <div class="form-group mt-4">
                <label class="form-label">Notes</label>
                <textarea name="notes" class="form-control" rows="2" placeholder="Payment reference, cheque number, etc..."></textarea>
            </div>

            <button type="submit" class="btn btn-primary w-full mt-6 py-4 font-bold">Process Payment</button>
        </form>
    </div>

    <div class="card">
        <div class="card-header"><h3 class="card-title">Purchase Summary</h3></div>
        
        <div class="flex justify-between py-2 border-b border-soft">
            <span class="text-muted">Date</span>
            <span><?= date('M j, Y', strtotime($purchase['created_at'])) ?></span>
        </div>

        <?php if ($purchase['supplier_name']): ?>
        <div class="flex justify-between py-2 border-b border-soft">
            <span class="text-muted">Supplier</span>
            <span><?= e($purchase['supplier_name']) ?></span>
        </div>
        <?php endif; ?>

        <div class="flex justify-between py-2 border-b border-soft">
            <span class="text-muted">Status</span>
            <span>
                <?php 
                $statusBadge = [
                    'received' => 'badge-success',
                    'pending'  => 'badge-warning',
                    'ordered'  => 'badge-info',
                    'cancelled' => 'badge-danger'
                ];
                $badge = $statusBadge[$purchase['status']] ?? 'badge-muted';
                ?>
                <span class="badge <?= $badge ?>"><?= ucfirst(e($purchase['status'])) ?></span>
            </span>
        </div>

        <div class="flex justify-between py-2 border-b border-soft">
            <span class="text-muted">Payment Status</span>
            <span>
                <?php 
                $paymentBadge = [
                    'paid' => 'badge-success',
                    'partial' => 'badge-info',
                    'credit' => 'badge-info',
                    'due' => 'badge-danger'
                ];
                $paymentBadgeClass = $paymentBadge[$purchase['payment_status']] ?? 'badge-muted';
                ?>
                <span class="badge <?= $paymentBadgeClass ?>"><?= ucfirst(e($purchase['payment_status'])) ?></span>
            </span>
        </div>

        <?php if ($purchase['note']): ?>
        <div class="mt-4">
            <div class="text-xs text-muted mb-1">Notes</div>
            <div class="text-sm"><?= e($purchase['note']) ?></div>
        </div>
        <?php endif; ?>

        <div class="mt-6 pt-4 border-t border-soft">
            <div class="text-xs text-muted mb-3">Purchase Items</div>
            <div class="table-wrapper">
                <table class="table-sm">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th class="text-right">Qty</th>
                            <th class="text-right">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($purchase['items'])): ?>
                        <tr>
                            <td colspan="3" class="text-center py-2">No items</td>
                        </tr>
                        <?php else: foreach ($purchase['items'] as $item): ?>
                        <tr>
                            <td class="text-sm"><?= e($item['product_name']) ?></td>
                            <td class="text-right text-sm"><?= $item['quantity'] ?></td>
                            <td class="text-right text-sm"><?= money($item['total']) ?></td>
                        </tr>
                        <?php endforeach; endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
