<table class="table-address-add">
  <tr>
    <td style="text-align: left;">
      <div class="pagination">
        <%= paginate @user_logs %>
      </div>
    </td>
  </tr>
</table>
<table class="table-address">
  <tr>
    <th><%= t('activerecord.attributes.send_matters.created_at') %></th>
    <th><%= t('views.user_log.receiver_name') %></th>
    <th><%= t('views.user_log.receiver_mail_address') %></th>
  </tr>

<% tmp_record_h_name = Hash.new %>
<% tmp_record_h_mail_address = Hash.new %>
<% tmp_record_h = Hash.new{ |h,k| h[k]=Hash.new(&h.default_proc) } %>
<% unless @user_logs.blank? %>
  <% tmp_id  = "" %>
  <% @user_logs.each do |record| %>
    <% unless record.id == tmp_id %>
    <tr id="send_matter_item_<%= record.id %>" data-id="<%= record.id %>">
      <td class="address_name">
        <%= record.created_at.strftime("%Y-%m-%d %H:%M:%S") %>
        <%= link_to '',
            { controller: :user_log, action: :send_matter_info, id: record.id },
            id: "show_send_matter_#{record.id}", 
            class: 'show_send_matter', 
            style: 'visibility:hidden;',
            data: {turbo: false} %>
      </td>
      <td class="address_name">
        <%- count = 0 -%>
        <%- for tmp_receiver in get_receivers(record.id, 'name') -%>
          <%- if count > 0 -%>
           <br/>
          <%- end -%>
          <%= tmp_receiver %>
          <%- count += 1 -%>
        <%- end -%>
      </td>
      <td class="address_email">
        <%- count = 0 -%>
        <%- for tmp_receiver in get_receivers(record.id, 'mail_address') -%>
          <%- if count > 0 -%>
           <br/>
          <%- end -%>
          <%= tmp_receiver %>
          <%- count += 1 -%>
        <%- end -%>
      </td>
    </tr>
    <% end %>
    <% tmp_id = record.id %>
  <% end %>
<% end %>
</table>
<table class="table-address-add">
  <tr>
    <td style="text-align: left;">
      <div class="pagination">
        <%= paginate @user_logs %>
      </div>
    </td>
  </tr>
</table>

<script type="text/javascript">
// ページネーション差し替え後も動作するようdocumentレベルでイベント委譲
// 重複登録防止
if (!window._userLogIndexResultClickBound) {
  window._userLogIndexResultClickBound = true;
  document.addEventListener('click', function(e) {
    var resultEl = document.getElementById('result');
    if (!resultEl || !resultEl.contains(e.target)) return;
    var target = e.target.closest('.address_name, .address_email, .address_notes');
    if (target) {
      var id = target.parentElement.dataset.id;
      var link = document.querySelector("a#show_send_matter_" + id);
      if (link) link.click();
    }
  });
}
</script>
