<?php
// Initialize default values for option_arr if not set
$option_arr = isset($tpl['option_arr']) ? $tpl['option_arr'] : array();
$datetime_format = isset($option_arr['o_datetime_format']) && !empty($option_arr['o_datetime_format'])
    ? $option_arr['o_datetime_format']
    : 'd-m-Y H:i';
$install_url = isset($option_arr['o_install_url']) ? $option_arr['o_install_url'] : '';
// page_prefix is not needed for clean URLs.

// Status labels - same as backend
$status_labels = __('order_statuses', true);
if (!is_array($status_labels)) {
    $status_labels = array(
        'new' => 'New (not paid)',
        'pending' => 'Pending',
        'completed' => 'Completed',
        'cancelled' => 'Cancelled'
    );
}

$payment_methods_arr = isset($tpl['payment_titles']) && is_array($tpl['payment_titles']) ? $tpl['payment_titles'] : __('payment_methods', true);
if (!is_array($payment_methods_arr)) {
    $payment_methods_arr = array();
}
$actions_label = __('front_order_actions', true);
if (empty($actions_label)) {
    $actions_label = 'Actions';
}

// Status badge classes — semantic, mapped to Template styling colours via CSS
function getStatusBadgeClass($status)
{
    switch ($status) {
        case 'completed':
            return 'sc-status-success';
        case 'pending':
            return 'sc-status-brand-dark';
        case 'new':
            return 'sc-status-accent';
        case 'cancelled':
        case 'refunded':
            return 'sc-status-error';
        case 'processing':
            return 'sc-status-processing';
        default:
            return 'sc-status-brand';
    }
}

?>
<div class="container-fluid pjScOrdersHistory">
    <h2 class="text-uppercase"><strong><?php __('front_orders_history'); ?></strong></h2><br>

    <?php if (isset($tpl['order_arr']) && !empty($tpl['order_arr'])) { ?>
        <div class="table-responsive sc-oh-table-wrap">
            <table class="table table-striped table-hover sc-oh-table">
                <thead>
                    <tr>
                        <th class="sc-oh-col-order"><?php __('front_order_number'); ?></th>
                        <th class="sc-oh-col-items"><?php __('order_products'); ?></th>
                        <th class="sc-oh-col-address"><?php __('front_order_tab_shipping_details'); ?></th>
                        <th class="sc-oh-col-payment"><?php __('order_payment'); ?></th>
                        <th class="sc-oh-col-total"><?php __('front_order_total'); ?></th>
                        <th class="sc-oh-col-status"><?php __('front_order_status'); ?></th>
                        <th class="sc-oh-col-actions"><?php echo pjSanitize::html($actions_label); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($tpl['order_arr'] as $order) { ?>
                        <tr>
                            <td class="sc-oh-order-cell" data-label="<?php __('front_order_number'); ?>">
                                <a href="<?php echo $install_url; ?>/order/<?php echo pjSanitize::html($order['uuid']); ?>" class="sc-oh-order-link scSelectorOrderDetails">
                                    <?php echo pjSanitize::html($order['uuid']); ?>
                                </a>
                                <div class="sc-oh-meta"><?php echo date($datetime_format, strtotime($order['created'])); ?></div>
                                <?php
                                $order_notes = isset($order['notes']) ? (string) $order['notes'] : '';
                                $order_notes = trim(preg_replace('/<br\\s*\\/?>\\s*<b>(Deprecated|Warning|Notice)<\\/b>:[\\s\\S]*?<br\\s*\\/?>/i', '', $order_notes));
                                if (!empty($order_notes)) { ?>
                                    <div class="sc-oh-note"><?php echo pjSanitize::html($order_notes); ?></div>
                                <?php } ?>
                            </td>

                            <td class="sc-oh-items-cell" data-label="<?php __('order_products'); ?>">
                                <?php
                                if (!empty($order['products_text'])) {
                                    echo '<div class="sc-oh-products-text">' . nl2br(pjSanitize::html($order['products_text'])) . '</div>';
                                } else {
                                    echo '<span class="text-muted">-</span>';
                                }
                                ?>
                            </td>

                            <td class="sc-oh-address-cell" data-label="<?php __('front_order_tab_shipping_details'); ?>">

                                <?php
                                $s_firstname = isset($order['s_firstname']) ? trim((string)$order['s_firstname']) : '';
                                $s_lastname  = isset($order['s_name']) ? trim((string)$order['s_name']) : '';
                                $s_fullname  = trim($s_firstname . ' ' . $s_lastname);

                                $s_address_1 = isset($order['s_address_1']) ? trim((string)$order['s_address_1']) : '';
                                $s_house_nr  = isset($order['s_house_number']) ? trim((string)$order['s_house_number']) : '';
                                $s_zip       = isset($order['s_zip']) ? trim((string)$order['s_zip']) : '';
                                $s_city      = isset($order['s_city']) ? trim((string)$order['s_city']) : '';
                                $s_country   = isset($order['s_country']) ? trim((string)$order['s_country']) : '';

                                $s_street = trim($s_address_1 . (!empty($s_house_nr) ? ' ' . $s_house_nr : ''));
                                ?>

                                <div class="sc-oh-address-block">

                                    <div class="sc-oh-address-title">
                                        <?php __('front_order_tab_shipping_details'); ?>
                                    </div>

                                    <?php if (!empty($s_fullname)) { ?>
                                        <div class="sc-oh-name">
                                            <?php echo pjSanitize::html($s_fullname); ?>
                                        </div>
                                    <?php } ?>

                                    <?php if (!empty($s_street)) { ?>
                                        <div class="sc-oh-line">
                                            <?php echo pjSanitize::html($s_street); ?>
                                        </div>
                                    <?php } ?>

                                    <?php
                                    $shipping_location = array();

                                    if (!empty($s_zip)) {
                                        $shipping_location[] = pjSanitize::html($s_zip);
                                    }

                                    if (!empty($s_city)) {
                                        $shipping_location[] = pjSanitize::html($s_city);
                                    }

                                    if (!empty($shipping_location)) { ?>
                                        <div class="sc-oh-line">
                                            <?php echo implode(' ', $shipping_location); ?>
                                        </div>
                                    <?php } ?>

                                    <?php if (!empty($s_country)) { ?>
                                        <div class="sc-oh-country">
                                            <?php echo pjSanitize::html($s_country); ?>
                                        </div>
                                    <?php } ?>

                                </div>

                                <?php
                                $b_firstname = isset($order['b_firstname']) ? trim((string)$order['b_firstname']) : '';
                                $b_lastname  = isset($order['b_name']) ? trim((string)$order['b_name']) : '';
                                $b_fullname  = trim($b_firstname . ' ' . $b_lastname);

                                $b_address_1 = isset($order['b_address_1']) ? trim((string)$order['b_address_1']) : '';
                                $b_house_nr  = isset($order['b_house_number']) ? trim((string)$order['b_house_number']) : '';
                                $b_zip       = isset($order['b_zip']) ? trim((string)$order['b_zip']) : '';
                                $b_city      = isset($order['b_city']) ? trim((string)$order['b_city']) : '';
                                $b_country   = isset($order['b_country']) ? trim((string)$order['b_country']) : '';

                                $b_street = trim($b_address_1 . (!empty($b_house_nr) ? ' ' . $b_house_nr : ''));
                                ?>

                                <div class="sc-oh-address-block sc-oh-address-block--billing">

                                    <div class="sc-oh-address-title">
                                        <?php __('front_order_tab_billing_details'); ?>
                                    </div>

                                    <?php if (!empty($b_fullname)) { ?>
                                        <div class="sc-oh-name">
                                            <?php echo pjSanitize::html($b_fullname); ?>
                                        </div>
                                    <?php } ?>

                                    <?php if (!empty($b_street)) { ?>
                                        <div class="sc-oh-line">
                                            <?php echo pjSanitize::html($b_street); ?>
                                        </div>
                                    <?php } ?>

                                    <?php
                                    $billing_location = array();

                                    if (!empty($b_zip)) {
                                        $billing_location[] = pjSanitize::html($b_zip);
                                    }

                                    if (!empty($b_city)) {
                                        $billing_location[] = pjSanitize::html($b_city);
                                    }

                                    if (!empty($billing_location)) { ?>
                                        <div class="sc-oh-line">
                                            <?php echo implode(' ', $billing_location); ?>
                                        </div>
                                    <?php } ?>

                                    <?php if (!empty($b_country)) { ?>
                                        <div class="sc-oh-country">
                                            <?php echo pjSanitize::html($b_country); ?>
                                        </div>
                                    <?php } ?>

                                </div>

                            </td>

                            <td class="sc-oh-payment-cell" data-label="<?php __('order_payment'); ?>">
                                <?php
                                $payment_method = isset($order['payment_method']) ? $order['payment_method'] : '';
                                echo pjSanitize::html(isset($payment_methods_arr[$payment_method]) ? $payment_methods_arr[$payment_method] : ucfirst(str_replace('_', ' ', $payment_method)));
                                ?>
                            </td>

                            <td class="sc-oh-total-cell" data-label="<?php __('front_order_total'); ?>">
                                <div class="sc-oh-price-row"><span><?php __('front_sub_total'); ?></span><strong><?php echo pjCurrency::formatPrice(isset($order['price']) ? $order['price'] : 0); ?></strong></div>
                                <?php if ((float) @$order['discount'] > 0) { ?>
                                    <div class="sc-oh-price-row"><span><?php __('order_discount'); ?></span><strong>-<?php echo pjCurrency::formatPrice($order['discount']); ?></strong></div>
                                <?php } ?>
                                <?php if ((float) @$order['insurance'] > 0) { ?>
                                    <div class="sc-oh-price-row"><span><?php __('front_order_insurance'); ?></span><strong><?php echo pjCurrency::formatPrice($order['insurance']); ?></strong></div>
                                <?php } ?>
                                <?php if ((float) @$order['shipping'] > 0) { ?>
                                    <div class="sc-oh-price-row"><span><?php __('front_shipping'); ?></span><strong><?php echo pjCurrency::formatPrice($order['shipping']); ?></strong></div>
                                <?php } ?>
                                <?php if ((float) @$order['tax'] > 0) { ?>
                                    <div class="sc-oh-price-row"><span><?php __('front_tax'); ?></span><strong><?php echo pjCurrency::formatPrice($order['tax']); ?></strong></div>
                                <?php } ?>
                                <div class="sc-oh-price-row sc-oh-price-row--total"><span><?php __('front_total'); ?></span><strong><?php echo pjCurrency::formatPrice(isset($order['total']) ? $order['total'] : 0); ?></strong></div>
                            </td>

                            <td class="sc-oh-status-cell" data-label="<?php __('front_order_status'); ?>">
                                <?php
                                $status = isset($order['status']) ? $order['status'] : 'pending';
                                $status_label = isset($status_labels[$status]) ? $status_labels[$status] : ucfirst(str_replace('_', ' ', $status));
                                $badge_class = getStatusBadgeClass($status);
                                ?>
                                <span class="sc-status-label <?php echo $badge_class; ?>"><?php echo pjSanitize::html($status_label); ?></span>
                            </td>

                            <td class="text-right" data-label="<?php echo pjSanitize::html($actions_label); ?>">

                                <div class="sc-oh-actions">

                                    <!-- <?php if (isset($order['pay_link']) && $order['pay_link'] === true && !empty($order['payment_retry_url'])) { ?>
                                        <a href="<?php echo $install_url; ?>/pay/<?php echo (int) $order['id']; ?>/<?php echo pjSanitize::html($order['payment_method']); ?>"
                                            class="sc-oh-btn sc-oh-btn--pay scSelectorPayOrder"
                                            data-order-id="<?php echo (int) $order['id']; ?>"
                                            data-payment-method="<?php echo pjSanitize::html($order['payment_method']); ?>"
                                            data-tooltip="<?php echo __('front_order_pay_order', true) ?: 'Pay order'; ?>"
                                            title="<?php echo __('front_order_pay_order', true) ?: 'Pay order'; ?>"
                                            aria-label="<?php echo __('front_order_pay_order', true) ?: 'Pay order'; ?>">

                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none"
                                                stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                                stroke-linejoin="round">
                                                <rect x="1" y="4" width="22" height="16" rx="2" ry="2"></rect>
                                                <line x1="1" y1="10" x2="23" y2="10"></line>
                                            </svg>

                                            <span><?php echo __('front_order_pay_order', true) ?: 'Pay order'; ?></span>
                                        </a>
                                    <?php } ?> -->

                                    <?php if (isset($order['invoice_uuid']) && !empty($order['invoice_uuid'])) { ?>
                                        <a href="<?php echo PJ_INSTALL_URL; ?>index.php?controller=pjFrontPublic&amp;action=pjActionOrderInvoice&amp;uuid=<?php echo pjSanitize::html($order['uuid']); ?>"
                                            class="sc-oh-btn sc-oh-btn--invoice"
                                            data-tooltip="<?php echo __('front_print_invoice', true) ?: 'Invoice'; ?>"
                                            target="_blank"
                                            title="<?php echo __('front_print_invoice', true) ?: 'Invoice'; ?>"
                                            aria-label="<?php echo __('front_print_invoice', true) ?: 'Invoice'; ?>">

                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none"
                                                stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                                stroke-linejoin="round">
                                                <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
                                                <polyline points="14 2 14 8 20 8"></polyline>
                                                <line x1="16" y1="13" x2="8" y2="13"></line>
                                                <line x1="16" y1="17" x2="8" y2="17"></line>
                                                <polyline points="10 9 9 9 8 9"></polyline>
                                            </svg>

                                            <span><?php echo __('front_print_invoice', true) ?: 'Print invoice'; ?></span>
                                        </a>
                                    <?php } ?>

                                    <?php if (!empty($order['has_downloads'])) {
                                        $dl_label = __('front_digital_downloads_title', true) ?: 'Digital downloads';
                                    ?>
                                        <a href="<?php echo $install_url; ?>/order/<?php echo pjSanitize::html($order['uuid']); ?>#digital-downloads"
                                            class="sc-oh-btn sc-oh-btn--digital"
                                            data-tooltip="<?php echo pjSanitize::html($dl_label); ?>"
                                            title="<?php echo pjSanitize::html($dl_label); ?>"
                                            aria-label="<?php echo pjSanitize::html($dl_label); ?>">

                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none"
                                                stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                                stroke-linejoin="round">
                                                <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                                                <polyline points="7 10 12 15 17 10"></polyline>
                                                <line x1="12" y1="15" x2="12" y2="3"></line>
                                            </svg>

                                            <span><?php echo pjSanitize::html($dl_label); ?></span>
                                        </a>
                                    <?php } ?>

                                    <button class="sc-oh-btn sc-oh-btn--reorder pjScBtnReorder"
                                        data-tooltip="<?php echo __('front_button_reorder', true) ?: 'Reorder'; ?>"
                                        data-confirm-title="<?php echo __('front_reorder_confirm_title', true) ?: 'Reorder'; ?>"
                                        data-confirm-text="<?php echo __('front_reorder_confirm_text', true) ?: 'Add the same products from this order to your cart?'; ?>"
                                        data-confirm-button="<?php echo __('front_button_reorder', true) ?: 'Reorder'; ?>"
                                        data-cancel-button="<?php echo __('btnCancel', true) ?: 'Cancel'; ?>"
                                        data-id="<?php echo $order['id']; ?>"
                                        type="button"
                                        title="<?php echo __('front_button_reorder', true) ?: 'Reorder'; ?>"
                                        aria-label="<?php echo __('front_button_reorder', true) ?: 'Reorder'; ?>">

                                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none"
                                            stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                            stroke-linejoin="round">
                                            <polyline points="23 4 23 10 17 10"></polyline>
                                            <path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path>
                                        </svg>

                                        <span><?php echo __('front_button_reorder', true) ?: 'Reorder'; ?></span>
                                    </button>

                                    <a href="<?php echo $install_url; ?>/order/<?php echo pjSanitize::html($order['uuid']); ?>"
                                        class="sc-oh-btn sc-oh-btn--details scSelectorOrderDetails"
                                        data-tooltip="<?php echo __('front_view_order_details', true) ?: 'View details'; ?>"
                                        title="<?php echo __('front_view_order_details', true) ?: 'View details'; ?>"
                                        aria-label="<?php echo __('front_view_order_details', true) ?: 'View details'; ?>">

                                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none"
                                            stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                            stroke-linejoin="round">
                                            <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"></path>
                                            <circle cx="12" cy="12" r="3"></circle>
                                        </svg>

                                        <span><?php echo __('front_view_order_details', true) ?: 'View details'; ?></span>
                                    </a>

                                </div>

                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>

        <?php
        // Pagination
        if (isset($tpl['paginator']) && $tpl['paginator']['pages'] > 1) {
            $paginator = $tpl['paginator'];
        ?>
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <?php if ($paginator['page'] > 1) { ?>
                        <li>
                            <a href="<?php echo $install_url; ?>/orders?page=<?php echo ($paginator['page'] - 1); ?>" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    <?php } ?>

                    <?php for ($i = 1; $i <= $paginator['pages']; $i++) { ?>
                        <li<?php echo $i == $paginator['page'] ? ' class="active"' : ''; ?>>
                            <a href="<?php echo $install_url; ?>/orders?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                            </li>
                        <?php } ?>

                        <?php if ($paginator['page'] < $paginator['pages']) { ?>
                            <li>
                                <a href="<?php echo $install_url; ?>/orders?page=<?php echo ($paginator['page'] + 1); ?>" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        <?php } ?>
                </ul>
            </nav>
        <?php } ?>

    <?php } else { ?>
        <div class="alert alert-info">
            <?php __('front_orders_not_found'); ?>
        </div>
    <?php } ?>
</div>