{#
 # @author    SWM
 # @copyright 2025 SWM
 # @license   https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 or later
 #}
{% if simulations is empty %}
    <div class="tdbinpacking-no-results">
        <p>{{ trans('m3d.simulation.no_results') }}</p>
    </div>
{% else %}

<div id="tdbinpacking-simulation-top"></div>

{% if simulations|length >= 2 %}
<div class="tdbinpacking-sticky-nav">
    <a href="#tdbinpacking-simulation-top" class="tdbinpacking-nav-btn tdbinpacking-nav-btn-top" title="{{ trans('m3d.order.tooltip.back_to_top') }}">
        <span class="dashicons dashicons-arrow-up-alt2"></span>
    </a>
    {% for simulation in simulations %}
    <a href="#tdbinpacking-carrier-{{ loop.index }}" class="tdbinpacking-nav-btn">
        {{ simulation.carrier_name|default(trans('m3d.simulation.unknown_carrier')) }}
    </a>
    {% endfor %}
</div>
{% endif %}

<div class="tdbinpacking-results-wrapper">
    {% for simulation in simulations %}
    <div class="tdbinpacking-simulation-card tdbinpacking-carrier-card" id="tdbinpacking-carrier-{{ loop.index }}">
        
        <div class="tdbinpacking-simulation-header">
            <h3>
                {{ simulation.carrier_name|default(trans('m3d.simulation.unknown_carrier')) }}
            </h3>
            <div>
                {{ simulation.date_add|default('') }} | 
                <strong>{{ trans('m3d.simulation.bins_count') }}</strong>
                {{ simulation.bins_count|default(0) }}
            </div>
        </div>

        {% if simulation.status == 'success' %}
        <div class="tdbinpacking-stats-grid">
            
            <div class="tdbinpacking-stats-section">
                <h4>
                    {{ trans('m3d.simulation.general_stats') }}
                </h4>
                <table>
                    <tr>
                        <td>{{ trans('m3d.simulation.cargo_spaces') }}</td>
                        <td>{{ simulation.bins_count|default(0) }}</td>
                    </tr>
                    <tr>
                        <td>{{ trans('m3d.simulation.total_volume_used') }}</td>
                        <td>{{ simulation.total_volume_used|default(0)|number_format(2) }} %</td>
                    </tr>
                    <tr>
                        <td>{{ trans('m3d.simulation.packed_items_count') }}</td>
                        <td>{{ simulation.packed_items_count|default(0) }}</td>
                    </tr>
                    <tr>
                        <td>{{ trans('m3d.simulation.total_weight') }}</td>
                        <td>{{ simulation.total_weight|default(0)|number_format(0) }} {{ weightUnit|default('kg') }}</td>
                    </tr>
                    <tr>
                        <td>{{ trans('m3d.simulation.gross_weight') }}</td>
                        <td>{{ simulation.gross_weight|default(0)|number_format(0) }} {{ weightUnit|default('kg') }}</td>
                    </tr>
                </table>
            </div>

            <div class="tdbinpacking-stats-section">
                <h4>
                    {{ trans('m3d.simulation.cargo_spaces_total') }}
                </h4>
                {% if simulation.bins_aggregated is not empty %}
                    <table>
                        {% for binGroup in simulation.bins_aggregated %}
                        <tr>
                            <td>{{ binGroup.bin_id|default('') }}</td>
                            <td>{{ binGroup.bin_count|default(1) }}</td>
                        </tr>
                        {% endfor %}
                    </table>
                {% else %}
                    <p>{{ trans('m3d.simulation.no_data') }}</p>
                {% endif %}
            </div>

            <div class="tdbinpacking-stats-section">
                <h4>
                    {{ trans('m3d.simulation.packed_items_total') }}
                </h4>
                {% if simulation.packed_items_summary is not empty %}
                    <table>
                        {% for item in simulation.packed_items_summary %}
                        <tr>
                            <td>{{ item.product_name|default('') }}</td>
                            <td>{{ item.total_quantity|default(0) }}</td>
                        </tr>
                        {% endfor %}
                    </table>
                {% else %}
                    <p>{{ trans('m3d.simulation.all_items_packed') }}</p>
                {% endif %}
            </div>

            <div class="tdbinpacking-stats-section">
                <h4>
                    {{ trans('m3d.simulation.unpacked_items') }}
                </h4>
                {% set allUnpackedItems = {} %}
                
                {% if simulation.not_packed_aggregated is not empty %}
                    {% for item in simulation.not_packed_aggregated %}
                        {% set productName = item.product_name|default('') %}
                        {% if allUnpackedItems[productName] is not defined %}
                            {% set allUnpackedItems = allUnpackedItems|merge({
                                (productName): {
                                    'product_name': productName,
                                    'quantity': 0,
                                    'reason': 'not_packed_by_api'
                                }
                            }) %}
                        {% endif %}
                        {% set currentItem = allUnpackedItems[productName] %}
                        {% set allUnpackedItems = allUnpackedItems|merge({
                            (productName): {
                                'product_name': currentItem.product_name,
                                'quantity': currentItem.quantity + (item.quantity|default(0)),
                                'reason': currentItem.reason
                            }
                        }) %}
                    {% endfor %}
                {% endif %}
                
                {% if products_without_dimensions is not empty %}
                    {% for item in products_without_dimensions %}
                        {% set productName = item.product_name|default('') %}
                        {% if allUnpackedItems[productName] is not defined %}
                            {% set allUnpackedItems = allUnpackedItems|merge({
                                (productName): {
                                    'product_name': productName,
                                    'quantity': 0,
                                    'reason': 'missing_dimensions'
                                }
                            }) %}
                        {% else %}
                            {% set currentItem = allUnpackedItems[productName] %}
                            {% if currentItem.reason == 'not_packed_by_api' %}
                                {% set allUnpackedItems = allUnpackedItems|merge({
                                    (productName): {
                                        'product_name': currentItem.product_name,
                                        'quantity': currentItem.quantity,
                                        'reason': 'both'
                                    }
                                }) %}
                            {% endif %}
                        {% endif %}
                        {% set currentItem = allUnpackedItems[productName] %}
                        {% set allUnpackedItems = allUnpackedItems|merge({
                            (productName): {
                                'product_name': currentItem.product_name,
                                'quantity': currentItem.quantity + (item.quantity|default(0)),
                                'reason': currentItem.reason
                            }
                        }) %}
                    {% endfor %}
                {% endif %}
                
                {% if allUnpackedItems is not empty %}
                    <table>
                        {% for item in allUnpackedItems %}
                        <tr>
                            <td class="unpacked-item">{{ item.product_name }}</td>
                            <td class="unpacked-item">{{ item.quantity }}</td>
                        </tr>
                        {% endfor %}
                    </table>
                {% else %}
                    <p>{{ trans('m3d.simulation.all_items_packed') }}</p>
                {% endif %}
            </div>
        </div>

        {% if simulation.bins is not empty %}
        <div class="tdbinpacking-bins-grid">
            
            {% for binIndex, bin in simulation.bins %}
            <div class="tdbinpacking-bin-card">
                
                <div class="tdbinpacking-bin-header">
                    <h4>
                        <span>
                            {% set binNumber = binIndex + 1 %}
                            {% set totalBins = simulation.bins|length %}
                            {% set binType = bin.bin_type|default('bin') %}
                            
                            {% set typeTranslations = {
                                'bin': trans('m3d.simulation.bin'),
                                'pallet': trans('m3d.simulation.pallet'),
                                'crate': trans('m3d.simulation.crate'),
                                'container': trans('m3d.simulation.container'),
                                'vehicle': trans('m3d.simulation.vehicle')
                            } %}
                            
                            {% set binTypeLabel = typeTranslations[binType]|default(binType|capitalize) %}
                            
                            {{ binTypeLabel }}: {{ bin.bin_id|default('N/A') }} ({{ binNumber }} / {{ totalBins }})
                        </span>
                        {% if bin.comment is not empty %}
                        <span class="dashicons dashicons-info" 
                              title="{{ bin.comment }}"></span>
                        {% endif %}
                    </h4>
                </div>

                <div class="tdbinpacking-bin-stats">
                    <table>
                        <tr>
                            <td>{{ trans('m3d.simulation.packed') }}</td>
                            <td>
                                <strong>
                                    {% set packedCount = 0 %}
                                    {% if bin.items is not empty %}
                                        {% for item in bin.items %}
                                            {% set packedCount = packedCount + (item.quantity|default(1)) %}
                                        {% endfor %}
                                    {% endif %}
                                    {{ packedCount }}
                                </strong>
                            </td>
                            <td>{{ trans('m3d.simulation.weight') }}</td>
                            <td>
                                <strong>
                                    {{ bin.gross_weight|default(0)|number_format(0) }} [{{ weightUnit|default('kg') }}]
                                </strong>
                            </td>
                        </tr>
                        <tr>
                            <td>{{ trans('m3d.simulation.used_space') }}</td>
                            <td>
                                <strong>
                                    {{ bin.used_space|default(0)|number_format(2) }} %
                                </strong>
                            </td>
                            <td>{{ trans('m3d.simulation.net_weight') }}</td>
                            <td>
                                <strong>
                                    {{ bin.content_weight|default(0)|number_format(0) }} [{{ weightUnit|default('kg') }}]
                                </strong>
                            </td>
                        </tr>
                        <tr>
                            <td>{{ trans('m3d.simulation.stack_height') }}</td>
                            <td>
                                <strong>
                                    {{ bin.stack_height|default(0)|number_format(0) }} [{{ dimensionUnit|default('cm') }}]
                                </strong>
                            </td>
                            <td>{{ trans('m3d.simulation.dimensions') }}</td>
                            <td>
                                <strong>
                                    {{ bin.width|default(0)|number_format(0) }}×{{ bin.height|default(0)|number_format(0) }}×{{ bin.depth|default(0)|number_format(0) }} [{{ dimensionUnit|default('cm') }}]
                                </strong>
                            </td>
                        </tr>
                    </table>
                </div>

                {% if bin.image_complete is not empty %}
                <div class="tdbinpacking-bin-visualization">
                    <button type="button" 
                            class="button button-small tdbinpacking-show-visualization" 
                            data-bin-index="{{ binIndex }}"
                            data-image-url="{{ bin.image_complete }}">
                        <span class="dashicons dashicons-visibility"></span>
                        <span>{{ trans('m3d.simulation.show_visualization') }}</span>
                    </button>
                </div>
                {% endif %}

                {% if bin.items is not empty %}
                <div class="tdbinpacking-bin-items">
                    <table>
                        <thead>
                            <tr>
                                <th>{{ trans('m3d.simulation.packed_items') }}</th>
                                <th>{{ trans('m3d.simulation.dimensions_short') }}</th>
                                <th>{{ trans('m3d.simulation.weight_short') }}</th>
                                <th>{{ trans('m3d.simulation.quantity_short') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for item in bin.items %}
                            <tr>
                                <td>
                                    {{ item.product_name|default('') }}
                                    {% if item.combination_label is not empty %}
                                        <br><small>{{ item.combination_label }}</small>
                                    {% endif %}
                                </td>
                                <td>
                                    {{ item.width|default(0)|number_format(0) }}×{{ item.height|default(0)|number_format(0) }}×{{ item.depth|default(0)|number_format(0) }}
                                </td>
                                <td>
                                    {{ item.unit_weight|default(0)|number_format(1) }}
                                </td>
                                <td>
                                    <strong>{{ item.quantity|default(1) }}</strong>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% endif %}
            </div>
            {% endfor %}
        </div>
        {% endif %}

        {% if simulation.errors is not empty %}
        <div class="tdbinpacking-errors">
            <h4>
                <span class="dashicons dashicons-info"></span>
                {{ trans('m3d.simulation.api_messages') }}
            </h4>
            {% for error in simulation.errors %}
            <div>
                <strong>{{ error.error_level|default('info') }}:</strong>
                {{ error.error_message|default('') }}
            </div>
            {% endfor %}
        </div>
        {% endif %}

        {% else %}
        <div class="tdbinpacking-error">
            <p>
                <strong>{{ trans('m3d.simulation.error') }}</strong>
                {{ simulation.error_message|default(trans('m3d.simulation.unknown_error')) }}
            </p>
        </div>
        {% endif %}

    </div>
    {% endfor %}
</div>

{% endif %}
