<?php

/**
 * My Account - Subscriptions List Template
 *
 * @package ArraySubs
 * @var array $arraysubs_subscriptions Array of subscription post objects
 * @var int $arraysubs_total_subscriptions Total number of subscriptions
 * @var int $arraysubs_total_pages Total number of pages
 * @var int $arraysubs_current_page Current page number
 * @var int $arraysubs_per_page Items per page
 */

defined('ABSPATH') || exit;

// Ensure variables are defined (for backwards compatibility)
$arraysubs_total_subscriptions = isset($arraysubs_total_subscriptions) ? $arraysubs_total_subscriptions : count($arraysubs_subscriptions);
$arraysubs_total_pages = isset($arraysubs_total_pages) ? $arraysubs_total_pages : 1;
$arraysubs_current_page = isset($arraysubs_current_page) ? $arraysubs_current_page : 1;
$arraysubs_per_page = isset($arraysubs_per_page) ? $arraysubs_per_page : 10;

?>

<div class="arraysubs-my-account-subscriptions">
    <h2><?php esc_html_e('My Subscriptions', 'arraysubs'); ?></h2>

    <?php if (empty($arraysubs_subscriptions)) : ?>
        <div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
            <?php esc_html_e('You have no subscriptions yet.', 'arraysubs'); ?>
        </div>
    <?php else : ?>
        <table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table arraysubs-subscriptions-table">
            <thead>
                <tr>
                    <th class="subscription-product"><span class="nobr"><?php esc_html_e('Product', 'arraysubs'); ?></span></th>
                    <th class="subscription-status"><span class="nobr"><?php esc_html_e('Status', 'arraysubs'); ?></span></th>
                    <th class="subscription-next-payment"><span class="nobr"><?php esc_html_e('Next Payment', 'arraysubs'); ?></span></th>
                    <th class="subscription-total"><span class="nobr"><?php esc_html_e('Total', 'arraysubs'); ?></span></th>
                    <th class="subscription-actions"><span class="nobr"><?php esc_html_e('Actions', 'arraysubs'); ?></span></th>
                </tr>
            </thead>
            <tbody>
                <?php
                foreach ($arraysubs_subscriptions as $arraysubs_subscription_item) :
                    $arraysubs_subscription_id = $arraysubs_subscription_item->ID;
                    $arraysubs_product_id = get_post_meta($arraysubs_subscription_id, '_product_id', true);
                    $arraysubs_product = $arraysubs_product_id ? wc_get_product($arraysubs_product_id) : null;
                    $arraysubs_next_payment = get_post_meta($arraysubs_subscription_id, '_next_payment_date', true);
                    $arraysubs_recurring_amount = function_exists('arraysubs_get_subscription_effective_recurring_amount')
                        ? arraysubs_get_subscription_effective_recurring_amount($arraysubs_subscription_id)
                        : get_post_meta($arraysubs_subscription_id, '_recurring_amount', true);
                    $arraysubs_billing_interval = get_post_meta($arraysubs_subscription_id, '_billing_interval', true);
                    $arraysubs_billing_period = get_post_meta($arraysubs_subscription_id, '_billing_period', true);
                    $arraysubs_status = get_post_status($arraysubs_subscription_id);
                    $arraysubs_status_name = arraysubs_get_subscription_status_name($arraysubs_status);
                    $arraysubs_subscription_url = wc_get_endpoint_url('view-subscription', $arraysubs_subscription_id, wc_get_page_permalink('myaccount'));
                    $arraysubs_row_is_waiting_cancellation = function_exists('arraysubs_is_waiting_cancellation')
                        && arraysubs_is_waiting_cancellation($arraysubs_subscription_id);
                    $arraysubs_row_scheduled_cancel_date = $arraysubs_row_is_waiting_cancellation && function_exists('arraysubs_get_scheduled_cancel_date')
                        ? arraysubs_get_scheduled_cancel_date($arraysubs_subscription_id)
                        : null;
                ?>
                    <tr class="subscription subscription-<?php echo esc_attr($arraysubs_status); ?><?php echo $arraysubs_row_is_waiting_cancellation ? ' subscription-pending-cancel' : ''; ?>">
                        <td class="subscription-product" data-title="<?php esc_attr_e('Product', 'arraysubs'); ?>">
                            <a href="<?php echo esc_url($arraysubs_subscription_url); ?>">
                                #<?php echo esc_html($arraysubs_subscription_id); ?>
                            </a>
                            <div>
                                <?php echo $arraysubs_product ? esc_html($arraysubs_product->get_name()) : '—'; ?>
                            </div>
                        </td>
                        <td class="subscription-status" data-title="<?php esc_attr_e('Status', 'arraysubs'); ?>">
                            <span class="subscription-status-badge subscription-status-<?php echo esc_attr(str_replace('arraysubs-', '', $arraysubs_status)); ?>">
                                <?php echo esc_html($arraysubs_status_name); ?>
                            </span>
                            <?php if ($arraysubs_row_is_waiting_cancellation) : ?>
                                <span class="subscription-status-badge subscription-status-pending-cancel">
                                    <?php esc_html_e('Pending cancellation', 'arraysubs'); ?>
                                </span>
                            <?php endif; ?>
                        </td>
                        <td class="subscription-next-payment" data-title="<?php esc_attr_e('Next Payment', 'arraysubs'); ?>">
                            <?php
                            if ($arraysubs_row_is_waiting_cancellation) {
                                if (!empty($arraysubs_row_scheduled_cancel_date)) {
                                    printf(
                                        /* translators: %s: scheduled cancellation date */
                                        esc_html__('Cancels on %s', 'arraysubs'),
                                        esc_html(arraysubs_format_date_local($arraysubs_row_scheduled_cancel_date))
                                    );
                                } else {
                                    esc_html_e('Cancels at end of period', 'arraysubs');
                                }
                            } elseif (!empty($arraysubs_next_payment) && in_array($arraysubs_status, ['arraysubs-active', 'arraysubs-on-hold', 'arraysubs-trial'], true)) {
                                echo esc_html(arraysubs_format_date_local($arraysubs_next_payment));
                            } else {
                                echo '—';
                            }
                            ?>
                        </td>
                        <td class="subscription-total" data-title="<?php esc_attr_e('Total', 'arraysubs'); ?>">
                            <?php echo wp_kses_post(wc_price($arraysubs_recurring_amount ?: 0)); ?>
                            <small><?php echo esc_html(arraysubs_get_billing_schedule_string($arraysubs_billing_interval ?: 1, $arraysubs_billing_period ?: 'month')); ?></small>
                        </td>
                        <td class="subscription-actions" data-title="<?php esc_attr_e('Actions', 'arraysubs'); ?>">
                            <a href="<?php echo esc_url($arraysubs_subscription_url); ?>" class="woocommerce-button button view">
                                <?php esc_html_e('View', 'arraysubs'); ?>
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <?php if ($arraysubs_total_pages > 1) : ?>
            <nav class="woocommerce-pagination arraysubs-pagination">
                <?php
                $arraysubs_pagination_big = 999999999;
                $arraysubs_base_url = str_replace(
                    (string) $arraysubs_pagination_big,
                    '%#%',
                    esc_url_raw(wc_get_endpoint_url('subscriptions', $arraysubs_pagination_big, wc_get_page_permalink('myaccount')))
                );

                // Output pagination
                echo wp_kses_post(paginate_links([
                    'base'      => $arraysubs_base_url,
                    'format'    => '',
                    'current'   => $arraysubs_current_page,
                    'total'     => $arraysubs_total_pages,
                    'prev_text' => '&larr; ' . __('Previous', 'arraysubs'),
                    'next_text' => __('Next', 'arraysubs') . ' &rarr;',
                    'type'      => 'list',
                    'end_size'  => 1,
                    'mid_size'  => 2,
                ]));
                ?>
            </nav>
            <p class="arraysubs-pagination__info">
                <?php
                $arraysubs_start = (($arraysubs_current_page - 1) * $arraysubs_per_page) + 1;
                $arraysubs_end = min($arraysubs_current_page * $arraysubs_per_page, $arraysubs_total_subscriptions);
                printf(
                    /* translators: 1: start item, 2: end item, 3: total items */
                    esc_html__('Showing %1$d-%2$d of %3$d subscriptions', 'arraysubs'),
                    intval($arraysubs_start),
                    intval($arraysubs_end),
                    intval($arraysubs_total_subscriptions)
                );
                ?>
            </p>
        <?php endif; ?>

    <?php endif; ?>
</div>
