<?php
Yii::app()->clientScript->registerCoreScript('jquery.ui');
cs()->registerScriptFile(Yii::app()->baseUrl . '/js/plDialog/plDialog.js', CClientScript::POS_HEAD);

echo "<h1>" . __('SYSTEM_UPDATE_STATUS_SCREEN_HEADER') . "</h1>";

echo "<ul id='updateContent'></ul>";

echo "<div class='toggleUpdateDoneBtns'>";
echo CHtml::linkButton(__('SYSTEM_UPDATE_UPGRADE_DONE_LOGOUT_NOW'), array(
    'class' => 'modalSaveBtn btn btn-primary',
    'href' => url('systemUpdate/default/rebootSystem'),
    'data-translation-id' => 'SYSTEM_UPDATE_UPGRADE_DONE_LOGOUT_NOW'
        )
);
echo CHtml::linkButton(__('SYSTEM_UPDATE_UPGRADE_DONE_STAY'), array(
    'class' => 'modalSaveBtn btn btn-info',
    'href' => url('systemUpdate'),
    'data-translation-id' => 'SYSTEM_UPDATE_UPGRADE_DONE_STAY'
        )
);
echo "</div>";
?>
<script>
    getStatusOutput();
    toggleUpdateDoneBtns(false);

    function getStatusOutput() {
        $.ajax({
            url: "<?php echo url('systemUpdate/default/getStatusOutput'); ?>",
            type: "POST",
            dataType: "json",
            data: {},
            success: function (result) {
                if (typeof result.status !== "undefined") {
                    if (result.status === 'progress') {
                        showOutput(result);
                        setTimeout(function () {
                            getStatusOutput();
                        }, 200);
                    } else {
                        showOutput(result);
                        toggleUpdateDoneBtns(true);
                    }
                }
            },
            error: function () {
                setTimeout(function () {
                    getStatusOutput();
                }, 200);
            }
        });
    }

    function showOutput(result) {
        var lastContentID = $('#updateContent li').last().attr("data-updateLine-id");
        if (typeof lastContentID === "undefined") {
            lastContentID = 0;
        }

        if (typeof result.output !== "undefined" && typeof result.output[lastContentID] !== "undefined") {
            var debugOutput = '';
            $.each(result.output, function (key, value) {
                if (value === '') {
                    return true;
                }
                debugOutput = debugOutput + "<li data-updateLine-id='" + key + "'>" + value + "</li>";
            });
            $('#updateContent').html(debugOutput);
        }
    }

    function toggleUpdateDoneBtns(updateDone) {
        if (updateDone === true) {
            removeUpdateIsRunningMsg();
            $('.toggleUpdateDoneBtns').show();
        } else {
            addUpdateIsRunningMsg();
            $('.toggleUpdateDoneBtns').hide();
        }
    }

    function removeUpdateIsRunningMsg() {
        var systemUpdateIsRunningMsg = $('#systemUpdateIsRunningMsg');
        systemUpdateIsRunningMsg.remove();
    }

    function addUpdateIsRunningMsg() {
        var systemUpdateIsRunningMsg = '<?php echo __('SYSTEM_UPDATE_UPGRADE_IS_RUNNING_MSG'); ?>';
        var img = $('<img />')
                .attr("src", "/img/loading.gif");
        var span = $('<span />')
                .attr("id", "systemUpdateIsRunningMsg")
                .append(img)
                .append(" ")
                .append(systemUpdateIsRunningMsg);
        $('.toggleUpdateDoneBtns').before(span);
    }
</script>
