<script>
  if(document.querySelectorAll("#orbit-bar").length==0) location.reload();
  if( Array.prototype.delete == undefined ){
    Array.prototype.delete = function(v){
      var idx = this.indexOf(v);
      if(idx != -1){
        return this.splice(idx, 1)[0];
      }else{
        return null;
      }
    }
  }
  function reload_tbody(url){
    console.log("done");
    if(url == undefined){
      url = window.location.href;
    }
    $.get(url).done(function(data){
      $('#index_table tbody').replaceWith($(data).find('tbody'));
      if(window.scroll_top){
          $(window).scrollTop(window.scroll_top);
      }
      window.archive_delete_ids = [];
      $(".delete_archives_btn").addClass('hide');
    });
  }
  $(document).on("click", ".select_delete", function(){
    var that = $(this);
    var id = that.data("id");
    if(window.archive_delete_ids == undefined){
      window.archive_delete_ids = [];
    }
    if(that.prop("checked")){
      window.archive_delete_ids.push(id);
    }else{
      window.archive_delete_ids.delete(id);
    }
    if(window.archive_delete_ids.length == 0){
      $(".delete_archives_btn").addClass('hide');
    }else{
      $(".delete_archives_btn").removeClass('hide');
    }
  })
  function delete_archives(){
    if(!window.archive_delete_ids || window.archive_delete_ids.length == 0){
      return null;
    }
    var data = new FormData();
    var csrf_token = document
      .querySelector("meta[name='csrf-token']")
      .getAttribute("content");
    var csrf_param = document
      .querySelector("meta[name='csrf-param']")
      .getAttribute("content");
    if (csrf_token && csrf_param) {
      data.append(csrf_param, csrf_token);
    }
    window.archive_delete_ids.forEach(function(val){
      data.append('ids[]', val);
    });
    $.ajax({
      url: "<%=admin_archive_files_delete_path%>",
      method: 'DELETE',
      data: data,
      headers: {
        // 'Content-Type': 'multipart/form-data',
        // 使用 multipart/form-data 在此不需要設定 Content-Type。
        'X-Requested-With': 'XMLHttpRequest',
        'Authorization': `Bearer ${ csrf_token }`,
      },
      contentType: false, //required
      processData: false, // required
      statusCode: {
        204: reload_tbody,
        403: function() {
          alert( "Delete failed!" );
        }
      }
    })
  }
  $(document).on("click", ".delete_archives_btn", function(){
    if(window.confirm("<%=t(:sure?)%>")){
      if(window.confirm("<%=t(:sure?)%>")){
        delete_archives();
      }
    }
  })
</script>

<table class="table main-list">
  <thead>
    <tr class="sort-header">
    <th style="width: 1em;"></th>
    <% @table_fields.each do |f| %>
      <% if f == "archive.downloaded_times" %>
        <%= thead(f,true,false) %>
      <% else %>
        <%= thead(f) %>
      <% end %>
    <% end %>
    </tr>
  </thead>
  <tbody>
  <% @archives.each do |archive| %>
    <tr>
      <% is_editable = can_edit_or_delete?(archive) %>
      <td><% if is_editable %><input type="checkbox" class="select_delete" data-id="<%= archive.uid %>"><% end %></td>
      <td><%= archive.status_for_table %></td>
      <td>
        <%= archive.category.title rescue "" %>
         <% if (archive.category.disable rescue false) %>
            <span class='label'><%= t(:disabled) %></span>
          <% end %>
      </td>
      <td>
        <a href="/admin/archive_files/show?title=<%= archive.title %>" target="_blank"><%= archive.title %></a>
        <div class="quick-edit">
          <ul class="nav nav-pills">
            <% if is_editable %>
              <li><a href="/<%= I18n.locale.to_s %>/admin/archive_files/<%= archive.id.to_s %>/edit"><%= t(:edit) %></a></li>
              <li><a href="/admin/archive_files/<%= archive.id.to_s %>" class="delete text-error" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
            <% end %>
          </ul>
        </div>
      </td>
      <td><%= format_value archive.updated_at rescue nil %></td>
      <td><%= User.find(archive.update_user_id).user_name rescue nil %></td>
      <% download_total = 0
        archive.archive_file_multiples.each do |afm|
          download_total = download_total + afm.download_count
        end
      %>
      <td><%= download_total %></td>
    </tr>
  <% end %>
  </tbody>
</table>

<%=
  content_tag :div, class: "bottomnav clearfix" do
    content_tag(:div, paginate(@archives), class: "pagination pagination-centered") +
    content_tag(:div, link_to(t("archive.delete_selected"), "javascript:void(0)", :class=>"btn btn-primary"), class: "pull-right hide delete_archives_btn") +
    content_tag(:div, link_to(t(:new_),new_admin_archive_file_path, :class=>"btn btn-danger"), class: "pull-right")
  end
%>