<!--
 ~ 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 class="text-center">#</th>
                <th>[[#{type}]]</th>
                <th>[[#{url}]]</th>
                <th>[[#{content}]]</th>
            </tr>
            </thead>
            <tbody id="searchResultListBody">
            </tbody>
        </table>
    </div>
</div>
<script layout:fragment="scripts" th:inline="javascript">
/*<![CDATA[*/
ezyadmin.messages.feature = /*[[#{feature}]]*/;
ezyadmin.messages.data_table_filter = /*[[#{data_table.filter}]]*/;
/*]]>*/

// ========== 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 = async function(searchResultList) {
    var html = await ezyadmin.buildSearchResultListBodyHtml(searchResultList);
    $('#searchResultListBody').html(html);
    ezyadmin.createDataTable('searchResultList', 30, undefined, undefined, {
        language: {
            search: ezyadmin.messages.data_table_filter
        }
    });
}

ezyadmin.buildSearchResultListBodyHtml = async function(searchResultList) {
    var messageKeys = [];
    searchResultList.forEach((result) => {
        result.resultType = result.resultType || '';
        var messageKey = result.resultType.toLowerCase();
        if (messageKeys.indexOf(messageKey) < 0) {
            messageKeys.push(messageKey);
        }
    });
    var messageByKey = await ezyadmin.translateMessagesAsync(messageKeys);
    var html = '';
    searchResultList.forEach((result, index) => {
        result.resultType = messageByKey[result.resultType.toLowerCase()];
        html += ezyadmin.buildSearchResultListItemHtml(result, index);
    });
    return html;
}

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