@using Bit.Admin.Controllers
@model HomeModel
@{
    ViewData["Title"] = "Dashboard";
}

@section Scripts {
    <script>
        (() => {
            let loadedWebLatest = false;
            let loadedWebInstalled = false;

            fetch('@Url.Action("GetLatestVersion", new { project = @ProjectType.Web })').then((response) => {
                return response.json();
            }).then((version) => {
                document.getElementById('web-latest').innerText = version;
                loadedWebLatest = true;
                if (loadedWebInstalled) {
                    checkVersions('web', document.getElementById('web-installed').innerText, version);
                }
            });

            fetch('@Url.Action("GetLatestVersion", new { project = @ProjectType.Core })').then((response) => {
                return response.json();
            }).then((version) => {
                document.getElementById('server-latest').innerText = version;
                checkVersions('core', '@Model.CurrentVersion', version);
            });

            fetch('@Url.Action("GetInstalledWebVersion")').then((response) => {
                return response.json();
            }).then((version) => {
                document.getElementById('web-installed').innerText = version;
                loadedWebInstalled = true;
                if (loadedWebLatest) {
                    checkVersions('web', version, document.getElementById('web-latest').innerText);
                }
            });

            function checkVersions(platform, installed, latest) {
                if (installed === '-' || latest === '-') {
                    return;
                }

                if (installed !== latest) {
                    document.getElementById(platform + '-warning').classList.remove('d-none');
                } else {
                    document.getElementById(platform + '-success').classList.remove('d-none');
                }
            }
        })();
    </script>
}

<h1>Dashboard</h1>

<p>Welcome to the Bitwarden System Administration Portal.</p>

<h2>Version</h2>

<div class="row">
    <div class="col-md">
        <dl class="row">
            <dt class="col-sm-5">Server Installed</dt>
            <dd class="col-sm-7">
                <span id="server-installed">@Model.CurrentVersion</span>
                <i class="fa fa-check text-success d-none" id="core-success" title="You are up to date!"></i>
                <i class="fa fa-warning text-warning d-none" id="core-warning"
                   title="This version is not the latest. You should update."></i>
            </dd>

            <dt class="col-sm-5">Server Latest</dt>
            <dd class="col-sm-7">
                <span id="server-latest" title="Checking version..."><i class="fa fa-spinner fa-spin"></i></span>
            </dd>

            <dt class="col-sm-5">Web Installed</dt>
            <dd class="col-sm-7">
                <span id="web-installed"><i class="fa fa-spinner fa-spin" title="Checking version..."></i></span>
                <i class="fa fa-check text-success d-none" id="web-success" title="You are up to date!"></i>
                <i class="fa fa-warning text-warning d-none" id="web-warning"
                   title="This version is not the latest. You should update."></i>
            </dd>

            <dt class="col-sm-5">Web Latest</dt>
            <dd class="col-sm-7">
                <span id="web-latest"><i class="fa fa-spinner fa-spin" title="Checking version..."></i></span>
            </dd>
        </dl>
    </div>
    <div class="col-md">
        <ul class="list-unstyled">
            <li><a href="https://github.com/bitwarden/server/releases" target="_blank" rel="noreferrer">Check for Server
                    updates</a></li>
            <li><a href="https://github.com/bitwarden/clients/releases" target="_blank" rel="noreferrer">Check for Web
                    updates</a></li>
            <li><a href="https://help.bitwarden.com/article/updating-on-premise/" target="_blank" rel="noreferrer">How
                    do I update?</a></li>
        </ul>
    </div>
</div>

<h2>Settings</h2>

<h3>SMTP</h3>
@if(!Bit.Core.Utilities.CoreHelpers.SettingHasValue(Model.GlobalSettings.Mail?.Smtp?.Host))
{
    <p class="text-body-secondary">Not configured</p>
}
else
{
    <dl class="row">
        <dt class="col-sm-4 col-lg-3">Host</dt>
        <dd class="col-sm-8 col-lg-9">
            @(string.IsNullOrWhiteSpace(Model.GlobalSettings.Mail.Smtp.Host) ? "-" : Model.GlobalSettings.Mail.Smtp.Host)
        </dd>

        <dt class="col-sm-4 col-lg-3">Port</dt>
        <dd class="col-sm-8 col-lg-9">@Model.GlobalSettings.Mail.Smtp.Port</dd>

        <dt class="col-sm-4 col-lg-3">SSL</dt>
        <dd class="col-sm-8 col-lg-9">@(Model.GlobalSettings.Mail.Smtp.Ssl ? "Yes" : "No")</dd>

        <dt class="col-sm-4 col-lg-3">StartTLS</dt>
        <dd class="col-sm-8 col-lg-9">
            @(Model.GlobalSettings.Mail.Smtp.StartTls ? "Yes" : Model.GlobalSettings.Mail.Smtp.Port == 25 ? "No" : "Discover")
        </dd>

        <dt class="col-sm-4 col-lg-3">Username</dt>
        <dd class="col-sm-8 col-lg-9">
            @(string.IsNullOrWhiteSpace(Model.GlobalSettings.Mail.Smtp.Username) ? "-" : Model.GlobalSettings.Mail.Smtp.Username)
        </dd>
    </dl>
}

<h3>Other</h3>

<dl class="row">
    <dt class="col-sm-4 col-lg-3">Installation Id</dt>
    <dd class="col-sm-8 col-lg-9">
        @if(Model.GlobalSettings.Installation?.Id != null)
        {
            <code>@Model.GlobalSettings.Installation.Id</code>
        }
        else
        {
            <text>-</text>
        }
    </dd>

    <dt class="col-sm-4 col-lg-3">User Registration</dt>
    <dd class="col-sm-8 col-lg-9">@(Model.GlobalSettings.DisableUserRegistration ? "Disabled" : "Enabled")</dd>

    <dt class="col-sm-4 col-lg-3">Push Notifications</dt>
    <dd class="col-sm-8 col-lg-9">
        @if(Bit.Core.Utilities.CoreHelpers.SettingHasValue(Model.GlobalSettings.PushRelayBaseUri))
        {
            <span><i class="fa fa-check"></i> Configured</span>
        }
        else
        {
            <span class="text-body-secondary">Not configured</span>
        }
    </dd>

    <dt class="col-sm-4 col-lg-3">Yubico 2FA</dt>
    <dd class="col-sm-8 col-lg-9">
        @if(Bit.Core.Utilities.CoreHelpers.SettingHasValue(Model.GlobalSettings.Yubico?.Key) && Bit.Core.Utilities.CoreHelpers.SettingHasValue(Model.GlobalSettings.Yubico?.ClientId))
        {
            <span><i class="fa fa-check"></i> Configured</span>
        }
        else
        {
            <span class="text-body-secondary">Not configured</span>
        }
    </dd>

    <dt class="col-sm-4 col-lg-3">Duo 2FA</dt>
    <dd class="col-sm-8 col-lg-9">
        @if(Bit.Core.Utilities.CoreHelpers.SettingHasValue(Model.GlobalSettings.Duo?.AKey))
        {
            <span><i class="fa fa-check"></i> Configured</span>
        }
        else
        {
            <span class="text-body-secondary">Not configured</span>
        }
    </dd>
</dl>
