<metal:form define-macro="form"
            i18n:domain="ploneintranet"
>
  <form class="pat-form pat-inject"
        id="global-search"
        action="${here/portal_url}/apps/search/@@search#results"
        data-pat-inject="source: #results; target: #results; &amp;&amp; source: #tabs-nav; target: #tabs-nav; history: record"
        tal:define="
          is_initial_search python: not (view.is_searching() or view.is_filtering());
        "
  >

    <div class="search-header discrete-injection"
         id="search-header"
    >
      <div class="container search-form ${python: is_initial_search and 'initial-search' or None}"
           tal:define="
             view_mode python: request.get('search-mode') == 'advanced' and 'view-extended' or 'view-compact';
           "
      >
        <div class="search-form ${view_mode}"
             id="search-form"
        >

          <fieldset class="search-box-body">
            <label class="search-field pat-subform ${python: not is_initial_search and 'pat-autosubmit' or None} pat-inject"
                   data-pat-inject="source: #results; target: #results; ${python: is_initial_search and 'delay: 1s; &amp;&amp; source: #search-header; target: #search-header;' or ' &amp;&amp; source: #tabs-nav; target: #tabs-nav; history: record;'}"
            >
              <input class="quick-search"
                     autofocus
                     name="SearchableText"
                     placeholder="Search the entire intranet"
                     type="search"
                     value="${request/SearchableText|nothing}"
                     i18n:attributes="placeholder label_search_entire_intranet"
              />
              <button class="search-button-main"
                      name="standard"
                      type="submit"
                      i18n:translate=""
              >Search</button>
            </label>

            <fieldset class="advanced-search pat-inject pat-subform"
                      data-pat-inject="source: #results; target: #results; ${python: is_initial_search and 'delay: 1s; &amp;&amp; source: #search-header; target: #search-header;' or '&amp;&amp; source: #tabs-nav; target: #tabs-nav;'}"
            >
              <fieldset class="advanced pat-clone"
                        data-pat-clone="trigger-element: .add-clone-rule; remove-behaviour: none; template: #clone-template"
              >
                <tal:blank-rule tal:condition="python: not request.get('search-rule')">
                  <input name="search-rule.and-or:records"
                         type="hidden"
                         value=""
                  />
                  <p class="clone search-rule row-1">
                    <label class="pat-select field-choice pat-select">
                      <select name="search-rule.data-field:records">
                        <option value="SearchableText" i18n:translate="">Any text field</option>
                        <optgroup label="Metadata"  i18n:attributes="label">
                          <option value="Title" i18n:translate="">Title</option>
                          <option value="Description" i18n:translate="">Description</option>
                          <option value="created" i18n:translate="">Creation date</option>
                          <option value="modified" i18n:translate="">Modification date</option>
                        </optgroup>
                      </select>
                    </label>
                    <span class="parameters lemma">
                      <label class="pat-select">
                        <select name="search-rule.lemma-operator:records">
                          <option value="contains" i18n:translate="">Contains</option>
                          <option value="contains-not" i18n:translate="">Does not contain</option>
                        </select>
                      </label>
                      <input class="lemma-field"
                             name="search-rule.lemma-value:records"
                             placeholder="Lemma"
                             i18n:attributes="placeholder"
                             type="text"
                      />
                    </span>
                    <span class="parameters date">
                      <label class="pat-select">
                        <select name="search-rule.date-operator:records">
                          <option value="is" i18n:translate="">Is</option>
                          <option value="is-before" i18n:translate="">Is before</option>
                          <option value="is-after" i18n:translate="">Is after</option>
                        </select>
                      </label>
                      <input class="date-field pat-date-picker"
                             name="search-rule.date-value:records"
                             placeholder="Date"
                             data-pat-date-picker="${here/@@proto/date_picker_options}"
                             i18n:attributes="placeholder"
                             type="date"
                      />
                    </span>
                  </p>
                </tal:blank-rule>

                <tal:rule tal:repeat="rule request/search-rule|nothing">
                  <tal:rule tal:condition="python: rule['lemma-value'] or rule['date-value']">
                    <input name="search-rule.and-or:records"
                           type="hidden"
                           value=""
                           tal:condition="repeat/rule/start"
                    />
                    <p class="clone search-rule ${python: repeat['rule'].start and 'row-1' or None}">
                      <label class="and-or pat-select bare"
                             tal:condition="not:repeat/rule/start"
                      >
                        <select name="search-rule.and-or:records">
                          <option selected="${python: rule['and-or'] == 'and' and 'selected' or None}"
                                  value="and"
                          >
                            <tal:i18n i18n:translate="">And</tal:i18n>&hellip;
                          </option>
                          <option selected="${python: rule['and-or'] == 'or' and 'selected' or None}"
                                  value="or"
                          >
                            <tal:i18n i18n:translate="">Or</tal:i18n>&hellip;
                          </option>
                        </select>
                      </label>
                      <label class="field-choice pat-select">
                        <select name="search-rule.data-field:records">
                          <option selected="${python: rule['data-field'] == attrs['value'] and 'selected' or None}"
                                  value="SearchableText"
                                  i18n:translate=""
                          >Any text field</option>
                          <optgroup label="Metadata" i18n:attributes="label">
                            <option selected="${python: rule['data-field'] == attrs['value'] and 'selected' or None}"
                                    value="Title"
                                    i18n:translate=""
                            >Title</option>
                            <option selected="${python: rule['data-field'] == attrs['value'] and 'selected' or None}"
                                    value="Description"
                                    i18n:translate=""
                            >Description</option>
                            <option selected="${python: rule['data-field'] == attrs['value'] and 'selected' or None}"
                                    value="created"
                                    i18n:translate=""
                            >Creation date</option>
                            <option selected="${python: rule['data-field'] == attrs['value'] and 'selected' or None}"
                                    value="modified"
                                    i18n:translate=""
                            >Modification date</option>
                          </optgroup>
                        </select>
                      </label>
                      <span class="parameters lemma">
                        <label class="pat-select">
                          <select name="search-rule.lemma-operator:records">
                            <option selected="${python: rule['lemma-operator'] == attrs['value'] and 'selected' or None}"
                                    value="contains"
                                    i18n:translate=""
                            >Contains</option>
                            <option selected="${python: rule['lemma-operator'] == attrs['value'] and 'selected' or None}"
                                    value="contains-not"
                                    i18n:translate=""
                            >Does not contain</option>
                          </select>
                        </label>
                        <input class="lemma-field"
                               name="search-rule.lemma-value:records"
                               placeholder="Lemma"
                               type="text"
                               i18n:attributes="placeholder"
                               value="${rule/lemma-value}"
                        />
                      </span>
                      <span class="parameters date">
                        <label class="pat-select">
                          <select name="search-rule.date-operator:records">
                            <option selected="${python: rule['date-operator'] == attrs['value'] and 'selected' or None}"
                                    value="is"
                                    i18n:translate=""
                            >Is</option>
                            <option selected="${python: rule['date-operator'] == attrs['value'] and 'selected' or None}"
                                    value="is-before"
                                    i18n:translate=""
                            >Is before</option>
                            <option selected="${python: rule['date-operator'] == attrs['value'] and 'selected' or None}"
                                    value="is-after"
                                    i18n:translate=""
                            >Is after</option>
                          </select>
                        </label>
                        <input class="date-field pat-date-picker"
                               name="search-rule.date-value:records"
                               placeholder="Date"
                               type="date"
                               data-pat-date-picker="${here/@@proto/date_picker_options}"
                               i18n:attributes="placeholder"
                               value="${rule/date-value}"
                        />
                      </span>
                      <button class="remove-clone"
                              type="button"
                              tal:condition="not:repeat/rule/start"
                              i18n:translate=""
                      >Remove</button>
                    </p>
                  </tal:rule>
                </tal:rule>

              </fieldset>

              <p class="button-bar">
                <button class="pat-button default icon-search"
                        type="submit"
                        i18n:translate=""
                >Search</button>
                <button class="pat-button add-clone add-clone-rule icon-plus-circle"
                        type="button"
                        i18n:translate=""
                >Add another search rule</button>
              </p>

              <p class="clone search-rule animated fadeIn"
                 id="clone-template"
                 hidden
              >
                <label class="and-or pat-select bare">
                  <select name="search-rule.and-or:records">
                    <option value="and">
                      <tal:i18n i18n:translate="">And</tal:i18n>&hellip;
                    </option>
                    <option value="or">
                      <tal:i18n i18n:translate="">Or</tal:i18n>&hellip;
                    </option>
                  </select>
                </label>
                <label class="field-choice pat-select">
                  <select name="search-rule.data-field:records">
                    <option value="SearchableText" i18n:translate="">Any text field</option>
                    <optgroup label="Metadata" i18n:attributes="label">
                      <option value="Title" i18n:translate="">Title</option>
                      <option value="Description" i18n:translate="">Description</option>
                      <option value="created" i18n:translate="">Creation date</option>
                      <option value="modified" i18n:translate="">Modification date</option>
                    </optgroup>
                  </select>
                </label>
                <span class="parameters lemma">
                  <label class="pat-select">
                    <select name="search-rule.lemma-operator:records">
                      <option value="contains" i18n:translate="">Contains</option>
                      <option value="contains-not" i18n:translate="">Does not contain</option>
                    </select>
                  </label>
                  <input class="lemma-field"
                         name="search-rule.lemma-value:records"
                         placeholder="Lemma"
                         i18n:attributes="placeholder"
                         type="text"
                  />
                </span>
                <span class="parameters date">
                  <label class="pat-select">
                    <select name="search-rule.date-operator:records">
                      <option value="is" i18n:translate="">Is</option>
                      <option value="is-before" i18n:translate="">Is before</option>
                      <option value="is-after" i18n:translate="">Is after</option>
                    </select>
                  </label>
                  <input class="date-field pat-date-picker"
                         name="search-rule.date-value:records"
                         placeholder="Date"
                         data-pat-date-picker="${here/@@proto/date_picker_options}"
                         i18n:attributes="placeholder"
                         type="date"
                  />
                </span>
                <button class="remove-clone"
                        type="button"
                        i18n:translate=""
                >Remove</button>
              </p>

              <fieldset class="toggle-bar pat-checklist free-form">
                <label class="pat-switch toggle-standard"
                       data-pat-switch="selector: #search-form; remove: view-extended; add: view-compact"
                >
                  <input checked="${python: request.get('search-mode') != 'advanced' and 'checked' or None}"
                         name="search-mode"
                         type="radio"
                         value="standard"
                  />
                   <tal:i18n i18n:translate="">Standard search</tal:i18n>
                </label>
                <label class="pat-switch toggle-advanced"
                       data-pat-switch="selector: #search-form; remove: view-compact; add: view-extended"
                >
                  <input checked="${python: request.get('search-mode') == 'advanced' and 'checked' or None}"
                         name="search-mode"
                         type="radio"
                         value="advanced"
                  />
                  <tal:i18n i18n:translate="">Advanced search</tal:i18n>
                </label>
              </fieldset>
            </fieldset>
          </fieldset>

          <tal:nav replace="structure here/search_tabs" />

        </div>
      </div>
    </div>

    <div class="search-body"
         id="search-body"
    >
      <div class="container results-pane">
        <div class="results"
             id="results"
             tal:define="
               search_response search_response|nothing;
             "
        >
          <metal:results define-slot="results">
            <div class="container results">

            </div>
          </metal:results>
        </div>
      </div>
    </div>
  </form>
</metal:form>

<metal:autoload define-macro="autoload">
  <tal:next define="
              next_url python: view.next_page_url(search_response.total_results);
            "
            tal:condition="next_url"
  >
    <a class="item pat-inject"
       id="search-autoload-target"
       href="${next_url}"
       data-pat-inject="source: #listing; target: #search-autoload-target::element; trigger: autoload-visible;"
       i18n:translate=""
    >Loading more search results&hellip;</a>
  </tal:next>
</metal:autoload>

<metal:counter define-macro="counter">
  <p tal:condition="not:search_response"
     i18n:translate=""
  >No results found</p>
  <p class="introduction" tal:condition="search_response">
    <span tal:omit-tag=""
          i18n:translate="message_search_results_count"
    ><tal:var i18n:name="count">${python:search_response.total_results}</tal:var> items matched your search.</span>
  </p>
</metal:counter>
