<div id="forums">
  <%= form_for(@forum, html: { class: 'square' }) do |f| %>
    <%= render 'shared/errors', messages: @forum.errors.full_messages %>

    <div class="fields horizontal">
      <%= f.label :title %>
      <%= f.text_field :title %>
    </div>
    <div class="fields horizontal">
      <%= f.label :description %>
      <%= f.text_field :description %>
    </div>
    <div class="fields horizontal">
      <%= f.label :category %>
      <%= f.select :category_id, Category.domain(Category::DOMAIN_FORUMS).ordered.collect{|c| [c, c.id]} %>
    </div>
    <div class="controls">
      <%= f.submit 'Save' %>
    </div>
  <% end %>

  <% unless @forum.new_record? %>
    <h2>Access Rights</h2>
    <p>If no groups are specified, everyone is allowed for the specific access type.</p>
    
    <table id="acl" class="striped">
      <tr>
        <th class="group">Group</th>
        <th class="level">Access Level</th>
        <th class="options">Options</th>
      </tr>

      <% @forum.forumers.each do |forumer| %>
        <tr>
          <td><%= namelink forumer.group %></td>
          <td>
            <%= form_for forumer do |g| %>
              <div class="fields">
                <%= g.select :access, forumer.accesses.invert, size: 20 %>
              </div>
            <% end %>
          </td>
          <td>
            <%= content_tag :a, { class: 'button tiny', data: { forumer: forumer.id } } do %>
              Update
            <% end %>
            <%= link_to "Remove", forumer, method: :delete, class: 'button tiny' %>
          </td>
        </tr>
      <% end %>
    </table>

    <%= form_for @forum.forumers.build, html: { class: 'add-acl' } do |f| %>
      <%= render 'shared/errors', messages: @forum.errors.full_messages %>

      <%= f.hidden_field :forum_id %>
      
      <div class="fields permissions">
        <h4>Grant Access</h4>
        <%= f.select :group_id, Group.all.collect{|g| [g, g.id]} %>
        <%= f.select :access, f.object.accesses.invert, :size => 20 %>
      </div>
      <div class="controls">
        <%= f.submit "Add" %>  
      </div>
    <% end %>
  <% end %>

  <script>
    $('a[data-forumer]').click(function() {
      $('#edit_forumer_' + $(this).data('forumer')).submit();
    });
  </script>
</div>
