{#
/**
 * This file is part of FacturaScripts
 * Copyright (C) 2017-2026 Carlos Garcia Gomez <carlos@facturascripts.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */
#}
{% extends "Master/MenuTemplate.html.twig" %}
{% import 'Macro/Utils.html.twig' as Utils %}
{% from 'Macro/Utils.html.twig' import messageCompat as showMessage %}

{% block messages %}
    {% include 'Macro/Toasts.html.twig' %}
    <div class="container">
        <div class="row">
            <div class="col-12">
                {{ showMessage(log, ['error', 'critical'], 'danger') }}
                {{ showMessage(log, ['warning'], 'warning') }}
                {{ showMessage(log, ['notice'], 'success') }}
                {{ showMessage(log, ['info'], 'info') }}
            </div>
        </div>
    </div>
{% endblock %}

{% block body %}
    {% set betaUpdatesEnabled = settings('default', 'enableupdatesbeta', false) %}
    {% set hasErrorFiles = fsc.hasErrorFiles() %}
    <div class="container">
        <div class="row pt-3 pb-3">
            <div class="col-sm-6">
                <div class="btn-group">
                    <a href="{{ asset('AdminPlugins') }}" class="btn btn-secondary">
                        <i class="fa-solid fa-arrow-left fa-fw" aria-hidden="true"></i>
                        <span class="d-none d-md-inline-block">{{ trans('plugins') }} </span>
                    </a>
                    <a href="{{ fsc.url() }}" class="btn btn-secondary" title="{{ trans('refresh') }}">
                        <i class="fa-solid fa-redo" aria-hidden="true"></i>
                    </a>
                </div>
                {% if fsc.telemetryManager.ready() %}
                    <button class="btn btn-secondary" type="button" data-bs-toggle="modal"
                            data-bs-target="#modalTelemetry">
                        <i class="fa-solid fa-registered fa-fw" aria-hidden="true"></i>
                        <span class="d-none d-md-inline-block">{{ trans('manage-installation') }} </span>
                    </button>
                {% else %}
                    <button class="btn btn-warning" type="button" data-bs-toggle="modal"
                            data-bs-target="#modalTelemetry">
                        <i class="fa-solid fa-registered fa-fw" aria-hidden="true"></i>
                        <span class="d-none d-md-inline-block">{{ trans('register-installation') }}</span>
                    </button>
                {% endif %}
            </div>
            <div class="col-sm-6 text-end">
                <h1 class="h3">
                    {{ trans('updater') }}
                    <small class="text-info">{{ fsc.getCoreVersion() }}</small>
                    <i class="fa-solid fa-cloud-download-alt" aria-hidden="true"></i>
                </h1>
            </div>
        </div>
        <div class="row">
            <div class="col-sm">
                <div class="card shadow-sm mb-4">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead>
                            <tr>
                                <th scope="col">{{ trans('component') }}</th>
                                <th scope="col">{{ trans('description') }}</th>
                                <th scope="col" class="text-end">{{ trans('action') }}</th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for item in fsc.updaterItems %}
                                <tr class="{{ item.stable ? 'table-success' : 'table-warning' }}">
                                    <td>{{ item.name }}</td>
                                    <td>{{ item.description }}</td>
                                    <td class="text-end">
                                        {% if item.mincore > fsc.getCoreVersion() %}
                                            {{ trans('requires-core', {'%version%': item.mincore}) }}
                                        {% elseif item.downloaded %}
                                            <a href="{{ fsc.url() }}?action=update&item={{ item.id }}&multireqtoken={{ formToken(false) }}"
                                               class="btn btn-spin-action btn-sm btn-success"
                                               onclick="animateSpinner('add')">
                                                <i class="fa-solid fa-rocket fa-fw fa-bounce me-1"
                                                   aria-hidden="true"></i>{{ trans('update') }}
                                            </a>
                                            <a href="{{ fsc.url() }}?action=cancel&item={{ item.id }}&multireqtoken={{ formToken(false) }}"
                                               class="btn btn-spin-action btn-sm btn-warning"
                                               onclick="animateSpinner('add')">
                                                <i class="fa-solid fa-times fa-fw me-1"
                                                   aria-hidden="true"></i>{{ trans('cancel') }}
                                            </a>
                                        {% elseif item.name == 'CORE' and fsc.coreUpdateWarnings %}
                                            <button type="button" class="btn btn-spin-action btn-warning"
                                                    data-bs-toggle="modal"
                                                    data-bs-target="#coreWarningModal">
                                                <i class="fa-solid fa-download fa-fw me-1" aria-hidden="true"></i>{{ trans('download') }}
                                            </button>
                                        {% elseif item.stable %}
                                            <a href="{{ fsc.url() }}?action=download&item={{ item.id }}&multireqtoken={{ formToken(false) }}"
                                               class="btn btn-spin-action btn-sm btn-secondary"
                                               onclick="animateSpinner('add')">
                                                <i class="fa-solid fa-download fa-fw me-1" aria-hidden="true"></i>{{ trans('download') }}
                                            </a>
                                        {% else %}
                                            <a href="{{ fsc.url() }}?action=download&item={{ item.id }}&multireqtoken={{ formToken(false) }}"
                                               class="btn btn-spin-action btn-sm btn-secondary"
                                               onclick="animateSpinner('add')">
                                                <i class="fa-solid fa-flask-vial fa-fw me-1" aria-hidden="true"></i>{{ trans('beta') }}
                                            </a>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% else %}
                                <tr class="table-success">
                                    <td colspan="3">
                                        <p class="p-2 mb-0">✅ {{ trans('no-updates-now') }}</p>
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
                {% if betaUpdatesEnabled == false and fsc.telemetryManager.ready() == false %}
                    <hr/>
                    <div class="mt-4 mb-3">
                        {{ Utils.registerInstall(false) }}
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
    {% if hasErrorFiles or betaUpdatesEnabled %}
        <div class="container">
            <hr/>
            <div class="row g-3 mt-2 mb-4">
                {% if hasErrorFiles %}
                    <div class="col-12{% if betaUpdatesEnabled %} col-lg-6{% endif %}">
                        <div class="card shadow-sm h-100">
                            <div class="card-body">
                                <h5>
                                    <i class="fa-solid fa-bug text-danger me-2" aria-hidden="true"></i>
                                    {{ trans('crash-files-found') }}
                                </h5>
                                <p class="card-text">{{ trans('crash-files-p') }}</p>
                            </div>
                            <div class="card-footer p-2">
                                <div class="d-flex gap-2">
                                    <form action="{{ fsc.url() }}" method="post" onsubmit="animateSpinner('add')">
                                        {{ formToken() }}
                                        <input type="hidden" name="action" value="send-crash-files"/>
                                        <button type="submit" class="btn btn-sm btn-success btn-spin-action">
                                            <i class="fa-solid fa-paper-plane fa-fw me-1" aria-hidden="true"></i>{{ trans('send') }}
                                        </button>
                                    </form>
                                    <form action="{{ fsc.url() }}" method="post" onsubmit="animateSpinner('add')">
                                        {{ formToken() }}
                                        <input type="hidden" name="action" value="delete-crash-files"/>
                                        <button type="submit" class="btn btn-sm btn-danger btn-spin-action">
                                            <i class="fa-solid fa-trash fa-fw me-1" aria-hidden="true"></i>{{ trans('delete') }}
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endif %}
                {% if betaUpdatesEnabled %}
                    <div class="col-12{% if hasErrorFiles %} col-lg-6{% endif %}">
                        <div class="card shadow-sm h-100">
                            <div class="card-body">
                                <h5>
                                    <i class="fa-solid fa-flask-vial text-danger me-2" aria-hidden="true"></i>
                                    {{ trans('updates-beta-warning') }}
                                </h5>
                                <p class="card-text">{{ trans('updates-beta-warning-p') }}</p>
                            </div>
                            <div class="card-footer p-2">
                                <form method="post">
                                    {{ formToken() }}
                                    <input type="hidden" name="action" value="disable-beta"/>
                                    <button type="submit" class="btn btn-sm btn-outline-secondary btn-spin-action">
                                        {{ trans('disable') }}
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                {% endif %}
            </div>
        </div>
    {% endif %}
    <div class="modal fade" id="modalTelemetry" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                {% if fsc.telemetryManager.ready() %}
                    <div class="modal-header">
                        <h5 class="modal-title">
                            <i class="fa-solid fa-registered me-1"></i>
                            {{ trans('registered-installation', {'%number%': fsc.telemetryManager.id()}) }}
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"
                                aria-label="{{ trans('close') }}"></button>
                    </div>
                    <div class="modal-body">
                        {% if fsc.justRegistered %}
                            <div class="alert alert-success">
                                <i class="fa-solid fa-circle-check fa-fw" aria-hidden="true"></i>
                                {{ trans('registered-installation-claim-p') }}
                            </div>
                        {% endif %}
                        <p>{{ trans('registered-installation-p') }}</p>
                        <p>{{ trans('telemetry-data-to-send') }}</p>
                        <a class="btn btn-primary float-start" href="{{ fsc.url() }}?action=claim-install"
                           target="_blank">
                            {% if fsc.justRegistered %}
                                <i class="fa-solid fa-arrow-up-right-from-square fa-fw fa-beat me-1" aria-hidden="true"></i>
                                {{ trans('finalize') }}
                            {% else %}
                                <i class="fa-solid fa-cog fa-fw me-1" aria-hidden="true"></i>
                                {{ trans('manage') }}
                            {% endif %}
                        </a>
                        {% if not fsc.justRegistered %}
                            <form action="{{ fsc.url() }}" method="post" onsubmit="animateSpinner('add')">
                                {{ formToken() }}
                                <input type="hidden" name="action" value="unlink"/>
                                <button type="submit" class="btn btn-danger btn-spin-action float-end">
                                    <i class="fa-solid fa-unlink fa-fw me-1" aria-hidden="true"></i>
                                    {{ trans('unlink') }}
                                </button>
                            </form>
                        {% endif %}
                    </div>
                {% else %}
                    <form action="{{ fsc.url() }}" method="post" onsubmit="animateSpinner('add')">
                        {{ formToken() }}
                        <input type="hidden" name="action" value="register"/>
                        <div class="modal-header">
                            <h5 class="modal-title">
                                <i class="fa-solid fa-registered me-1"></i> {{ trans('register-installation') }}
                            </h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal"
                                    aria-label="{{ trans('close') }}"></button>
                        </div>
                        <div class="modal-body">
                            <p>{{ trans('register-installation-p') }}</p>
                            <p>{{ trans('telemetry-data-to-send') }}</p>
                            <button type="submit" class="btn btn-primary btn-spin-action w-100">
                                <i class="fa-solid fa-arrow-right fa-beat me-1" aria-hidden="true"></i>
                                {{ trans('register-installation') }}
                            </button>
                        </div>
                    </form>
                {% endif %}
            </div>
        </div>
    </div>
    {% if fsc.coreUpdateWarnings %}
        <div class="modal fade" id="coreWarningModal" tabindex="-1" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">
                            <i class="fa-solid fa-exclamation-triangle me-1" aria-hidden="true"></i>
                            {{ trans('core-update-warning') }}
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <ul class="mb-0">
                            {% for message in fsc.coreUpdateWarnings %}
                                <li>{{ message }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    <div class="modal-footer">
                        {% for item in fsc.updaterItems %}
                            {% if item.name == 'CORE' and fsc.coreUpdateWarnings %}
                                <div class="btn-group">
                                    <a href="{{ fsc.url() }}?action=download&item={{ item.id }}&disable={{ fsc.coreUpdateWarnings | keys | join(',') }}&multireqtoken={{ formToken(false) }}"
                                       class="btn btn-spin-action btn-warning"
                                       onclick="animateSpinner('add')">
                                        <i class="fa-solid fa-download fa-fw me-1" aria-hidden="true"></i>{{ trans('download') }}
                                    </a>
                                    <button type="button"
                                            class="btn btn-spin-action btn-warning dropdown-toggle dropdown-toggle-split"
                                            data-bs-toggle="dropdown" aria-expanded="false">
                                        <span class="visually-hidden">{{ trans('options') }}</span>
                                    </button>
                                    <div class="dropdown-menu dropdown-menu-end">
                                        <a href="{{ fsc.url() }}?action=download&item={{ item.id }}&multireqtoken={{ formToken(false) }}"
                                           class="btn btn-warning"
                                           onclick="animateSpinner('add')">
                                            <i class="fa-solid fa-download fa-fw me-1" aria-hidden="true"></i>{{ trans('do-not-disable-plugins') }}
                                        </a>
                                    </div>
                                </div>
                            {% endif %}
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>
    {% endif %}
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    {% if fsc.justRegistered %}
        <script>
            // tras registrar, mostramos el modal para que el usuario pulse el botón de
            // vincular: el claim necesita la cookie del portal, así que lo tiene que abrir
            // el navegador desde un click real (window.open automático lo bloquean)
            document.addEventListener('DOMContentLoaded', function () {
                const modalEl = document.getElementById('modalTelemetry');
                if (modalEl) {
                    new bootstrap.Modal(modalEl).show();
                }
            });
        </script>
    {% endif %}
{% endblock %}
