<%=javascript_include_tag "work_time", :plugin=>'redmine_work_time' %>
<%=stylesheet_link_tag 'work_time', :plugin => 'redmine_work_time' %>

<%= @message.html_safe %>
<div style="text-align:right;">
<%= render :partial=>'select_project' %> <br/>
[<%= link_to(l(:wt_each_member_report), @link_params.merge(:action=>"show")) %>]
[<%= link_to(l(:wt_edit_relay), @link_params.merge(:action=>"edit_relay")) %>]
[<%= link_to(l(:wt_raw_total), @link_params.merge(:action=>"total")) %>]
</div>

<h2>
  <%=l(:wt_relay_total)%>
  <%= link_to("<<", @link_params.merge(:day=>@last_month.day, :month=>@last_month.month, :year=>@last_month.year))%>
  <%= @first_date.strftime("%Y/%m/%d") %> - <%= @last_date.strftime("%Y/%m/%d") %>
  <%= link_to(">>", @link_params.merge(:day=>@next_month.day, :month=>@next_month.month, :year=>@next_month.year))%>
</h2>

<%
if !User.current.allowed_to?(:view_work_time_other_member, @project) then
  return
end
############################################## 集計表 #####################
%>
<table border="1">
<tr valign="top">
<td>
</td>
<td style="background:#ddd;"></td>
<%
max = @members.size
@members.each do |mem_info|
  pos = mem_info[0]
  user = mem_info[1]
%>
<td align="center">
<%if User.current.allowed_to?(:edit_work_time_total, @project) then%>
<a href="#"
  onclick="JavaScript:
    member_pos(
      '<%=url_for(@link_params)%>',
      <%=user.id%>,
      <%=pos%>,
      <%=max%>);
    return false;">
  <%=pos%>
</a>
<%end%>
<div style="top:2px;width:56px;font-size:10px;">
<%= link_to(user, @link_params.merge(:action=>"show", :user=>user.id))%>
</div>
</td>
<%
end
%>
</tr>

<tr>
<td style="background:#ddd;">
</td>
<td style="background:#ddd;text-align:right;">
<%=sprintf('%1.2f', @total_cost)%>
</td>
<%
#---------------------------------------各人の合計を表示
@members.each do |mem_info|
  user=mem_info[1]
%>
<td style="background:#ddd;text-align:right;">
<%= (@member_cost.key?(user.id) && @member_cost[user.id] !=0 ) ? sprintf('%1.2f', @member_cost[user.id]) : " "%>
</td>
<%
end
%>
</tr>

<%
prjs = WtProjectOrders.where("uid=-1").order("dsp_pos").all

tickets = WtTicketRelay.order("position").all
tic_max = tickets.size

#-------------------------------------- チケット行のループ
prjs.each do |po|
 dsp_prj = po.dsp_prj
 dsp_pos = po.dsp_pos
 next unless @r_prj_cost.key?(dsp_prj) # 値の無いプロジェクトはパス
 next unless @r_prj_cost[dsp_prj].key?(-1) # 値の無いプロジェクトはパス
 next if @r_prj_cost[dsp_prj][-1] == 0 # 値の無いプロジェクトはスパ
 prj =Project.find(dsp_prj)
%>
<tr style="background:#000;color:#fff;">
 <td>
<%if User.current.allowed_to?(:edit_work_time_total, @project) then%>
    <a href="#"
      style="color:#8ff;"
      onclick="JavaScript:
        prj_pos(
          '<%=url_for(@link_params)%>',
          <%=dsp_prj%>,
          <%=dsp_pos%>,
          <%=prjs.size%>);
        return false;">
      <%=dsp_pos%>:
    </a>
<%end%>
   <%=prj.name%>
 </td>
 <td style="text-align:right;"> <%=sprintf('%1.2f', (@r_prj_cost[dsp_prj])[-1])%> </td>
<%
@members.each do |mem_info|
  user=mem_info[1]
  uid=user.id
%>
  <td style="text-align:right;">
    <%= @r_prj_cost[dsp_prj].key?(uid) ? sprintf('%1.2f', (@r_prj_cost[dsp_prj])[uid]) : " "%>
  </td>
<%
end
%>
</tr>
<%
tickets.each do |tic|
  issue_id = tic.issue_id
  next unless @r_issue_cost.key?(issue_id) # 値の無いチケットはパス
  next unless @r_issue_cost[issue_id].key?(-1) # 値の無いチケットはパス
  next if @r_issue_cost[issue_id][-1] == 0 # 値の無いチケットはパス

  issue = Issue.find_by_id(issue_id)
  next if issue.nil? # チケットが削除されていたらパス
  next if issue.project_id != dsp_prj # このプロジェクトに表示するチケットでない場合はパス

%>
<tr>
<td>
<%if User.current.allowed_to?(:edit_work_time_total, @project) then%>
    <a href="#"
      onclick="JavaScript:
        ticket_pos(
          '<%=url_for(@link_params)%>',
          <%=issue_id%>,
          <%=tic.position%>,
          <%=tic_max%>);
        return false;">
      <%=tic.position%>
    </a>
<%end%>
  <%= link_to( wk_pretty_issue_name(issue), {:controller=>"issues", :action=>"show", :id=>issue.id}, :popup=>true) %>
  <%= print_issue_cost(issue) %>
</td>

<td style="background:#ddd;text-align:right;">
<%=sprintf('%1.2f', (@r_issue_cost[issue_id])[-1])%>
</td>

<%
@members.each do |mem_info|
  user=mem_info[1]
%>
<td style="text-align:right;">
<% if @r_issue_cost.key?(issue_id) && @r_issue_cost[issue_id].key?(user.id) then %>
<%= sprintf('%1.2f', (@r_issue_cost[issue_id])[user.id]) %>
<%else%>
&nbsp;
<%end%>
</td>
<%
end
%>

</tr>
<%
 end
end
if @r_prj_cost.has_key?(-1) then
%>
<tr style="background:#000;color:#fff;">
 <td>private</td>
 <td style="text-align:right;"> <%=sprintf('%1.2f', (@r_prj_cost[-1])[-1])%> </td>
<%
@members.each do |mem_info|
  user=mem_info[1]
  uid=user.id
%>
  <td style="text-align:right;">
    <%= @r_prj_cost[-1].key?(uid) ? sprintf('%1.2f', (@r_prj_cost[-1])[uid]) : " "%>
  </td>
<%end%>
</tr>
<%end%>
</table>
<input type="button"
       onclick="location.href='<%=url_for(@link_params.merge(:action=>"relay_total_data"))%>'"
       value="<%=l(:wt_data_download)%>"
/>
<input type="button"
       onclick="location.href='<%=url_for(@link_params.merge(:action=>"relay_total_data_with_act"))%>'"
       value="<%=l(:wt_data_download_with_act)%>"
/>

<div style="text-align:right;">
<%= render :partial=>'select_project' %> <br/>
[<%= link_to(l(:wt_each_member_report), @link_params.merge(:action=>"show")) %>]
[<%= link_to(l(:wt_edit_relay), @link_params.merge(:action=>"edit_relay")) %>]
[<%= link_to(l(:wt_raw_total), @link_params.merge(:action=>"total")) %>]
</div>
