<h4 class="center">
  <div><span class="yellow"><%= @query %></span> results from your notebook</div>
</h4>

<div class="row">
  <% @matched_attributes.each do |attribute| %>
    <%
      seen_key = "#{attribute.entity_type}-#{attribute.entity_id}"
      next if @seen_result_pages.key?(seen_key)
      @seen_result_pages[seen_key] = true
    %>

    <% entity = content_class_from_name(attribute.entity_type).find_by(id: attribute.entity_id) %>
    <% next unless entity %>
    <% next unless entity.user_id == current_user.id %>

    <div class="col s12 m6">
      <div class="card hoverable">
        <div class="card-content black-text blue-grey lighten-5" style="height: 180px; overflow: hidden">
          <div class="card-title">
            <%= link_to entity, class: "#{entity.class.text_color}" do %>
              <i class="material-icons" style="position: relative; top: 3px;"><%= entity.class.icon %></i>
              <%= entity.name %>
            <% end %>
          </div>
          <p class="black-text darken-2">
            <sup>Last edited <%= time_ago_in_words entity.updated_at %> ago</sup>
          </p>
          <p class="black-text">
            <strong class="left"><%= attribute.attribute_field.label %></strong>:
            <%=
                ContentFormatterService.show(
                  text: truncate(attribute.value, length: 180, separator: ' '),
                  viewing_user: current_user
                ).gsub(
                  /#{@query}/i,
                  "<span class='yellow'>#{@query}</span>".html_safe
                ).gsub("<p>", ' ').gsub("</p>", ' ').html_safe
               %>
          </p>
        </div>
        <div class="card-action <%= entity.class.color %> lighten-1">
          <%= link_to polymorphic_path(entity.page_type.downcase, id: entity.id), class: 'black-text' do %>
            <i class="material-icons tiny" style="position: relative; top: 2px;"><%= entity.class.icon %></i> View
          <% end %>
          <%= link_to edit_polymorphic_path(entity.page_type.downcase, id: entity.id), class: 'black-text' do %>
            <i class="material-icons tiny" style="position: relative; top: 2px;">edit</i> Edit
          <% end %>
          <% entity_universe = entity.try(:universe) %>
          <% if entity_universe %>
            <%= link_to entity_universe, class: "black-text right", style: 'padding: 0 10px' do %>
              <i class="material-icons tiny" style="position: relative; top: 2px"><%= Universe.icon %></i>
              <span><%= entity_universe.name %></span>
            <% end %>
          <% end %>
        </div>
      </div>
    </div>
  <% end %>
</div>

<% if @matched_attributes.empty? %>
  <div class="card-panel">
    <p>
      Sorry, nothing matched your search query!
    </p>

    <p>
      Think this is a bug?
      <%= link_to 'Please report it!', 'https://docs.google.com/forms/d/e/1FAIpQLSe0jnqJlcPJDqwogGere5j8-8F1nSGGYkzbsI-XkOeMnGwLrA/viewform' %>
    </p>
  </div>
<% else %>
  <h4 class="center">
    <small class="grey-text">
      <%= pluralize @seen_result_pages.keys.count, 'match' %> found
    </small>
  </h4>
<% end %>
