Take a look at the following thread which discusses a means of showing customers the remaining stock level within the ticket form on single event pages:
If you look within the template example I provided (in that thread) you’ll see code such as $product->is_in_stock() and $ticket->stock … you could basically adapat this to determine if any tickets remain in stock and if not display a message or form.
So in the thread I referenced the change went into tickets.php – the reason for that being that that template can access the $product and $ticket objects, which contain the information we need.
So the easiest thing here would be to work in there rather than in events/single.php (which the code you shared seems to belong to).