<?php $editing = !empty($lead); ?>
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6"><a href="?page=leads" class="text-gray-400 hover:text-gray-600"><svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg></a><h1 class="text-2xl font-bold text-gray-900"><?= $editing ? 'Edit Lead' : 'Add Lead' ?></h1></div>
    <form method="POST" action="?page=leads&action=<?= $editing ? 'update&id='.$lead['id'] : 'store' ?>" class="space-y-6"><?= CSRF::field() ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">First Name <span class="text-red-500">*</span></label><input type="text" name="first_name" value="<?= htmlspecialchars(old('first_name', $lead['first_name'] ?? '')) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Last Name <span class="text-red-500">*</span></label><input type="text" name="last_name" value="<?= htmlspecialchars(old('last_name', $lead['last_name'] ?? '')) ?>" required class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Email</label><input type="email" name="email" value="<?= htmlspecialchars(old('email', $lead['email'] ?? '')) ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Phone</label><input type="text" name="phone" value="<?= htmlspecialchars(old('phone', $lead['phone'] ?? '')) ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Desired Property</label>
                    <select name="desired_property_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select</option>
                    <?php foreach ($properties ?? [] as $p): ?><option value="<?= $p['id'] ?>" <?= old('desired_property_id', $lead['desired_property_id'] ?? '') == $p['id'] ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Move-in Date</label><input type="date" name="move_in_date" value="<?= old('move_in_date', $lead['move_in_date'] ?? '') ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Source</label>
                    <select name="source" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Select</option>
                    <?php foreach (['website','zillow','apartments_com','referral','walk_in','phone','social_media','craigslist','other'] as $s): ?><option value="<?= $s ?>" <?= old('source', $lead['source'] ?? '') === $s ? 'selected' : '' ?>><?= ucwords(str_replace('_',' ',$s)) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                    <select name="status" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['new'=>'New','contacted'=>'Contacted','tour_scheduled'=>'Tour Scheduled','tour_completed'=>'Tour Completed','applied'=>'Applied','approved'=>'Approved','leased'=>'Leased','lost'=>'Lost'] as $v=>$l): ?><option value="<?= $v ?>" <?= old('status', $lead['status'] ?? 'new') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Assigned Agent</label>
                    <select name="assigned_agent_id" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">Unassigned</option>
                    <?php foreach ($agents ?? [] as $a): ?><option value="<?= $a['id'] ?>" <?= old('assigned_agent_id', $lead['assigned_agent_id'] ?? '') == $a['id'] ? 'selected' : '' ?>><?= htmlspecialchars($a['first_name'].' '.$a['last_name']) ?></option><?php endforeach; ?></select></div>
            </div>
            <div class="mt-4"><label class="block text-sm font-medium text-gray-700 mb-1">Notes</label><textarea name="notes" rows="3" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><?= htmlspecialchars(old('notes', $lead['notes'] ?? '')) ?></textarea></div>
        </div>
        <div class="flex justify-end gap-3"><a href="?page=leads" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Cancel</a><button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium"><?= $editing ? 'Update' : 'Add Lead' ?></button></div>
    </form>
</div>
