<div id="agendaTab">
  <ul id="agendaTab-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="box wide tabs">
    <div class="tab" id="teams">
      <table class="data">
        <tr>
          <th>Name</th>
          <th>Comment</th>
          <th>Rank</th>
          <th>Actions</th>
        </tr>

        <% @user.teamers.active.each do |teamer| %>
          <tr>
            <td <% if teamer.team == @user.team %>class="bold"<% end %>>
              <%= 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 do |u| %>
                  <%= link_to("Set as Primary", user_path(@user, :user => { :team_id => teamer.team.id }), :method => :put, :confirm => "Are you sure?") %>
                  <%= link_to 'Leave', teamer, :confirm => 'Are you sure?', :method => :delete %>
                <% end %>
              <% else %>
                <%= link_to 'Leave', teamer, :confirm => 'Are you sure?', :method => :delete %>
              <% end %>
            </td>
          </tr>
        <% end %>
      </table>

      <% if @user.teamers.joining.count == 0  %>
        <%= form_for @teamer do |f| %>
          <%= f.error_messages %>
          <%= f.hidden_field :user_id %>
          <p>
          <h3>
            Join an existing team:
          </h3>

          <%= f.select :team_id, Team.active.collect {|t| [t.name, t.id]} %>
          <%= f.submit "Join" %>
          </p>
        <% end %>
      <% else %>
        <p>
        You can join only one team at time.
        Currently you are trying to join <%= namelink @user.teamers.joining.first.team %>.
        You can cancel this request by clicking <%= link_to "here", @user.teamers.joining.first, :method => :delete %>.
        </p>
      <% end %>

      <p>
      <%= link_to 'Create a new team', new_team_path %>
      </p>
    </div>

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

        <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>
        <br />
      <% end %>

      <% if @user.challenges_sent.future.count > 0 %>
        <h3 class="center">
          Sent challenges
        </h3>

        <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 %>
        <h3 class="center">
          Matches without referee
        </h3>

        <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 %>
            <h3 class="center">
              Upcoming matches for your concern
            </h3>

            <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>
            <br />
          <% end %>

          <% if @user.past_matches.length > 0 %>
            <h3 class="center">
              Past matches waiting for scoring or lineup
            </h3>

            <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>
            <br />
          <% end %>
        </div>

        <div class="tab" id="articles">
          <%= render :partial => "articles/list", :locals => {:articles => @user.articles} %>
          <%= link_to 'New article', new_article_path %>
        </div>

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

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

        <div class="tab" id="issues">
          <h3>
            Issues created by you
          </h3>
          <%= render :partial => "issues/list", :locals => {:issues => @user.issues} %>

          <h3>
            Open issues assigned to you
          </h3>
          <%= render :partial => "issues/list", :locals => {:issues => @user.open_issues} %>
          <%= link_to 'New issue', new_issue_path %>
        </div>

        <div class="tab" id="bans">
          <%= render :partial => "bans/list", :locals => {:bans => @user.bans.effective} %>
        </div>
      </div>
    </div>

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