<% content_for :title, "Posts tagged #{@tag.name}" %>

<h1 class="has-margin-0 has-margin-top-4">
  Posts tagged <span class="<%= tag_classes(@tag, @category) %> is-large"><%= @tag.name %></span>
</h1>
<p class="has-color-tertiary-900 has-font-weight-normal has-margin-0 has-font-family-brand">
  <% if @tag.parent_id.present? %>
      Subtag of <%= link_to @tag.parent.name, tag_path(id: @category.id, tag_id: @tag.parent_id),
                            class: tag_classes(@tag.parent, @category) %>
  <% end %>
  <% child_count = @tag.children.count %>
  <% if @tag.parent_id.present? && child_count > 0 %>
    &middot;
  <% end %>
  <% if child_count > 0 %>
    <%= link_to pluralize(child_count, 'child tag'), tag_children_path(id: @category.id, tag_id: @tag.id) %>
  <% end %>
</p>

<div class="widget">
  <div class="widget--body has-font-size-caption has-color-tertiary-900">
    <%= raw(sanitize(@tag.excerpt, scrubber: scrubber)&.gsub("\n", '<br/>')) %>
    <% unless @tag.excerpt.present? %>
      <p class="has-font-size-caption has-margin-0">
        <em>
          This tag doesn't have any usage information yet.
          <% if current_user&.has_privilege?('EditTag') %>
            <%= link_to 'Help us create it', edit_tag_path(id: @category.id, tag_id: @tag.id) %>.
          <% end %>
        </em>
      </p>
    <% end %>
  </div>
  <div class="widget--body">
    <%= raw(sanitize(@tag.wiki, scrubber: scrubber)) %>
    <% unless @tag.wiki.present? %>
      <p class="has-font-size-caption has-margin-0">
        <em>
          This tag doesn't have a detailed wiki yet.
          <% if current_user&.has_privilege?('EditTag') %>
            <%= link_to 'Help us create it', edit_tag_path(id: @category.id, tag_id: @tag.id) %>.
          <% end %>
        </em>
      </p>
    <% end %>
  </div>
</div>

<% if current_user&.has_privilege?('EditTag') %>
  <%= link_to 'Edit', edit_tag_path(id: @category.id, tag_id: @tag.id), class: 'button is-muted is-outlined' %>
<% end %>

<% post_count = @posts.count %>
<div class="has-color-tertiary-500 category-meta" title="<%= post_count %>">
  <div>
    <%= short_number_to_human post_count, precision: 1, significant: false %>
    <%= 'post'.pluralize(post_count) %>
  </div>

  <div class="button-list is-gutterless has-margin-2">
    <%= link_to 'Tag Only', query_url(self: 1),
                class: "button is-muted is-outlined #{params[:self].present? ? 'is-active' : ''}" %>
    <%= link_to 'Tag + Children', tag_path(id: @category.id, tag_id: @tag.id),
                class: "button is-muted is-outlined #{params[:self].nil? ? 'is-active' : ''}" %>
  </div>
</div>

<div class="item-list">
  <% @posts.each do |post| %>
    <%= render 'posts/type_agnostic', post: post %>
  <% end %>
</div>

<div class="has-padding-top-4">
  <%= will_paginate @posts, renderer: BootstrapPagination::Rails %>
</div>

<div class="has-padding-top-4">
  <%= link_to tag_path(id: @category.id, tag_id: @tag.id, format: 'rss') do %>
    <i class="fas fa-rss"></i> Tag RSS feed
  <% end %>
</div>
