<%=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_raw_total), @link_params.merge(:action=>"total")) %>]
[<%= link_to(l(:wt_relay_total), @link_params.merge(:action=>"relay_total")) %>]
</div>
<%
###################################################### チケット関係テーブルの表示
%>
<br/>
<h2>
  <%=l(:wt_edit_relay)%>
  <%= 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>

<table border="1" id="relay_table">
  <%
     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 if (!@prj_cost.has_key?(dsp_prj) || @prj_cost[dsp_prj][-1]==0.0) && (!@r_prj_cost.has_key?(dsp_prj) || @r_prj_cost[dsp_prj][-1]==0.0)
       prj =Project.find_by_id(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;">
          <%if @prj_cost.has_key?(dsp_prj) && @prj_cost[dsp_prj][-1]!=0%>
              <%=sprintf('%1.2f', @prj_cost[dsp_prj][-1])%>
          <%else%>
              &nbsp;
          <%end%>
        </td>
        <td style="text-align:right;"><b>
          <%if @r_prj_cost.has_key?(dsp_prj) && @r_prj_cost[dsp_prj][-1]!=0%>
              <%=sprintf('%1.2f', @r_prj_cost[dsp_prj][-1]) %>
          <%else%>
              &nbsp;
          <%end%>
        </b></td>
        <td>&nbsp;</td>
      </tr>
      <%
         tic_num = 0
         tickets.each do |tic|
           issue_id = tic.issue_id
           tic_num += 1
           if tic.position != tic_num then # 番号が間違っていたらつけなおし
             tic.position = tic_num; tic.save
           end
           # 当該チケットに集計が無かったらスキップ
           next unless @issue_cost.has_key?(issue_id) || @r_issue_cost.has_key?(issue_id)
           issue = Issue.find_by_id(issue_id)
           parent_id = ""
           next if issue.nil? # チケットが削除されていたらパス
           next if issue.project_id != dsp_prj

           if tic.parent && tic.parent != 0 then
             parent = Issue.find_by_id(tic.parent)
             if parent.nil? then
               parentHtml = "<del>Issue:"+tic.parent.to_s+"</del>"
             else
               parent_id = parent.id
               parentHtml = parent.closed? ? "<del>"+parent.to_s+"</del>" : parent.to_s
             end
           else
             parentHtml = ""
           end

           if @issue_cost.key?(issue_id) &&
                   @issue_cost[issue_id].key?(-1) &&
                   @issue_cost[issue_id][-1]!=0 then
             cost=sprintf("%.2f",@issue_cost[issue_id][-1])
           else
             cost="&nbsp"
           end

           if @r_issue_cost.key?(issue_id) &&
                   @r_issue_cost[issue_id].key?(-1) &&
                   @r_issue_cost[issue_id][-1]!=0 then
             r_cost=sprintf("%.2f",@r_issue_cost[issue_id][-1])
           else
             r_cost="&nbsp"
           end

      %>
          <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="text-align:right;"><%=cost.html_safe%></td>
            <td style="text-align:right;"><%=r_cost.html_safe%></td>
            <td id="ticket_relay_<%=issue_id%>"></td>
            <script>
                jQuery.ajax({
                    url: '<%=url_for(:action=>"ajax_relay", :issue_id=>issue_id.to_s)%>',
                    data: {asynchronous: true, method: 'get'},
                    success: function (response) {
                        jQuery('#ticket_relay_<%=issue_id.to_s%>').html(response);
                    }
                });
            </script>
          </tr>
      <%
         end
         end
      %>
  <% if @prj_cost.has_key?(-1) || @r_prj_cost.has_key?(-1) then%>
      <tr style="background:#000;color:#fff;">
        <td>private</td>
        <td style="text-align:right;">
          <%if @prj_cost.has_key?(-1) then%>
              <%=sprintf('%1.2f', @prj_cost[-1][-1])%>
          <%else%>
              &nbsp;
          <%end%>
        </td>
        <td style="text-align:right;">
          <%if @r_prj_cost.has_key?(-1) then%>
              <b><%=sprintf('%1.2f', @r_prj_cost[-1][-1]) if @r_prj_cost[-1][-1]%></b>
          <%else%>
              &nbsp;
          <%end%>
        </td>
        <td>&nbsp;</td>
      </tr>
  <% end %>

</table>
<%= submit_tag(l('wt_bulkupdate_relations'),
              {:type => 'button',
               :onclick => "set_ticket_relay_by_issue_relation('#{url_for(:action=>'ajax_relay', :parent_id=>-1)}')".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_raw_total), @link_params.merge(:action=>"total")) %>]
[<%= link_to(l(:wt_relay_total), @link_params.merge(:action=>"relay_total")) %>]
</div>
