<h1>All <%= @tournament.name %> matches</h1>

<% matches_path = "/tournaments/#{@tournament.id}/matches" %>

<%= form_tag(matches_path, method: :get, id: "search-form") do %>
  <%= text_field_tag :search, params[:search], placeholder: "Search wrestler, school, or bout #" %>
  <%= submit_tag "Search" %>
<% end %>

<p>Search by wrestler name, school name, or bout number.</p>

<br>
<table class="table table-striped table-bordered table-condensed" id="matchesList">
  <thead>
    <tr>
      <th>Bout number</th>
      <th>Bracket Position</th>
      <th>Matchup</th>
      <th>Finished?</th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% @matches.each do |match| %>
      <tr>
        <td><%= match.bout_number %></td>
        <td><%= match.bracket_position %></td>
        <td><%= match.w1_bracket_name.html_safe %> vs <%= match.w2_bracket_name.html_safe %></td>
        <td><%= match.finished %></td>
        <td><%= link_to 'Show', match, :class=>"btn btn-default btn-sm" %>
            <%= link_to 'Edit Wrestlers', edit_match_path(match), :class=>"btn btn-primary btn-sm" %>
            <%= link_to 'Edit Mat/Queue', edit_assignment_match_path(match), :class=>"btn btn-primary btn-sm" %>
            <%= link_to 'Stat Match', "/matches/#{match.id}/stat", :class=>"btn btn-primary btn-sm" %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

<% if @total_pages.present? && @total_pages > 1 %>
  <nav aria-label="Matches pagination">
    <ul class="pagination">
      <% if @page > 1 %>
        <li class="page-item">
          <%= link_to "Previous", { controller: "tournaments", action: "matches", id: @tournament.id, page: @page - 1, search: params[:search] }, class: "page-link" %>
        </li>
      <% else %>
        <li class="page-item disabled"><span class="page-link">Previous</span></li>
      <% end %>

      <% window = 5
         left = [1, @page - window / 2].max
         right = [@total_pages, left + window - 1].min
         left = [1, right - window + 1].max
      %>
      <% (left..right).each do |p| %>
        <% if p == @page %>
          <li class="page-item active"><span class="page-link"><%= p %></span></li>
        <% else %>
          <li class="page-item"><%= link_to p, { controller: "tournaments", action: "matches", id: @tournament.id, page: p, search: params[:search] }, class: "page-link" %></li>
        <% end %>
      <% end %>

      <% if @page < @total_pages %>
        <li class="page-item">
          <%= link_to "Next", { controller: "tournaments", action: "matches", id: @tournament.id, page: @page + 1, search: params[:search] }, class: "page-link" %>
        </li>
      <% else %>
        <li class="page-item disabled"><span class="page-link">Next</span></li>
      <% end %>
    </ul>
  </nav>

  <p class="text-muted">
    <% start_index = ((@page - 1) * @per_page) + 1
       end_index = [@page * @per_page, @total_count].min
    %>
    Showing <%= start_index %> - <%= end_index %> of <%= @total_count %> matches
  </p>
<% end %>

<br>
<p>Total matches without byes: <%= @matches_without_byes_count %></p>
<p>Unfinished matches: <%= @unfinished_matches_without_byes_count %></p>
