<%
  # encoding: utf-8
  #
  # Redmine plugin for Document Management System "Features"
  #
  # Copyright © 2011-22 Karel Pičman <karel.picman@kontron.com>
  #
  # This program 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 2
  # of the License, or (at your option) any later version.
  #
  # This program 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 this program; if not, write to the Free Software
  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<% if file_approval_allowed %>
  <% case file.last_revision.workflow %>
    <% when DmsfWorkflow::STATE_WAITING_FOR_APPROVAL %>
        <% if wf %>
            <% assignments = wf.next_assignments(file.last_revision.id) %>
            <% index = assignments.find_index{|assignment| assignment.user_id == User.current.id} if assignments %>
            <% if assignments && index %>
                <%= link_to '',
                            action_dmsf_workflow_path(project_id: project.id, id: wf.id,
                                                      dmsf_workflow_step_assignment_id: assignments[index].id,
                                                      dmsf_file_revision_id: file.last_revision.id,
                                                      back_url: back_url),
                            title: l(:title_waiting_for_approval),
                            remote: true,
                            class: 'icon-only icon-ok' %>
            <% else %>
              <span class="icon-only"></span>
            <% end %>
        <% else %>
            <span class="icon-only"></span>
        <% end %>
    <% when DmsfWorkflow::STATE_ASSIGNED %>
        <% if User.current && (file.last_revision.dmsf_workflow_assigned_by_user == User.current) && wf %>
            <%= link_to '',
                        start_dmsf_workflow_path(id: file.last_revision.dmsf_workflow_id,
                                                 dmsf_file_revision_id: file.last_revision.id,
                                                 back_url: back_url),
                        title: l(:label_dmsf_wokflow_action_start),
                        class: 'icon-only icon-ok' %>
        <% else %>
            <span class="icon"></span>
        <% end %>
    <% when DmsfWorkflow::STATE_APPROVED, DmsfWorkflow::STATE_REJECTED, DmsfWorkflow::STATE_OBSOLETE %>
        <span class="icon-only"></span>
    <% else %>
        <% if workflows_available %>
          <%= link_to '',
            dmsf_link_id ?
              assign_dmsf_workflow_path(id: project.id, project_id: project.id, dmsf_link_id: dmsf_link_id,
                                        back_url: back_url) :
              assign_dmsf_workflow_path(id: project.id, project_id: project.id,
                                        dmsf_file_revision_id: file.last_revision.id,
                                        back_url: back_url),
            title: l(:label_dmsf_wokflow_action_assign),
            remote: true,
            class: 'icon-only icon-ok' %>
        <% else %>
            <span class="icon-only"></span>
        <% end %>
    <% end %>
<% end %>
