<?php
/**
 * Couple Portal — Add Budget Item Form (PRI-001)
 * Location: app/views/budget/create_expense.php
 */
?>

<div class="max-w-3xl mx-auto space-y-6" x-data="{ reminderEnabled: <?= !empty($old['reminder_enabled']) ? 'true' : 'false' ?> }">

    <!-- ── 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>
                <span>Add Item</span>
            </div>
            <h1 class="page-h1">
                <span class="page-header-icon">
                    <i class="fa-solid fa-file-invoice-dollar"></i>
                </span>
                Add Budget Item
            </h1>
            <p class="page-subtitle">Add a new wedding expense — vendor, category, and budget amount.</p>
        </div>
        <div>
            <a href="<?= url('/app/couple/budget') ?>" class="cp-btn-back">
                <i class="fa-solid fa-arrow-left"></i> Cancel
            </a>
        </div>
    </div>

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

            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <!-- Name -->
                <div class="form-group md:col-span-2 space-y-1.5">
                    <label for="name" class="form-label">Item Description <span class="text-rose-500">*</span></label>
                    <div class="relative">
                        <span class="absolute left-3 top-2.5 text-slate-400 text-sm"><i class="fa-solid fa-pencil"></i></span>
                        <input type="text" id="name" name="name" value="<?= escape_html($old['name'] ?? '') ?>" placeholder="e.g. Venue Booking, Wedding Dress, Photographer Deposit" class="form-input pl-9" required>
                    </div>
                    <?php if (isset($errors['name'])): ?>
                        <p class="form-error"><?= escape_html($errors['name']) ?></p>
                    <?php endif; ?>
                </div>

                <!-- Category -->
                <div class="form-group space-y-1.5">
                    <label for="category_id" class="form-label">Category <span class="text-rose-500">*</span></label>
                    <select id="category_id" name="category_id" class="form-select text-sm" required>
                        <option value="">-- Choose Category --</option>
                        <?php foreach ($categories as $cat): ?>
                            <option value="<?= $cat['id'] ?>" <?= (isset($old['category_id']) && (int)$old['category_id'] === (int)$cat['id']) ? 'selected' : '' ?>>
                                <?= escape_html($cat['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <?php if (isset($errors['category_id'])): ?>
                        <p class="form-error"><?= escape_html($errors['category_id']) ?></p>
                    <?php endif; ?>
                </div>

                <!-- Planned Budget (Estimated Cost) -->
                <div class="form-group space-y-1.5">
                    <label for="estimated_cost" class="form-label">Planned Budget (Estimated)</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'] ?? '0.00') ?>" 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 (Contract 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'] ?? '0.00') ?>" 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'] ?? '') ?>" placeholder="e.g. Grand Empire, Studio A" class="form-input pl-9">
                    </div>
                </div>

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

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

                <!-- Payment Method -->
                <div class="form-group space-y-1.5">
                    <label for="payment_method" class="form-label">Preferred Payment Method</label>
                    <select id="payment_method" name="payment_method" class="form-select text-sm">
                        <option value="">-- Choose Method --</option>
                        <option value="Bank Transfer" <?= (isset($old['payment_method']) && $old['payment_method'] === 'Bank Transfer') ? 'selected' : '' ?>>Bank Transfer</option>
                        <option value="Cash" <?= (isset($old['payment_method']) && $old['payment_method'] === 'Cash') ? 'selected' : '' ?>>Cash</option>
                        <option value="Credit Card" <?= (isset($old['payment_method']) && $old['payment_method'] === 'Credit Card') ? 'selected' : '' ?>>Credit Card</option>
                        <option value="Debit Card" <?= (isset($old['payment_method']) && $old['payment_method'] === 'Debit Card') ? 'selected' : '' ?>>Debit Card</option>
                    </select>
                </div>

                <!-- Due Date -->
                <div class="form-group space-y-1.5">
                    <label for="due_date" class="form-label">Payment Due Date</label>
                    <input type="date" id="due_date" name="due_date" value="<?= escape_html($old['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">Item Notes / Inclusions</label>
                    <textarea id="notes" name="notes" placeholder="Specify inclusions, terms of payments, key clauses, etc." class="form-textarea" rows="3"><?= escape_html($old['notes'] ?? '') ?></textarea>
                </div>

                <!-- Reminder Setting -->
                <div class="form-group md:col-span-2 border-t border-slate-100 pt-4 mt-2">
                    <label class="flex items-center gap-2 cursor-pointer select-none">
                        <input type="checkbox" name="reminder_enabled" value="1" x-model="reminderEnabled" class="form-checkbox">
                        <span class="text-xs font-bold text-slate-700">Enable Due-Date Reminder Scheduler</span>
                    </label>
                </div>

                <!-- Reminder Date -->
                <div class="form-group space-y-1.5" x-show="reminderEnabled" x-transition style="display: none;">
                    <label for="reminder_date" class="form-label">Reminder Date <span class="text-rose-500">*</span></label>
                    <input type="date" id="reminder_date" name="reminder_date" value="<?= escape_html($old['reminder_date'] ?? '') ?>" class="form-input" :required="reminderEnabled">
                </div>

                <!-- Reminder Notes -->
                <div class="form-group md:col-span-2 space-y-1.5" x-show="reminderEnabled" x-transition style="display: none;">
                    <label for="reminder_notes" class="form-label">Reminder Notification Details</label>
                    <textarea id="reminder_notes" name="reminder_notes" placeholder="Enter instructions or alert notes to show when the payment date approaches." class="form-textarea" rows="2"><?= escape_html($old['reminder_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') ?>" 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 Item
                </button>
            </div>
        </form>
    </div>

</div>
