<!--
 ~ 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>
<th:block layout:fragment="modals">
<th:block th:if="${downloadedVersion != null}"
		  th:replace="~{fragments/prompt :: content(id='delete-platform-modal', title=confirm, body=${#messages.msg('delete_platform_version', downloadedVersion) + '?'}, confirmButton=delete, closeButton=close)}" />
</th:block>
<script layout:fragment="scripts">
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">
<script th:replace="~{ezyplatform/scripts :: scripts}" />
</th:block>
</body>
</html>
