<style>
  .pagination a {
    color: #FFCC66;
    text-decoration: none;
  }
  .pagination{
    padding-top:10px;
    padding-buttom:10px;
    width:600px;
    text-align: center;
  }

  #duels{width: 600px;text-align: center}
  #duels thead {line-height:25px; background-color:#1c1b1b; height:25px; color:#d6edff; font-weight:bold; }
  #duels tbody {line-height:25px; padding-top:3px; font-weight:bold;}
  #duels .index {color:white}
  #duels .win {color:#58db3e}
  #duels .lose {color:red}
  #duels .current {color:red}
</style>
<table id="duels">
  <thead>
    <tr>
      <% if @user %>
        <th><%= Duel.human_attribute_name :id %></th>
        <th><%= Duel.human_attribute_name :opponent %></th>
        <th><%= Duel.human_attribute_name :result %></th>
        <th><%= Duel.human_attribute_name :points %></th>
        <th><%= Duel.human_attribute_name :detail %></th>
      <% else %>
        <th><%= Duel.human_attribute_name :id %></th>
        <th><%= Duel.human_attribute_name :winner %></th>
        <th><%= Duel.human_attribute_name :points %></th>
        <th><%= Duel.human_attribute_name :loser %></th>
        <th><%= Duel.human_attribute_name :points %></th>
        <th><%= Duel.human_attribute_name :detail %></th>
      <% end %>
    </tr>
  </thead>
  <tfoot>
    <tr><td colspan="7"><%= will_paginate @duels %></td></tr>
  </tfoot>
  <tbody>
    <% first_index = @user.duels.where("id<=?",@duels.to_enum.first.id).count if @user and !@user.duels.empty? %><!--这里的效率很低，待优化-->
    <% @duels.each_with_index do |duel, index| %>
      <tr>
        <% if @user %>
          <td class="index"><%= link_to (first_index - index), duel %></td>
          <td class="user opponent"><% opponent = duel.user1 == @user ? duel.user2 : duel.user1 %><%= link_to opponent.name, opponent %></td>
          <td class="result <%= duel.winner == @user ? "win" : "lose" %>"><%= duel.winner == @user ? "WIN" : "LOSE" %></td>
          <td class="credits <%= duel.winner == @user ? "win" : "lose" %>"><%= duel.user1 == @user ? duel.user1_credits : duel.user2_credits %></td>
          <td class="show"><%= link_to Duel.human_attribute_name(:show), duel %></td>
        <% else %>
          <% if duel.winner == duel.user1
            winner = duel.user1
            loser = duel.user2
            winner_points = duel.user1_credits
            loser_points = duel.user2_credits
          else
            winner = duel.user2
            loser = duel.user1
            winner_points = duel.user2_credits
            loser_points = duel.user1_credits
          end %>
          <td class="index"><%= link_to duel.id, duel %></td>
          <td class="user win"><%= link_to winner.name, winner %></td>
          <td class="credits win"><%= winner_points %></td>
          <td class="user lose"><%= link_to loser.name, loser %></td>
          <td class="credits lose"><%= loser_points %></td>
          <td class="show"><%= link_to Duel.human_attribute_name(:show), duel %></td>
        <% end %>
      </tr>
    <% end %>
  </tbody>
</table>


