{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
        <button type="button" id="button-upload" data-toggle="tooltip" title="{{ button_upload }}" class="btn btn-primary"><i class="fa fa-upload"></i></button>
      </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">{% if error_warning %}
      <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}
        <button type="button" class="close" data-dismiss="alert">&times;</button>
      </div>
    {% endif %}
    {% if success %}
      <div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> {{ success }}
        <button type="button" class="close" data-dismiss="alert">&times;</button>
      </div>
    {% endif %}
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-exchange"></i> {{ heading_title }}</h3>
      </div>
      <div class="panel-body">
        <form class="form-horizontal">
          <fieldset>
            <legend>{{ text_progress }}</legend>
            <div class="form-group">
              <label class="col-sm-2 control-label">{{ entry_progress }}</label>
              <div class="col-sm-10">
                <div id="progress-backup" class="progress">
                  <div id="progress-bar" class="progress-bar"></div>
                </div>
                <div id="progress-text"></div>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <legend>{{ text_option }}</legend>
            <div class="form-group">
              <label class="col-sm-2 control-label">{{ entry_export }}</label>
              <div class="col-sm-10">
                <div class="well well-sm" style="height: 150px; overflow: auto;">
                  {% for table in tables %}
                    <div class="checkbox">
                      <label><input type="checkbox" name="backup[]" value="{{ table }}" checked="checked"/> {{ table }}</label>
                    </div>
                  {% endfor %}
                </div>
                <button type="button" onclick="$(this).parent().find(':checkbox').prop('checked', true);" class="btn btn-link">{{ text_select_all }}</button> / <button type="button" onclick="$(this).parent().find(':checkbox').prop('checked', false);" class="btn btn-link">{{ text_unselect_all }}</button>
              </div>
            </div>
            <div class="form-group">
              <div class="col-sm-offset-2 col-sm-10">
                <button type="button" id="button-backup" data-loading-text="{{ text_loading }}" class="btn btn-success"><i class="fa fa-download"></i> {{ button_backup }}</button>
              </div>
            </div>
          </fieldset>
          <fieldset>
            <legend>{{ text_history }}</legend>
            <div class="alert alert-info"><i class="fa fa fa-exclamation-circle"></i> {{ text_import }}</div>
            <div id="history"></div>
          </fieldset>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('#history').delegate('.pagination a', 'click', function(e) {
	e.preventDefault();

	$('#history').load(this.href);
});

$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');
//--></script>
<script type="text/javascript"><!--
$('#button-backup').on('click', function() {
	$('#progress-bar').css('width', '0%');
	$('#progress-bar').removeClass('progress-bar-danger progress-bar-success');

	backup('index.php?route=tool/backup/backup&user_token={{ user_token }}&table=' + $('input[name^=\'backup\']:checked').first().val());
});

function backup(url) {
	$('#progress-bar').css('width', '0%');
	$('#progress-bar').removeClass('progress-bar-danger progress-bar-success');

	$.ajax({
		url: url,
		type: 'post',
		data: $('input[name^=\'backup\']:checked'),
		dataType: 'json',
		beforeSend: function() {
			$('#button-backup').button('loading');
			$('#button-upload, #history .btn').prop('disabled', true);
		},
		success: function(json) {
			$('.alert-dismissible').remove();

			if (json['progress']) {
				$('#progress-bar').css('width', json['progress'] + '%');
			}

			if (json['error']) {
				$('#progress-bar').addClass('progress-bar-danger');
				$('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');

				$('#button-backup').button('reset');
				$('#button-upload, #history .btn').prop('disabled', false);
			}

			if (json['success']) {
				$('#progress-bar').css('width', '100%').addClass('progress-bar-success');
				$('#progress-text').html('<div class="text-success">' + json['success'] + '</div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');

				$('#button-backup').button('reset');
				$('#button-upload, #history .btn').prop('disabled', false);
			}

			if (json['text']) {
				$('#progress-text').html(json['text']);
			}

			if (json['next']) {
				backup(json['next']);
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);

			$('#button-backup').button('reset');
			$('#button-upload, #history .btn').prop('disabled', false);
		}
	});
};

// Restore
$('#history').on('click', '.btn-warning', function() {
	$('#progress-bar').css('width', '0%');
	$('#progress-bar').removeClass('progress-bar-danger progress-bar-success');

	restore('index.php?route=tool/backup/restore&user_token={{ user_token }}&filename=' + encodeURIComponent($(this).val(), this));
});

function restore(url, element) {
	$.ajax({
		url: url,
		type: 'post',
		dataType: 'json',
		beforeSend: function() {
			$(element).button('loading');
			$('#button-upload, #button-backup, #history .btn').not(element).prop('disabled', true);
		},
		success: function(json) {
			$('.alert-dismissible').remove();

			if (json['progress']) {
				$('#progress-bar').css('width', json['progress'] + '%');
			}

			if (json['error']) {
				$('#progress-bar').addClass('progress-bar-danger');
				$('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');

				$(element).button('reset');
				$('#button-upload, #button-backup, #history .btn').not(element).prop('disabled', false);
			}

			if (json['success']) {
				$('#progress-bar').css('width', '100%').addClass('progress-bar-success');
				$('#progress-text').html('<div class="text-success">' + json['success'] + '</div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');

				$(element).button('reset');
				$('#button-upload, #button-backup, #history .btn').not(element).prop('disabled', false);
			}

			if (json['text']) {
				$('#progress-text').html(json['text']);
			}

			if (json['next']) {
				restore(json['next'], element);
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);

			$(element).button('reset');
			$('#button-upload, #button-backup, #history .btn').not(element).prop('disabled', false);
		}
	});
}

$('#button-upload').on('click', function() {
	$('#form-upload').remove();

	$('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="upload" /></form>');

	$('#form-upload input[name=\'upload\']').trigger('click');

	if (typeof timer != 'undefined') {
		clearInterval(timer);
	}

	timer = setInterval(function() {
		if ($('#form-upload input[name=\'upload\']').val() != '') {
			clearInterval(timer);

			$.ajax({
				url: 'index.php?route=tool/backup/upload&user_token={{ user_token }}',
				type: 'post',
				dataType: 'json',
				data: new FormData($('#form-upload')[0]),
				cache: false,
				contentType: false,
				processData: false,
				beforeSend: function() {
					$('#button-upload').button('loading');
				},
				complete: function() {
					$('#button-upload').button('reset');
				},
				success: function(json) {
					$('.alert-dismissible').remove();

					if (json['error']) {
						$('#content > .container-fluid').prepend('<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
					}

					if (json['success']) {
						$('#content > .container-fluid').prepend('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');

						$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			});
		}
	}, 500);
});

// Delete
$('#history').on('click', '.btn-danger', function() {
	var element = this;

	$.ajax({
		url: 'index.php?route=tool/backup/delete&user_token={{ user_token }}&filename=' + encodeURIComponent($(element).val()),
		type: 'post',
		dataType: 'json',
		beforeSend: function() {
			$(element).button('loading');
		},
		complete: function() {
			$(element).button('reset');
		},
		success: function(json) {
			$('.alert-dismissible').remove();

			if (json['error']) {
				$('#history').before('<div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> ' + json['error'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');
			}

			if (json['success']) {
				$('#history').before('<div class="alert alert-success alert-dismissible"><i class="fa fa-check-circle"></i> ' + json['success'] + ' <button type="button" class="close" data-dismiss="alert">&times;</button></div>');

				$('#history').load('index.php?route=tool/backup/history&user_token={{ user_token }}');
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});
//--></script>
{{ footer }}
