<% define_admin_breadcrumbs do %>
  <% add_admin_breadcrumb('用户排行榜', admins_path) %>
<% end %>


<div class="box search-form-container user-list-form">
  <%= form_tag(admins_users_rank_index_path, method: :get, class: 'form-inline search-form flex-1', id: 'user-rank-date-form') do %>
    <div class="form-group mr-2">
      <label for="status">日期：</label>
      <% dates_array = (0..30).to_a.map { |item| [(Date.today-item.days).to_s, (Date.today-item.days).to_s] } %>
      <%= select_tag(:date, options_for_select(dates_array, params[:date]), class:"form-control",id: "user-rank-date-select")%>
    </div>
  <% end %>
  
</div>

<div class="box admin-list-container project-language-list-container">
  <table class="table table-hover text-center subject-list-table">
    <thead class="thead-light">
    <tr>
      <th width="20%">排名</th>
      <th width="20%">用户</th>
      <th width="20%">得分</th>
      <th width="20%">影响力</th>
      <th width="20%">贡献度</th>
      <th width="20%">活跃度</th>
      <th width="20%">项目经验</th>
      <th width="20%">语言能力</th>
    </tr>
    </thead>
    <tbody>
      <% @date_rank.each_with_index do |item, index| %>
        <tr class="">
          <td><%= index + 1%></td>
          <% owner_common = $redis_cache.hgetall("v2-owner-common:#{item[0]}")%>
          <td>
            <a target="_blank" href="/<%= owner_common["login"] %>">
              <%= owner_common["name"] %>
            </a>
          </td>
          <% user_date_statistic_key = "v2-user-statistic:#{item[0]}-#{@rank_date}"%>
          <% follow_count = $redis_cache.hget(user_date_statistic_key, "follow-count") || 0 %>
          <% pullrequest_count = $redis_cache.hget(user_date_statistic_key, "pullrequest-count") || 0 %>
          <% issues_count = $redis_cache.hget(user_date_statistic_key, "issue-count") || 0 %>
          <% project_count = $redis_cache.hget(user_date_statistic_key, "project-count") || 0 %>
          <% fork_count = $redis_cache.hget(user_date_statistic_key, "fork-count") || 0 %>
          <% project_watchers_count = $redis_cache.hget(user_date_statistic_key, "project-watcher-count") || 0 %>
          <% project_praises_count = $redis_cache.hget(user_date_statistic_key, "project-praise-count") || 0 %>
          <% project_language = $redis_cache.hget(user_date_statistic_key, "project-language") %>
          <% project_languages_count = project_language.nil? || project_language == "{}" ? 0 : JSON.parse(project_language).length %>

          <% influence = (60.0 + follow_count.to_i / (follow_count.to_i + 20.0) * 40.0).to_i  %>
          <% contribution = (60.0 + pullrequest_count.to_i / (pullrequest_count.to_i + 20.0) * 40.0).to_i %>
          <% activity = (60.0 + issues_count.to_i / (issues_count.to_i + 80.0) * 40.0).to_i %>
          <% experience = 10 * project_count.to_i + 5 * fork_count.to_i + project_watchers_count.to_i + project_praises_count.to_i  %>
          <% experience = (60.0 + experience / (experience + 100.0) * 40.0).to_i %>
          <% language = (60.0 + project_languages_count.to_i / (project_languages_count.to_i + 5.0) * 40.0).to_i %>
          <% score = influence+ contribution + activity + experience + language%>
          <td><%= score %></td>
          <td><%= influence%></td>
          <td><%= contribution%></td>
          <td><%= activity%></td>
          <td><%= experience%></td>
          <td><%= language%></td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>
<script>
  $("#user-rank-date-select").on('change', function() {
    $("#user-rank-date-form").submit()
  });
</script>