@model TightWiki.Models.ViewModels.Utility.NotifyViewModel
@{
    Layout = "/Views/Shared/_Layout.cshtml";
    var sessionState = ViewData["SessionState"] as TightWiki.SessionState ?? throw new Exception("Wiki State Context cannot be null.");
    bool _isFirst = true;
}

@if (string.IsNullOrEmpty(@Model.RedirectURL) == false && string.IsNullOrEmpty(Model.ErrorMessage) == true && Model.RedirectTimeout > 0)
{
    <script>
        document.addEventListener("DOMContentLoaded", function () {
            var countdownElement = document.getElementById('countdown');
            var countdownTime = @(Model.RedirectTimeout - 1);

            var interval = setInterval(function () {
                countdownElement.textContent = countdownTime;
                countdownTime--;

                if (countdownTime < 0) {
                    clearInterval(interval);
                    window.location.href = "@Model.RedirectURL";
                }
            }, 1000);
        });
    </script>
}

@if (!string.IsNullOrEmpty(Model.ErrorMessage))
{
    <div class="alert alert-danger">
        @{
            var errorMessages = @Model.ErrorMessage.Replace("\r\n", "\n").Split("\n");
        }
        @foreach (var message in errorMessages)
        {
            if (_isFirst)
            {
                <strong>@message</strong>
                if (errorMessages.Count() > 1)
                {
                    <br />
                    <hr />
                }
                _isFirst = false;
            }
            else
            {
                @message
                <br />
            }
        }
        @if (string.IsNullOrEmpty(@Model.RedirectURL) == false)
        {
            <br />
            <a href="@Model.RedirectURL">继续</a>
        }
    </div>
}
@if (!string.IsNullOrEmpty(Model.SuccessMessage))
{
    <div class="alert alert-success">
        @{
            var successMessages = @Model.SuccessMessage.Replace("\r\n", "\n").Split("\n");
        }
        @foreach (var message in successMessages)
        {
            if (_isFirst)
            {
                <strong>@message</strong>
                if (successMessages.Count() > 1)
                {
                    <br />
                    <hr />
                }
                _isFirst = false;
            }
            else
            {
                @message
                <br />
            }
        }

        @if (string.IsNullOrEmpty(@Model.RedirectURL) == false && Model.RedirectTimeout > 0)
        {
            <p>
                <a href="@Model.RedirectURL">继续</a> 或者即将跳转至 <span id="countdown">@Model.RedirectTimeout</span> seconds.
            </p>
        }
    </div>
}
