@page
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Extensions
@using bahnplan.web.database
@model DeleteModel
@{
    ViewData["Title"] = "Home";

    if (HttpContext.Session.GetString("authorized") != "true") {
        Response.Redirect($"/Login?&redir={Request.GetDisplayUrl().UrlEncode()}");
        return;
    }

    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();

            switch (Request.Query["item"]) {
                case "trip": {
                    var trip = db.Trips.First(p => p.TripId == int.Parse(Request.Query["id"]));
                    if (trip.UserId != int.Parse(HttpContext.Session.GetString("uid"))) {
                        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;
                }
                case "ticket": {
                    Response.Redirect($"/Delete?item={Request.Query["item"]}&id={Request.Query["id"]}&confirm=true&redir={Request.Query["redir"].ToString().UrlEncode()}");
                    break;
                }
                case "card": {
                    var card = db.Cards.First(p => p.CardId == int.Parse(Request.Query["id"]));
                    if (card.UserId != int.Parse(HttpContext.Session.GetString("uid"))) {
                        return;
                    }
                    <span>
                        the card <b>@card.CardInfo.TrimEnd('#')</b> with the number <i>@card.CardNumber</i>, owned by <b>@card.Traveller</b>
                    </span>
                    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>
}