<%
  # Redmine plugin for Document Management System "Features"
  #
  # Karel Pičman <karel.picman@kontron.com>
  #
  # This file is part of Redmine DMSF plugin.
  #
  # Redmine DMSF plugin is free software: you can redistribute it and/or modify it under the terms of the GNU General
  # Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
  # later version.
  #
  # Redmine DMSF plugin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  # the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  # more details.
  #
  # You should have received a copy of the GNU General Public License along with Redmine DMSF plugin. If not, see
  # <https://www.gnu.org/licenses/>.
%>

<span class="dmsf-attachments-icons hidden">
  <%= sprite_icon('del', icon_only: true, css_class: 'svg-del') %>
  <%= sprite_icon('attachment', icon_only: true, size: 16, css_class: 'svg-attachment') %>
  <%= sprite_icon('checked', icon_only: true, size: 16, css_class: 'svg-dmsf-assignment') %>
  <%= sprite_icon('link', icon_only: true, size: 16, css_class: 'svg-dmsf-link') %>
</span>
<span id="dmsf_attachments_fields" class="attachments_fields">
  <% if defined?(container) && container && container.saved_dmsf_attachments.present? %>
    <% container.saved_dmsf_attachments.each_with_index do |attachment, i| %>
      <% i += 1 %>
      <span id="dmsf_attachments_p<%= i %>" class="attachment">
        <%= hidden_field_tag "dmsf_attachments[p#{i}][token]", "#{attachment.token}" %>
        <%= sprite_icon('attachment', icon_only: true, size: 16, css_class: 'svg-attachment') %>
        <%= text_field_tag("dmsf_attachments[p#{i}][filename]", attachment.filename,
                           class: 'filename icon icon-attachment readonly') %>
        <%= link_to sprite_icon('del', l(:button_delete), icon_only: true),
                    dmsf_attachment_path(attachment, attachment_id: "p#{i}", format: 'js'),
                    method: 'delete', remote: true, title: l(:button_delete),
                    class: 'remove-upload icon-only icon-del' %>
        <% wf = container.saved_dmsf_attachments_wfs[attachment.id] %>
        <% if wf %>
          <%= link_to sprite_icon('checked', l(:title_assigned), icon_only: true), '#', remote: true,
                      title: l(:title_assigned), class: 'modify-upload icon-only icon-ok' %>
          <%= hidden_field_tag("dmsf_attachments_wfs[p#{i}]", wf.id) %>
        <% else %>
          <%= link_to sprite_icon('checked', l(:title_assignment), icon_only: true),
                      assign_dmsf_workflow_path(id: i, project_id: container.project&.id,
                        attachment_id: i), title: l(:label_dmsf_wokflow_action_assign),
                      remote: true, class: 'modify-upload icon-only icon-ok' %>
        <% end %>
      </span>
    <% end %>
  <% end %>
</span>

<span id="dmsf_links_attachments_fields" class="attachments_fields">
  <% if defined?(container) && container && container.saved_dmsf_links.present? %>
      <% container.saved_dmsf_links.each_with_index do |dmsf_link, index| %>
          <span id="dmsf_links_attachments_<%= index %>" class="attachment">
            <input name="dmsf_links[<%= index %>]" value="<%= dmsf_link.id %>" type="hidden">
            <input type="text" class='filename readonly' value="<%= dmsf_link.name %>">
            <%= link_to '', dmsf_link_attachment_path(dmsf_link, link_id: "#{index}", format: 'js'),
                method: 'delete', remote: true, class: 'remove-upload icon-only icon-del' %>
            <% wf = container.saved_dmsf_links_wfs[dmsf_link.id] %>
            <% if wf %>
              <%= link_to sprite_icon('checked', l(:title_assigned), icon_only: true), '#', remote: true,
                          class: 'modify-upload icon-only icon-ok' %>
                <%= hidden_field_tag("dmsf_links_wfs[#{dmsf_link.id}]", wf.id) %>
            <% else %>
              <%= render partial: 'dmsf_workflows/approval_workflow_button',
                 locals: { file: dmsf_link.target_file,
                           file_approval_allowed: User.current.allowed_to?(:file_approval, dmsf_link.target_file.project),
                           workflows_available: DmsfWorkflow.where(
                                 ['project_id = ? OR project_id IS NULL', dmsf_link.target_file.project_id]).exists?,
                           project: dmsf_link.target_file.project, wf: wf, dmsf_link_id: dmsf_link.id } %>
            <% end %>
          </span>
      <% end %>
  <% end %>
</span>

<span class="dmsf_add_attachment add_attachment">
  <%
    if defined?(container) && container
      project = container.project
      folder = container.system_folder
    else
      project = @project
      folder = @folder
    end
    project_or_folder = folder ? folder : project
    files = []
    if project_or_folder
      project_or_folder.dmsf_files.visible.each do |dmsf_file|
        rev = dmsf_file.last_revision
        if rev
          files << [dmsf_file.name, rev.major_version, rev.minor_version, rev.patch_version, dmsf_file.locked_for_user? ]
        end
      end
    end
  %>
  <%= file_field_tag 'dmsf_attachments[dummy][file]',
                   id: nil,
                   class: 'file_selector',
                   multiple: multiple,
                   onchange: 'dmsfAddInputFiles(this);',
                   data: {
                       max_number_of_files_message: l(:error_attachments_too_many,
                                                      max_number_of_files: (multiple ? 10 : 1)),
                       max_file_size: Setting.attachment_max_size.to_i.kilobytes,
                       max_file_size_message: l(:error_attachment_too_big,
                       max_size: number_to_human_size(Setting.attachment_max_size.to_i.kilobytes)),
                       max_concurrent_uploads: Redmine::Configuration['max_concurrent_ajax_uploads'].to_i,
                       upload_path: dmsf_uploads_path(format: 'js'),
                       project: project&.identifier,
                       awf: awf,
                       dmsf_file_details_form: controller.send(:render_to_string,
                         { partial: 'dmsf_upload/upload_file',
                           locals: { upload: DmsfUpload.new(project, folder, nil), i: 0 } }),
                       dmsf_file_details_form_locked: controller.send(:render_to_string,
                         { partial: 'dmsf_upload/upload_file_locked',
                           locals: { upload: DmsfUpload.new(project, folder, nil), i: 0 } }),
                       files: JSON.generate(files)
                   }
  %>
</span>
<% if defined?(container) && container %>
    <span class="dmsf-add-link">
        <%= link_to sprite_icon('add', l(:label_link_from)),
                    new_dmsf_link_path(project_id: project&.id, type: 'link_from', container: container.class.name),
                    title: l(:title_create_link), class: 'icon icon-add file_selector', remote: true %>
      </span>
<% end %>
<span class="info">
    (<%= l(:label_max_size) %>: <%= number_to_human_size(Setting.attachment_max_size.to_i.kilobytes) %>)
</span>
