<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=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>
            <div>
                <h1 class="text-2xl font-bold text-gray-900"><?= htmlspecialchars($request['title']) ?></h1>
                <p class="text-sm text-gray-500"><?= htmlspecialchars(($request['property_name'] ?? '') . ($request['unit_number'] ? ' / Unit '.$request['unit_number'] : '')) ?> · <?= timeAgo($request['created_at']) ?></p>
            </div>
        </div>
        <div class="flex items-center gap-2">
            <?= priorityBadge($request['priority']) ?>
            <?= statusBadge($request['status']) ?>
            <a href="?page=maintenance&action=edit&id=<?= $request['id'] ?>" class="ml-2 px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Edit</a>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 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">Category</p>
            <p class="text-lg font-bold text-gray-900 mt-1"><?= ucwords(str_replace('_',' ',$request['category'] ?? 'General')) ?></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">Estimated Cost</p>
            <p class="text-lg font-bold text-gray-900 mt-1"><?= $request['estimated_cost'] ? formatMoney($request['estimated_cost']) : '-' ?></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">Actual Cost</p>
            <p class="text-lg font-bold text-gray-900 mt-1"><?= $request['actual_cost'] ? formatMoney($request['actual_cost']) : '-' ?></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">Tenant</dt><dd class="font-medium"><?= $request['first_name'] ? '<a href="?page=tenants&action=show&id='.$request['tenant_id'].'" class="text-indigo-600 hover:underline">'.htmlspecialchars($request['first_name'].' '.$request['last_name']).'</a>' : '-' ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Phone</dt><dd class="font-medium"><?= htmlspecialchars($request['tenant_phone'] ?? '-') ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Vendor</dt><dd class="font-medium"><?= htmlspecialchars($request['vendor_name'] ?? 'Unassigned') ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Entry Permission</dt><dd class="font-medium"><?= $request['permission_to_enter'] ? '<span class="text-green-600">Yes</span>' : '<span class="text-red-600">No</span>' ?></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">Description</h3>
            <p class="text-sm text-gray-700 whitespace-pre-line"><?= htmlspecialchars($request['description'] ?? 'No description provided.') ?></p>
        </div>
    </div>

    <!-- Work Orders -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
        <div class="flex items-center justify-between mb-4">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider">Work Orders</h3>
            <a href="?page=workorders&action=create&maintenance_id=<?= $request['id'] ?>" class="text-sm text-indigo-600 hover:underline">+ Create Work Order</a>
        </div>
        <?php if (!empty($workOrders)): ?>
            <div class="space-y-3">
                <?php foreach ($workOrders as $wo): ?>
                    <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                        <div>
                            <p class="font-medium text-sm"><?= htmlspecialchars($wo['title'] ?? 'Work Order #'.$wo['id']) ?></p>
                            <p class="text-xs text-gray-500"><?= htmlspecialchars($wo['vendor_name'] ?? 'No vendor') ?> · <?= ucwords(str_replace('_',' ',$wo['status'])) ?></p>
                        </div>
                        <span class="text-sm font-medium"><?= $wo['cost'] ? formatMoney($wo['cost']) : '-' ?></span>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?><p class="text-gray-400 text-sm">No work orders yet</p><?php endif; ?>
    </div>
</div>
