@page
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Extensions
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using trainav.web.Utils
@model TripModel
@{
	ViewData["Title"] = "Trip";

	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 != Model.AuthorizedUser.UserId) {
		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.Delimit(35))
				</td>
				<td>
					<b>@arrt</b> @Html.Raw(leg.ArrStation.Delimit(35))
				</td>
				<td>@leg.TrainType @leg.TrainNr</td>
				@if (!Request.Query.ContainsKey("edit")) {
					<td>
						@if (!string.IsNullOrWhiteSpace(leg.Comment)) {
							@Html.Raw(leg.Comment)
						}
					</td>
					<td>
						<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>
					</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="text-center">
		<div class="d-flex flex-row" style="justify-content: center">
			<div class="d-flex p-2" style="width: 60%">
				<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">transit.ztn.sh</a></h3>
					<br/>
					<input type="hidden" name="action" value="addleg">
					<input type="hidden" name="tripid" value="@Request.Query["id"]">
					<div class="input-group">
						<input type="text" class="form-control" name="link" placeholder="paste link / shortcode here">
						<div class="input-group-append">
							<button type="submit" class="btn btn-primary">Submit</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
}
