<div id="agenda" class="tabbed">
  <ul id="agenda-nav" class="tabs">
    <li><a href="#teams">Teams</a></li>
    <li><a href="#matches">Matches</a></li>
    <li><a href="#articles">Articles</a></li>
    <li><a href="#movies">Movies</a></li>
    <li><a href="#servers">Servers</a></li>
    <li><a href="#issues">Issues</a></li>
    <li><a href="#bans">Bans</a></li>
  </ul>

  <div class="tabbed-contents">
    <div class="tab" id="teams">
      <h4>Your Teams</h4>

      <table class="teams">
        <tr>
          <th>Name</th>
          <th>Comment</th>
          <th>Rank</th>
          <th>Actions</th>
        </tr>

        <% @user.teamers.active.each do |teamer| %>
          <tr>
            <td class="<%= 'current' if teamer.team == @user.team %>">
              <%= link_to (h teamer.team.name), teamer.team %>
            </td>
            <td><%=h teamer.comment %></td>
            <td><%=h teamer.ranks[teamer.rank] %></td>
            <td>
              <% if @user.team != teamer.team %>
                <%= form_for @user, html: { class: 'inline' } do |u| %>
                  <%= link_to "Set as Primary", user_path(@user, user: { team_id: teamer.team.id }), method: :put, confirm: "Are you sure?", class: 'button tiny' %>
                <% end %>
              <% end %>

              <%= link_to 'Leave', teamer, confirm: 'Are you sure?', method: :delete, class: 'button tiny' %>
            </td>
          </tr>
        <% end %>
      </table>

      <% if @user.teamers.joining.count == 0  %>
        <%= form_for @teamer do |f| %>
          <%= f.error_messages %>
          <%= f.hidden_field :user_id %>

          <h4>Join an existing team</h4>

          <div class="fields inline">
            <%= f.select :team_id, Team.active.collect {|t| [t.name, t.id]} %>
          </div>
          <div class="controls inline">
             <%= f.submit "Join", class: 'button' %>
          </div>
        <% end %>
      <% else %>
        <div class="flash warning">
          <p>You can only request to join one team at a time.</p>
          <p>Currently you are trying to join <%= namelink @user.teamers.joining.first.team %></p>
          <p>You can cancel this request by clicking "Cancel Request"</p>
        </div>

        <%= link_to "Cancel Request", @user.teamers.joining.first, method: :delete, class: 'button' %>
      <% end %>

      <%= link_to 'Create a new team', new_team_path, class: 'button' %>
    </div>

    <div class="tab" id="matches">
      <% if @user.challenges_received.future.pending.count > 0 %>
        <h4>Pending challenges for your response</h4>

        <table class="data">
          <tr>
            <td>Actions</td>
            <th>Opponent</th>
            <th>Match time</th>
            <th>Default time</th>
          </tr>

          <% @user.challenges_received.future.pending.each do |challenge| %>
            <tr>
              <td>
                <%= link_to ("Open"), challenge %>
                <%= link_to "Send a PM", controller: "messages", action: "new", id: "Team", id2: challenge.contester1.team %>
              </td>
              <td><%= namelink challenge.contester1 %></td>
              <td><%= longtime challenge.match_time %></td>
              <td>
                <% challenge.mandatory ? longtime(challenge.default_time) : "--" %>
              </td>
            </tr>
          <% end %>
        </table>
      <% end %>

      <% if @user.challenges_sent.future.count > 0 %>
        <h4>Sent challenges</h4>

        <table class="data">
          <tr>
            <th>Actions</th>
            <th>Opponent</th>
            <th>Match time</th>
            <th>Default time</th>
            <th>Status</th>
          </tr>

          <% @user.challenges_sent.future.each do |challenge| %>
            <tr>
              <td><%= link_to ("Open"), challenge %></td>
              <td><%= namelink challenge.contester2 %></td>
              <td><%= shorttime challenge.match_time %></td>
              <td>
                <% if challenge.mandatory %>
                  <%= shorttime challenge.default_time %>
                <% else %>
                  --
                <% end %>
              </td>
              <td><%= challenge.statuses[challenge.status] %></td>
            </tr>
          <% end %>
        </table>
      <% end %>

      <% if @user.ref? and Match.future.unreffed.ordered.count > 0 %>
        <h4>Matches without referee</h4>

        <table class="data">
          <tr>
            <th>Teams</th>
            <th>Date</th>
            <th>Actions</th>
          </tr>

          <% Match.future.unreffed.ordered.each do |match| %>
            <tr>
              <td><%= namelink match %></td>
              <td><%= shorttime match.match_time %></td>
              <td>
                <%= link_to 'Referee this match',
                  remote: true,
                  url: { 
                    controller: "matches", action: "update", id: match.id,
                    match: { 
                      referee_id: @user.id 
                    }
                  }, 
                  method: "put",
                  loading: "$('matchlink_#{match.id}').parentNode.innerHTML='Processing..';",
                  success: "$('matchlink_#{match.id}').parentNode.innerHTML='Complete';",
                  html: { id: "matchlink_#{match.id}" } %>
              </td>
            </tr>
          <% end %>
        </table>
      <% end %>

      <% if @user.upcoming_matches.length > 0  # TODO: fix this %>
        <h4>Upcoming matches for your concern</h4>

        <table class="data">
          <tr>
            <th>Teams</th>
            <th>Date</th>
          </tr>

          <% @user.upcoming_matches.each do |match| %>
            <tr>
              <td><%= namelink match %></td>
              <td><%= shorttime match.match_time %>
              </td>
            </tr>
          <% end %>
        </table>
      <% end %>

      <% if @user.past_matches.length > 0 %>
        <h4>Past matches waiting for scoring or lineup</h4>

        <table class="data">
          <tr>
            <th>Teams</th>
            <th>Date</th>
          </tr>

          <% @user.past_matches.each do |match| %>
            <tr>
              <td><%= namelink match %></td>
              <td><%= shorttime match.match_time %>
              </td>
            </tr>
          <% end %>
        </table>
      <% end %>
    </div>

    <div class="tab" id="articles">
      <% if @user.articles.size > 0 %>
        <%= render partial: "articles/list", locals: { articles: @user.articles } %>
      <% end %>

      <%= link_to 'New article', new_article_path, class: 'button' %>
    </div>

    <div class="tab" id="movies">
      <%= render partial: "movies/movie", collection: @user.movies %>

      <%= link_to "New Movie", { controller: "data_files", action: "new", id: Directory::MOVIES }, { class: 'button' } %>
    </div>

    <div class="tab" id="servers">
      <%= render partial: "servers/server", collection: @user.servers %>

      <%= link_to 'New Server', new_server_path, class: 'button' %>
    </div>

    <div class="tab" id="issues">
      <% if @user.issues.size > 0 %>
        <h4>Issues created by you</h4>
        <%= render partial: "issues/list", locals: { issues: @user.issues } %>
      <% end %>
      
      <% if @user.open_issues.size > 0 %>
        <h4>Open issues assigned to you</h4>
        <%= render partial: "issues/list", locals: { issues: @user.open_issues } %>
      <% end %>

      <%= link_to 'New issue', new_issue_path, class: 'button' %>
    </div>

    <div class="tab" id="bans">
      <% if @user.bans.effective.size > 0 %>
        <%= render partial: "bans/list", locals: { bans: @user.bans.effective } %>
      <% else %>
        <p>You have no effective bans currently.</p>
      <% end %>
    </div>
  </div>
</div>

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