{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU 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 General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% extends "pages/setup/general/base_form.html.twig" %}
{% import 'components/form/fields_macros.html.twig' as fields %}
{% import 'components/form/basic_inputs_macros.html.twig' as inputs %}

{% block config_fields %}
<div class="hr-text">
    <i class="ti ti-brand-mysql"></i>
    <span>{{ __('Replica configuration') }}</span>
</div>

<div class="row ps-4">
    {{ fields.textField('_dbreplicate_dbhost', replica_config['host'], __('SQL server (MariaDB or MySQL)')) }}
    {{ fields.textField('_dbreplicate_dbdefault', replica_config['default'], _n('Database', 'Databases', 1)) }}

    {{ fields.textField('_dbreplicate_dbuser', replica_config['user'], __('SQL user')) }}
    {{ fields.passwordField('_dbreplicate_dbpassword', replica_config['password'], __('SQL password'), {
        clearable: false
    }) }}

    {% set replica_search_opts = {
        0: __('Never'),
        1: __('If synced (all changes)'),
        2: __('If synced (current user changes)'),
        3: __('If synced or read-only account'),
        4: __('Always'),
    } %}
    {{ fields.dropdownArrayField('use_slave_for_search', config['use_slave_for_search'], replica_search_opts, __('Use the replica for the search engine')) }}

    {{ inputs.hidden('_dbslave_status', 1) }}
</div>

<div class="hr-text">
    <i class="ti ti-check"></i>
    <span>{{ __('Replication status') }}</span>
</div>

{% set source_ok      = replication_status['source'] is defined and replication_status['source']['file'] is defined %}
{% set source_unknown = replication_status['source'] is defined and replication_status['source']['error'] is defined and replication_status['source']['error'] is not null %}

<div class="row ps-2 mb-3">
   <div class="col-5">
        <div class="accordion" id="accordion-source">
            <div class="accordion-item">
                <div class="accordion-header" id="heading-source">
                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-source" aria-expanded="false" aria-controls="collapse-source">
                    <h3>
                        <span class="status status-{{ source_ok ? 'green' : source_unknown ? 'muted' : 'red' }}">
                            <span class="status-dot status-dot-animated"></span>
                            {{ source_ok ? _x('status', 'Up') : source_unknown ? _x('status', 'Unknown') : _x('status', 'Down') }}
                        </span>
                        <span class="mx-2">{{ __('Source') }} ({{ source_dbhost }})</span>
                        {% if not source_unknown %}
                            {% if replication_status['source']['read_only'] == 'OFF' %}
                                <span class="badge badge-outline text-green">{{ __('Writable') }}</span>
                            {% else %}
                                <span class="badge badge-outline text-blue">{{ __('Read-Only') }}</span>
                            {% endif %}
                        {% endif %}
                    </h3>
                    </button>
                </div>
                <div id="collapse-source" class="accordion-collapse collapse" aria-labelledby="heading-source" data-bs-parent="#accordion-source">
                    <div class="accordion-body">
                    {% if source_unknown %}
                        <div class="alert alert-danger" role="alert">
                            <span class="text-secondary">{{ __('MySQL server returned an error:') }}</span>
                            <p class="mt-2">{{ replication_status['source']['error'] }}</p>
                        </div>
                    {% else %}
                        <ul class="p-0 list-unstyled space-y-3">
                            <li>
                                <span class="text-secondary">{{ __('Server ID:') }}</span>
                                {{ replication_status['source']['server_id']|default('') }}
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('Binary log file:') }}</span>
                                {{ replication_status['source']['file']|default('') }}
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('Binary log position:') }}</span>
                                {{ replication_status['source']['position']|default('') }}
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('Server version:') }}</span>
                                {{ replication_status['source']['version']|default('') }}
                            </li>
                        </ul>
                    {% endif %}
                    </div>
                </div>
            </div>
        </div>
   </div>
   <div class="col-7">
        {% for replica_num, replica_data in replication_status['replica'] %}

            {# Replica is unknown when there is an error (connection, missing grants, ...) #}
            {% set replica_unkown = replica_data['error'] is defined and replica_data['error'] is not null %}


            {# Replica is OK when both IO and SQL are running and seconds_behind_source is 0 #}
            {% set replica_ok = not replica_unkown and replica_data['io_running'] == 'Yes' and replica_data['sql_running'] == 'Yes' and replica_data['seconds_behind_source'] == 0 %}

        <div class="accordion mb-2"
             id="accordion-replica{{ replica_num }}">
            <div class="accordion-item">
                <div class="accordion-header" id="heading-replica{{ replica_num }}">

                    <button class="accordion-button collapsed" type="button"
                            data-bs-toggle="collapse" data-bs-target="#collapse-replica{{ replica_num }}"
                            aria-expanded="false" aria-controls="collapse-replica{{ replica_num }}">
                    <h3>
                        <span class="status status-{{ replica_ok ? 'green' : replica_unkown ? 'muted' : 'red' }}">
                            <span class="status-dot status-dot-animated"></span>
                            {{ replica_ok ? _x('status', 'Up') : replica_unkown ? _x('status', 'Unknown') : _x('status', 'Down') }}
                        </span>
                        <span class="mx-1">{{ __('Replica') }} ({{ replica_data['host'] }})</span>
                        {% if not replica_unkown %}
                            {% if replica_data['read_only'] == 'OFF' %}
                                <span class="badge badge-outline text-green">{{ __('Writable') }}</span>
                            {% else %}
                                <span class="badge badge-outline text-blue">{{ __('Read-Only') }}</span>
                            {% endif %}
                        {% endif %}
                    </h3>
                    </button>
                </div>
                <div id="collapse-replica{{ replica_num }}" class="accordion-collapse collapse" aria-labelledby="heading-replica{{ replica_num }}" data-bs-parent="#accordion-replica{{ replica_num }}">
                    <div class="accordion-body">
                    {% if replica_unkown %}
                        <div class="alert alert-danger" role="alert">
                            <span class="text-secondary">{{ __('MySQL server returned an error:') }}</span>
                            <p class="mt-2">{{ replica_data['error'] }}</p>
                        </div>
                    {% else %}
                        <ul class="p-0 list-unstyled space-y-3">
                            <li>
                                <span class="text-secondary">{{ __('Server ID:') }}</span>
                                {{ replica_data['server_id']|default(__('Undefined')) }}
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('Source binary log file:') }}</span>
                                {{ replica_data['source_log_file']|default(__('Undefined')) }}
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('Source binary log position:') }}</span>
                                {{ replica_data['source_log_pos']|default(__('Undefined')) }}
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('Seconds behind source:') }}</span>
                                {% if replica_data['seconds_behind_source'] is defined and replica_data['seconds_behind_source'] is not null %}
                                    <span class="badge bg-{{ replica_data['seconds_behind_source'] > 0 ? 'red' : 'green' }}-lt">{{ replica_data['seconds_behind_source'] }}</span>
                                {% else %}
                                    <span class="badge bg-red-lt">{{ __('Undefined') }}</span>
                                {% endif %}
                            </li>
                            <li>
                                <span class="text-secondary" id="glpi_delay_{{ replica_num }}">{{ __('GLPI history delay:') }}</span>
                                {% if replication_delay[replica_num] is defined %}
                                    {% if replication_delay[replica_num] > 0 %}
                                        <span class="badge bg-red-lt">{{ replication_delay[replica_num]|formatted_duration }}</span>
                                    {% elseif replication_delay[replica_num] == 0 %}
                                        <span class="badge bg-green-lt">{{ __('None') }}</span>
                                    {% else %}
                                        <span class="badge bg-red-lt">{{ __('Undefined') }}</span>
                                    {% endif %}
                                {% else %}
                                    <span class="badge bg-red-lt">{{ __('Undefined') }}</span>
                                {% endif %}
                                {% do call('Html::showToolTip', [
                                    __('This indicates the delay between the source and the replica for GLPI history (table: glpi_logs, column: date_mod).')
                                ]) %}
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('IO running:') }}</span>
                                <span class="badge bg-{{ replica_data['io_running'] == 'Yes' ? 'green' : 'red' }}-lt">
                                {{ replica_data['io_running']|default(__('Undefined')) }}
                                </span>
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('SQL running:') }}</span>
                                <span class="badge bg-{{ replica_data['sql_running'] == 'Yes' ? 'green' : 'red' }}-lt">
                                {{ replica_data['sql_running']|default(__('Undefined')) }}
                                </span>
                            </li>
                            <li>
                                <span class="text-secondary">{{ __('Server version:') }}</span>
                                {{ replica_data['version']|default(__('Undefined')) }}
                            </li>
                        </ul>

                        {# Display errors if io_running or sql_running are not running #}
                        {% set display_alert = (replica_data['io_running'] != 'Yes' or replica_data['sql_running'] != 'Yes') and (replica_data['last_io_error']|length or replica_data['last_sql_error']|length) %}
                        {% if display_alert %}
                            <div class="alert alert-danger" role="alert">
                                <span class="text-secondary">{{ __('MySQL server returned an error:') }}</span>
                                <p class="mt-2">
                                    {% if replica_data['last_io_error'] is defined and replica_data['last_io_error'] is not empty %}
                                        <strong>{{ __('IO Error:') }}</strong> {{ replica_data['last_io_error'] }}<br>
                                    {% endif %}
                                    {% if replica_data['last_sql_error'] is defined and replica_data['last_sql_error'] is not empty %}
                                        <strong>{{ __('SQL Error:') }}</strong> {{ replica_data['last_sql_error'] }}
                                    {% endif %}
                                </p>
                            </div>
                        {% endif %}
                    {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
   </div>
</div>
{% endblock %}
