{#
 # @author    SWM
 # @copyright 2025 SWM
 # @license   https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 or later
 #}
{% extends "admin/layout.html.twig" %}

{% block content %}
    <h2 class="packaging-page-title">
        {{ trans('m3d.packaging.info.title') }}: {{ carrier.name }}
        {{ help_trigger('packaging')|raw }}
    </h2>

    <div class="notice notice-info packaging-info-notice">
        <p>
            <span class="dashicons dashicons-info packaging-info-icon"></span>
            <span>
                {{ trans('m3d.packaging.info.description') }}<br>
                {{ trans('m3d.packaging.info.description2') }}
            </span>
        </p>
    </div>

    <div class="packaging-add-button-wrapper">
        <a href="?page=tdbinpack-settings&tab=packaging&carrier_id={{ carrier.id_carrier_3dbp }}&action=add" class="button button-primary">
            {{ trans('m3d.packaging.action.add_new') }}
        </a>
    </div>

    {% if message %}
        <div class="notice notice-{{ messageType }} is-dismissible packaging-flash-message">
            <p>{{ message }}</p>
        </div>
    {% endif %}

    {% if showCostWarning %}
        <div class="notice notice-warning packaging-cost-warning-notice">
            <p>
                <span class="dashicons dashicons-warning" style="color: #f0ad4e; font-size: 20px; vertical-align: middle;"></span>
                <strong style="vertical-align: middle;">
                    {{ trans('m3d.carrier.warning.cost_mode_packagings')|replace({'%carrier_name%': carrier.name}) }}
                </strong>
            </p>
        </div>
    {% endif %}

    <table class="wp-list-table widefat fixed striped packaging-table">
        <thead>
            <tr>
                <th class="packaging-col-type">{{ trans('m3d.packaging.column.type') }}</th>
                <th class="packaging-col-name">{{ trans('m3d.packaging.column.name') }}</th>
                <th class="packaging-col-dimension">{{ trans('m3d.packaging.column.width_short') }}<br>({{ dimensionUnit }})</th>
                <th class="packaging-col-dimension">{{ trans('m3d.packaging.column.height_short') }}<br>({{ dimensionUnit }})</th>
                <th class="packaging-col-dimension">{{ trans('m3d.packaging.column.depth_short') }}<br>({{ dimensionUnit }})</th>
                <th class="packaging-col-quantity">{{ trans('m3d.packaging.column.quantity') }}</th>
                <th class="packaging-col-weight">{{ trans('m3d.packaging.column.weight') }}<br>({{ weightUnit }})</th>
                <th class="packaging-col-weight">{{ trans('m3d.packaging.column.max_weight') }}<br>({{ weightUnit }})</th>
                <th class="packaging-col-cost">{{ trans('m3d.packaging.column.cost') }}<br>({{ currency }})</th>
                <th class="packaging-col-status">{{ trans('m3d.packaging.column.status') }}</th>
                <th class="packaging-col-comment">{{ trans('m3d.packaging.column.comment') }}</th>
                <th class="packaging-col-actions">{{ trans('m3d.label.actions') }}</th>
            </tr>
        </thead>
        <tbody>
            {% if packagings|length > 0 %}
                {% for packaging in packagings %}
                    <tr>
                        <td>
                            {% if packaging.type == 'bin' %}
                                {{ trans('m3d.packaging.type.bin') }}
                            {% elseif packaging.type == 'pallet' %}
                                {{ trans('m3d.packaging.type.pallet') }}
                            {% elseif packaging.type == 'crate' %}
                                {{ trans('m3d.packaging.type.crate') }}
                            {% elseif packaging.type == 'container' %}
                                {{ trans('m3d.packaging.type.container') }}
                            {% elseif packaging.type == 'vehicle' %}
                                {{ trans('m3d.packaging.type.vehicle') }}
                            {% endif %}
                        </td>
                        <td><strong>{{ packaging.name }}</strong></td>
                        <td class="packaging-cell-dimension">{{ packaging.width }}</td>
                        <td class="packaging-cell-dimension">{{ packaging.height }}</td>
                        <td class="packaging-cell-dimension">{{ packaging.depth }}</td>
                        <td class="packaging-cell-quantity">{{ packaging.quantity ?? '-' }}</td>
                        <td class="packaging-cell-weight">{{ packaging.weight ?? '-' }}</td>
                        <td class="packaging-cell-weight">{{ packaging.max_weight ?? '-' }}</td>
                        <td class="packaging-cell-cost">
                            {{ packaging.cost ?? '-' }}
                            {% if carrier.packing_optimisation_mode == 'cost' and (packaging.cost is empty or packaging.cost <= 0) %}
                                <span class="dashicons dashicons-warning" 
                                      style="color: #dc3232; margin-left: 5px; vertical-align: middle;" 
                                      title="{{ trans('m3d.packaging.warning.cost_missing') }}"></span>
                            {% endif %}
                        </td>
                        <td>
                            {% if packaging.active %}
                                {{ trans('m3d.label.active') }}
                            {% else %}
                                {{ trans('m3d.label.inactive') }}
                            {% endif %}
                        </td>
                        <td class="packaging-comment" title="{{ packaging.comment ? packaging.comment : '' }}">{{ packaging.comment ? packaging.comment : '—' }}</td>
                        <td class="packaging-actions">
                            <a href="?page=tdbinpack-settings&tab=packaging&carrier_id={{ carrier.id_carrier_3dbp }}&action=edit&id={{ packaging.id_packaging_3dbp }}" 
                               class="packaging-action-icon"
                               title="{{ trans('m3d.action.edit') }}">
                                <span class="dashicons dashicons-edit"></span>
                            </a>
                            <form method="post" 
                                  action="?page=tdbinpack-settings&tab=packaging&carrier_id={{ carrier.id_carrier_3dbp }}&action=delete&id={{ packaging.id_packaging_3dbp }}" 
                                  class="tdbinpack-delete-form"
                                  data-confirm-message="{{ trans('m3d.packaging.delete_confirm') }}"
                                  style="display: inline;">
                                <input type="hidden" name="_wpnonce" value="{{ wp_create_nonce('delete_packaging_' ~ packaging.id_packaging_3dbp) }}">
                                <button type="submit" 
                                        class="packaging-action-icon button-link"
                                        title="{{ trans('m3d.action.delete') }}"
                                        style="background: none; border: none; padding: 0; cursor: pointer;">
                                    <span class="dashicons dashicons-trash"></span>
                                </button>
                            </form>
                        </td>
                    </tr>
                {% endfor %}
            {% else %}
                <tr>
                    <td colspan="12" class="packaging-empty-state">
                        {{ trans('m3d.packaging.info.empty') }}
                    </td>
                </tr>
            {% endif %}
        </tbody>
    </table>

    <div class="packaging-back-link">
        <a href="?page=tdbinpack-settings&tab=carriers">
            <span class="dashicons dashicons-arrow-left-alt2"></span> {{ trans('m3d.action.back_to_carriers') }}
        </a>
    </div>
    
    {{ help_dialog('packaging')|raw }}

    <link rel="stylesheet" href="{{ pluginUrl }}assets/css/admin-common.css">
    <link rel="stylesheet" href="{{ pluginUrl }}assets/css/admin-packaging.css">
    <script src="{{ pluginUrl }}assets/js/admin-common.js"></script>
{% endblock %}
