<?php $editing = !empty($request); ?>
<div class="max-w-3xl mx-auto">
    <div class="flex items-center gap-3 mb-6">
        <a href="?page=maintenance" 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 Request' : 'New Maintenance Request' ?></h1>
    </div>
    <form method="POST" action="?page=maintenance&action=<?= $editing ? 'update&id='.$request['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 class="md:col-span-2"><label class="block text-sm font-medium text-gray-700 mb-1">Title <span class="text-red-500">*</span></label><input type="text" name="title" value="<?= htmlspecialchars(old('title', $request['title'] ?? '')) ?>" 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">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</option>
                    <?php foreach ($properties as $p): ?><option value="<?= $p['id'] ?>" <?= old('property_id', $request['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</label>
                    <select name="unit_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 ($units as $u): ?><option value="<?= $u['id'] ?>" <?= old('unit_id', $request['unit_id'] ?? $_GET['unit_id'] ?? '') == $u['id'] ? 'selected' : '' ?>><?= htmlspecialchars($u['unit_number'].' ('.$u['property_name'].')') ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Tenant</label>
                    <select name="tenant_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 ($tenants as $t): ?><option value="<?= $t['id'] ?>" <?= old('tenant_id', $request['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">Assign Vendor</label>
                    <select name="assigned_vendor_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="">None</option>
                    <?php foreach ($vendors as $vn): ?><option value="<?= $vn['id'] ?>" <?= old('assigned_vendor_id', $request['assigned_vendor_id'] ?? '') == $vn['id'] ? 'selected' : '' ?>><?= htmlspecialchars($vn['company_name']) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Category</label>
                    <select name="category" 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 (['plumbing','electrical','hvac','appliance','pest_control','structural','flooring','painting','cleaning','landscaping','other'] as $c): ?><option value="<?= $c ?>" <?= old('category', $request['category'] ?? '') === $c ? 'selected' : '' ?>><?= ucwords(str_replace('_',' ',$c)) ?></option><?php endforeach; ?></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Priority</label>
                    <select name="priority" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
                    <?php foreach (['low'=>'Low','medium'=>'Medium','high'=>'High','emergency'=>'Emergency'] as $v=>$l): ?><option value="<?= $v ?>" <?= old('priority', $request['priority'] ?? 'medium') === $v ? 'selected' : '' ?>><?= $l ?></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','reviewed'=>'Reviewed','assigned'=>'Assigned','in_progress'=>'In Progress','waiting_parts'=>'Waiting for Parts','waiting_approval'=>'Waiting for Approval','completed'=>'Completed','closed'=>'Closed','cancelled'=>'Cancelled'] as $v=>$l): ?><option value="<?= $v ?>" <?= old('status', $request['status'] ?? 'new') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?></select></div>
                <?php if ($editing): ?>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Estimated Cost</label><input type="number" name="estimated_cost" value="<?= old('estimated_cost', $request['estimated_cost'] ?? '') ?>" 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">Actual Cost</label><input type="number" name="actual_cost" value="<?= old('actual_cost', $request['actual_cost'] ?? '') ?>" 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>
                <?php endif; ?>
            </div>
            <div class="mt-4"><label class="flex items-center gap-2"><input type="checkbox" name="permission_to_enter" value="1" <?= old('permission_to_enter', $request['permission_to_enter'] ?? 0) ? 'checked' : '' ?> class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500"><span class="text-sm text-gray-700">Permission to enter unit</span></label></div>
            <div class="mt-4"><label class="block text-sm font-medium text-gray-700 mb-1">Description</label><textarea name="description" rows="4" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><?= htmlspecialchars(old('description', $request['description'] ?? '')) ?></textarea></div>
        </div>
        <div class="flex justify-end gap-3">
            <a href="?page=maintenance" 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' : 'Submit Request' ?></button>
        </div>
    </form>
</div>
