<%
  modul_uniq_id ||= ''
  block_name ||= f.object_name.to_s
  options ||= {}
  is_default_query = query.is_used_as_default_query?
  # TEMPORARY ADJUSTMNETS available outputs REMAKE !!!!!!!!!!!!!
  output_black_list = %w(scrum scrum_backlog agile_backlog)
%>
<%= error_messages_for query -%>
<%= hidden_field_tag :confirm, 1 -%>
<%= hidden_field_tag :set_filter, 1 -%>
<%= hidden_field_tag :back_url , params[:back_url] -%>
<%= hidden_field_tag :type , params[:type] -%>
<% query.settings.each_pair do |key, value| %>
  <% next if value.is_a?(Array) || key.to_s.start_with?('tagged') %>
  <%= hidden_field_tag "#{block_name}[settings][#{key}]", value -%>
<% end if query.settings.is_a?(Hash) %>
<% presenter = EasyExtensions::EasyQueryHelpers::EasyQueryPresenter.new(query, self, modul_uniq_id: modul_uniq_id, hide_columns_selection: local_assigns.fetch(:hide_columns_selection, true)) %>

<div class="easy-query-filter-settings">
  <div class="tabular">
    <%= hidden_field_tag 'gantt', '1' if params[:gantt] %>
    <p>
      <%= f.label(:name, :required => true, :class => 'required') %>
      <%= f.text_field(:name, :size => 80, :id => '_name', :class => 'block') -%>
    </p>

    <% if User.current.admin? || User.current.allowed_to?(:manage_public_queries, query.project, :global => true) %>
      <p><label><%=l(:field_visible)%></label>
        <label class="block"><%= f.radio_button :visibility, EasyQuery::VISIBILITY_PRIVATE %> <%= l(:label_visibility_private) %></label>
        <label class="block"><%= f.radio_button :visibility, EasyQuery::VISIBILITY_PUBLIC %> <%= l(:label_visibility_public) %></label>
        <label class="inline"><%= f.radio_button :visibility, EasyQuery::VISIBILITY_ROLES %> <%= l(:label_visibility_roles) %>:</label>
        <% query_role_ids = query.role_ids %>
        <% Role.givable.sorted.each do |role| %>
          <label class="inline role-visibility"><%= check_box_tag "#{block_name}[role_ids][]", role.id, query_role_ids.include?(role.id), :id => nil %> <%= role.name %></label>
        <% end %>
        <%= hidden_field_tag "#{block_name}[role_ids][]", '' %>
        <br/>
        <label class="inline"><%= f.radio_button :visibility, EasyQuery::VISIBILITY_EASY_USER_TYPES %> <%= l(:label_visibility_user_types) %>:</label>
        <% EasyUserType.sorted.each do |easy_user_type| %>
          <label class="inline role-visibility"><%= check_box_tag "#{block_name}[easy_user_type_ids][]", easy_user_type.id, query.easy_user_types.include?(easy_user_type), :id => nil %> <%= easy_user_type.name %></label>
        <% end %>
        <%= hidden_field_tag "#{block_name}[easy_user_type_ids][]", '' %>
      </p>
    <% end %>

    <p>
      <%= check_box_tag 'query_is_for_all', 1, query.project.nil?, class: (User.current.admin? ? '' : 'disable-unless-private'), data: {disables: '.project-specific'} %>
      <%= label_tag :query_is_for_all, l(:field_is_for_all) %>
    </p>
    <p>
      <%= check_box_tag 'query_is_for_project_cbx', 1, !query.project.nil?, class: 'project-specific'.concat(User.current.admin? ? '' : 'disable-unless-private'), data: {disables: '#query_is_for_all'} %>
      <%= label_tag :query_is_for_project_cbx, l(:field_is_for_project) %>
      <%= autocomplete_field_tag('project_id', query.all_projects_values, Array(query.project_id.to_s), show_toggle_button: true, multiple: false, load_immediately: true) %>
    </p>
    <p>
      <%= check_box_tag 'is_for_subprojects', 1, query.is_for_subprojects, class: 'project-specific', disabled: (query.project.nil? || (!query.new_record? && query.is_public? && !User.current.admin?)), data: {disables: '#query_is_for_all'} %>
      <%= label_tag :is_for_subprojects, l(:field_is_for_subprojects) %>
    </p>
  </div>

  <fieldset class="easy-query-filters-field tabular"><legend><%= l(:label_options) %></legend>
    <% if query.display_filter_columns_on_edit && !is_default_query -%>
      <p>
        <%= label_tag :default_columns, l(:label_default_columns) -%>
        <%= check_box_tag 'default_columns', 1, query.has_default_columns?, :id => 'default_columns' %>
      </p>
      <%= javascript_tag("$('#default_columns').change(function(e){ if ($(this).is(':checked')) {$('#columns').hide()} else {$('#columns').show()} });") %>
    <% end -%>

    <p class="color-pickers">
      <%= f.label :is_tagged, l(:field_easy_query_is_tagged) -%>
      <%= f.check_box :is_tagged, :disabled => query.require_is_tagged? %>
      <%= easy_icon_select_tag("settings[tagged_icon]", query.settings['tagged_icon']) %>
      <% options = Array.new
         1.upto(35) do |i|
           options << ["palette-#{i}", "palette-#{i}", {:class => "palette-#{i}"}]
         end
      %>
      <%= select_tag "settings[tagged_color]", options_for_select(options, query.settings['tagged_color']), :class => 'select-color inline' %>
    </p>

    <% if params[:gantt] %>
      <p><label><%= l(:label_gantt) %></label>
        <label class="inline"><%= check_box_tag "#{block_name}[draw_relations]", '1', query.draw_relations %> <%= l(:label_related_issues) %></label>
        <label class="inline"><%= check_box_tag "#{block_name}[draw_progress_line]", '1', query.draw_progress_line %> <%= l(:label_gantt_progress_line) %></label>
      </p>
    <% end %>
  </fieldset>

  <fieldset id="available_outputs" class="easy-query-filters-field tabular">
    <legend><%= l(:label_easy_query_outputs) %></legend>
    <p>
      <% presenter.outputs.each(:available) do |output| %>
        <%= output.render_edit_box if !output.key.in?(output_black_list) %>
      <% end %>
    </p>
  </fieldset>

  <fieldset id="filters" class="easy-query-filters-field">
    <legend><%= l(:label_filter_plural) -%></legend>
    <%= render_with_fallback :partial => 'filters', :prefixes => query, :locals => {:query => query} -%>
  </fieldset>

  <fieldset id="custom_formatting" class="easy-query-custom-formatting-field">
    <legend><%= l(:label_custom_formatting) -%></legend>
    <%= render_with_fallback :partial => 'filters_custom_formatting', :prefixes => query, :locals => {:query => query} -%>
  </fieldset>

  <% presenter.outputs.each(:available) do |output| %>
    <%= output.render_edit %>
  <% end %>

</div>

<script type="text/javascript">
  $(function(){
    $("input[name='query[visibility]']").change(function(){
      var roles_checked = $('#query_visibility_<%= EasyQuery::VISIBILITY_ROLES %>').is(':checked');
      var private_checked = $('#query_visibility_<%= EasyQuery::VISIBILITY_PRIVATE %>').is(':checked');
      $("input[name='query[role_ids][]'][type=checkbox]").attr('disabled', !roles_checked);
      if (!private_checked) $("input.disable-unless-private").attr('checked', false);
      $("input.disable-unless-private").attr('disabled', !private_checked);
    }).trigger('change');

    $(".<%= modul_uniq_id %>output_switch").change(function(){
      var $self = $(this);
      var $box = $('.'+ $self.val() +'_settings');
      $box.toggle($self.is(':checked'));
      $(document).trigger('erui_interface_change_vertical');
    });
  });
</script>
