<?php $isEdit = !empty($property['id']); ?>
<div class="p-4 lg:p-6 space-y-6">
    <div class="flex items-center gap-3">
        <a href="?page=properties" class="p-2 rounded-lg hover:bg-gray-100 transition">
            <svg class="w-5 h-5 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor"><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"><?= $isEdit ? 'Edit Property' : 'Add Property' ?></h1>
    </div>

    <form method="POST" action="?page=properties&action=<?= $isEdit ? 'update' : 'store' ?>" class="max-w-3xl space-y-6">
        <?= CSRF::field() ?>
        <?php if ($isEdit): ?><input type="hidden" name="id" value="<?= $property['id'] ?>"><?php endif; ?>

        <div class="bg-white rounded-2xl border border-gray-200/80 p-6 space-y-5">
            <h2 class="text-sm font-semibold text-gray-900">Property Details</h2>

            <div class="grid sm:grid-cols-2 gap-5">
                <div class="sm:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">Property Name *</label>
                    <input type="text" name="name" value="<?= e($property['name'] ?? old('name')) ?>" required
                        class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-400"
                        placeholder="e.g. Sunset Apartments">
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">Property Type *</label>
                    <select name="type" required class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 bg-white">
                        <option value="">Select type</option>
                        <?php foreach (['single-family'=>'Single Family','duplex'=>'Duplex','triplex'=>'Triplex','fourplex'=>'Fourplex','multifamily'=>'Multifamily','apartment'=>'Apartment','commercial'=>'Commercial','retail'=>'Retail','office'=>'Office','salon-suite'=>'Salon Suite','storage'=>'Storage','mixed-use'=>'Mixed Use','other'=>'Other'] as $val => $lbl): ?>
                            <option value="<?= $val ?>" <?= ($property['type'] ?? old('type')) === $val ? 'selected' : '' ?>><?= $lbl ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">Owner</label>
                    <select name="owner_id" class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 bg-white">
                        <option value="">No owner assigned</option>
                        <?php foreach ($owners as $o): ?>
                            <option value="<?= $o['id'] ?>" <?= ($property['owner_id'] ?? old('owner_id')) == $o['id'] ? 'selected' : '' ?>><?= e($o['first_name'] . ' ' . $o['last_name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">Property Manager</label>
                    <select name="manager_id" class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 bg-white">
                        <option value="">Not assigned</option>
                        <?php foreach ($managers as $m): ?>
                            <option value="<?= $m['id'] ?>" <?= ($property['manager_id'] ?? old('manager_id')) == $m['id'] ? 'selected' : '' ?>><?= e($m['first_name'] . ' ' . $m['last_name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-2xl border border-gray-200/80 p-6 space-y-5">
            <h2 class="text-sm font-semibold text-gray-900">Address</h2>
            <div class="grid sm:grid-cols-2 gap-5">
                <div class="sm:col-span-2">
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">Street Address *</label>
                    <input type="text" name="address" value="<?= e($property['address'] ?? old('address')) ?>" required
                        class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-400">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1.5">City *</label>
                    <input type="text" name="city" value="<?= e($property['city'] ?? old('city')) ?>" required
                        class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-400">
                </div>
                <div class="grid grid-cols-2 gap-3">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1.5">State *</label>
                        <input type="text" name="state" value="<?= e($property['state'] ?? old('state')) ?>" required maxlength="2"
                            class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-400" placeholder="TX">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1.5">ZIP *</label>
                        <input type="text" name="zip" value="<?= e($property['zip'] ?? old('zip')) ?>" required
                            class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-400">
                    </div>
                </div>
                <input type="hidden" name="country" value="<?= e($property['country'] ?? 'US') ?>">
            </div>
        </div>

        <div class="bg-white rounded-2xl border border-gray-200/80 p-6 space-y-5">
            <h2 class="text-sm font-semibold text-gray-900">Notes</h2>
            <textarea name="notes" rows="4" class="w-full px-4 py-2.5 text-sm border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-400" placeholder="Optional notes about this property..."><?= e($property['notes'] ?? old('notes')) ?></textarea>
        </div>

        <div class="flex items-center gap-3">
            <button type="submit" class="px-6 py-2.5 text-sm font-semibold text-white bg-slate-900 rounded-xl hover:bg-slate-800 transition">
                <?= $isEdit ? 'Save Changes' : 'Create Property' ?>
            </button>
            <a href="?page=properties" class="px-6 py-2.5 text-sm font-medium text-gray-600 hover:text-gray-800 transition">Cancel</a>
        </div>
    </form>
</div>
