<!--
 ~ 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 d-flex flex-column justify-content-center align-items-center">
		<img src="/images/ezyplatform-logo-color.png" width="128">
		<div>
			<span>
				<a href="https://ezyplatform.com">EzyPlatform</a>
				[[${#strings.toLowerCase(#messages.msg('version'))}]]
				[[${platformVersion}]]</span>
		</div>
		<div class="d-flex mt-3 mobile-button-group">
			<button type="button" class="btn btn-primary"
					th:classappend="${hasNewPlatformVersion ? '' : 'd-none'}"
					th:if="${adminRoles.isAccessible('/api/v1/platform/download', 'POST') && downloadedVersion == null}"
					onclick="ezyadmin.downloadEzyPlatform();">
				[[#{download}]]
				[[${#strings.toLowerCase(#messages.msg('version'))}]]
				[[${platformLatestVersion}]]
			</button>
			<button type="button" class="btn btn-primary" id="prepareToInstallButton"
					th:if="${adminRoles.isAccessible('/api/v1/platform/prepare-to-install', 'POST')
					&& downloadedVersion != null && osType.name() == 'WINDOWS'}"
					onclick="ezyadmin.prepareToInstallEzyPlatform();">
				[[#{prepare_to_install}]]
				[[${#strings.toLowerCase(#messages.msg('version'))}]]
				[[${downloadedVersion}]]
			</button>
			<button type="button" class="btn btn-primary"
					th:if="${adminRoles.isAccessible('/api/v1/platform/install', 'POST')
					&& downloadedVersion != null && osType.name() != 'WINDOWS'}"
					onclick="ezyadmin.installEzyPlatform();">
				[[#{install}]]
				[[${#strings.toLowerCase(#messages.msg('version'))}]]
				[[${downloadedVersion}]]
			</button>
			<button type="button" class="btn btn-danger ml-1 mr-1"
					th:if="${adminRoles.isAccessible('/api/v1/platform', 'DELETE') && downloadedVersion != null}"
					data-toggle="modal" data-target="#delete-platform-modal">
				[[#{delete}]]
				[[${#strings.toLowerCase(#messages.msg('version'))}]]
				[[${downloadedVersion}]]
			</button>
			<a class="btn btn-info"
					th:if="${adminRoles.isAccessible('/api/v1/platform/sql') && downloadedVersion != null}"
					href="/api/v1/platform/sql" target="_blank">
				[[#{export_sql}]] <i class="fas fa-external-link-alt"></i>
			</a>
		</div>
		<div th:if="${downloadedVersion != null && osType.name() == 'WINDOWS'}"
			 class="mt-3"
			 th:utext="#{windows_must_update_manually}">
		</div>
	</div>
	<div class="col-12 mb-4"></div>
	<div class="col-12">
        <div class="latest-ezyplatform-release-notes-container"></div>
    </div>
    <div class="col-12">
        <div class="ezyplatform-release-notes-container"></div>
    </div>
</div>
<th:block layout:fragment="modals">
	<th:block th:if="${downloadedVersion != null}">
		<th:block th:replace="~{fragments/prompt :: content(id='delete-platform-modal', title=confirm, body=${#messages.msg('delete_platform_version', downloadedVersion) + '?'}, confirmButton=delete, closeButton=close)}" />
	</th:block>
</th:block>
<script layout:fragment="scripts" th:inline="javascript">
/*<![CDATA[*/
ezyadmin.platformVersion = /*[[${platformVersion}]]*/ '';
ezyadmin.platformLatestVersion = /*[[${platformLatestVersion}]]*/ '';
/*]]>*/

ezyadmin.onHistoryButtonClick = function() {
	window.location = '/ezyplatform/history'
		+ (ezyadmin.lang ? '?lang=' + ezyadmin.lang : '');
}

// ========== update platform =============
ezyadmin.downloadEzyPlatform = function() {
	ezyadmin.doEzyPlatformAction('download');
}

ezyadmin.onPromptConfirmed = function(modalId) {
    if (modalId == 'delete-platform-modal') {
        ezyadmin.deleteEzyPlatform();
    }
}

ezyadmin.deleteEzyPlatform = function() {
    $.ajax({
        type: 'DELETE',
        url: '/api/v1/platform',
        success: function () {
        	location.reload();
        },
        error: function (data, e) {
            ezyadmin.processUpdateApiErrors({}, data);
        }
    });
}
</script>
<th:block layout:fragment="post-scripts">
    	<th:block th:replace="~{ezyplatform/scripts :: scripts}" />
</th:block>
<script layout:fragment="final-scripts">
ezyadmin.loadEzyPlatformReleaseNotes = function() {
    var languageCode = ezyadmin.lang || 'en';
    $.ajax({
        type: 'GET',
        url: `/api/v1/platforms/${ezyadmin.platformVersion}/release-notes`,
        data: {
        	limit: 1,
            i18n_lang: languageCode
        },
        success: function (data) {
            $('.ezyplatform-release-notes-container').html(data.content || '');
            if (data.additionalHead) {
                $('head').append(data.additionalHead);
            }
            if (data.additionalFoot) {
                $('body').append(data.additionalFoot);
            }
        },
        error: function (e) {
            ezyadmin.processGetApiErrors(e);
        }
    });
}

ezyadmin.loadLatestEzyPlatformReleaseNotes = function() {
    var languageCode = ezyadmin.lang || 'en';
    $.ajax({
        type: 'GET',
        url: `/api/v1/platforms/${ezyadmin.platformLatestVersion}/release-notes`,
        data: {
        	limit: 4,
            i18n_lang: languageCode
        },
        success: function (data) {
            if (data.additionalHead) {
                $('head').append(data.additionalHead);
            }
            if (data.additionalFoot) {
                $('body').append(data.additionalFoot);
            }
            var html = data.content || '';
            data.previousVersionReleaseNotesList.forEach((previousNotes) => {
            	if (ezyadmin.platformVersion != ezyadmin.platformLatestVersion
            		&& ezyadmin.platformVersion >= previousNotes.platformVersion
				) {
            		return;
            	}
                if (previousNotes.additionalHead) {
                    $('head').append(data.additionalHead);
                }
                if (previousNotes.additionalFoot) {
                    $('body').append(previousNotes.additionalFoot);
                }
                html += `
                    ${previousNotes.content || ''}
                `;
            });
            $('.latest-ezyplatform-release-notes-container').html(html);
        },
        error: function (e) {
            ezyadmin.processGetApiErrors(e);
        }
    });
}

$(document).ready(() => {
	ezyadmin.loadLatestEzyPlatformReleaseNotes();
	if (ezyadmin.platformVersion != ezyadmin.platformLatestVersion) {
		ezyadmin.loadEzyPlatformReleaseNotes();
	}
});
</script>
</body>
</html>
