<h1 class="title">Editing Contest</h1>

<div id="contest" class="contest-manage tabbed">
  <ul id="contest-nav" class="tabs">
    <li><a href="#general">General</a></li>
    <% if @contest.contest_type == Contest::TYPE_BRACKET %>
      <li><a href="#brackets">Brackets</a></li>
    <% end %>
    <li><a href="#maps">Maps</a></li>
    <li><a href="#teams">Teams</a></li>
    <li><a href="#weeks">Weeks</a></li>
    <li><a href="#matches">Matches</a></li>
  </ul>

  <div class="tabbed-contents">
    <div class="tab" id="general">
      <%= form_for @contest, html: { class: 'square' } do |f| %>
        <%= render 'shared/errors', messages: @contest.errors.full_messages %>

        <div class="fields horizontal">
          <%= f.label :name %>
          <%= f.text_field :name %>
        </div>
        <div class="fields horizontal">
          <%= f.label :short_name %>
          <%= f.text_field :short_name %>
        </div>
        <div class="fields horizontal">
          <%= f.label :start %>
          <%= f.datetime_select :start, datetime_separator: '', time_separator: '' %>
        </div>
        <div class="fields horizontal">
          <%= f.label :end %>
          <%= f.datetime_select :end, datetime_separator: '', time_separator: '' %>
        </div>
        <div class="fields horizontal">
          <%= f.label :status %>
          <%= f.select :status, @contest.statuses.invert %>
        </div>
        <div class="fields horizontal">
          <%= f.label :demos_id %>
          <%= f.select :demos_id, Directory.all.collect{|d| [d, d.id]} %>
        </div>
        <div class="fields horizontal">
          <%= f.label :default_time %>
          <%= f.time_select :default_time, time_separator: '' %>
        </div>
        <div class="fields horizontal">
          <%= f.label :winner_id %>
          <%= f.select :winner_id, @contest.contesters.collect { |t| [t.team, t.id] }, include_blank: true %>
        </div>
        <div class="fields horizontal">
          <%= f.label :rules_id %>
          <%= f.select :rules_id, Category.find(Category::RULES).articles.collect { |a| [a, a.id] }, include_blank: true %>
        </div>

        <% if @contest.contest_type == Contest::TYPE_LADDER %>
          <div class="fields horizontal">
            <%= f.label :weight %>
            <%= f.text_field :weight %>
          </div>
          <div class="fields horizontal">
            <%= f.label :modulus_base %>
            <%= f.text_field :modulus_base %>
          </div>
          <div class="fields horizontal">
            <%= f.label :modulus_even %>
            <%= f.text_field :modulus_even %>
          </div>
          <div class="fields horizontal">
            <%= f.label :modulus_3to1 %>
            <%= f.text_field :modulus_3to1 %>
          </div>
          <div class="fields horizontal">
            <%= f.label :modulus_4to0 %>
            <%= f.text_field :modulus_4to0 %>
          </div>
        <% end %>

        <div class="controls">
          <%= f.submit 'Save Contest' %>
        </div>
      <% end %>
    </div>

    <% if @contest.contest_type == Contest::TYPE_BRACKET %>
      <div class="tab" id="brackets">
        <table class="brackets-list striped">
          <tr>
            <th class="bracket">Bracket</th>
            <th class="slots">Slots</th>
            <th class="actions"></th>
          </tr>

          <% @contest.brackets.each do |bracket| %>
            <tr>
              <td><%= namelink bracket %></td>
              <td><%= bracket.slots %></td>
              <td class="actions">
                <%= link_to icon('pencil'), edit_bracket_path(bracket) %>
                <%= link_to icon('times'), bracket, confirm: 'Are you sure?', method: :delete %>
              </td>
            </tr>
          <% end %>
        </table>

        <%= form_for @contest.brackets.build, html: { class: 'square' } do |f| %>
          <%= render 'shared/errors', messages: @contest.brackets.last.errors.full_messages %>

          <%= f.hidden_field :contest_id %>

          <div class="fields horizontal">
            <%= f.label :slots %>
            <%= f.text_field :slots %>
          </div>
          <div class="controls">
            <%= f.submit 'Add Bracket' %>
          </div>
        <% end %>
      </div>
    <% end %>

    <div class="tab" id="maps">
      <table class="maps striped">
        <tr>
          <th class="name">Name</th>
          <th class="download">Download</th>
          <th class="actions"></th>
        </tr>

        <% @contest.maps.basic.each do |map| %>
          <tr>
            <td><%= namelink map %></td>
            <td><%= h map.download %></td>
            <td class="actions">
              <%= link_to icon('pencil'), edit_map_path(map) %>
              <%= link_to icon('times'), action: "del_map", id: @contest.id, id2: map.id  %>
            </td>
          </tr>
        <% end %>
      </table>

      <%= form_for @contest do |f| %>
        <%= render 'shared/errors', messages: @contest.errors.full_messages %>

        <div class="fields">
          <%= select_tag :map, options_from_collection_for_select(Map.basic, :id, :name) %>
        </div>
        <div class="controls">
          <%= f.submit 'Add map' %>
        </div>
      <% end %>
    </div>

    <div class="tab" id="teams">
      <table class="teams striped">
        <tr>
          <th class="team">Team</th>
          <th class="score">Score</th>
          <th class="status">Status</th>
          <th class="actions"></th>
        </tr>

        <% @contest.contesters.each do |contester| %>
          <tr>
            <td><%= link_to (h contester.team), contester %></td>
            <td><%= h contester.score %></td>
            <td><%= contester.statuses[contester.active] %></td>
            <td class="actions">
              <% if contester.active %>
                <%= link_to icon('pencil'), edit_contester_path(contester) %>
                <%= link_to icon('times'), contester, confirm: 'Are you sure?', method: :delete %>
              <% else %>
                <%= form_for contester do |c| %>
                  <%= c.hidden_field :active, { value: 1 } %>
                  <%= link_to icon('pencil'), edit_contester_path(contester) %>
                  <%= link_to_function icon('rotate-left'), "$('edit_contester_#{contester.id}').submit()" %>
                <% end %>
              <% end %>
            </td>
          </tr>
        <% end %>
      </table>

      <%= form_for @contest.contesters.build do |f| %>
        <%= render 'shared/errors', messages: @contest.contesters.last.errors.full_messages %>

        <%= f.hidden_field :contest_id %>

        <div class="fields">
          <%= f.select :team_id, Team.active.ordered.collect{|t| [t, t.id]} %>
        </div>
        <div class="controls">
          <%= f.submit 'Add Team' %>  
        </div>
      <% end %>
    </div>

    <div class="tab" id="weeks">
      <table class="weeks striped">
        <tr>
          <th class="name">Name</th>
          <th class="date">Start date</th>
          <th class="map1">Map1</th>
          <th class="map2">Map2</th>
          <th class="actions"></th>
        </tr>

        <% @contest.weeks.each do |week| %>
          <tr>
            <td><%= h week.name %></td>
            <td><%= week.start_date.strftime("%d %B %y") %></td>
            <td><%= h week.map1 %></td>
            <td><%= h week.map2 %></td>
            <td class="actions">
              <%= link_to icon('pencil'), edit_week_path(week) %>
              <%= link_to icon('times'), week, confirm: 'Are you sure?', method: :delete %>
            </td>
          </tr>
        <% end %>
      </table>

      <%= link_to "New Week", { controller: :weeks, action: :new, id: @contest }, { class: 'button' } %>
    </div>

    <div class="tab" id="matches">
      <table class="matches striped">
        <tr>
          <th class="team">Teams</th>
          <th class="date">Date</th>
          <th class="score">Score</th>
          <th class="actions"></th>
        </tr>

        <% @contest.matches.each do |match| %>
          <tr>
            <td>
              <%= namelink match.contester1.team %>
              vs
              <%= namelink match.contester2.team %>
            </td>
            <td>
              <%= longtime match.match_time %>
            </td>
            <td>
              <% link_to match do %>
                <b><%= h match.score1 %> - <%= h match.score2 %></b>
              <% end %>
            </td>
            <td class="actions">
              <%= link_to icon('flag-checkered'), controller: :matches, action: :ref, id: match %>
              <%= link_to icon('pencil'), edit_match_path(match) %>
              <%= link_to icon('times'), match, confirm: 'Are you sure?', method: :delete %>
            </td>
          </tr>
        <% end %>
      </table>

      <%= link_to "New Match", { controller: :matches, action: :new, id: @contest }, { class: 'button' } %>
    </div>
  </div>
</div>

<script type="text/javascript">
  new Yetii({
    id: 'contest'
  });
</script>
