<!--
`nuxeo-embedding-search-search-results`
@group Nuxeo UI
@element nuxeo-embedding-search-search-results
-->
<dom-module id="nuxeo-embedding-search-search-results">
  <template>
    <style include="nuxeo-styles"></style>
    
    <nuxeo-results name="embedding-search" nx-provider="[[nxProvider]]" selected-items="{{selectedItems}}">
      
      <nuxeo-data-grid name="grid" icon="nuxeo:view-thumbnails" class="results" empty-label="[[i18n('searchResults.noResults')]]" last-index="[[_lastIndex]]" selection-enabled>
        <template>
          <nuxeo-document-grid-thumbnail tabindex$="{{_computeTabAndLastIndex(index)}}" selected$="{{selected}}" doc="[[item]]" index="[[index]]" 
                                         on-navigate="_navigate" selected-items="[[selectedItems]]">
          </nuxeo-document-grid-thumbnail>
        </template>
      </nuxeo-data-grid>

      <nuxeo-data-table class="results" settings-enabled empty-label="[[i18n('searchResults.noResults')]]" selection-enabled on-row-clicked="_navigate" icon="nuxeo:view-list" name="table">

        <nuxeo-data-table-column name="Title" field="dc:title" flex="100">
          <template>
            <nuxeo-document-thumbnail document="[[item]]"></nuxeo-document-thumbnail>
            <a class="ellipsis" href$="[[urlFor(item)]]" on-click="_navigate">[[item.title]]</a>
          </template>
        </nuxeo-data-table-column>

        <nuxeo-data-table-column name="Modified" field="dc:modified" flex="50">
          <template>
            [[formatDate(item.properties.dc:modified)]]
          </template>
        </nuxeo-data-table-column>

        <nuxeo-data-table-column name="Last Contributor" field="dc:lastContributor" flex="50">
          <template>
            <nuxeo-user-tag user="[[item.properties.dc:lastContributor]]"></nuxeo-user-tag>
          </template>
        </nuxeo-data-table-column>
      </nuxeo-data-table>

    </nuxeo-results>
  </template>
  
  <script>
  Polymer({
    is: 'nuxeo-embedding-search-search-results',

    behaviors: [Nuxeo.LayoutBehavior],

    properties: {

      nxProvider: Object,

      _lastIndex: {
        type: Number,
        value: 0,
      },
    },

    _navigate: function(e) {
      this.fire('navigate', { doc: (e.model || e.detail).item, index: (e.model || e.detail).index });
    },

    _computeTabAndLastIndex(index) {
      let  tabindex = index+1;
      this._lastIndex = this.nxProvider.resultsCount;
      return tabindex.toString();
    }

  });
  </script>
</dom-module>
