<% content_for :script do %>
  <script>
    $j(document).ready(function () {
      $j(".issue-filter-star").click(function () {
        var filterId = $j(this).attr('filter-id');
        var star = $j(this);
        $j.ajax({
          type: 'POST',
          url: baseUrl + "/issues/toggle_fav",
          data: {id: filterId},
          success: function (data) {
            if (data == 'true') {
              star.removeClass('icon-not-favorite').addClass('icon-favorite');
              star.attr('title', '<%= escape_javascript message('click_to_remove_from_favorites') -%>');
            } else {
              star.removeClass('icon-favorite').addClass('icon-not-favorite');
              star.attr('title', '<%= escape_javascript message('click_to_add_to_favorites') -%>');
            }
            $j('#sidebar-favourites').load(baseUrl + '/issues/favourites');
          }
        });
      });
    });

    function onSaveAs(data) {
      window.location = baseUrl + '/issues/search#id=' + data;
    }

    function onCopy(data) {
      window.location = baseUrl + '/issues/search#id=' + data;
    }

    function onEdit(data) {
      window.location = baseUrl + '/issues/search#id=' + data;
    }
  </script>
<% end %>
<div>
  <div class="page">
    <h1><%= message 'issue_filter.manage.my_filters' -%></h1>
    <table class="data marginbottom10" id="my-issue-filters">
      <thead>
      <tr>
        <th class="thin"></th>
        <th><%= message('issue_filter.form.name') -%></th>
        <th><%= message('issue_filter.sharing') -%></th>
        <th class="right"><%= message('operations') -%></th>
      </tr>
      </thead>
      <tbody>
      <% if @filters.empty? %>
        <tr class="even">
          <td colspan="4"><%= message('issue_filter.no_filters') -%></td>
        </tr>
      <% else %>
        <% @filters.each do |filter| %>
          <tr id="my-issue-filter-<%= filter.name.parameterize -%>" class="<%= cycle('even', 'odd', :name => 'my-filters') -%>">
            <td>
              <%= issue_filter_star(filter, @favourite_filter_ids.include?(filter.id)) -%>
            </td>
            <td>
              <a href="<%= ApplicationController.root_context -%>/issues/search#id=<%= h filter.id -%>"><%= h filter.name -%></a>
              <% if filter.description %>
                <div class="note"><%= h filter.description -%></div>
              <% end %>
            </td>
            <td>
              <% if filter.shared %>
                <%= message 'issue_filter.shared_with_all_users' -%>
              <% else %>
                <%= message 'issue_filter.private' -%>
              <% end %>
            </td>
            <td class="thin nowrap right">
              <a id="copy-<%= filter.name.parameterize -%>" href="<%= ApplicationController.root_context -%>/issues/copy_form/<%= filter.id -%>"
                 class="link-action open-modal"><%= message('copy') -%></a>
              &nbsp;
              <a id="edit_<%= filter.name.parameterize -%>" href="<%= ApplicationController.root_context -%>/issues/edit_form/<%= filter.id -%>"
                 class="link-action open-modal"><%= message('edit') -%></a>
              &nbsp;
              <%= link_to_action message('delete'), "#{ApplicationController.root_context}/issues/delete/#{filter.id}",
                                 :class => 'link-action link-red',
                                 :id => "delete_#{filter.name.parameterize}",
                                 :confirm_button => message('delete'),
                                 :confirm_title => 'issue_filter.delete_confirm_title',
                                 :confirm_msg => 'issue_filter.are_you_sure_want_delete_filter_x',
                                 :confirm_msg_params => [filter.name] -%>
            </td>
          </tr>
        <% end %>
      <% end %>
      </tbody>
    </table>

    <br/>

    <h1><%= message 'issue_filter.manage.shared_filters' -%></h1>
    <table class="data" id="shared-filters">
      <thead>
      <tr>
        <th class="thin"></th>
        <th><%= message('issue_filter.form.name') -%></th>
        <th><%= message('shared_by') -%></th>
        <th class="right"><%= message('operations') -%></th>
      </tr>
      </thead>
      <tbody>
      <% if @shared_filters.empty? %>
        <tr class="even">
          <td colspan="4"><%= message('issue_filter.no_filters') -%></td>
        </tr>
      <% else %>
        <% @shared_filters.each do |filter| %>
          <tr id="shared-<%= filter.name.parameterize -%>" class="<%= cycle('even', 'odd', :name => 'shared-filters') -%>">
            <td>
              <%= issue_filter_star(filter, @favourite_filter_ids.include?(filter.id)) -%>
            </td>
            <td>
              <a href="<%= ApplicationController.root_context -%>/issues/search#id=<%= h filter.id -%>|<%= h filter.data -%>"><%= h filter.name -%></a>
              <% if filter.description %>
                <div class="note"><%= h filter.description -%></div>
              <% end %>
            </td>
            <td>
              <%= h Api.users.findByLogin(filter.user).name -%>
            </td>
            <td class="thin nowrap right">
              <a id="copy-<%= filter.name.parameterize -%>" href="<%= ApplicationController.root_context -%>/issues/copy_form/<%= filter.id -%>" class="link-action open-modal"><%= message('copy') -%></a>
              <% if has_role?(:admin) %>
                &nbsp;
                <a id="edit_shared_<%= filter.name.parameterize -%>" href="<%= ApplicationController.root_context -%>/issues/edit_form/<%= filter.id -%>" class="link-action open-modal"><%= message('edit') -%></a>
                &nbsp;
                <%= link_to_action message('delete'), "#{ApplicationController.root_context}/issues/delete/#{filter.id}",
                                   :class => 'link-action link-red',
                                   :id => "delete_system_#{filter.name.parameterize}",
                                   :confirm_button => message('delete'),
                                   :confirm_title => 'issue_filter.delete_confirm_title',
                                   :confirm_msg => 'issue_filter.are_you_sure_want_delete_filter_x',
                                   :confirm_msg_params => [filter.name] -%>
              <% end %>
            </td>
          </tr>
        <% end %>
      <% end %>
      </tbody>
    </table>

  </div>
</div>
