<%= form_with(url: {action: :get_csv_of_request_log}) do %>
  <%= select_tag 'month_for_out', options_for_select(@select_month) %> のログを
  <%= submit_tag "　CSV保存　" %>
<% end %>

<br>

<%= form_with(url: {action: :request_log}, method: :get) do %>
  <%= select_tag :type, options_for_select([["依頼人", 0], ["依頼人メールアドレス", 1],
                       ["送信者名", 2], ["送信者メールアドレス", 3]], @type.to_i ) %>
  <%= text_field_tag :keyward, @keyward %>
  <%= submit_tag "　検索　" %>
<% end %>
<br>

データ数：<%= @total_data %>　
[ 
<%= render '/layouts/original_paginate', controller: :sys_log, action: :request_log, page: @page, total_page: @total_page, w_in: 4, w_out: 2 %> ]

<table border=1 cellspacing=0 cellpadding=3>
  <tr bordercolor=gray>
  <td nowrap bgcolor=#e5f0fe><br></td>
  <td nowrap bgcolor=#e5f0fe>登録日</td>
  <td nowrap bgcolor=#e5f0fe>依頼ＩＤ</td>
  <td nowrap bgcolor=#e5f0fe>依頼人</td>
  <td nowrap bgcolor=#e5f0fe>依頼人メールアドレス</td>
  <td nowrap bgcolor=#e5f0fe>送信者名</td>
  <td nowrap bgcolor=#e5f0fe>送信者メールアドレス</td>
  </tr>
  <% count = 0 -%>
  <% cr_id = 0 -%>
<% created_at = "" -%>
<% request_matter_id = "" -%>
<% request_matter_name = "" -%>
<% request_matter_mail = "" -%>
<% requested_matter_id = "" -%>
<% requested_matter_name = "" -%>
<% requested_matter_mail = "" -%>
<% requested_matter_name_lists = Array.new -%>
<% requested_matter_mail_lists = Array.new -%>
<% bf_created_at = "" -%>
<% bf_request_matter_id = "" -%>
<% bf_request_matter_id_b = "" -%>
<% bf_request_matter_name = "" -%>
<% bf_request_matter_mail = "" -%>
<% bf_requested_matter_name = "" -%>
<% bf_requested_matter_mail = "" -%>

<%# @requested_matters.each.with_index do | requested_matter, count | -%>
<% @rs.each do | data | -%>
  <% bf_created_at = (Time.parse(data.created_at.to_s)).strftime("%Y/%m/%d %H:%M:%S") -%>
  <% bf_request_matter_id = data.request_matter_id -%>
  <% bf_request_matter_name = data.request_name -%>
  <% bf_request_matter_mail = data.request_mail_address -%>
  <% requested_matter_id = data.requested_matter_id -%>
  <% bf_requested_matter_name = "<a href=\"/sys_log/requested_matter_info/" + requested_matter_id.to_s + "\">#{data.requested_name}</a>" -%>
  <% bf_requested_matter_mail = data.requested_mail_address -%>

  <% if bf_request_matter_id_b != "" -%>
    <% if bf_request_matter_id == bf_request_matter_id_b -%>
      <% requested_matter_name = requested_matter_name + "<br>" + bf_requested_matter_name -%>
      <% requested_matter_mail = requested_matter_mail + "<br>" + bf_requested_matter_mail -%>
    <% else -%>
      <% cr_id = cr_id + 1 -%>
      <% if @s_data <= cr_id and cr_id <= @e_data -%>
        <tr bordercolor=gray>
          <td nowrap align=center><%= cr_id %></td>
          <td nowrap><%= created_at %></td>
          <td nowrap><%= request_matter_id %></td>
          <td nowrap><%= request_matter_name %></td>
          <td nowrap><%= request_matter_mail %></td>
          <td nowrap>
          <% count = 0 %>
          <% for requested_matter_name_list in requested_matter_name_lists %>
            <% if count > 0 %>
            <br>
            <% end %>
            <%= link_to requested_matter_name_list[0], {controller: :sys_log, action: :requested_matter_info, id: requested_matter_name_list[1]} %>
            <% count += 1 %>
          <% end %>
          </td>
          <td nowrap>
          <% count = 0 %>
          <% for requested_matter_mail_list in requested_matter_mail_lists %>
            <% if count > 0 %>
            <br>
            <% end %>
            <%= requested_matter_mail_list %>
            <% count += 1 %>
          <% end %>
          </td>
        </tr>
      <% end -%>
      <% requested_matter_name_lists = Array.new -%>
      <% requested_matter_mail_lists = Array.new -%>

      <% bf_request_matter_id_b = bf_request_matter_id -%>

      <% created_at = bf_created_at -%>
      <% request_matter_id = bf_request_matter_id -%>
      <% request_matter_name = bf_request_matter_name -%>
      <% request_matter_mail = bf_request_matter_mail -%>
      <% requested_matter_name = bf_requested_matter_name -%>
      <% requested_matter_mail = bf_requested_matter_mail -%>
    <% end -%>
  <% else -%>
    <% bf_request_matter_id_b = bf_request_matter_id -%>

    <% created_at = bf_created_at -%>
    <% request_matter_id = bf_request_matter_id -%>
    <% request_matter_name = bf_request_matter_name -%>
    <% request_matter_mail = bf_request_matter_mail -%>
    <% requested_matter_name = bf_requested_matter_name -%>
    <% requested_matter_mail = bf_requested_matter_mail -%>
  <% end -%>
  <% requested_matter_name_lists << [data.requested_name, requested_matter_id] -%>
  <% requested_matter_mail_lists << data.requested_mail_address -%>
<% end -%>

  <% if cr_id == 0 and @total_data == 0 -%>
    <tr><td colspan=8 algin=center>データがありません</td></tr>
  <% else -%>
    <% cr_id = cr_id + 1 %>
    <% if @s_data <= cr_id and cr_id <= @e_data -%>
      <tr bordercolor=gray>
      <td nowrap align=center><%= cr_id %></td>
      <td nowrap><%= created_at %></td>
      <td nowrap><%= request_matter_id %></td>
      <td nowrap><%= request_matter_name %></td>
      <td nowrap><%= request_matter_mail %></td>
      <td nowrap><%#= raw requested_matter_name %>
        <% count = 0 %>
        <% for requested_matter_name_list in requested_matter_name_lists %>
          <% if count > 0 %>
          <br>
          <% end %>
          <%= link_to requested_matter_name_list[0], {controller: :sys_log, action: :requested_matter_info, id: requested_matter_name_list[1]} %>
          <% count += 1 %>
        <% end %>
      </td>
      <td nowrap><%#= raw requested_matter_mail %>
        <% count = 0 %>
        <% for requested_matter_mail_list in requested_matter_mail_lists %>
          <% if count > 0 %>
          <br>
          <% end %>
          <%= requested_matter_mail_list %>
          <% count += 1 %>
        <% end %>
      </td>
      </tr>
    <% end -%>
  <% end -%>
</table>

