{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
        <button type="submit" form="form-openbay" data-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary" onclick="validateForm(); return false;"><i class="fa fa-save"></i></button>
        <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>
        <li><a href="#tab-setting" data-toggle="tab">{{ tab_setting }}</a></li>
        <li><a href="#tab-developer" data-toggle="tab">{{ tab_developer }}</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">{{ feed_openbaypro_version }}</span></div>
          <input type="hidden" name="feed_openbaypro_version" value="{{ feed_openbaypro_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">
                  <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>
            <h4>{{ text_patch_option }}</h4>
            <p>{{ text_patch_description }}</p>
            <div class="well">
              <div class="form-group">
                <label class="col-sm-3 control-label" for="button-patch"><span data-toggle="tooltip" title="{{ help_patch }}">{{ entry_patch }}</span></label>
                <div class="col-sm-8">
                  <button class="btn btn-primary" id="button-patch">{{ button_patch }}</button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="tab-pane" id="tab-setting">
          <div class="well">
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-language">{{ text_language }}</label>
              <div class="col-sm-10">
                <select name="feed_openbaypro_language" id="input-language" class="form-control">
                  {% for language_iso, language_text in api_languages %}
                    <option value="{{ language_iso }}" {% if language_iso == feed_openbaypro_language %} selected="selected"{% endif %}>{{ language_text }}</option>
                  {% endfor %}
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label" for="button-clear-faq"><span data-toggle="tooltip" title="{{ help_clear_faq }}">{{ text_clear_faq }}</span></label>
              <div class="col-sm-10">
                <button class="btn btn-primary" id="button-clear-faq">{{ button_clear }}</button>
              </div>
            </div>
          </div>
        </div>
        <div class="tab-pane" id="tab-developer">
          <div class="well">
            <div class="form-group">
              <label class="col-sm-2 control-label" for="button-clear-data"><span data-toggle="tooltip" title="{{ help_empty_data }}">{{ entry_empty_data }}</span></label>
              <div class="col-sm-10"> <a class="btn btn-primary" id="button-clear-data">{{ button_clear }}</a> </div>
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
</div>

<script type="text/javascript"><!--
  $('#button-patch').bind('click', function(e) {
    e.preventDefault();

    $.ajax({
      url: 'index.php?route=marketplace/openbay/patch&user_token={{ user_token }}',
      type: 'post',
      dataType: 'json',
      beforeSend: function() {
        $('#button-patch').empty().html('<i class="fa fa-cog fa-lg fa-spin"></i>');
        $("#button-patch").attr('disabled', 'disabled');
      },
      success: function() {
        $('#button-patch').empty().removeClass('btn-primary').addClass('btn-success').html('{{ text_complete }}');
        alert('{{ text_patch_complete }}');
      },
      error: function (xhr, ajaxOptions, thrownError) {
        $('#button-patch').empty().html('{{ button_patch }}');
        if (xhr.status != 0) { alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); }
      }
    });
  });

  $('#button-clear-faq').bind('click', function(e) {
    e.preventDefault();

    $.ajax({
      url: 'index.php?route=marketplace/openbay/faqclear&user_token={{ user_token }}',
      beforeSend: function() {
        $('#button-clear-faq').empty().html('<i class="fa fa-cog fa-lg fa-spin"></i>');
        $("#button-clear-faq").attr('disabled','disabled');
      },
      type: 'post',
      dataType: 'json',
      success: function(json) {
        $('#button-clear-faq').empty().removeClass('btn-primary').addClass('btn-success').html('{{ text_complete }}');
        alert('{{ text_clear_faq_complete }}');
      },
      error: function (xhr, ajaxOptions, thrownError) {
        $('#button-clear-faq').empty().html('{{ button_clear }}');
        if (xhr.status != 0) { alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); }
      }
    });
  });

  $('#button-clear-data').bind('click', function(e) {
    e.preventDefault();

    var pass = prompt("{{ entry_password_prompt }}", "");

    if (pass != '') {
      $.ajax({
        url: 'index.php?route=marketplace/openbay/purge&user_token={{ user_token }}',
        type: 'post',
        dataType: 'json',
        data: 'pass=' + pass,
        beforeSend: function() {
          $('#button-clear-data').empty().html('<i class="fa fa-cog fa-lg fa-spin"></i>');
        },
        success: function(json) {
          setTimeout(function() {
            alert(json.msg);
            $('#button-clear-data').empty().html('{{ button_clear }}');
          }, 500);
        },
        error: function (xhr, ajaxOptions, thrownError) {
          if (xhr.status != 0) { alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); }
        }
      });
    } else {
      alert('{{ text_action_warning }}');
      $('#button-clear-data').empty().html('{{ button_clear }}');
    }
  });

  $('#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=marketplace/openbay/update&stage=check_server&user_token={{ user_token }}&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 + '%');
          updateCheckVersion(beta);
        }
      },
      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=marketplace/openbay/update&stage=check_version&user_token={{ user_token }}&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=marketplace/openbay/update&stage=download&user_token={{ user_token }}&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=marketplace/openbay/update&stage=extract&user_token={{ user_token }}&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=marketplace/openbay/update&stage=remove&user_token={{ user_token }}&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=marketplace/openbay/update&stage=run_patch&user_token={{ user_token }}&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=marketplace/openbay/update&stage=update_version&user_token={{ user_token }}&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');
        }
      },
      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 }}
