{{ header }}{{ column_left }}
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="pull-right">
                <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default"><i class="fa fa-reply"></i></a>
            </div>
            <h1>{{ heading_title }}</h1>
            <ul class="breadcrumb">
                {% for breadcrumb in breadcrumbs %}
                    <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
                {% endfor %}
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-openbay" class="form-horizontal">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#tab-update" data-toggle="tab">{{ tab_update }}</a></li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="tab-update">
                    <div class="alert alert-info text-left">{{ text_version_installed }} <span id="text-version">{{ text_version }}</span></div>
                    <input type="hidden" name="entegrasyon_version" value="{{ entegrasyon_version }}" />

                    <div class="container-fluid">
                        <h4>{{ text_software_update }}</h4>
                        <p>{{ text_update_description }}</p>
                        <div class="well">
                            <div class="alert alert-danger" id="update-error" style="display:none;"></div>
                            <div id="update-v2-box">
                                <div class="form-group hidden">
                                    <label class="col-sm-3 control-label" for="update-v2-beta"><span data-toggle="tooltip" title="{{ help_beta }}">{{ entry_beta }}</span></label>
                                    <div class="col-sm-8">
                                        <select id="update-v2-beta" class="form-control">
                                            <option value="1">{{ text_yes }}</option>
                                            <option value="0" selected="selected">{{ text_no }}</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label" for="update-v2"><span data-toggle="tooltip" title="{{ help_easy_update }}">{{ entry_update }}</span></label>
                                    <div class="col-sm-8">
                                        <button class="btn btn-primary" id="update-v2">{{ button_update }}</button>
                                    </div>
                                </div>
                            </div>
                            <div id="update-v2-progress" style="display:none;">
                                <div class="progress" style="height:50px;">
                                    <div class="progress-bar progress-bar-striped active progress-bar-info" role="progressbar" aria-valuenow="1" aria-valuemin="0" aria-valuemax="100" style="width: 0%;" id="loading-bar"></div>
                                </div>
                                <h4 class="text-center" id="update-text"></h4>
                            </div>
                        </div>


                        <div class="row" style="margin-top: 10px">
                            <h2 class="well">Güncelleme ile gelen değişiklikler ve yenilikler.</h2>
                            <ul class="list-group">
                                <li class="list-group-item">
                                    {{versiyon_content}}


                                </li>


                            </ul>

                        </div>
                    </div>

                </div>
        </form>
    </div>
</div>

<script type="text/javascript"><!--


    $('#update-v2').bind('click', function(e) {
        e.preventDefault();

        var text_confirm = confirm('{{ text_confirm_backup }}');

        if (text_confirm == true) {
            $('#update-error').hide();
            $('#update-v2-box').hide();
            $('#update-v2-progress').fadeIn();
            $('#update-text').text('{{ text_check_server }}');
            $('#loading-bar').css('width', '5%');

            var beta = $('#update-v2-beta :selected').val();

            updateCheckServer(beta);
        }
    });

    function updateCheckServer(beta) {
        $.ajax({
            url: 'index.php?route=entegrasyon/setting/update&stage=check_server&{{ token_link }}&beta=' + beta,
            type: 'post',
            dataType: 'json',
            beforeSend: function() { },
            success: function(json) {


                if (json.error == 1) {
                    updateError(json.response);
                } else {
                    $('#update-text').text(json.status_message);
                    $('#loading-bar').css('width', json.percent_complete + '%');
                    {%  if mode =='again' %}

                    updateDownload(beta);

                    {% else  %}
                    updateCheckVersion(beta)
                    {% endif  %}
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if (xhr.status != 0) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }
        });
    }

    function updateCheckVersion(beta) {
        $.ajax({
            url: 'index.php?route=entegrasyon/setting/update&stage=check_version&{{ token_link }}&beta=' + beta,
            type: 'post',
            dataType: 'json',
            beforeSend: function() { },
            success: function(json) {
                if (json.error == 1) {
                    $('#update-error').removeClass('alert-danger').addClass('alert-info').html('<i class="fa fa-check"></i> ' + json.response).show();
                    $('#update-v2-progress').hide();
                    $('#update-v2-box').fadeIn();
                } else {
                    $('#update-text').text(json.status_message);
                    $('#loading-bar').css('width', json.percent_complete + '%');
                    updateDownload(beta);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if (xhr.status != 0) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }
        });
    }

    function updateDownload(beta) {
        $.ajax({
            url: 'index.php?route=entegrasyon/setting/update&stage=download&{{ token_link }}&beta=' + beta,
            type: 'post',
            dataType: 'json',
            beforeSend: function() { },
            success: function(json) {
                if (json.error == 1) {
                    updateError(json.response);
                } else {
                    $('#update-text').text(json.status_message);
                    $('#loading-bar').css('width', json.percent_complete + '%');
                    updateExtract(beta);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if (xhr.status != 0) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }
        });
    }

    function updateExtract(beta) {
        $.ajax({
            url: 'index.php?route=entegrasyon/setting/update&stage=extract&{{ token_link }}&beta=' + beta,
            type: 'post',
            dataType: 'json',
            beforeSend: function() { },
            success: function(json) {
                if (json.error == 1) {
                    updateError(json.response);
                } else {
                    $('#update-text').text(json.status_message);
                    $('#loading-bar').css('width', json.percent_complete + '%');
                    updateRemove(beta);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if (xhr.status != 0) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }
        });
    }

    function updateRemove(beta) {
        $.ajax({
            url: 'index.php?route=entegrasyon/setting/update&stage=remove&{{ token_link }}&beta=' + beta,
            type: 'post',
            dataType: 'json',
            beforeSend: function() { },
            success: function(json) {
                if (json.error == 1) {
                    $('#update-v2-progress').prepend('<div class="alert alert-warning">' + json.response + '</div>');
                }

                $('#update-text').text(json.status_message);
                $('#loading-bar').css('width', json.percent_complete + '%');
                updatePatch(beta);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if (xhr.status != 0) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }
        });
    }

    function updatePatch(beta) {
        $.ajax({
            url: 'index.php?route=entegrasyon/setting/update&stage=run_patch&{{ token_link }}&beta=' + beta,
            type: 'post',
            dataType: 'json',
            beforeSend: function() { },
            success: function(json) {
                if (json.error == 1) {
                    updateError(json.response);
                } else {
                    $('#update-text').text(json.status_message);
                    $('#loading-bar').css('width', json.percent_complete + '%');
                    updateVersion(beta);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if (xhr.status != 0) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }
        });
    }

    function updateVersion(beta) {
        $.ajax({
            url: 'index.php?route=entegrasyon/setting/update&stage=update_version&{{ token_link }}&beta=' + beta,
            type: 'post',
            dataType: 'json',
            beforeSend: function() { },
            success: function(json) {
                if (json.error == 1) {
                    updateError(json.response);
                } else {
                    $('#update-text').text(json.status_message);
                    $('#text-version').text(json.response);
                    $('#loading-bar').css('width', json.percent_complete + '%').removeClass('progress-bar-info').addClass('progress-bar-success');
                    $('#update-v2-progress').hide();
                    $('#update-v2-box').fadeIn();

                    window.location.href='index.php?route=entegrasyon/setting/update_after&{{ token_link }}';
                    window.location.replace('index.php?route=entegrasyon/setting/update_after&{{ token_link }}');

                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                if (xhr.status != 0) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            }
        });
    }

    function updateError(errors) {
        $('#update-error').text(errors).show();

        $('#update-v2-progress').hide();
        $('#update-v2-box').fadeIn();
    }

    function validateForm() {
        $('#form-openbay').submit();
    }
    //--></script>
{{ footer }}
