<?php
/**
 * Couple Portal — Edit Budget Item Form (PRI-002)
 * Location: app/views/budget/edit_expense.php
 */
$id = (int) $expense['id'];
?>

<div class="max-w-2xl mx-auto space-y-6">

    <!-- ── Header Section ────────────────────────────────────────────────── -->
    <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
        <div>
            <div class="page-breadcrumb">
                <a href="<?= url('/app/couple/budget') ?>">Budget Planner</a>
                <span class="sep">/</span>
                <a href="<?= url("/app/couple/budget/expenses/{$id}") ?>"><?= escape_html($expense['category_name']) ?></a>
                <span class="sep">/</span>
                <span>Edit</span>
            </div>
            <h1 class="page-h1">
                <span class="page-header-icon">
                    <i class="fa-solid fa-pen-to-square"></i>
                </span>
                Edit Details
            </h1>
            <p class="page-subtitle">Update the budget, deal price, and vendor information.</p>
        </div>
        <div>
            <a href="<?= url("/app/couple/budget/expenses/{$id}") ?>" class="cp-btn-back">
                <i class="fa-solid fa-arrow-left"></i> Back
            </a>
        </div>
    </div>

    <!-- ── Form Panel ────────────────────────────────────────────────────── -->
    <div class="bg-white rounded-3xl border border-slate-100 shadow-sm overflow-hidden max-w-2xl">
        <form method="POST" action="<?= url("/app/couple/budget/expenses/edit/{$id}") ?>" class="p-6 space-y-6">
            <input type="hidden" name="_csrf" value="<?= escape_html($_SESSION['csrf_token'] ?? '') ?>">
            <!-- Carry category_id and status internally -->
            <input type="hidden" name="category_id" value="<?= (int) $expense['category_id'] ?>">
            <input type="hidden" name="name" value="<?= escape_html($expense['name']) ?>">
            <input type="hidden" name="status" value="<?= escape_html($expense['status']) ?>">

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <!-- Planned Budget (Estimated Cost) -->
                <div class="form-group space-y-1.5">
                    <label for="estimated_cost" class="form-label">Budget (Planned Budget)</label>
                    <div class="relative">
                        <span class="absolute left-3 top-2.5 text-slate-400 text-xs font-bold">Amt</span>
                        <input type="number" step="0.01" min="0" id="estimated_cost" name="estimated_cost" value="<?= escape_html($old['estimated_cost'] ?? $expense['estimated_cost']) ?>" class="form-input pl-14 font-bold" required>
                    </div>
                </div>

                <!-- Deal Price (Actual Cost) -->
                <div class="form-group space-y-1.5">
                    <label for="actual_cost" class="form-label">Deal Price</label>
                    <div class="relative">
                        <span class="absolute left-3 top-2.5 text-slate-400 text-xs font-bold">Amt</span>
                        <input type="number" step="0.01" min="0" id="actual_cost" name="actual_cost" value="<?= escape_html($old['actual_cost'] ?? $expense['actual_cost']) ?>" class="form-input pl-14 font-bold">
                    </div>
                </div>

                <!-- Vendor Name -->
                <div class="form-group space-y-1.5">
                    <label for="vendor_name" class="form-label">Vendor Name</label>
                    <div class="relative">
                        <span class="absolute left-3 top-2.5 text-slate-400 text-sm"><i class="fa-solid fa-store"></i></span>
                        <input type="text" id="vendor_name" name="vendor_name" value="<?= escape_html($old['vendor_name'] ?? $expense['vendor_name'] ?? '') ?>" placeholder="e.g. Grand Empire" class="form-input pl-9">
                    </div>
                </div>

                <!-- Vendor PIC -->
                <div class="form-group space-y-1.5">
                    <label for="vendor_pic" class="form-label">PIC</label>
                    <input type="text" id="vendor_pic" name="vendor_pic" value="<?= escape_html($old['vendor_pic'] ?? $expense['vendor_pic'] ?? '') ?>" placeholder="e.g. Jenny" class="form-input">
                </div>

                <!-- Vendor Phone/WhatsApp -->
                <div class="form-group space-y-1.5">
                    <label for="vendor_phone" class="form-label">Phone / WhatsApp</label>
                    <input type="text" id="vendor_phone" name="vendor_phone" value="<?= escape_html($old['vendor_phone'] ?? $expense['vendor_phone'] ?? '') ?>" placeholder="e.g. +62812345678" class="form-input">
                </div>

                <!-- Next Payment Due Date -->
                <div class="form-group space-y-1.5">
                    <label for="due_date" class="form-label">Next Payment Date</label>
                    <input type="date" id="due_date" name="due_date" value="<?= escape_html($old['due_date'] ?? $expense['due_date'] ?? '') ?>" class="form-input">
                </div>

                <!-- Notes / Special Instructions -->
                <div class="form-group md:col-span-2 space-y-1.5">
                    <label for="notes" class="form-label">Notes</label>
                    <textarea id="notes" name="notes" placeholder="Notes..." class="form-textarea" rows="3"><?= escape_html($old['notes'] ?? $expense['notes'] ?? '') ?></textarea>
                </div>
            </div>

            <!-- Submit buttons -->
            <div class="flex justify-end gap-2 border-t border-slate-100 pt-5 mt-4">
                <a href="<?= url("/app/couple/budget/expenses/{$id}") ?>" class="btn btn-secondary btn-md">
                    Cancel
                </a>
                <button type="submit" class="btn btn-primary btn-md flex items-center gap-1.5">
                    <i class="fa-solid fa-floppy-disk"></i> Save Details
                </button>
            </div>
        </form>
    </div>

</div>
