<% content_for :head do %>
  <meta name="turbolinks-cache-control" content="no-cache">
<% end %>

<% provide(:head_title, t("libraries.show.head_title", library: @repository.name)) %>
<%= stylesheet_link_tag 'datatables' %>
<% provide(:container_class, "no-second-nav-container") %>

<%= render partial: "sidebar", locals: { repositories: @repositories } %>
<div id="alert-container"></div>

<div class="content-pane">
  <div id="repository-toolbar">

    <% if can_create_repository_rows?(@repository.team) %>
      <button type="button" class="btn btn-primary editAdd" id="addRepositoryRecord" onclick="onClickAddRecord()">
        <span class="fas fa-plus"></span>
        <span class="hidden-xs"><%= t("repositories.add_new_record") %></span>
      </button>
    <% end %>

    <div class="repository-cog dropdown">
        <button id="repository-acitons-dropdown"
                class="btn btn-default"
                type="button"
                data-toggle="dropdown"
                aria-haspopup="true"
                aria-expanded="true"
                <%= "disabled" unless can_perform_repository_actions(@repository) %>>
          <span class="fas fa-ellipsis-v"></span>
          <span class="hidden-xs"><%= t('repositories.index.advanced') %></span>
          <span class="caret"></span>
        </button>

        <% if can_perform_repository_actions(@repository) %>
          <ul class="dropdown-menu pull-right">
            <li class="dropdown-header">
              <%= t("repositories.index.options_dropdown.header") %>
            </li>
            <% if can_create_repository_columns?(@repository.team) %>
              <li>
                <%= link_to t('repositories.index.options_dropdown.manage_columns'),
                            repository_repository_columns_path(@repository) %>
              </li>
            <% end %>
            <% if can_create_repository_rows?(@repository.team) %>
            <li>
              <a href="#" id="importRecordsButton" data-turbolinks="false">
                <%= t('repositories.index.options_dropdown.import_items') %>
              </a>
            <li>
            <% end %>
            <% if can_read_team?(@repository.team) %>
            <li>
              <a href="#"  id="exportRepositoriesButton" data-turbolinks="false">
                <%= t("repositories.index.options_dropdown.export_items") %>
              </a>
            </li>
            <% end %>
            <% if can_manage_repository?(@repository) %>
              <li>
                <%= link_to t('repositories.index.options_dropdown.rename'),
                            team_repository_rename_modal_path(current_team, repository_id: @repository),
                            class: "rename-repo-option",
                            remote: true %>
              </li>
            <% end %>
            <% if can_create_repositories?(@repository.team) %>
              <li>
                <%= link_to t('repositories.index.options_dropdown.copy'),
                            team_repository_copy_modal_path(current_team, repository_id: @repository),
                            class: "copy-repo-option",
                            remote: true %>
              </li>
            <% end %>
            <% if can_manage_repository?(@repository) %>
              <li data-hook="destroy-repository-option" role="separator" class="divider"></li>
              <li data-hook="destroy-repository-option">
                <%= link_to t('repositories.index.options_dropdown.delete'),
                            team_repository_destroy_modal_path(current_team, repository_id: @repository),
                            class: "delete-repo-option",
                            remote: true %>
              </li>
            <% end %>
          </ul>
        <% end %>
    </div>


    <div id="datatables-buttons" style="display: inline;">
      <div class="new-repository-button">
        <% if can_create_repositories?(@repository.team) %>
          <a class="btn btn-default"
             id="create-new-repository"
             data-remote="true"
             href="<%= create_modal_team_repositories_path(@repository.team) %>">
            <span class="fas fa-plus"></span>
            <span class="hidden-xs"><%= t('libraries.index.no_libraries.create_new_button') %></span>
          </a>
        <% end %>
      </div>
      <%= render partial: "columns_reorder_dropdown", formats: :html %>
    </div>
  </div>

  <div class="btn-group inline" id="saveCancel" data-toggle="buttons" style="display:none">
    <button type="button" class="btn btn-success" id="saveRecord" onclick="onClickSave()">
      <span class="fas fa-save"></span>
      <%= t("repositories.save_record") %>
    </button>
    <button type="button" class="btn btn-default" id="cancelSave" onclick="onClickCancel()">
      <span class="fas fa-times visible-xs-inline"></span>
      <span class="hidden-xs"><%= t("repositories.cancel_save") %></span>
    </button>
  </div>

  <!-- These buttons are appended to table in javascript, after table initialization -->
  <div class="toolbarButtons" style="display:none">
    <% if can_manage_repository_rows?(@repository.team) %>
      <button type="button" class="btn btn-default editAdd" id="editRepositoryRecord" onclick="onClickEdit()" disabled>
        <span class="fas fa-pencil-alt"></span>
        <span class="hidden-xs-custom"><%= t("repositories.edit_record") %></span>
      </button>
      <button type="button" class="btn btn-default"
        id="deleteRepositoryRecordsButton" onclick="onClickDelete()" disabled>
        <span class="fas fa-trash-alt"></span>
        <span class="hidden-xs-custom"><%= t'repositories.delete_record' %></span>
          <%= submit_tag I18n.t('repositories.delete_record'), :class => "hidden
          delete_repository_records_submit" %>
      </button>
      <button type="button" class="btn btn-default copyRow" id="copyRepositoryRecords" onclick="onClickCopyRepositoryRecords()" disabled>
        <span class="fas fa-copy"></span>
        <span class="hidden-xs-custom"><%= t("repositories.copy_record") %></span>
      </button>
    <% end %>
  </div>

  <%= render partial: "repositories/repository_table.html.erb",
             locals: {
               repository: @repository,
               repository_index_link: repository_table_index_path(@repository)
             }
  %>
</div>

<%= render partial: 'repositories/import_repository_records_modal.html.erb',
                    locals: { repository: @repository } %>

<%= render partial: "repositories/delete_record_modal.html.erb" %>
<%= render partial: 'repositories/export_repository_modal.html.erb',
           locals: { repository: @repository } %>

<%= javascript_include_tag 'repositories/edit' %>
<%= javascript_include_tag 'repositories/repository_datatable' %>
<%= javascript_include_tag "repositories/show" %>
<%= javascript_include_tag "repositories/index" %>
