@page
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Extensions
@model TripModel
@{
    ViewData["Title"] = "Trip";

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

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

    if (Model.RedirToIndex) {
        Response.Redirect("/");
    }

    if (Request.HasFormContentType) {
        Response.Redirect(Request.GetEncodedUrl());
        return;
    }

    if (!Model.Legs.Any()) {
        Response.Redirect("/");
        return;
    }

    if (Model.Legs.First().UserId != int.Parse(HttpContext.Session.GetString("uid"))) {
        return;
    }

    var dep = Model.Legs.First().DepStation;
    var arr = Model.Legs.Last().ArrStation;
}

<div>
    <p>
        Your Trip from <b>@dep</b> to <b>@arr</b>, starting @DateTime.Parse(Model.Legs.First().DepTime).ToString("yyyy-MM-dd HH:mm")
    </p>
</div>
<div>
    <table class="table table-striped">
        <thead>
        <tr>
            <th scope="col">Date</th>
            <th scope="col">Departure</th>
            <th scope="col">Arrival</th>
            <th scope="col">Train</th>
            <th scope="col">Comment</th>
            @if (!Request.Query.ContainsKey("edit")) {
                <th scope="col">Actions</th>
            }
            else {
                <th scope="col">Danger zone</th>
            }
        </tr>
        </thead>
        <tbody>
        @foreach (var leg in Model.Legs) {
            if (leg.TrainType == "placeholder") {
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                @if (!Request.Query.ContainsKey("edit")) {
                    <td></td>
                }
                else {
                    <td>
                        <a class="btn btn-sm btn-outline-danger" href="/Delete?item=leg&id=@leg.LegId&redir=@Request.GetDisplayUrl().UrlEncode()">- Separator</a>
                    </td>
                }
                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 = deptime.ToString("HH:mm");
            var arrt = arrtime.ToString("HH:mm");
            <tr>
                <td>
                    @date
                </td>
                <td>
                    <b>@dept</b> @Html.Raw(leg.DepStation.PadRight(30, ' ').Substring(0, 30).TrimEnd())
                </td>
                <td>
                    <b>@arrt</b> @Html.Raw(leg.ArrStation.PadRight(30, ' ').Substring(0, 30).TrimEnd())
                </td>
                <td>@leg.TrainType @leg.TrainNr</td>
                @if (!Request.Query.ContainsKey("edit")) {
                    <td>
                        @if (!string.IsNullOrWhiteSpace(leg.Comment)) {
                            @Html.Raw(leg.Comment)
                        }
                    </td>
                    <td>
                        @if (leg.TicketId != 0) {
                            <a class="btn btn-sm btn-danger" href="/Inspection?leg=@leg.LegId" target="_blank">Ticket</a>
                        }
                        else {
                            <a class="btn btn-sm btn-danger disabled">Ticket</a>
                        }
                        <a class="btn btn-sm btn-primary" href="https://marudor.de/details/@leg.TrainType @leg.TrainNr/@(deptime.ToUniversalTime().ToString("O"))/?station=@leg.DepStationId" target="_blank">Marudor</a>
                        <a class="btn btn-sm btn-warning" href="https://travelynx.de/s/@leg.DepStationId?train=@leg.TrainType @leg.TrainNr" target="_blank">Travelynx</a>
                    </td>
                }
                else {
                    <td>
                        <form method="POST">
                            <input type="hidden" name="id" value="@leg.LegId">
                            <input type="text" class="form-control" name="comment" value="@leg.Comment"/>
                        </form>
                    </td>
                    <td>
                        @{
                            if (Model.Legs.All(p => p.DepTime != leg.DepTime + "_placeholder") && Model.Legs.IndexOf(leg) != Model.Legs.Count - 1) {
                                <a class="btn btn-sm btn-outline-secondary" href="?separator=true&legid=@leg.LegId&id=@leg.TripId&redir=@Request.GetDisplayUrl().UrlEncode()">+ Separator</a>
                            }
                            else {
                                <a class="btn btn-sm btn-outline-secondary disabled">+ Separator</a>
                            }

                        }
                        <a class="btn btn-sm btn-outline-danger" href="/Delete?item=leg&id=@leg.LegId&redir=@Request.GetDisplayUrl().UrlEncode()">- Leg</a>
                        @if (leg.TicketId != 0) {
                            <a class="btn btn-sm btn-outline-danger" href="/Delete?item=ticket&id=@leg.LegId&redir=@Request.GetDisplayUrl().UrlEncode()">- Ticket</a>
                        }
                        else {
                            <a class="btn btn-sm btn-outline-danger disabled">- Ticket</a>
                        }
                    </td>
                }
            </tr>
        }
        </tbody>
    </table>
</div>

@if (!Request.Query.ContainsKey("edit")) {
    <a class="btn btn-sm btn-success" href="/SharedTrip?id=@Request.Query["id"]&user=@Model.User">Share Trip</a>
    <a class="btn btn-sm btn-primary" href="/GenIcs?id=@Request.Query["id"]&user=@Model.User">Download ICS</a>
    <a class="btn btn-sm btn-secondary" href="/Trip?id=@Request.Query["id"]&edit=true">Edit Trip</a>
}
else {
    <a class="btn btn-sm btn-secondary" href="/Trip?id=@Request.Query["id"]">Back</a>
    <br/>
    <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 leg(s) from ticket</h3>
                <br/>
                <input type="hidden" name="action" value="addleg">
                <input type="hidden" name="tripid" value="@Request.Query["id"]">
                <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 leg(s) from <a href="https://oeffisear.ch" target="_blank">oeffisear.ch</a> or <a href="https://transit.ztn.sh" target="_blank">transitztn.sh</a></h3>
                <br/>
                <input type="hidden" name="action" value="addleg">
                <input type="hidden" name="tripid" value="@Request.Query["id"]">
                <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>
}
