<% provide :title, t('.title') %>
<div style="margin-bottom: 1em;">
  <%= form_tag({:action => "index"}, :method => :get) do %>
    <%= text_field_tag "query", params[:query] %>
    <%= submit_tag t('.search') %>
    <%= submit_tag t('tag_implication.index.search') %>
  <% end %>
</div>

<div id="aliases">
  <%= form_tag(:action => "update") do %>
    <table width="100%" class="highlightable">
      <thead>
        <tr>
          <th width="1%"></th>
          <th width="19%"><%= t('.alias') %></th>
          <th width="20%"><%= t('.to') %></th>
          <th width="60%"><%= t('.reason') %></th>
        </tr>
      </thead>
      <tfoot>
        <tr>
          <td colspan="4">
            <% if @current_user.is_mod_or_higher? %>
              <%= button_tag t(".pending"), :type => "button", :class => "js-check_all", :data => { :target => "tag-aliases" } %>
              <%= submit_tag t('.approve') %>
            <% end %>
            <%= button_to_function t('.delete'), "$('reason-box').show(); $('reason').focus()" %>
            <%= button_to_function t('.add'), "$('add-box').show().scrollTo(); $('tag_alias_name').focus()" %>

            <div id="reason-box" style="display: none; margin-top: 1em;">
              <strong><%= t('.reason') %>:</strong>
              <%= text_field_tag "reason", "", :size => 40 %>
              <%= submit_tag t('.delete') %>
            </div>
          </td>
        </tr>
      </tfoot>
      <tbody>
        <% @aliases.each do |a| %>
          <tr class="<%= cycle 'even', 'odd' %> <%= a.is_pending? ? 'pending-tag' : nil %>">
            <td>
              <input
                type="checkbox"
                name="aliases[<%= a.id %>]"
                value="1"
                data-target="tag-aliases"
                class="<%= "js-check_all-target pending" if a.is_pending? %>"
              >
            </td>
            <td><%= link_to h(a.name), :controller => "post", :action => "index", :tags => a.name %> (<%= Tag.find_by_name(a.name).post_count rescue 0 %>)</td>
            <td><%= link_to h(a.alias_name), :controller => "post", :action => "index", :tags => a.alias_name %> (<%= Tag.find(a.alias_id).post_count rescue 0 %>)</td>
            <td><%= h a.reason %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
  <% end %>
</div>

<div id="add-box" style="display: none;">
  <%= form_tag(:action => "create") do %>
    <h4><%= t('.add_new.title') %></h4>
    <p><%= t('.add_new.info') %></p>

    <table>
      <tr>
        <th><label for="tag_alias_name"><%= t('.add_new.name') %></label></th>
        <td><%= text_field "tag_alias", "name", :size => 40 %></td>
      </tr>
      <tr>
        <th><label for="tag_alias_alias"><%= t('.add_new.alias_to') %></label></th>
        <td><%= text_field "tag_alias", "alias", :size => 40 %></td>
      </tr>
      <tr>
        <th><label for="tag_alias_reason"><%= t('.reason') %></label></th>
        <td><%= text_area "tag_alias", "reason", :size => "40x2" %></td>
      </tr>
      <tr>
        <td colspan="2"><%= submit_tag t('.add_new.submit') %></td>
      </tr>
    </table>
  <% end %>
</div>

<div id="paginator">
  <%= will_paginate(@aliases) %>
</div>

<%= render :partial => "/tag/footer" %>
