<%
  # 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/>.
%>

<% cls = link ? 'dmsf-gray' : '' %>

<td class="<%= cls %>">
  <% file_view_url = url_for({ controller: :dmsf_files, action: 'view', id: dmsf_file }) %>
  <% icon_name = icon_for_mime_type(Redmine::MimeType.css_class_of(dmsf_file.name)) %>
  <%= link_to sprite_icon(icon_name, h(link ? link.name : dmsf_file.title)),
    file_view_url,
    target: '_blank',
    rel: 'noopener',
    title: h(dmsf_file.last_revision.try(:tooltip)),
    'data-downloadurl' => "#{dmsf_file.last_revision.detect_content_type}:#{h(dmsf_file.name)}:#{file_view_url}" %>
</td>
<td class="<%= cls %>">
  <span class="size">(<%= number_to_human_size dmsf_file.last_revision.size %>)</span>
  <%= link_to '', view_dmsf_file_url(dmsf_file, download: dmsf_file.last_revision, disposition: 'attachment'),
        title: l(:title_title_version_version_download, title: h(dmsf_file.last_revision.title),
        version: dmsf_file.last_revision.version), class: 'icon icon-download dmsf-revision-action-button' %>
</td>
<td class="<%= cls %>">
  <%= dmsf_file.description unless dmsf_file.description.blank? %>
</td>
<td class="<%= cls %>">
  <span class="author"><%= dmsf_file.last_revision.user %>, <%= format_time(dmsf_file.last_revision.updated_at) %></span>
</td>
<% # Command icons %>
<td class="<%= cls %>">
  <span class="dmsf_upload_select">
    <% # Details %>
    <% if User.current.allowed_to? :file_manipulation, dmsf_file.project %>
      <%= link_to sprite_icon('edit', ''), dmsf_file_path(id: dmsf_file, back_url: issue_path(@issue)),
        title: l(:link_details, title: h(dmsf_file.last_revision.title)),
        class: 'icon-only icon-edit' %>
    <% else %>
      <span class="icon-only"></span>
    <% end %>
    <% # Email %>
    <%= link_to sprite_icon('email', ''), entries_operations_dmsf_path(id: dmsf_file.project_id, email_entries: 'email',
                                                                       ids: ["file-#{dmsf_file.id}"],
                                                                       back_url: issue_path(@issue)),
                method: :post, title: l(:heading_send_documents_by_email), class: 'icon-only icon-email-disabled' %>
    <% # Lock %>
    <% if !dmsf_file.locked? %>
      <%= link_to sprite_icon('lock', ''), lock_dmsf_files_path(id: dmsf_file, back_url: issue_path(@issue)),
                  title: l(:title_lock_file), class: 'icon-only icon-lock' %>
    <% elsif dmsf_file.unlockable? && (!dmsf_file.locked_for_user? || User.current.allowed_to?(:force_file_unlock, dmsf_file.project)) %>
      <%= link_to sprite_icon('unlock', ''), unlock_dmsf_files_path(id: dmsf_file, back_url: issue_path(@issue)),
                  title: dmsf_file.locked_title, class: 'icon-only icon-unlock' %>
    <% else %>
      <%= content_tag('span', sprite_icon('unlock', ''), title: dmsf_file.locked_title) %>
    <% end %>
    <% if !dmsf_file.locked? %>
      <% # Notifications %>
      <% if dmsf_file.notification %>
        <%= link_to sprite_icon('email', ''), notify_deactivate_dmsf_files_path(id: dmsf_file,
                                                                                back_url: issue_path(@issue)),
                    title: l(:title_notifications_active_deactivate), class: 'icon-only icon-email' %>
      <% else %>
        <%= link_to sprite_icon('email-disabled', ''), notify_activate_dmsf_files_path(id: dmsf_file,
                                                                                       back_url: issue_path(@issue)),
                    title: l(:title_notifications_not_active_activate), class: 'icon-only icon-email-add' %>
      <% end %>
    <% else %>
      <span class="icon-only"></span>
      <span class="icon-only"></span>
    <% end %>
    <% # Delete %>
    <% if @issue.attributes_editable? &&
       ((link && User.current.allowed_to?(:file_manipulation, dmsf_file.project)) ||
         (!link && User.current.allowed_to?(:file_delete, dmsf_file.project))) %>
      <% url = if link
                 dmsf_link_path(link, commit: 'yes', back_url: issue_path(@issue))
               else
                 dmsf_file_path(id: dmsf_file, commit: 'yes', back_url: issue_path(@issue))
               end %>
      <%= delete_link url, {}, '' %>
    <% end %>
    <% # Approval workflow %>
    <% wf = DmsfWorkflow.find_by(id: dmsf_file.last_revision.dmsf_workflow_id) if dmsf_file.last_revision.dmsf_workflow_id %>
    <%= render partial: 'dmsf_workflows/approval_workflow_button',
      locals: { file: dmsf_file,
         file_approval_allowed: User.current.allowed_to?(:file_approval, dmsf_file.project),
         workflows_available: DmsfWorkflow.where(['project_id = ? OR project_id IS NULL', dmsf_file.project_id]).exists?,
         project: dmsf_file.project, wf: wf, dmsf_link_id: nil, back_url: issue_path(@issue) } %>
  </span>
</td>
