<?php
/**
 * Edit Purchase Form
 * Uses $purchase, $suppliers, $locations, and $products from PurchaseController::edit()
 */
?>
<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>Edit Purchase: <?= e($purchase['purchase_no']) ?></h1>
    <div class="page-subtitle">Modify purchase details and payment information.</div>
</div>

<div class="card mb-6">
    <form action="<?= APP_URL ?>/purchases/<?= $purchase['id'] ?>/update" method="POST">
        <?= Security::csrfField() ?>
        
        <div class="grid-2 gap-6 mb-6">
            <div class="form-group">
                <label class="form-label">Supplier</label>
                <select name="supplier_id" class="form-control">
                    <option value="">Select Supplier...</option>
                    <?php foreach ($suppliers as $s): ?>
                    <option value="<?= $s['id'] ?>" <?= $purchase['supplier_id'] == $s['id'] ? 'selected' : '' ?>><?= e($s['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="form-group">
                <label class="form-label">Status</label>
                <select name="status" class="form-control">
                    <option value="received" <?= $purchase['status'] === 'received' ? 'selected' : '' ?>>Received</option>
                    <option value="pending" <?= $purchase['status'] === 'pending' ? 'selected' : '' ?>>Pending</option>
                    <option value="ordered" <?= $purchase['status'] === 'ordered' ? 'selected' : '' ?>>Ordered</option>
                    <option value="cancelled" <?= $purchase['status'] === 'cancelled' ? 'selected' : '' ?>>Cancelled</option>
                </select>
            </div>
        </div>

        <div class="grid-2 gap-6 mb-6">
            <div class="form-group">
                <label class="form-label">Subtotal</label>
                <input type="number" name="subtotal" class="form-control" value="<?= $purchase['subtotal'] ?>" step="0.01" required>
            </div>

            <div class="form-group">
                <label class="form-label">Discount Amount</label>
                <input type="number" name="discount_amount" class="form-control" value="<?= $purchase['discount_amount'] ?>" step="0.01">
            </div>
        </div>

        <div class="grid-2 gap-6 mb-6">
            <div class="form-group">
                <label class="form-label">Tax Amount</label>
                <input type="number" name="tax_amount" class="form-control" value="<?= $purchase['tax_amount'] ?>" step="0.01">
            </div>

            <div class="form-group">
                <label class="form-label">Total</label>
                <input type="number" name="total" class="form-control" value="<?= $purchase['total'] ?>" step="0.01" required>
            </div>
        </div>

        <div class="grid-2 gap-6 mb-6">
            <div class="form-group">
                <label class="form-label">Amount Paid</label>
                <input type="number" name="amount_paid" class="form-control" value="<?= $purchase['amount_paid'] ?>" step="0.01">
            </div>

            <div class="form-group">
                <label class="form-label">Payment Method</label>
                <select name="payment_method" class="form-control">
                    <option value="cash" <?= $purchase['payment_method'] === 'cash' ? 'selected' : '' ?>>Cash</option>
                    <option value="card" <?= $purchase['payment_method'] === 'card' ? 'selected' : '' ?>>Card</option>
                    <option value="transfer" <?= $purchase['payment_method'] === 'transfer' ? 'selected' : '' ?>>Bank Transfer</option>
                    <option value="cheque" <?= $purchase['payment_method'] === 'cheque' ? 'selected' : '' ?>>Cheque</option>
                    <option value="credit" <?= $purchase['payment_method'] === 'credit' ? 'selected' : '' ?>>Credit</option>
                    <option value="other" <?= $purchase['payment_method'] === 'other' ? 'selected' : '' ?>>Other</option>
                </select>
            </div>
        </div>

        <div class="grid-2 gap-6 mb-6">
            <div class="form-group">
                <label class="form-label">Payment Status</label>
                <select name="payment_status" class="form-control" readonly>
                    <option value="paid" <?= $purchase['payment_status'] === 'paid' ? 'selected' : '' ?>>Paid in Full</option>
                    <option value="partial" <?= $purchase['payment_status'] === 'partial' ? 'selected' : '' ?>>Partial Payment</option>
                    <option value="due" <?= $purchase['payment_status'] === 'due' ? 'selected' : '' ?>>Due</option>
                    <option value="credit" <?= $purchase['payment_status'] === 'credit' ? 'selected' : '' ?>>Credit</option>
                </select>
                <div class="form-hint">Payment status is calculated automatically based on amount paid vs total</div>
            </div>
        </div>

        <div class="form-group mb-6">
            <label class="form-label">Notes</label>
            <textarea name="notes" class="form-control" rows="3"><?= e($purchase['note'] ?? '') ?></textarea>
        </div>

        <div class="flex gap-3">
            <button type="submit" class="btn btn-primary">Update Purchase</button>
            <a href="<?= APP_URL ?>/purchases/<?= $purchase['id'] ?>" class="btn btn-outline">Cancel</a>
        </div>
    </form>
</div>

<div class="card">
    <div class="card-header"><h3 class="card-title">Purchase Items</h3></div>
    <div class="table-wrapper">
        <table>
            <thead>
                <tr>
                    <th>Product</th>
                    <th>SKU</th>
                    <th class="text-right">Unit Cost</th>
                    <th class="text-right">Quantity</th>
                    <th class="text-right">Total</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($purchase['items'])): ?>
                <tr>
                    <td colspan="5" class="text-center py-4">No items found.</td>
                </tr>
                <?php else: foreach ($purchase['items'] as $item): ?>
                <tr>
                    <td><?= e($item['product_name']) ?></td>
                    <td><?= e($item['sku'] ?? '-') ?></td>
                    <td class="text-right"><?= money($item['unit_cost']) ?></td>
                    <td class="text-right"><?= $item['quantity'] ?></td>
                    <td class="text-right"><?= money($item['total']) ?></td>
                </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>
