<?php $editing = !empty($lease); ?>
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6">
        <a href="?page=leases" 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 Lease' : 'New Lease' ?></h1>
    </div>
    <form method="POST" action="?page=leases&action=<?= $editing ? 'update&id='.$lease['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">Assignment</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">Tenant <span class="text-red-500">*</span></label>
                    <select name="tenant_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 Tenant</option>
                    <?php foreach ($tenants as $t): ?><option value="<?= $t['id'] ?>" <?= old('tenant_id', $lease['tenant_id'] ?? '') == $t['id'] ? 'selected' : '' ?>><?= htmlspecialchars($t['first_name'].' '.$t['last_name']) ?></option><?php endforeach; ?></select></div>
                <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', $lease['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 <span class="text-red-500">*</span></label>
                    <select name="unit_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 Unit</option>
                    <?php foreach ($units as $u): ?><option value="<?= $u['id'] ?>" <?= old('unit_id', $lease['unit_id'] ?? '') == $u['id'] ? 'selected' : '' ?>><?= htmlspecialchars($u['unit_number'].' ('.$u['property_name'].')') ?></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">Lease Terms</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">Start Date <span class="text-red-500">*</span></label><input type="date" name="start_date" value="<?= old('start_date', $lease['start_date'] ?? '') ?>" 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">End Date <span class="text-red-500">*</span></label><input type="date" name="end_date" value="<?= old('end_date', $lease['end_date'] ?? '') ?>" 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">Rent Amount <span class="text-red-500">*</span></label><input type="number" name="rent_amount" value="<?= old('rent_amount', $lease['rent_amount'] ?? '') ?>" step="0.01" min="0" 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">Frequency</label>
                    <select name="rent_frequency" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['daily'=>'Daily','weekly'=>'Weekly','biweekly'=>'Biweekly','monthly'=>'Monthly','quarterly'=>'Quarterly','annually'=>'Annually'] as $v=>$l): ?>
                        <option value="<?= $v ?>" <?= old('rent_frequency', $lease['rent_frequency'] ?? 'monthly') === $v ? 'selected' : '' ?>><?= $l ?></option>
                    <?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Security Deposit</label><input type="number" name="security_deposit" value="<?= old('security_deposit', $lease['security_deposit'] ?? '') ?>" 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">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 (['draft'=>'Draft','active'=>'Active','expired'=>'Expired','renewed'=>'Renewed','terminated'=>'Terminated','month-to-month'=>'Month-to-Month'] as $v=>$l): ?>
                        <option value="<?= $v ?>" <?= old('status', $lease['status'] ?? 'draft') === $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">Late Fee Rules</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">Late Fee Amount</label><input type="number" name="late_fee_amount" value="<?= old('late_fee_amount', $lease['late_fee_amount'] ?? '0') ?>" 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">Grace Period (days)</label><input type="number" name="grace_period_days" value="<?= old('grace_period_days', $lease['grace_period_days'] ?? '5') ?>" 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">
            <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', $lease['notes'] ?? '')) ?></textarea>
        </div>

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