<?php

/**
 * Order Subscriptions Display
 *
 * @package ArraySubs\Features\SubscriptionCheckout
 * @var \WC_Order $order Order object
 * @var array     $arraysubs_subscription_ids Array of subscription IDs
 */

defined('ABSPATH') || exit;

if (empty($arraysubs_subscription_ids) || !is_array($arraysubs_subscription_ids)) {
    return;
}
?>

<section class="arraysubs-order-subscriptions">
    <h2><?php esc_html_e('Related Subscriptions', 'arraysubs'); ?></h2>

    <table class="woocommerce-table woocommerce-table--order-subscriptions shop_table order_subscriptions">
        <thead>
            <tr>
                <th><?php esc_html_e('Subscription', 'arraysubs'); ?></th>
                <th><?php esc_html_e('Status', 'arraysubs'); ?></th>
                <th><?php esc_html_e('Next Payment', 'arraysubs'); ?></th>
                <th><?php esc_html_e('Total', 'arraysubs'); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($arraysubs_subscription_ids as $arraysubs_sub_id) : ?>
                <?php
                $arraysubs_sub_post = arraysubs_get_subscription($arraysubs_sub_id);
                if (!$arraysubs_sub_post) {
                    continue;
                }

                $arraysubs_next_payment = get_post_meta($arraysubs_sub_id, '_next_payment_date', true);
                $arraysubs_recurring_amount = get_post_meta($arraysubs_sub_id, '_recurring_amount', true);
                $arraysubs_currency = get_post_meta($arraysubs_sub_id, '_currency', true) ?: get_woocommerce_currency();
                $arraysubs_billing_period = get_post_meta($arraysubs_sub_id, '_billing_period', true);
                $arraysubs_billing_interval = get_post_meta($arraysubs_sub_id, '_billing_interval', true);

                $arraysubs_status_label = arraysubs_get_status_label($arraysubs_sub_post->post_status);
                $arraysubs_billing_schedule = arraysubs_get_billing_schedule_string((int) $arraysubs_billing_interval, $arraysubs_billing_period);
                $arraysubs_subscription_url = wc_get_endpoint_url('view-subscription', $arraysubs_sub_id, wc_get_page_permalink('myaccount'));
                ?>
                <tr>
                    <td>
                        <a href="<?php echo esc_url($arraysubs_subscription_url); ?>">
                            #<?php echo esc_html($arraysubs_sub_id); ?>
                        </a>
                    </td>
                    <td>
                        <span class="subscription-status status-<?php echo esc_attr($arraysubs_sub_post->post_status); ?>">
                            <?php echo esc_html($arraysubs_status_label); ?>
                        </span>
                    </td>
                    <td>
                        <?php
                        if (!empty($arraysubs_next_payment)) {
                            echo esc_html(arraysubs_format_date_local($arraysubs_next_payment, get_option('date_format')));
                        } else {
                            echo '—';
                        }
                        ?>
                    </td>
                    <td>
                        <?php echo wp_kses_post(wc_price($arraysubs_recurring_amount, ['currency' => $arraysubs_currency])); ?>
                        <small>/ <?php echo esc_html($arraysubs_billing_schedule); ?></small>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</section>