{# Template : Contact #}
{% extends 'layouts/admin.twig' %}

{% block content %}

    <ul class="breadcrumbs el">
        <li><a href="/admin/">Home</a> /</li>
        <li><a href="/shop/admin/orders/manage.orders.php">Manage Orders</a> /</li>
        <li class="end">{{ page_meta.title }}</li>
    </ul>

    <h1>{{ page_meta.title }}</h1>

    {% if is_mtc_admin %}
        <form id="label-settings-form" action="?action=save_label_settings&order_ids={{ order_ids.implode(',') }}" method="post">
            {{ csrf_token('label_settings')|raw }}
            <fieldset class="show" id="label-settings-fieldset">
                <h1><a class="show el">Label Settings (MTC only) <i class="fa fa-caret-down"></i></a></h1>

                <p>Defaults persist for everyone. Adjustments here apply to the next print and preview.</p>

                <div class="row">
                    <label for="label_settings_width">Width (mm)</label>
                    <input type="number" step="0.1" min="10" name="label_settings[width]" id="label_settings_width" value="{{ label_settings.width }}">
                </div>

                <div class="row">
                    <label for="label_settings_height">Height (mm)</label>
                    <input type="number" step="0.1" min="10" name="label_settings[height]" id="label_settings_height" value="{{ label_settings.height }}">
                </div>

                <div class="row">
                    <label for="label_settings_orientation">Orientation</label>
                    <select name="label_settings[orientation]" id="label_settings_orientation">
                        <option value="L"{% if label_settings.orientation == 'L' %} selected{% endif %}>Landscape</option>
                        <option value="P"{% if label_settings.orientation == 'P' %} selected{% endif %}>Portrait</option>
                    </select>
                </div>

                <div class="row">
                    <label for="label_settings_font_size">Body font (pt)</label>
                    <input type="number" step="0.5" min="4" name="label_settings[font_size]" id="label_settings_font_size" value="{{ label_settings.font_size }}">
                </div>

                <div class="row">
                    <label for="label_settings_footer_font_size">Footer font (pt)</label>
                    <input type="number" step="0.5" min="4" name="label_settings[footer_font_size]" id="label_settings_footer_font_size" value="{{ label_settings.footer_font_size }}">
                </div>

                <div class="row">
                    <label for="label_settings_bottom_space">Footer area (mm)</label>
                    <input type="number" step="0.5" min="4" name="label_settings[bottom_space]" id="label_settings_bottom_space" value="{{ label_settings.bottom_space }}">
                </div>

                <div class="row">
                    <label for="label_settings_margin_sides">Side margin (mm)</label>
                    <input type="number" step="0.5" min="0" name="label_settings[margin_sides]" id="label_settings_margin_sides" value="{{ label_settings.margin_sides }}">
                </div>

                <div class="row">
                    <label for="label_settings_margin_top">Top margin (mm)</label>
                    <input type="number" step="0.5" min="0" name="label_settings[margin_top]" id="label_settings_margin_top" value="{{ label_settings.margin_top }}">
                </div>

                <div class="row">
                    <label for="label_settings_address">Pharmacy address (legal)</label>
                    <input type="text" name="label_settings[address]" id="label_settings_address" value="{{ label_settings.address }}">
                </div>

                <div class="row">
                    <button type="submit" class="btn">Save Defaults</button>
                    <button type="button" class="btn" id="label-preview-btn">Preview Sample Label</button>
                </div>
            </fieldset>
        </form>
    {% endif %}

    <form id="label-print-form" action="?action=print_pharmacy_labels_multi&order_ids={{ order_ids.implode(',') }}" method="post" target="_blank">

        <div class="js-vue-environment">
            {% for order in orders %}
                {% for order_item in order.pharmacy_label_items %}
                    <print-labels-manager
                        :order-item="{{ order_item.toJson() }}"
                        :groups="{{ order_item.getCustomersForLabels() | json_encode }}"
                    ></print-labels-manager>
                {% endfor %}
            {% endfor %}
        </div>

        <div class="row">
            <button type="submit" class="btn">
                Print Labels
            </button>
        </div>

        <div class="row">

            {% if orders.length > 1 %}
                <a href="/shop/admin/orders/manage.orders.php" class="btn">
                    Back to Orders
                </a>
            {% else %}
                <a href="/shop/admin/orders/manage.order.php?id={{ orders.0.id }}" class="btn">
                    Back to Order
                </a>
            {% endif %}

            &nbsp;&nbsp;

            {% if prescriptions_to_print %}
                <a href="?action=print_prescriptions&order_ids={{ order_ids.implode(',') }}" class="btn" target="_blank">
                    Print Prescriptions
                </a>&nbsp;&nbsp;
            {% else %}
                <span title="Order items not approved yet." class="btn disabled">
                    Print Prescriptions
                </span>&nbsp;&nbsp;
            {% endif %}

            <a href="?action=print_split_labels&order_ids={{ order_ids.implode(',') }}" class="btn" target="_blank">
                Print Invoice
            </a>

        </div>

    </form>

    {% if is_mtc_admin %}
        <script>
            (function () {
                const settingsForm = document.getElementById('label-settings-form');
                const previewBtn = document.getElementById('label-preview-btn');
                if (!settingsForm || !previewBtn) return;

                previewBtn.addEventListener('click', function () {
                    const orig = settingsForm.action;
                    const origTarget = settingsForm.target;
                    settingsForm.action = '?action=preview_pharmacy_label&order_ids={{ order_ids.implode(',') }}';
                    settingsForm.target = '_blank';
                    settingsForm.submit();
                    settingsForm.action = orig;
                    settingsForm.target = origTarget;
                });
            })();
        </script>
    {% endif %}
{% endblock %}
