<% javascript 'taxonomy_edit' %>

<%= form_for taxonomy do |f| %>
  <div class="form-group row">
    <ul class="nav nav-pills nav-stacked col-md-3" data-tabs="pills">
      <li class="active"><a href="#primary" data-toggle="tab"><%= _("Primary") %></a></li>
      <% if User.current.allowed_to?(:view_users) %>
        <li><a href="#users" data-toggle="tab"><%= _("Users") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_smart_proxies) %>
        <li><a href="#smart_proxies" data-toggle="tab"><%= _("Smart Proxies") %></a></li>
      <% end %>
      <% if SETTINGS[:unattended] %>
        <% if User.current.allowed_to?(:view_subnets) %>
          <li><a href="#subnets" data-toggle="tab"><%= _("Subnets") %></a></li>
        <% end %>
        <% if User.current.allowed_to?(:view_compute_resources) %>
          <li><a href="#compute_resources" data-toggle="tab"><%= _("Compute Resources") %></a></li>
        <% end %>
        <% if User.current.allowed_to?(:view_media) %>
          <li><a href="#media" data-toggle="tab"><%= _("Media") %></a></li>
        <% end %>
        <% if User.current.allowed_to?(:view_provisioning_templates) %>
          <li><a href="#provisioning_templates" data-toggle="tab"><%= _("Provisioning Templates") %></a></li>
        <% end %>
        <% if User.current.allowed_to?(:view_ptales) %>
          <li><a href="#ptables" data-toggle="tab"><%= _("Partition Tables") %></a></li>
        <% end %>
        <% if User.current.allowed_to?(:view_domains) %>
          <li><a href="#domains" data-toggle="tab"><%= _("Domains") %></a></li>
        <% end %>
        <% if User.current.allowed_to?(:view_realms) %>
          <li><a href="#realms" data-toggle="tab"><%= _("Realms") %></a></li>
        <% end %>
      <% end %>
      <% if User.current.allowed_to?(:view_environments) %>
        <li><a href="#environments" data-toggle="tab"><%= _("Environments") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_hostgroups) %>
        <li><a href="#hostgroups" data-toggle="tab"><%= _("Host Groups") %></a></li>
      <% end %>
      <% if taxonomy.is_a?(Organization) && show_location_tab? %>
        <li><a href="#locations" data-toggle="tab"><%= _('Locations') %></a></li>
      <% end %>
      <% if taxonomy.is_a?(Location) && show_organization_tab? %>
        <li><a href="#organizations" data-toggle="tab"><%= _("Organizations") %></a></li>
      <% end %>
      <% if User.current.allowed_to?(:view_params) %>
        <li><a href="#params" data-toggle="tab"><%= _("Parameters") %></a></li>
      <% end %>
    </ul>

    <div class="tab-content stacked-content col-md-9">
      <div class="tab-pane active" id="primary">
        <%= base_errors_for taxonomy %>
        <%= select_f(f, :parent_id, taxonomy.class.where("id NOT IN (#{taxonomy.subtree_ids.join(',')})").order(:title), :id, :title, { :include_blank => true },
                     { :label => _('Parent'), :onchange => 'parent_taxonomy_changed(this);',
                       :help_inline => :indicator,
                       :'data-url' => (controller_name == 'organizations' ? parent_taxonomy_selected_organization_path(taxonomy.id) :
                               parent_taxonomy_selected_location_path(taxonomy.id))
                     }) if show_parent?(taxonomy) %>
        <%= text_f f, :name %>
        <%= textarea_f f, :description, :rows => 5 %>
      </div>

      <%= show_resource_if_allowed(f, taxonomy, {:resource => :users, :association => User.except_admin}) %>
      <%= show_resource_if_allowed(f, taxonomy, :smart_proxies) %>

      <% if SETTINGS[:unattended] %>
        <%= show_resource_if_allowed(f, taxonomy, :subnets) %>
        <%= show_resource_if_allowed(f, taxonomy, :compute_resources) %>
        <%= show_resource_if_allowed(f, taxonomy, :media) %>
        <%= show_resource_if_allowed(f, taxonomy, :provisioning_templates) %>
        <%= show_resource_if_allowed(f, taxonomy, :ptables) %>
        <%= show_resource_if_allowed(f, taxonomy, :domains) %>
        <%= show_resource_if_allowed(f, taxonomy, :realms) %>
      <% end %>

      <%= show_resource_if_allowed(f, taxonomy, :environments) %>
      <%= show_resource_if_allowed(f, taxonomy, :hostgroups) %>

      <% if taxonomy.is_a?(Location) && show_organization_tab? %>
        <div class="tab-pane" id="organizations">
          <%= organization_selects f, taxonomy.selected_or_inherited_ids[:organization_ids],
                                   { :disabled => taxonomy.used_and_selected_or_inherited_ids[:organization_ids], :label => _('Select organizations')},
                                   { 'data-mismatches' => taxonomy.need_to_be_selected_ids[:organization_ids].to_json,
                                     'data-inheriteds' => taxonomy.inherited_ids[:organization_ids].to_json,
                                     'data-useds' => taxonomy.used_ids[:organization_ids].to_json } %>
        </div>
      <% elsif taxonomy.is_a?(Organization) && show_location_tab? %>
        <div class="tab-pane" id="locations">
          <%= location_selects f, taxonomy.selected_or_inherited_ids[:location_ids],
                               { :disabled => taxonomy.used_and_selected_or_inherited_ids[:location_ids], :label => _('Select locations')},
                               { 'data-mismatches' => taxonomy.need_to_be_selected_ids[:location_ids].to_json,
                                 'data-inheriteds' => taxonomy.inherited_ids[:location_ids].to_json,
                                 'data-useds' => taxonomy.used_ids[:location_ids].to_json } %>
        </div>
      <% end %>

      <% if User.current.allowed_to?(:view_params) %>
        <div class="tab-pane" id="params">
          <%= render "common_parameters/parameters", { :f => f, :type => "#{controller_name.singularize}_parameters".to_sym } %>
        </div>
      <% end %>
    </div>
    <%= submit_or_cancel f %>
  </div>
<% end %>
