{#
 # @author    SWM
 # @copyright 2025 SWM
 # @license   https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 or later
 #}
<div id="tdbinpacking-simulation-container" class="tdbinpacking-meta-box">
    {% include 'admin/_translations_script.html.twig' %}
    
    {% if api_configured %}
    <div id="tdbinpacking-order-api-verification-result"></div>
    {% endif %}
    
    <div id="tdbinpacking-messages"></div>

    {% if not api_configured %}
    <div class="notice notice-warning inline">
        <p>
            <span class="dashicons dashicons-warning" style="color: #f0ad4e; font-size: 20px; vertical-align: middle;"></span>
            <strong>{{ trans('m3d.order.message.api_not_configured') }}</strong>
        </p>
        <p>
            {{ trans('m3d.order.message.api_not_configured_desc') }}
        </p>
        <p>
            <a href="{{ api_config_url }}">
                {{ trans('m3d.order.button.configure_api') }}
            </a>
        </p>
    </div>
    {% endif %}

    {% if active_packagings == 0 %}
    <div class="notice notice-warning inline">
        <p>
            <span class="dashicons dashicons-warning" style="color: #f0ad4e; font-size: 20px; vertical-align: middle;"></span>
            <strong style="vertical-align: middle;">
                {{ trans('m3d.order.simulation.no_packagings_title') }}
            </strong>
        </p>
        <p>
            {{ trans('m3d.order.simulation.no_packagings') }}
        </p>
        <p>
            <a href="{{ carriers_url }}">
                {{ trans('m3d.order.simulation.goto_carriers') }}
            </a>
        </p>
    </div>
    {% endif %}

    {% if products_without_dimensions is not empty %}
        {{ include('admin/order/dimension_form.html.twig', {
            order_id: order_id,
            products_without_dimensions: products_without_dimensions,
            nonce: nonce
        }) }}
    {% endif %}

    {% if logistics_changes is defined and logistics_changes is not empty %}
    <div class="tdbinpacking-logistics-warning notice notice-warning inline">
        <div class="logistics-warning-content">
            <span class="dashicons dashicons-warning"></span>
            <div>
                <div class="warning-title">
                    <strong>{{ trans('m3d.changes.title') }}</strong>
                </div>
                <ul class="logistics-changes-list">
                    {% for change in logistics_changes %}
                        <li>
                            <strong>{{ change.product_name }}</strong>
                            <ul class="product-changes">
                                {% for diff in change.diffs %}
                                    <li>
                                        {% if diff.field == 'product_name' %}
                                            {{ trans('m3d.changes.field.name') }}
                                        {% elseif diff.field == 'width' %}
                                            {{ trans('m3d.changes.field.width') }}
                                        {% elseif diff.field == 'height' %}
                                            {{ trans('m3d.changes.field.height') }}
                                        {% elseif diff.field == 'depth' %}
                                            {{ trans('m3d.changes.field.depth') }}
                                        {% elseif diff.field == 'weight' %}
                                            {{ trans('m3d.changes.field.weight') }}
                                        {% elseif diff.field == 'vr' %}
                                            {{ trans('m3d.changes.field.vr') }}
                                        {% else %}
                                            {{ diff.field }}
                                        {% endif %}:
                                        
                                        {% if diff.field == 'vr' %}
                                            {% if diff.old %}{{ trans('m3d.label.yes') }}{% else %}{{ trans('m3d.label.no') }}{% endif %}
                                            →
                                            {% if diff.new %}{{ trans('m3d.label.yes') }}{% else %}{{ trans('m3d.label.no') }}{% endif %}
                                        {% else %}
                                            {{ diff.old }} → {{ diff.new }}
                                        {% endif %}
                                    </li>
                                {% endfor %}
                            </ul>
                        </li>
                    {% endfor %}
                </ul>
                <div class="warning-footer">
                    {{ trans('m3d.changes.footer') }}
                </div>
            </div>
        </div>
    </div>
    {% endif %}

    {% if products_without_dimensions is not empty %}
    <div id="tdbinpacking-save-button-wrapper" class="tdbinpacking-dimensions-actions">
        <button type="button" 
                id="tdbinpacking-save-all-dimensions"
                class="button button-primary"
                data-order-id="{{ order_id }}"
                data-nonce="{{ nonce }}">
            {{ trans('m3d.dimension_form.save_and_simulate') }}
        </button>
    </div>
    {% endif %}

    <div class="tdbinpacking-actions" style="text-align: right; margin: 15px 0;">
        {% if products_without_dimensions is empty %}
        <button 
            id="tdbinpacking-execute-btn" 
            class="button button-primary"
            style="float: right;"
            data-order-id="{{ order_id }}"
            data-nonce="{{ nonce }}"
            {% if not can_execute %}
                disabled="disabled"
                {% if tooltip %}
                    title="{{ tooltip }}"
                {% endif %}
            {% endif %}>
            {{ trans('m3d.order.button.run_simulation') }}
        </button>
        {% endif %}
    </div>
    <div style="clear: both;"></div>

    <h3 style="margin: 20px 0 10px 0;">
        {{ trans('m3d.order.simulation_results_title') }}
        {{ help_trigger('simulation', false)|raw }}
    </h3>

    <div id="tdbinpacking-loading" class="tdbinpacking-loading">
        <span class="spinner is-active"></span>
        <span class="tdbinpacking-loading-text">{{ trans('m3d.order.message.simulation_running') }}</span>
    </div>

    <div id="tdbinpacking-results">
        {% if has_simulations %}
            {{ include('admin/order/simulation_results.html.twig', {
                simulations: simulations,
                products_without_dimensions: products_without_dimensions,
                dimensionUnit: dimensionUnit,
                weightUnit: weightUnit
            }) }}
        {% endif %}
    </div>
    
    {{ help_dialog('simulation')|raw }}
</div>
