{#
  Copyright 2024-2026 Simply.IN Sp. z o.o.
  Licensed under the EUPL-1.2 or later.

  Renders the BO settings form with the classic PrestaShop HelperForm look
  (panels, col-lg-3/9 grid, prestashop-switch toggles) on top of the Symfony form.
#}
{% form_theme form '@Modules/simplyin/views/templates/admin/_form_theme.html.twig' %}

{{ form_start(form, {'attr': {'class': 'defaultForm form-horizontal', 'id': 'module_form'}}) }}

<div class="panel">
    <div class="panel-heading">
        <i class="icon-cogs"></i> {{ 'Settings'|trans({}, 'Modules.Simplyin.Admin') }}
    </div>
    <div class="form-wrapper">
        <div class="form-group">
            <label class="control-label col-lg-3" for="{{ form.environment.vars.id }}">
                {{ form.environment.vars.label|trans({}, 'Modules.Simplyin.Admin') }}
            </label>
            <div class="col-lg-9">
                {{ form_widget(form.environment) }}
                {% if form.environment.vars.help is defined and form.environment.vars.help %}
                    <p class="help-block">{{ form.environment.vars.help|trans({}, 'Modules.Simplyin.Admin') }}</p>
                {% endif %}
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-lg-3" for="{{ form.secret_key.vars.id }}">
                {{ form.secret_key.vars.label|trans({}, 'Modules.Simplyin.Admin') }}
            </label>
            <div class="col-lg-9">
                <div class="input-group simplyin-key-group">
                    {{ form_widget(form.secret_key, {'value': secret_key_value, 'attr': {'placeholder': 'Leave blank to keep the current key'|trans({}, 'Modules.Simplyin.Admin')}}) }}
                    <span class="input-group-btn">
                        <button type="button" class="btn btn-default simplyin-toggle" tabindex="-1" title="{{ 'Show / hide'|trans({}, 'Modules.Simplyin.Admin') }}">
                            <i class="material-icons">visibility</i>
                        </button>
                    </span>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-heading">
        <i class="icon-cogs"></i> {{ 'Carrier settings'|trans({}, 'Modules.Simplyin.Admin') }}
    </div>
    <div class="form-wrapper">
        {% for name in ['dpd_carrier_id', 'dhl_carrier_id', 'fedex_carrier_id', 'inpost_carrier_id', 'orlen_carrier_id', 'ups_carrier_id', 'default_carrier_id'] %}
            <div class="form-group">
                <label class="control-label col-lg-3" for="{{ form[name].vars.id }}">
                    {{ form[name].vars.label|trans({}, 'Modules.Simplyin.Admin') }}
                </label>
                <div class="col-lg-9">
                    {{ form_widget(form[name]) }}
                </div>
            </div>
        {% endfor %}
    </div>
</div>

<div class="panel">
    <div class="panel-heading">
        <i class="icon-cogs"></i> {{ 'Payment settings'|trans({}, 'Modules.Simplyin.Admin') }}
    </div>
    <div class="form-wrapper">
        {% for name in ['payments_enabled', 'payment_status_pending', 'payment_status_canceled', 'confirmation_heading_override'] %}
            <div class="form-group">
                <label class="control-label col-lg-3" for="{{ form[name].vars.id }}">
                    {{ form[name].vars.label|trans({}, 'Modules.Simplyin.Admin') }}
                </label>
                <div class="col-lg-9">
                    {{ form_widget(form[name]) }}
                    {% if form[name].vars.help is defined and form[name].vars.help %}
                        <p class="help-block">{{ form[name].vars.help|trans({}, 'Modules.Simplyin.Admin') }}</p>
                    {% endif %}
                </div>
            </div>
        {% endfor %}
    </div>
</div>

<div class="panel">
    <div class="panel-heading">
        <i class="icon-cogs"></i> {{ 'Additional settings'|trans({}, 'Modules.Simplyin.Admin') }}
    </div>
    <div class="form-wrapper">
        {% for name in ['save_checkbox', 'bar_enabled', 'bar_color', 'badge_color', 'accent_color', 'font_color'] %}
            <div class="form-group">
                <label class="control-label col-lg-3" for="{{ form[name].vars.id }}">
                    {{ form[name].vars.label|trans({}, 'Modules.Simplyin.Admin') }}
                </label>
                <div class="col-lg-9">
                    {% if name in ['accent_color', 'font_color'] %}
                        <div class="simplyin-color">
                            {{ form_widget(form[name], {'attr': {'class': 'simplyin-color-swatch'}}) }}
                            <input type="text" class="form-control simplyin-color-hex" value="{{ form[name].vars.value }}" maxlength="7" spellcheck="false" aria-label="{{ form[name].vars.label|trans({}, 'Modules.Simplyin.Admin') }}">
                        </div>
                    {% else %}
                        {{ form_widget(form[name]) }}
                    {% endif %}
                    {% if form[name].vars.help is defined and form[name].vars.help %}
                        <p class="help-block">{{ form[name].vars.help|trans({}, 'Modules.Simplyin.Admin') }}</p>
                    {% endif %}
                </div>
            </div>
        {% endfor %}

        <div class="form-group">
            <label class="control-label col-lg-3">
                {{ 'Cron URL'|trans({}, 'Modules.Simplyin.Admin') }}
            </label>
            <div class="col-lg-9">
                <input type="text" class="form-control" value="{{ cron_link }}" disabled>
                <p class="help-block">
                    {{ 'Below is the link to use when configuring cron on your server. Recommended frequency: once a day. The cron is used to update order statuses and settings.'|trans({}, 'Modules.Simplyin.Admin') }}
                </p>
            </div>
        </div>
    </div>
</div>

<div class="panel-footer">
    <button type="submit" name="submitSimplyinModule" class="btn btn-primary pull-right">
        <i class="process-icon-save"></i> {{ 'Save'|trans({}, 'Modules.Simplyin.Admin') }}
    </button>
</div>

{{ form_end(form) }}

{#
  The "generate your key in the merchant panel" link (help.tpl, rendered before this form,
  #simplyin-merchant-panel-link) is initialized server-side from the PERSISTED environment — that
  is wrong the moment a merchant switches the dropdown below without saving yet (e.g. picking Test
  to obtain its first key). Sync once on load too, not just on change: this is currently harmless
  no-op work (ConfigurationController::process() always rebuilds the form from persisted data, on
  a rejected submit included, so the select can never show something other than the persisted
  value on load today), but keeps the link correct by construction rather than by coincidence of
  that rebuild-on-every-POST behavior, should it ever change.
#}
<script type="text/javascript">
    (function () {
        var panelUrls = {{ merchant_panel_urls|json_encode|raw }};
        var select = document.getElementById('{{ form.environment.vars.id }}');
        var link = document.getElementById('simplyin-merchant-panel-link');
        if (!select || !link) {
            return;
        }
        var syncLink = function () {
            if (Object.prototype.hasOwnProperty.call(panelUrls, select.value)) {
                link.href = panelUrls[select.value];
            }
        };
        syncLink();
        select.addEventListener('change', syncLink);
    })();
</script>
