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

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

    if (Request.Query.ContainsKey("refresh")) {
        Response.Redirect("?edit=true");
        return;
    }
}

@if (Request.Query["edit"] == "true") {
    <table class="table table-striped">
        <thead>
        <tr>
            <th>Card</th>
            <th>Number</th>
            <th>Traveller</th>
            <th>Card Validity</th>
            <th>QR Validity</th>
            <th>Actions</th>
        </tr>
        </thead>
        <tbody>
        @foreach (var card in Model.Cards) {
            <tr>
                <td>@card.CardInfo.TrimEnd('#')</td>
                <td>@card.CardNumber</td>
                <td>@card.Traveller</td>
                <td>
                    @{
                        var from = DateTime.Parse(card.ValidFrom);
                        var to = DateTime.Parse(card.ValidTo);
                        if (from > DateTime.Now) {
                            <span style="color: red">
                                @card.ValidFrom.Split("T")[0]
                            </span>
                        }
                        else {
                            @card.ValidFrom.Split("T")[0]
                        }
                        <br/>
                        if (to < DateTime.Now) {
                            <span style="color: red">
                                @card.ValidTo.Split("T")[0]
                            </span>
                        }
                        else {
                            @card.ValidTo.Split("T")[0]
                        }
                    }
                </td>
                <td>
                    @{
                        var qrfrom = DateTime.Parse(card.QrValidFrom);
                        var qrto = DateTime.Parse(card.QrValidTo);
                        if (qrfrom > DateTime.Now) {
                            <span style="color: red">
                                @card.QrValidFrom.Split("T")[0]
                            </span>
                        }
                        else {
                            @card.QrValidFrom.Split("T")[0]
                        }
                        <br/>
                        if (qrto < DateTime.Now) {
                            <span style="color: red">
                                @card.QrValidTo.Split("T")[0]
                            </span>
                        }
                        else {
                            @card.QrValidTo.Split("T")[0]
                        }
                    }
                </td>
                <td>
                    <a class="btn btn-sm btn-primary" href="?refresh=@card.CardId">Refresh QR</a>
                    <a class="btn btn-sm btn-outline-danger" href="/Delete?item=card&id=@card.CardId&redir=@Request.GetDisplayUrl().UrlEncode()">Delete</a>
                </td>
            </tr>
        }
        </tbody>
    </table>
    <p>
        Please refresh your BahnCard list in the mobile DB Navigator before clicking "Refresh QR". This will be fixed in a later release.
    </p>
    <a class="btn btn-sm btn-secondary" href="@Request.Path">Back</a>
    <br/>
    <br/>
    <br/>
}
else if (Model.Cards.Any()) {
    foreach (var card in Model.Cards) {
        <div class="flex-grid">
            <div class="flex-col">
                <img src="@card.CardQr" alt="Ticketcode"/>
            </div>
            <div class="flex-col">
                <img src="@card.CardSecCode" alt="Sichtmerkmal"/>
            </div>
            <div class="flex-col">
                <div class="bahncard-outer">
                    <img class="bahncard" src='data:image/svg+xml;base64,@card.CardImage.Base64Encode()' alt="Bahncardimage"/>
                </div>
            </div>
        </div>
        var from = DateTime.Parse(card.ValidFrom);
        var to = DateTime.Parse(card.ValidTo);
        var qrfrom = DateTime.Parse(card.QrValidFrom);
        var qrto = DateTime.Parse(card.QrValidTo);

        if (from > DateTime.Now) {
            <div class="alert alert-info" role="alert">
                The above card is not valid yet.
            </div>
        }
        if (to < DateTime.Now) {
            <div class="alert alert-warning" role="alert">
                The above card is expired.
            </div>
        }
        if (qrfrom > DateTime.Now && !(from > DateTime.Now)) {
            <div class="alert alert-info" role="alert">
                The above QR code is somehow not valid yet. Congrats, please open a bug report.
            </div>
        }
        if (qrto < DateTime.Now) {
            <div class="alert alert-warning" role="alert">
                The above QR code is expired. Please refresh the card. <a href="/Cards?edit=true">You can do that now</a>.
            </div>
        }
        <br/>
    }
    <br/>
    <a class="btn btn-sm btn-secondary" href="?edit=true">Edit cards</a>
}

@if (!Model.Cards.Any() || Request.Query["edit"] == "true") {
    <h3>Get Cards from bahn.de account</h3>
    <br/>
    <form method="POST" action="/Card">
        <div class="form-group">
            <input type="text" class="form-control" id="InputUser" name="user" placeholder="bahn.de username">
        </div>
        <div class="form-group">
            <input type="password" class="form-control" id="InputPassword" name="pass" placeholder="bahn.de password">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" id="inputName" name="name" placeholder="Last name of traveller">
        </div>
        @Html.AntiForgeryToken()
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>
}