<% javascript('views/plans/share.js') %>

<div class="row">
  <div class="col-md-12">
    <!-- render the project title -->
    <h1><%= @plan.title %></h1>
  </div>
</div>

<div class="row">
  <div class="col-md-12">
    <div class="tabbed-area">
      <!-- render navigation tabs for the project-->
      <ul class="tabs" role="tablist">
        <li id="details-tab" role="tab" aria-controls="details-panel">
          <a href="<%= plan_path(@plan) %>" aria-selected="false"><%= _('Project Details') %></a>
        </li>

        <% @plan.template.phases.each do |phase| %>
          <li id="phase-#<%= phase.id %>-tab" role="tab" aria-controls="phase-#<%= phase.id %>-panel">
            <a href="<%= "#{edit_plan_phase_path(@plan, phase)}" %>" aria-selected="false"><%= (@plan.template.phases.length > 1 ? phase.title : _('Write Plan')) %></a>
          </li>
        <% end %>

        <li id="share-tab" role="tab" aria-controls="share-panel" class="active">
          <a href="#share-panel" aria-selected="true"><%= _('Share') %></a>
        </li>
        <li id="download-tab" role="tab" aria-controls="download-panel">
          <a href="<%= show_export_plan_path(@plan) %>" aria-selected="false"><%= _('Download') %></a>
        </li>
      </ul>

      <!-- project details -->
      <div class="tab-panels" role="tabpanel">
        <div class="tab-panel active share-plan" aria-hidden="false">

          <h2 class="left-indent"><%= _('Set plan visibility') %></h2>
          <p class="left-indent"><%= _('Public or organisational visibility is intended for finished plans. You must answer at least one question to enable these options.') %></p>

          <%= form_for @plan, html: {method: :put, class: "roadmap-form"} do |f| %>
            <fieldset class="standard">
              <%= f.hidden_field :id %>
              <div>
                <%= f.radio_button :visibility, :privately_visible, disabled: !@allow_visibility %>
                <%= f.label :visibility, _('Private: restricted to me and my collaborators'),
                            class: "checkbox-label regular-text#{(@allow_visibility ? '' : ' disabled')}" %>
              </div>
              <div>
                <%= f.radio_button :visibility, :organisationally_visible, disabled: !@allow_visibility %>
                <%= f.label :visibility, _('Organisation: anyone at my organisation can view'),
                            class: "checkbox-label regular-text#{(@allow_visibility ? '' : ' disabled')}" %>
              </div>
              <div>
                <%= f.radio_button :visibility, :publicly_visible, disabled: !@allow_visibility %>
                <%= f.label :visibility, _('Public: anyone can view'),
                            class: "checkbox-label regular-text#{(@allow_visibility ? '' : ' disabled')}" %>
              </div>
            </fieldset>
          <% end %>

          <h2 class="left-indent"><%= _('Manage collaborators')%></h2>

          <!--Text describing page content-->
          <p class="left-indent"><%= _('Invite specific people to read, edit, or administer your plan. Invitees will receive an email notification that they have access to this plan.') %></p>

          <% if @plan.roles.any? then %>
            <table class="dmp_table left-indent" id="collaborator-table">
              <thead>
                <tr>
                  <th class="col-large"><%= _('Email address')%></th>
                  <th class="col-medium"><%= _('Permissions')%></th>
                  <th class="col-small"><%= _('Action') %></th>
                </tr>
              </thead>
              <tbody>
                <% plan_roles = @plan.roles.where(active: true) %>
                <%  plan_roles.each do |role| %>
                  <tr class="table-data">
                    <td><%= role.user.name %></td>
                    <td>
                      <% if role.creator? %>
                        <span class="left-indent"><%= 'Owner' %></span>
                      <% elsif !role.creator? && role.user == current_user %>
                        <span class="left-indent"><%= display_role(role) %></span>
                      <% elsif !role.creator? && role.user != current_user %>
                        <%= form_for role, url: {controller: :roles, action: :update, id: role.id }, html: {method: :put} do |f| %>
                          <fieldset>
                            <%= f.hidden_field :id %>
                            <%= f.select :access_level, {"#{_('Co-owner')}": 3, "#{_('Editor')}": 2, "#{_('Read only')}": 1}, {}, {id: "#{role.id}-can-edit", class: "toggle-existing-user-access has-tooltip", 'data-toggle': "tooltip", 'title': _('Editors can contribute to plans. Co-owners have additional rights to edit plan details and control access.') } %>
                          </fieldset>
                        <% end %>
                      <% end %>
                    </td>
                    <td>
                      <% unless role.creator? || role.user == current_user then %>
                        <%= link_to _('Remove'), role, method: :delete, data: { confirm: _('Are you sure?') }, :class => "a-orange" %>
                      <% end %>
                    </td>
                  </tr>
                <% end %>
              </tbody>
            </table>
          <% end %>

          <h2 class="left-indent"><%= _('Invite collaborators') %></h2>
          <% new_role = Role.new %>
          <% new_role.plan = @plan %>
          <%= form_for new_role, url: {controller: :roles, action: :create },
                                 html: {method: :post, class: 'roadmap-form'} do |f| %>
            <fieldset class="standard">
              <%= f.hidden_field :plan_id %>

              <%= f.fields_for :user do |user| %>
                <%= user.label :email, _('Email') %>
                <%= user.email_field :email, for: :user, name: "user", class: "input-medium" %>
                <span role="" id="email_error" class="error-tooltip-right"></span>
              <% end %>

              <label><%= _('Permissions') %></label>
              <p></p>
              <div>
                <%= f.radio_button :access_level, 1 %>
                <%= f.label :access_level, _('Read only: can view but not make changes'), class: 'checkbox-label regular-text' %>
              </div>
              <div>
                <%= f.radio_button :access_level, 2 %>
                <%= f.label :access_level, _('Editor: can comment and make changes'), class: 'checkbox-label regular-text' %>
              </div>
              <div>
                <%= f.radio_button :access_level, 3 %>
                <%= f.label :access_level, _('Co-owner: can edit project details, change visibility, and add collaborators'), class: 'checkbox-label regular-text' %>
              </div>
              <div>
                <%= render partial: 'shared/accessible_submit_button',
                           locals: {id: 'add-collaborator-button',
                                    val: _('Add collaborator'),
                                    disabled_initially: true,
                                    classes: 'small-input-button',
                                    tooltip: _('Enter a valid email and permission level.')} %>
              </div>
            </fieldset>
          <% end %>

        </div>
      </div>
    </div>
  </div>
</div>