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

<div class="text-center">
    @if (HttpContext.Session.GetString("authorized") != "true") {
        <h1 class="display-4">Welcome</h1>
        <p>Please <a href="/Login">log in</a> to see your personal plan.</p>
    }
    else {
        <h1 class="display-4">
            Welcome, @Model.AuthorizedUser.Username
        </h1>
        <p>
            Here are your planned trips.
        </p>
        <table class="table table-striped">
            <thead>
            <tr>
                <th scope="col">Origin</th>
                <th scope="col">Via</th>
                <th scope="col">Destination</th>
                <th scope="col">Comments</th>
                <th scope="col">Actions</th>
            </tr>
            </thead>
            <tbody>
            @{
                await using var db = new Database.DbConn();
                var trips = db.Trips.Where(p => p.UserId == Model.AuthorizedUser.UserId).ToList();

                foreach (var trip in trips.Where(trip => !db.Legs.Any(p => p.TripId == trip.TripId))) {
                    await db.DeleteAsync(trip);
                    trips = db.Trips.Where(p => p.UserId == Model.AuthorizedUser.UserId).ToList();
                }

                foreach (var trip in trips.OrderBy(p => db.Legs.First(a => a.TripId == p.TripId).DepTime)) {
                    var legs = db.Legs.Where(p => p.TripId == trip.TripId).OrderBy(p => p.DepTime).ToList();
                    <tr>
                        <td>
                            <b>@legs.First().DepStation.Delimit(30)</b>
                            <br/>
                            <small>@DateTime.Parse(legs.First().DepTime).ToString("ddd dd.MM.yyyy, HH:mm")</small>
                        </td>
                        <td>
                            @((from index in from leg in legs where leg.TrainType == "placeholder" select legs.IndexOf(leg) select legs[index - 1].ArrStation.Delimit(30)).ToList().DefaultIfEmpty("-").Aggregate((s1, s2) => $"{s1}, {s2}"))
                        </td>
                        <td>
                            <b>@legs.Last().ArrStation.Delimit(30)</b>
                            <br/>
                            <small>@DateTime.Parse(legs.Last().ArrTime).ToString("ddd dd.MM.yyyy, HH:mm")</small>
                        </td>
                        <td>
                            @Html.Raw(legs.Where(leg => !string.IsNullOrWhiteSpace(leg.Comment)).Select(p => p.Comment.Replace(", ", "<br/>")).DefaultIfEmpty("").Aggregate((s1, s2) => $"{s1}<br/>{s2}"))
                        </td>
                        <td>
                            <a class="btn btn-sm btn-primary" href="/Trip?id=@trip.TripId">View</a>
                            &nbsp;
                            <a class="btn btn-sm btn-outline-danger" href="/Delete?item=trip&id=@trip.TripId&redir=@Request.GetDisplayUrl().UrlEncode()">Delete</a>
                        </td>
                    </tr>
                }
            }
            </tbody>
        </table>
        <br/>
        <div class="d-flex flex-row" style="justify-content: space-between">
            <div class="d-flex p-2" style="width: 40%">
                <form style="width: 100%" method="GET" action="/Ticket">
                    <h3>Add trip from ticket</h3>
                    <br/>
                    <div class="form-group">
                        <input type="text" class="form-control" name="order" placeholder="bahn.de order #">
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" name="name" placeholder="Last name of traveller">
                    </div>
                    <br/>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
            <div class="d-flex p-2" style="width: 40%">
                <form style="width: 100%" method="GET" action="/OEAPI">
                    <h3>Add trip from <a href="https://oeffisear.ch" target="_blank">oeffisear.ch</a></h3>
                    <br/>
                    <div class="form-group">
                        <input type="text" class="form-control" name="link" placeholder="oeffisear.ch link / shortcode">
                    </div>
                    <br/>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </form>
            </div>
        </div>
    }
</div>