<% content_for :script do %>
  <script>require(['measures/app']);</script>
<% end %>


<div class="page">
  <header class="page-header">
    <%= render :partial => 'search_header' -%>
  </header>

  <div class="navigator-filters"></div>

  <div class="navigator-content">
    <div class="navigator-main">
      <div class="navigator-details">
        <%= render :partial => 'search_body' -%>
      </div>
    </div>
  </div>
</div>


<script id="filter-bar-template" type="text/x-handlebars-template">
  <form method="get" action="<%= ApplicationController.root_context -%>/measures/search">
    <% if @filter.id %>
      <input type="hidden" name="id" value="<%= h @filter.id -%>">
    <% end %>
    <% if @filter.display %>
      <input type="hidden" name="display" value="<%= h @filter.display.key -%>"/>
      <%
         @filter.display.url_params.each do |k_v_array|
           if k_v_array[1].is_a?(String)
      %>
          <%= hidden_field_tag k_v_array[0], k_v_array[1] -%>
        <% else
             k_v_array[1].each do |string_val|
        %>
            <%= hidden_field_tag "#{k_v_array[0]}[]", string_val -%>
          <% end
             end
             end
             end
          %>

    <div class="navigator-filters-list"></div>
    <button class="navigator-filter-submit"><%= message('search_verb') -%></button>
  </form>
</script>



<script>
  jQuery('html').addClass('navigator-page measures-page');

  window.SS = typeof window.SS === 'object' ? window.SS : {};

  _.extend(window.SS, {
    qualifiers: <%= render :partial => 'measures/qualifiers' -%>,
    <% condition_metrics = Metric.all.select { |m| m.numeric? && !m.hidden } %>
    metrics: <%= condition_metrics.to_json -%>,
    metricPeriods: {
      '1': '<%= Api::Utils.period_label(1) -%>',
      '2': '<%= Api::Utils.period_label(2) -%>',
      '3': '<%= Api::Utils.period_label(3) -%>'
    },
    favorites: <%= render :partial => 'measures/favourites2' -%>,
    workDuration: {
      days: '<%= message('work_duration.x_days') -%>',
      hours: '<%= message('work_duration.x_hours') -%>',
      minutes: '<%= message('work_duration.x_minutes') -%>'
    },

    phrases: {
      'any':            '<%= escape_javascript message('any') -%>',
      'anytime':        '<%= escape_javascript message('anytime') -%>',
      'all':            '<%= escape_javascript message('all') -%>',
      'manage':         '<%= escape_javascript message('manage') -%>',
      'to':             '<%= escape_javascript message('to.downcase') -%>',
      'value':          '<%= escape_javascript message('value') -%>',
      'components':     '<%= escape_javascript message('measure_filter.criteria.components') -%>',
      'age':            '<%= escape_javascript message('measure_filter.criteria.age') -%>',
      'alert':          '<%= escape_javascript message('measure_filter.criteria.alert') -%>',
      'componentsOf':   '<%= escape_javascript message('measure_filter.criteria.components_of') -%>',
      'date':           '<%= escape_javascript message('measure_filter.criteria.date') -%>',
      'favoritesOnly':  '<%= escape_javascript message('measure_filter.criteria.only_favorites') -%>',
      'keyContains':    '<%= escape_javascript message('measure_filter.criteria.key_contains') -%>',
      'lastAnalysis':   '<%= escape_javascript message('measure_filter.criteria.last_analysis') -%>',
      'metric':         '<%= escape_javascript message('measure_filter.criteria.metric') -%>',
      'notSet':         '<%= escape_javascript message('measure_filter.criteria.metric.not_set') -%>',
      'nameContains':   '<%= escape_javascript message('measure_filter.name_contains') -%>',
      'moreCriteria':   '<%= escape_javascript message('measure_filter.more_criteria') -%>',
      'error':          '<%= escape_javascript message('measure_filter.criteria.alert.error') -%>',
      'warning':        '<%= escape_javascript message('measure_filter.criteria.alert.warn') -%>',
      'ok':             '<%= escape_javascript message('measure_filter.criteria.alert.ok') -%>',
      'days':           '<%= escape_javascript message('measure_filter.criteria.age.days') -%>',
      'filtersList':    '<%= escape_javascript message('measure_filter.filter_list') -%>',

      'work_duration': {
        'x_days': '<%= escape_javascript message('work_duration.x_days') -%>',
        'x_hours': '<%= escape_javascript message('work_duration.x_hours') -%>',
        'x_minutes': '<%= escape_javascript message('work_duration.x_minutes') -%>'
      }
    }
  });



  var queryParams = [
    { key: 'qualifiers[]', value: <%= json_escape(@filter.criteria['qualifiers'].to_json) -%> },
    { key: 'alertLevels[]', value: <%= json_escape(@filter.criteria['alertLevels'].to_json) -%> },
    { key: 'fromDate', value: '<%= escape_javascript @filter.criteria['fromDate'] -%>' },
    { key: 'toDate', value: '<%= escape_javascript @filter.criteria['toDate'] -%>' },
    { key: 'ageMinDays', value: '<%= escape_javascript @filter.criteria('ageMinDays') -%>' },
    { key: 'ageMaxDays', value: '<%= escape_javascript @filter.criteria['ageMaxDays'] -%>' },
    <% if @filter.base_resource %>
    { key: 'base', value: ['<%= escape_javascript @filter.base_resource.key -%>'], text: ['<%= escape_javascript @filter.base_resource.name(true) -%>'] },
    <% end %>
    { key: 'onFavourites', value: <%= @filter.criteria['onFavourites']=='true' -%> },
    { key: 'keySearch', value: '<%= escape_javascript @filter.criteria['keySearch'] -%>' },

    <% for i in 1..3 %>
      <% unless @filter.criteria("c#{i}_metric").blank? %>
      { key: '<%= "c#{i}_metric" -%>', value: '<%= escape_javascript @filter.criteria("c#{i}_metric") -%>' },
      { key: '<%= "c#{i}_period" -%>', value: '<%= escape_javascript @filter.criteria("c#{i}_period") -%>' },
      { key: '<%= "c#{i}_op" -%>', value: '<%= escape_javascript @filter.criteria("c#{i}_op") -%>' },
      { key: '<%= "c#{i}_val" -%>', value: '<%= escape_javascript @filter.criteria("c#{i}_val") -%>' },
      <% end %>
    <% end %>
    { key: 'nameSearch', value: '<%= escape_javascript @filter.criteria['nameSearch'] -%>' }
  ];
</script>
