<div id="<%= format %>-export-options" style="display:none;">
  <%= content_tag(:h3, l(:label_export_options, export_format: format.upcase), class: 'title') %>
  <%= form_tag(params.merge({format: format, page: nil}), method: :get, id: '-export-form') do %>
    <p>
      <%= label_tag("easy_query_columns_to_export_selected_#{format}", l(:description_selected_columns), class: 'inline') %>
      <%= radio_button_tag('easy_query[columns_to_export]', 'selected', true, id: "easy_query_columns_to_export_selected_#{format}") %>
    </p>
    <p>
      <%= label_tag("easy_query_columns_to_export_all_#{format}", l(:description_all_columns), class: 'inline') %>
      <%= radio_button_tag('easy_query[columns_to_export]', 'all', false, id: "easy_query_columns_to_export_all_#{format}") %>
    </p>
    <p>
      <%= label_tag("easy_query_export_all_groups_#{format}", l(:label_all_groups), class: 'inline') %>
      <%= check_box_tag('easy_query[export_all_groups]', true, false, id: "easy_query_export_all_groups_#{format}", onclick: "$('.cbx_open_categories_#{format}').attr('disabled', this.checked)") %>
    </p>
    <% if format == 'xlsx' %>
      <p>
        <%= label_tag(:easy_query_count_on_different_column, l(:description_count_on_different_column), class: 'inline') %>
        <%= check_box_tag('easy_query[count_on_different_column]', true, false) %>
      </p>
    <% end %>
    <% if format != 'pdf' || query.type !~ /EasyMoney/ %>
    <p>
      <%= hidden_field_tag('export_description', '0', id: nil) %>
      <%= label_tag("export_description_#{format}", l(:field_description), class: 'inline') %>
      <%= check_box_tag('export_description', '1', query.has_column?(:description), id: "export_description_#{format}") %>
    </p>
    <% end %>

    <%= call_hook(:view_additional_export_options, {format: format}) %>

    <p class="buttons">
      <%= submit_tag(l(:button_export), name: nil, class: 'button-positive') %>
      <%= submit_tag(l(:button_cancel), name: nil, onclick: 'hideModal(this); return false', type: 'button', class: 'button') %>
    </p>
    <% if ['xlsx', 'pdf'].include?(format) && query && groups %>
      <%= hidden_field_tag('easy_query[open_categories_ids][]', nil, id: nil) %>
      <p>
        <table>
          <thead>
            <tr>
              <th><%= l(:field_cached_group_names) %></th>
              <th><%= l(:field_export_group_count) %></th>
              <th><%= l(:field_export_list_items_in_group) %></th>
            </tr>
          </thead>
          <tbody>
            <% groups.each do |group, attributes| %>
              <tr>
                <td><%= format_groupby_html_entity_attribute(query.entity, query.group_by_column, attributes[:name], period: query.group_by_period, allow_avatar: query.show_avatars?, entity: attributes[:entity], editable: false).presence || l(:label_none) %></td>
                <td><%= attributes.try(:[], :count) %></td>
                <td><%= check_box_tag('easy_query[open_categories_ids][]', (group.present? ? (group.respond_to?(:id) ? group.id : group) : 'nil').to_s, true, class: "cbx_open_categories_#{format}") %></td>
            </tr>
            <% end %>
          </tbody>
        </table>
      </p>
    <% end %>
  <% end %>
</div>
<script type="text/javascript">
  $(document).ready(function() {
    $(".other-formats .<%= format %>").click(function(e){
      showModal("<%= format %>-export-options", "330px"); e.preventDefault();
      $("#<%= format %>-export-options").append( $("<input>").attr({name: "target_url", type: "hidden"}).val($(this).attr("href")) );
    });
    $("#<%= format %>-export-options form").submit(function(e){
      hideModal(this);
      e.preventDefault();
      var target_url = $("#<%= format %>-export-options input[name=\'target_url\']").val();
      if(!target_url.match(/\?/)) {
        target_url+="?"
      } else {
        target_url+="&"
      }
      window.location.replace(target_url+$(this).serialize());
    });
  });
</script>
