{% extends base.html %}

{% block body_right %}
    {% include system/component/admin_nav.html %}
{% end %}

{% block body_left %}

    {% include system/component/system_css.html %}

<div class="card">
    {% include common/title/base_title.html %}
</div>

<div class="card">
    {% for item in info_items %}
        <div class="settings-row">
            <div class="settings-key">{{item.name}}</div>
            <div class="settings-value">
                {% if item.link != "" %}
                    <a class="btn btn-default" href="{{_server_home}}{{item.link}}">{{item.value}}</a>
                {% else %}
                    {{item.value}}
                {% end %}
                {% if item.extra_link != "" %}
                    <a href="{{_server_home}}{{item.extra_link}}">查看详情</a>
                {% end %}
            </div>
        </div>
    {% end %}

    <div class="settings-row btn-line-height">
        <div class="settings-key">重启应用</div>
        <div class="settings-value">
            <button class="btn btn-default danger2" onclick="xnote.tmp.restart()">重启</button>
        </div>
    </div>

    <div class="settings-row btn-line-height">
        <div class="settings-key">升级系统</div>
        <div class="settings-value">
            <button class="btn btn-default danger2" onclick="xnote.tmp.upgrade()">升级</button>
        </div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var checkInterval = 500;
    var loadingIndex;

    function checkSystemStatus() {
        xnote.http.internalPost("/system/reload?runtime_id={{runtime_id}}", function (resp) {
            console.log(resp)
            if (resp.success) {
                xnote.toast("重启成功!");
                window.location.reload();                
            } else {
                setTimeout(checkSystemStatus, checkInterval)
            }
        }).fail(function (err) {
            console.error("checkSystemStatus failed, wait to retry", err);
            setTimeout(checkSystemStatus, checkInterval);
        })
    }

    function doRestart() {
        xnote.toast("重启中，请等待2-5分钟...")
        loadingIndex = layer.load(1);
        checkSystemStatus();
    }

    xnote.tmp.restart = function () {
        xnote.confirm("确定重启吗?", function () {
            doRestart();
        })
    };

    xnote.tmp.doUpgrade = function() {
        xnote.http.post("/system/pull_code", function (resp) {
            if (resp.success) {
                doRestart();
            } else {
                xnote.alert(resp.message);
            }
        })
    }

    xnote.tmp.upgrade = function() {
        xnote.confirm("确定升级系统吗?", function () {
            xnote.tmp.doUpgrade();
        })
    }
})
</script>

{% end %}