<?php
/**
 * Public user account subscriptions display.
 *
 * @since      1.0.0
 * @package    Subscription_System
 */

// If this file is called directly, abort.
if (!defined('ABSPATH')) {
    exit;
}

// Redirect if not logged in
if (!is_user_logged_in()) {
    wp_redirect(wp_login_url(get_permalink()));
    exit;
}

// Get current user ID
$current_user_id = get_current_user_id();

// Get currency symbol
$currency_symbols = array(
    'USD' => '$',
    'EUR' => '€',
    'GBP' => '£',
    'JPY' => '¥',
    'AUD' => '$',
    'CAD' => '$'
);

$settings = get_option('ss_settings', array(
    'currency' => 'USD',
    'currency_position' => 'before'
));

$currency_symbol = isset($currency_symbols[$settings['currency']]) ? $currency_symbols[$settings['currency']] : $settings['currency'];

// Format price with currency
function ss_format_price($price, $currency_symbol, $position = 'before') {
    if ($position === 'before') {
        return $currency_symbol . $price;
    } else {
        return $price . $currency_symbol;
    }
}

// Get subscriptions for the current user only
$subscriptions = isset($subscriptions) ? $subscriptions : array();
if (empty($subscriptions) && class_exists('Subs_Sys_DB')) {
    global $wpdb;
    $db = new Subs_Sys_DB();
    $subscriptions = $db->get_subscriptions_by_user_id($current_user_id);
}
?>

<div class="ss-account-container">
    <h2><?php esc_html_e('Your Subscriptions', 'subscription-system'); ?></h2>
    
    <?php if (empty($subscriptions)) : ?>
        <p><?php esc_html_e('You don\'t have any active subscriptions.', 'subscription-system'); ?></p>
        <p><a href="<?php echo esc_url(get_permalink(get_option('ss_plans_page', 0))); ?>" class="button"><?php esc_html_e('View Subscription Plans', 'subscription-system'); ?></a></p>
    <?php else : ?>
        <table class="ss-subscriptions-table">
            <thead>
                <tr>
                    <th><?php esc_html_e('Plan', 'subscription-system'); ?></th>
                    <th><?php esc_html_e('Status', 'subscription-system'); ?></th>
                    <th><?php esc_html_e('Start Date', 'subscription-system'); ?></th>
                    <th><?php esc_html_e('End Date', 'subscription-system'); ?></th>
                    <th><?php esc_html_e('Actions', 'subscription-system'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($subscriptions as $subscription) : ?>
                    <tr>
                        <td>
                            <?php
                            $plan = isset($plans[$subscription->plan_id]) ? $plans[$subscription->plan_id] : array('name' => $subscription->plan_id, 'price' => 0);
                            echo esc_html($plan['name']);
                            ?>
                        </td>
                        <td>
                            <?php
                            switch ($subscription->status) {
                                case 'active':
                                    esc_html_e('Active', 'subscription-system');
                                    break;
                                case 'cancelled':
                                    esc_html_e('Cancelled', 'subscription-system');
                                    break;
                                case 'expired':
                                    esc_html_e('Expired', 'subscription-system');
                                    break;
                                default:
                                    echo esc_html($subscription->status);
                                    break;
                            }
                            ?>
                        </td>
                        <td><?php echo esc_html(date_i18n(get_option('date_format'), strtotime($subscription->start_date))); ?></td>
                        <td>
                            <?php
                            if ($subscription->end_date) {
                                echo esc_html(date_i18n(get_option('date_format'), strtotime($subscription->end_date)));
                            } else {
                                esc_html_e('Unlimited', 'subscription-system');
                            }
                            ?>
                        </td>
                        <td>
                            <?php if ($subscription->status === 'active') : ?>
                                <a href="#" class="ss-cancel-subscription ss-cancel-button" data-id="<?php echo esc_attr($subscription->id); ?>"><?php esc_html_e('Cancel', 'subscription-system'); ?></a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div> 