@page
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Extensions
@using trainav.web.database
@using trainav.web.Utils
@model DeleteModel
@{
	ViewData["Title"] = "Home";
	
	if (Request.Query.ContainsKey("confirm") && Request.Query["confirm"] == "true") {
		Layout = null;

		if (Request.Query.ContainsKey("redir")) {
			Response.Redirect(Request.Query["redir"]);
		}
		else {
			Response.Redirect("/");
		}

		return;
	}

	<h3>Are you sure?</h3>
	<p>
		You are attempting to delete
		@{
			await using var db = new Database.DbConn();
			var authorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db));

			switch (Request.Query["item"]) {
				case "trip": {
					var trip = db.Trips.First(p => p.TripId == int.Parse(Request.Query["id"]));
					if (trip.UserId != authorizedUser!.UserId) {
						return;
					}
					var legs = db.Legs.Where(p => p.TripId == int.Parse(Request.Query["id"])).OrderBy(p => p.DepTime).ToList();

					<span>
						the trip from <b>@legs.First().DepStation</b> to <b>@legs.Last().ArrStation</b>, starting <i>@legs.First().DepTime</i>
					</span>
					break;
				}
				case "leg": {
					Response.Redirect($"/Delete?item={Request.Query["item"]}&id={Request.Query["id"]}&confirm=true&redir={Request.Query["redir"].ToString().UrlEncode()}");
					break;
				}
			}
		}
	</p>
	<a href="@Request.Query["redir"]" class="btn btn-sm btn-secondary">Cancel</a>
	<a href="/Delete?item=@Request.Query["item"]&id=@Request.Query["id"]&confirm=true&redir=@Request.Query["redir"].ToString().UrlEncode()" class="btn btn-sm btn-danger">Confirm</a>
}
