{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right"><a href="{{ refresh }}" data-toggle="tooltip" title="{{ button_refresh }}" class="btn btn-info"><i class="fa fa-refresh"></i></a> <a href="{{ clear }}" data-toggle="tooltip" title="{{ button_clear }}" class="btn btn-warning"><i class="fa fa-eraser"></i></a>
        <button type="button" data-toggle="tooltip" title="{{ button_delete }}" class="btn btn-danger" onclick="confirm('{{ text_confirm }}') ? $('#form-modification').submit() : false;"><i class="fa fa-trash-o"></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="alert alert-info"><i class="fa fa-info-circle"></i> {{ text_refresh }}</div>
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-list"></i> {{ text_list }}</h3>
      </div>
      <div class="panel-body">
        {% if modifications %}
        <div class="form-group">
          <label class="col-sm-2 control-label">{{ entry_progress }}</label>
          <div class="col-sm-10">
            <div class="progress">
              <div id="progress-bar" class="progress-bar" style="width: 0%;"></div>
            </div>
            <div id="progress-text"></div>
          </div>
        </div>
        {% endif %}
        <ul class="nav nav-tabs">
          <li class="active"><a href="#tab-general" data-toggle="tab">{{ tab_general }}</a></li>
          <li><a href="#tab-log" data-toggle="tab">{{ tab_log }}</a></li>
        </ul>
        <div class="tab-content">
          <div class="tab-pane active" id="tab-general">
            <form action="{{ delete }}" method="post" enctype="multipart/form-data" id="form-modification">
              <div class="table-responsive">
                <table class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
                      <td class="text-left">{% if sort == 'name' %}
                        <a href="{{ sort_name }}" class="{{ order|lower }}">{{ column_name }}</a>
                        {% else %}
                        <a href="{{ sort_name }}">{{ column_name }}</a>
                        {% endif %}</td>
                      <td class="text-left">{% if sort == 'author' %}
                        <a href="{{ sort_author }}" class="{{ order|lower }}">{{ column_author }}</a>
                        {% else %}
                        <a href="{{ sort_author }}">{{ column_author }}</a>
                        {% endif %}</td>
                      <td class="text-left">{% if sort == 'version' %}
                        <a href="{{ sort_version }}" class="{{ order|lower }}">{{ column_version }}</a>
                        {% else %}
                        <a href="{{ sort_version }}">{{ column_version }}</a>
                        {% endif %}</td>
                      <td class="text-left">{% if sort == 'status' %}
                        <a href="{{ sort_status }}" class="{{ order|lower }}">{{ column_status }}</a>
                        {% else %}
                        <a href="{{ sort_status }}">{{ column_status }}</a>
                        {% endif %}</td>
                      <td class="text-left">{% if sort == 'date_added' %}
                        <a href="{{ sort_date_added }}" class="{{ order|lower }}">{{ column_date_added }}</a>
                        {% else %}
                        <a href="{{ sort_date_added }}">{{ column_date_added }}</a>
                        {% endif %}</td>
                      <td class="text-right">{{ column_action }}</td>
                    </tr>
                  </thead>
                  <tbody>
                    {% if modifications %}
                    {% for modification in modifications %}
                    <tr>
                      <td class="text-center">{% if modification.modification_id in selected %}
                        <input type="checkbox" name="selected[]" value="{{ modification.modification_id }}" checked="checked" />
                        {% else %}
                        <input type="checkbox" name="selected[]" value="{{ modification.modification_id }}" />
                        {% endif %}</td>
                      <td class="text-left">{{ modification.name }}</td>
                      <td class="text-left">{{ modification.author }}</td>
                      <td class="text-left">{{ modification.version }}</td>
                      <td class="text-left">{{ modification.status }}</td>
                      <td class="text-left">{{ modification.date_added }}</td>
                      <td class="text-right">
                        <a href="{{modification.edit}}" data-toggle="tooltip" title="{{button_edit}}" class="btn btn-success"><i class="fa fa-pencil"></i></a>
                        <a href="{{modification.download}}" data-toggle="tooltip" title="{{button_download}}" class="btn btn-primary" download="{{modification.filename}}"><i class="fa fa-download"></i></a>
                        <button type="button" data-loading-text="{{text_loading}}" data-modification-id="{{modification.modification_id}}" data-toggle="tooltip" title="{{button_upload}}"  class="btn btn-primary button-upload"><i class="fa fa-upload"></i></button>
                        {% if modification.link %}
                        <a href="{{ modification.link }}" data-toggle="tooltip" title="{{ button_link }}" class="btn btn-info" target="_blank"><i class="fa fa-link"></i></a>
                        {% else %}
                        <button type="button" class="btn btn-info" disabled="disabled"><i class="fa fa-link"></i></button>
                        {% endif %}
                        {% if not modification.enabled %}
                        <a href="{{ modification.enable }}" data-toggle="tooltip" title="{{ button_enable }}" class="btn btn-success"><i class="fa fa-plus-circle"></i></a>
                        {% else %}
                        <a href="{{ modification.disable }}" data-toggle="tooltip" title="{{ button_disable }}" class="btn btn-danger"><i class="fa fa-minus-circle"></i></a>
                        {% endif %}</td>
                    </tr>
                    {% endfor %}
                    {% else %}
                    <tr>
                      <td class="text-center" colspan="7">{{ text_no_results }}</td>
                    </tr>
                    {% endif %}
                  </tbody>
                </table>
              </div>
            </form>
            <div class="row">
              <div class="col-sm-6 text-left">{{ pagination }}</div>
              <div class="col-sm-6 text-right">{{ results }}</div>
            </div>
          </div>
          <div class="tab-pane" id="tab-log">
            <p>
              <textarea wrap="off" rows="15" class="form-control">{{ log }}</textarea>
            </p>
            <div class="text-center"><a href="{{ clear_log }}" class="btn btn-danger"><i class="fa fa-eraser"></i> {{ button_clear }}</a></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript"><!--
  var step = new Array();
  var total = 0;

  $('.button-upload').on('click', function() {
    $('#form-upload').remove();
    var modification_id =  $(this).attr('data-modification-id');

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

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

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

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

        // Reset everything
        $('.alert').remove();
        $('#progress-bar').css('width', '0%');
        $('#progress-bar').removeClass('progress-bar-danger progress-bar-success');
        $('#progress-text').html('');

        $.ajax({
          url: 'index.php?route=marketplace/modification/upload&user_token={{user_token}}&modification_id='+modification_id,
          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) {
            if (json['error']) {
              $('#progress-bar').addClass('progress-bar-danger');
              $('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');
            }

            if (json['step']) {
              step = json['step'];
              total = step.length;

              if (json['overwrite'].length) {
                html = '';

                for (i = 0; i < json['overwrite'].length; i++) {
                  html += json['overwrite'][i] + "\n";
                }

                $('#overwrite').html(html);

                $('#button-continue').prop('disabled', false);
              } else {
                next();
              }
            }
          },
          error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
        });
      }
    }, 500);
  });

  function next() {
    data = step.shift();

    if (data) {
      $('#progress-bar').css('width', (100 - (step.length / total) * 100) + '%');
      $('#progress-text').html('<span class="text-info">' + data['text'] + '</span>');

      $.ajax({
        url: data.url,
        type: 'post',
        dataType: 'json',
        data: 'path=' + data.path,
        success: function(json) {
          if (json['error']) {
            $('#progress-bar').addClass('progress-bar-danger');
            $('#progress-text').html('<div class="text-danger">' + json['error'] + '</div>');
            $('#button-clear').prop('disabled', false);
          }

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

          if (!json['error'] && !json['success']) {
            next();
          }
        },
        error: function(xhr, ajaxOptions, thrownError) {
          alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
      });
    }
  }
  //--></script>
{{ footer }}