<div class="max-w-4xl mx-auto">
    <div class="flex items-center justify-between mb-6">
        <div class="flex items-center gap-3">
            <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>
            <div>
                <h1 class="text-2xl font-bold text-gray-900">Unit <?= htmlspecialchars($unit['unit_number']) ?></h1>
                <p class="text-sm text-gray-500"><?= htmlspecialchars($unit['property_name'] ?? '') ?></p>
            </div>
            <?= statusBadge($unit['status'] ?? 'vacant') ?>
        </div>
        <div class="flex gap-2">
            <a href="?page=units&action=edit&id=<?= $unit['id'] ?>" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Edit</a>
            <form method="POST" action="?page=units&action=delete&id=<?= $unit['id'] ?>" onsubmit="return confirm('Delete this unit?')">
                <?= CSRF::field() ?>
                <button class="px-4 py-2 border border-red-300 text-red-600 rounded-lg hover:bg-red-50 text-sm font-medium">Delete</button>
            </form>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs font-medium text-gray-500 uppercase">Market Rent</p>
            <p class="text-2xl font-bold text-gray-900 mt-1"><?= formatMoney($unit['market_rent'] ?? 0) ?></p>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs font-medium text-gray-500 uppercase">Current Rent</p>
            <p class="text-2xl font-bold text-gray-900 mt-1"><?= formatMoney($unit['current_rent'] ?? 0) ?></p>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs font-medium text-gray-500 uppercase">Deposit</p>
            <p class="text-2xl font-bold text-gray-900 mt-1"><?= formatMoney($unit['deposit_amount'] ?? 0) ?></p>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
        <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">Details</h3>
            <dl class="space-y-3 text-sm">
                <div class="flex justify-between"><dt class="text-gray-500">Type</dt><dd class="font-medium text-gray-900"><?= htmlspecialchars($unit['unit_type'] ?? '-') ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Bedrooms</dt><dd class="font-medium text-gray-900"><?= $unit['bedrooms'] ?? '-' ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Bathrooms</dt><dd class="font-medium text-gray-900"><?= $unit['bathrooms'] ?? '-' ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Sq Ft</dt><dd class="font-medium text-gray-900"><?= $unit['square_feet'] ? number_format($unit['square_feet']) : '-' ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Floor</dt><dd class="font-medium text-gray-900"><?= htmlspecialchars($unit['floor'] ?? '-') ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Available</dt><dd class="font-medium text-gray-900"><?= $unit['available_date'] ? formatDate($unit['available_date']) : '-' ?></dd></div>
            </dl>
        </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">Current Tenant</h3>
            <?php if (!empty($tenant)): ?>
                <div class="flex items-center gap-3 mb-3">
                    <div class="w-10 h-10 rounded-full bg-indigo-100 flex items-center justify-center text-indigo-600 font-bold text-sm"><?= strtoupper(substr($tenant['first_name'],0,1).substr($tenant['last_name'],0,1)) ?></div>
                    <div>
                        <a href="?page=tenants&action=show&id=<?= $tenant['id'] ?>" class="font-medium text-gray-900 hover:text-indigo-600"><?= htmlspecialchars($tenant['first_name'].' '.$tenant['last_name']) ?></a>
                        <p class="text-xs text-gray-500"><?= htmlspecialchars($tenant['email'] ?? '') ?></p>
                    </div>
                </div>
            <?php else: ?>
                <p class="text-gray-400 text-sm">No tenant assigned</p>
            <?php endif; ?>

            <?php if (!empty($lease)): ?>
                <div class="mt-4 pt-4 border-t border-gray-100">
                    <h4 class="text-xs font-medium text-gray-500 uppercase mb-2">Active Lease</h4>
                    <dl class="space-y-2 text-sm">
                        <div class="flex justify-between"><dt class="text-gray-500">Period</dt><dd class="font-medium"><?= formatDate($lease['start_date']) ?> - <?= formatDate($lease['end_date']) ?></dd></div>
                        <div class="flex justify-between"><dt class="text-gray-500">Rent</dt><dd class="font-medium"><?= formatMoney($lease['rent_amount']) ?>/mo</dd></div>
                    </dl>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <?php if ($unit['notes']): ?>
        <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-3">Notes</h3>
            <p class="text-sm text-gray-700 whitespace-pre-line"><?= htmlspecialchars($unit['notes']) ?></p>
        </div>
    <?php endif; ?>
</div>
