<?php $editing = !empty($unit); ?>
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6">
        <a href="?page=units" 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 Unit' : 'Add Unit' ?></h1>
    </div>

    <form method="POST" action="?page=units&action=<?= $editing ? 'update&id='.$unit['id'] : 'store' ?>" class="space-y-6">
        <?= CSRF::field() ?>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Unit Details</h3>
            <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">Property <span class="text-red-500">*</span></label>
                    <select name="property_id" required 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 Property</option>
                        <?php foreach (($properties ?? []) as $p): ?>
                            <option value="<?= $p['id'] ?>" <?= old('property_id', $unit['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">Unit Number <span class="text-red-500">*</span></label>
                    <input type="text" name="unit_number" value="<?= htmlspecialchars(old('unit_number', $unit['unit_number'] ?? '')) ?>" 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">Unit Type</label>
                    <select name="unit_type" 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 Type</option>
                        <?php foreach (['studio','1br','2br','3br','4br','penthouse','loft','townhouse','commercial','retail','office','storage','other'] as $t): ?>
                            <option value="<?= $t ?>" <?= old('unit_type', $unit['unit_type'] ?? '') === $t ? 'selected' : '' ?>><?= ucfirst(str_replace('br',' Bed', $t)) ?></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 (['vacant'=>'Vacant','occupied'=>'Occupied','notice'=>'Notice Given','maintenance'=>'Under Maintenance','unavailable'=>'Not Available','model'=>'Model Unit'] as $v => $l): ?>
                            <option value="<?= $v ?>" <?= old('status', $unit['status'] ?? 'vacant') === $v ? 'selected' : '' ?>><?= $l ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Specifications</h3>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Bedrooms</label>
                    <input type="number" name="bedrooms" value="<?= old('bedrooms', $unit['bedrooms'] ?? '') ?>" min="0" 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">Bathrooms</label>
                    <input type="number" name="bathrooms" value="<?= old('bathrooms', $unit['bathrooms'] ?? '') ?>" min="0" step="0.5" 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">Sq Ft</label>
                    <input type="number" name="square_feet" value="<?= old('square_feet', $unit['square_feet'] ?? '') ?>" min="0" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Financials</h3>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Market Rent</label>
                    <input type="number" name="market_rent" value="<?= old('market_rent', $unit['market_rent'] ?? '') ?>" step="0.01" min="0" 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">Current Rent</label>
                    <input type="number" name="current_rent" value="<?= old('current_rent', $unit['current_rent'] ?? '') ?>" step="0.01" min="0" 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">Deposit</label>
                    <input type="number" name="deposit_amount" value="<?= old('deposit_amount', $unit['deposit_amount'] ?? '') ?>" step="0.01" min="0" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Additional</h3>
            <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">Available Date</label>
                    <input type="date" name="available_date" value="<?= old('available_date', $unit['available_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">Floor</label>
                    <input type="text" name="floor" value="<?= htmlspecialchars(old('floor', $unit['floor'] ?? '')) ?>" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                </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', $unit['notes'] ?? '')) ?></textarea>
            </div>
        </div>

        <div class="flex justify-end gap-3">
            <a href="?page=units" 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 Unit' : 'Create Unit' ?></button>
        </div>
    </form>
</div>
