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

    if (HttpContext.Session.GetString("authorized") != "true") {
        Response.Redirect($"/Login?&redir={Request.GetDisplayUrl().UrlEncode()}");
        return;
    }
    await using var db = new Database.DbConn();
}

<div>
    <code>
        @foreach (var trip in db.Trips.Where(p => p.UserId == Model.AuthorizedUser.UserId).ToList().OrderBy(p => db.Legs.Where(l => l.TripId == p.TripId).OrderBy(l => l.DepTime).First().DepTime)) {
            <div>
                &nbsp;
            </div>
            foreach (var leg in db.Legs.Where(p => p.TripId == trip.TripId).OrderBy(p => p.DepTime)) {
                if (leg.TrainType == "placeholder") {
                    <div>
                        ... ... &nbsp;&nbsp; ..:.. ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - &nbsp; ... ... &nbsp;&nbsp; - &nbsp; ..:.. ... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...
                    </div>
                    continue;
                }
                var deptime = DateTime.Parse(leg.DepTime);
                var arrtime = DateTime.Parse(leg.ArrTime);
                var date = DateTime.Parse(leg.DepTime).ToString("ddd dd.MM.");
                var dept = date + " " + deptime.ToString("HH:mm");
                var depst = leg.DepStation.PadRight(20, ' ').Substring(0, 20).Replace(" ", "&nbsp;");
                var arrt = arrtime.ToString("HH:mm");
                var arrst = leg.ArrStation.PadRight(20, ' ').Substring(0, 20).Replace(" ", "&nbsp;");
                var line = leg.TrainType.PadRight(3).Replace(" ", "&nbsp;") + "&nbsp;" + leg.TrainNr.ToString().PadRight(6).Replace(" ", "&nbsp;");
                var comment = leg.Comment;
                <div>
                    @dept @Html.Raw(depst) - &nbsp; @Html.Raw(line) - &nbsp; @arrt @Html.Raw(arrst) @(string.IsNullOrWhiteSpace(comment) ? "..." : comment)
                </div>
            }
        }
    </code>
</div>