<!--
 ~ Copyright 2022 youngmonkeys.org
 ~ 
 ~ Licensed under the ezyplatform, Version 1.0.0 (the "License");
 ~ you may not use this file except in compliance with the License.
 ~ You may obtain a copy of the License at
 ~ 
 ~     https://youngmonkeys.org/licenses/ezyplatform-1.0.0.txt
 ~ 
 ~ Unless required by applicable law or agreed to in writing, software
 ~ distributed under the License is distributed on an "AS IS" BASIS,
 ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ~ See the License for the specific language governing permissions and
 ~ limitations under the License.
-->

<!DOCTYPE html>
<html
        xmlns="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org"
        xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
        layout:decorate="~{ezyadmin}">

<body>
<div layout:fragment="content" class="row">
    <div class="col-12">
        <table id="searchResultList" class="table table-bordered table-hover bg-white">
            <thead>
            <tr>
                <th>#</th>
                <th>Type</th>
                <th>URL</th>
                <th>Content</th>
            </tr>
            </thead>
            <tbody id="searchResultListBody">
            </tbody>
        </table>
    </div>
</div>
<script layout:fragment="scripts">

// ========== result list =============
ezyadmin.fetchSearchResultList = function() {
    var keyword = ezyadmin.getCurrentUrlParameter('keyword');
    $.ajax({
        url: '/api/v1/search?keyword=' + keyword,
        type: 'GET',
    })
    .done(function(data) {
        data.sort(function(a, b) {
            if (a.searchPriority < b.searchPriority) {
                return -1;
            } if (a.searchPriority > b.searchPriority) {
                return 1;
            }
            if (a.resultWeight < b.resultWeight) {
                return 1;
            } else if (a.resultWeight > b.resultWeight) {
                return -1;
            }
            return 0;
        });
        ezyadmin.renderSearchResultListTable(data);
    })
    .fail(function(e) {
    	ezyadmin.processGetApiErrors(e);
    });
}

ezyadmin.renderSearchResultListTable = function(searchResultList) {
    var html = ezyadmin.buildSearchResultListBodyHtml(searchResultList);
    $('#searchResultListBody').html(html);
    ezyadmin.createDataTable('searchResultList', 12);
}

ezyadmin.buildSearchResultListBodyHtml = function(searchResultList) {
    var html = '';
    searchResultList.forEach((result, index) => {
        html += ezyadmin.buildSearchResultListItemHtml(result, index);
    });
    return html;
}

ezyadmin.buildSearchResultListItemHtml = function(result, index) {
    var content = result.content || '';
    if (content && result.resultType == 'URL') {
        content = 'Feature: ' + ezyadmin.toDisplayString(content);
    }
    var html = '<tr>' +
	'	<td>' + (index + 1) + '</td>' +
	'	<td>' + (result.resultType || '').toUpperCase() + '</td>' +
	'	<td><a href="' + result.url + '">' + result.title + '</a></td>' +
	'	<td>' + content + '</td>' +
    '</tr>';
	return html;
}
ezyadmin.fetchSearchResultList();
</script>
</body>
</html>
