<% html_title l(:label_ux_toolkit_attachment_storage) %>

<div class="contextual">
  <%= link_to l(:label_ux_toolkit_settings),
              plugin_settings_path(:redmine_ux_toolkit, tab: 'attachment_storage'),
              class: 'icon icon-settings' %>
</div>

<h2><%= l(:label_ux_toolkit_attachment_storage) %></h2>

<p class="info"><%= l(:text_ux_toolkit_attachment_storage_warning) %></p>

<% unless UxToolkitAttachmentTrashRecord.table_available? %>
  <p class="nodata"><%= l(:text_ux_toolkit_attachment_storage_unavailable) %></p>
<% end %>

<% attachment_filters_open = @filters.values.any? {|value| value.present? } %>
<details class="box tabular ux-toolkit-attachment-storage-filters" <%= 'open' if attachment_filters_open %>>
  <summary class="ux-toolkit-attachment-storage-filters-toggle">
    <%= l(:label_filter_plural) %>
  </summary>

  <div class="ux-toolkit-attachment-storage-filters-body">
    <%= form_tag ux_toolkit_attachment_storage_path, method: :get do %>
      <p>
        <%= label_tag 'filename', l(:label_ux_toolkit_attachment_filename_contains) %>
        <%= text_field_tag 'filename', @filters[:filename], size: 24 %>
      </p>
      <p>
        <%= label_tag 'content_type', l(:label_ux_toolkit_attachment_content_type) %>
        <%= select_tag 'content_type',
                       options_for_select([[l(:label_all), '']] + @content_types.map {|type| [type, type] }, @filters[:content_type]) %>
      </p>
      <p>
        <%= label_tag 'min_size', l(:label_ux_toolkit_attachment_min_size) %>
        <%= number_field_tag 'min_size', @filters[:min_size], min: 0, step: 1, size: 10 %>
        <em class="info"><%= l(:label_ux_toolkit_bytes) %></em>
      </p>
      <p>
        <%= label_tag 'max_size', l(:label_ux_toolkit_attachment_max_size) %>
        <%= number_field_tag 'max_size', @filters[:max_size], min: 0, step: 1, size: 10 %>
        <em class="info"><%= l(:label_ux_toolkit_bytes) %></em>
      </p>
      <p>
        <%= label_tag 'older_than_days', l(:label_ux_toolkit_attachment_older_than_days) %>
        <%= number_field_tag 'older_than_days', @filters[:older_than_days], min: 0, step: 1, size: 6 %>
      </p>
      <p>
        <%= label_tag 'project_id', l(:field_project) %>
        <%= select_tag 'project_id',
                       options_for_select([[l(:label_all), '']] + @projects.map {|project| [project.name, project.id] }, @filters[:project_id]) %>
      </p>
      <p>
        <%= label_tag 'container_type', l(:label_ux_toolkit_attachment_container_type) %>
        <%= select_tag 'container_type',
                       options_for_select([[l(:label_all), '']] + @container_types.map {|type| [type, type] }, @filters[:container_type]) %>
      </p>
      <p>
        <%= label_tag 'file_status', l(:label_ux_toolkit_attachment_file_status) %>
        <%= select_tag 'file_status',
                       options_for_select([
                         [l(:label_all), ''],
                         [l(:label_ux_toolkit_attachment_status_active), 'active'],
                         [l(:label_ux_toolkit_attachment_status_in_trash), 'in_trash'],
                         [l(:label_ux_toolkit_attachment_status_permanently_deleted), 'permanently_deleted'],
                         [l(:label_ux_toolkit_attachment_status_missing), 'missing']
                       ], @filters[:file_status]) %>
      </p>
      <p>
        <%= submit_tag l(:button_apply), name: nil %>
        <%= link_to l(:button_clear), ux_toolkit_attachment_storage_path, class: 'icon icon-reload' %>
      </p>
    <% end %>
  </div>
</details>

<p>
  <strong><%= l(:label_ux_toolkit_attachment_storage_total_files) %>:</strong>
  <%= @attachment_count %>
  |
  <strong><%= l(:label_ux_toolkit_attachment_storage_total_size) %>:</strong>
  <%= number_to_human_size(@total_size.to_i) %>
</p>

<% if @attachments.blank? %>
  <p class="nodata"><%= l(:text_ux_toolkit_no_attachments_found) %></p>
<% else %>
  <%= form_tag bulk_update_ux_toolkit_attachment_storage_path(request.query_parameters), method: :post, class: 'ux-toolkit-attachment-storage-form' do %>
    <div class="contextual">
      <%= button_tag l(:button_ux_toolkit_move_to_trash),
                     name: 'storage_action',
                     value: 'move_to_trash',
                     disabled: !@attachment_trash_enabled,
                     title: (@attachment_trash_enabled ? nil : l(:text_ux_toolkit_attachment_trash_disabled_action_help)),
                     data: {confirm: l(:text_ux_toolkit_confirm_move_attachments_to_trash)} %>
      <%= button_tag l(:button_ux_toolkit_restore_from_trash),
                     name: 'storage_action',
                     value: 'restore',
                     data: {confirm: l(:text_ux_toolkit_confirm_restore_attachments_from_trash)} %>
      <%= button_tag l(:button_ux_toolkit_permanently_delete),
                     name: 'storage_action',
                     value: 'permanently_delete',
                     class: 'icon icon-del',
                     data: {confirm: l(:text_ux_toolkit_confirm_permanently_delete_attachments)} %>
    </div>
    <% unless @attachment_trash_enabled %>
      <p class="info"><%= l(:text_ux_toolkit_attachment_trash_disabled_action_help) %></p>
    <% end %>

    <table class="list ux-toolkit-attachment-storage">
      <thead>
        <tr>
          <th class="checkbox hide-when-print">
            <%= check_box_tag 'ux_toolkit_attachment_storage_check_all', '1', false, data: {ux_toolkit_attachment_storage_check_all: '1'} %>
          </th>
          <th><%= attachment_storage_sort_link('filename', l(:label_ux_toolkit_attachment_filename)) %></th>
          <th><%= attachment_storage_sort_link('content_type', l(:label_ux_toolkit_attachment_content_type)) %></th>
          <th><%= attachment_storage_sort_link('filesize', l(:label_ux_toolkit_attachment_file_size)) %></th>
          <th><%= attachment_storage_sort_link('created_on', l(:label_ux_toolkit_attachment_upload_date)) %></th>
          <th><%= l(:field_author) %></th>
          <th><%= l(:field_project) %></th>
          <th><%= l(:label_ux_toolkit_attachment_container_type) %></th>
          <th><%= l(:label_ux_toolkit_attachment_container_reference) %></th>
          <th><%= attachment_storage_sort_link('downloads', l(:label_ux_toolkit_attachment_downloads)) %></th>
          <th><%= l(:label_ux_toolkit_attachment_file_status) %></th>
          <th class="buttons"><%= l(:label_ux_toolkit_actions) %></th>
        </tr>
      </thead>
      <tbody>
        <% @attachments.each do |attachment| %>
          <% trash_record = @trash_records_by_attachment_id[attachment.id] %>
          <% status = attachment_storage_status(attachment, trash_record) %>
          <tr class="ux-toolkit-attachment-status-<%= h(status) %>">
            <td class="checkbox hide-when-print">
              <%= check_box_tag 'attachment_ids[]', attachment.id, false, class: 'ux-toolkit-attachment-storage-checkbox' %>
            </td>
            <td><%= h(attachment.filename) %></td>
            <td><%= h(attachment.content_type.presence || '-') %></td>
            <td><%= number_to_human_size(attachment.filesize.to_i) %></td>
            <td><%= attachment.created_on ? format_time(attachment.created_on) : '-' %></td>
            <td><%= attachment.author ? link_to_user(attachment.author) : '-' %></td>
            <td>
              <% project = attachment.project %>
              <%= project ? link_to_project(project) : '-' %>
            </td>
            <td><%= h(attachment.container_type.presence || '-') %></td>
            <td><%= ux_toolkit_attachment_container_link(attachment) %></td>
            <td class="center"><%= attachment.downloads.to_i %></td>
            <td>
              <span class="ux-toolkit-issue-badge ux-toolkit-attachment-badge-<%= h(status) %>">
                <%= attachment_storage_status_label(attachment, trash_record) %>
              </span>
              <% if trash_record&.last_message.present? %>
                <br>
                <small><%= h(trash_record.last_message) %></small>
              <% end %>
            </td>
            <td class="buttons">
              <% if status == 'active' && @attachment_trash_enabled %>
                <%= link_to l(:button_ux_toolkit_move_file_to_trash),
                            bulk_update_ux_toolkit_attachment_storage_path(request.query_parameters.merge(attachment_ids: [attachment.id], storage_action: 'move_to_trash')),
                            method: :post,
                            data: {confirm: l(:text_ux_toolkit_confirm_move_attachments_to_trash)},
                            class: 'icon icon-del' %>
              <% elsif status == 'in_trash' %>
                <%= link_to l(:button_ux_toolkit_restore_file_from_trash),
                            bulk_update_ux_toolkit_attachment_storage_path(request.query_parameters.merge(attachment_ids: [attachment.id], storage_action: 'restore')),
                            method: :post,
                            data: {confirm: l(:text_ux_toolkit_confirm_restore_attachments_from_trash)},
                            class: 'icon icon-reload' %>
                <%= link_to l(:button_ux_toolkit_permanently_delete_trash_file),
                            bulk_update_ux_toolkit_attachment_storage_path(request.query_parameters.merge(attachment_ids: [attachment.id], storage_action: 'permanently_delete')),
                            method: :post,
                            data: {confirm: l(:text_ux_toolkit_confirm_permanently_delete_attachments)},
                            class: 'icon icon-del' %>
              <% else %>
                -
              <% end %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  <% end %>

  <span class="pagination"><%= pagination_links_full @attachment_pages, @attachment_count %></span>

  <%= javascript_tag do %>
    document.addEventListener('DOMContentLoaded', function() {
      var toggle = document.querySelector('[data-ux-toolkit-attachment-storage-check-all]');
      if (!toggle) { return; }

      toggle.addEventListener('change', function() {
        var checkboxes = document.querySelectorAll('.ux-toolkit-attachment-storage-checkbox');
        Array.prototype.forEach.call(checkboxes, function(checkbox) {
          checkbox.checked = toggle.checked;
        });
      });
    });
  <% end %>
<% end %>
