<?php
/**
 * Account subscription tab template
 *
 * @link       https://yourwebsite.com
 * @since      1.0.0
 *
 * @package    Subscription_System
 * @subpackage Subscription_System/public/partials/account-tabs
 */

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

// Check if subscriptions are enabled and if pause button should be shown
$enable_subscriptions = get_option('enable_subscriptions');
$show_pause_button = get_option('show_pause_button');

// Get user subscriptions from database
global $wpdb;
$subscriptions_table = $wpdb->prefix . 'subscriptions';
$plans_table = $wpdb->prefix . 'subscription_plans';

// Get subscription information
$subscription = $wpdb->get_row($wpdb->prepare(
    "SELECT s.*, p.plan_name, p.duration 
     FROM $subscriptions_table s 
     LEFT JOIN $plans_table p ON s.plan_id = p.id 
     WHERE s.user_id = %d 
     ORDER BY s.id DESC 
     LIMIT 1",
    $user_id
));

// Get currency from options
$currency = get_option('subscription_currency', 'USD');

// Only show features if the column exists and has data
$features_column_exists = $wpdb->get_var("SHOW COLUMNS FROM $plans_table LIKE 'features'");
?>

<div class="account-tab-content subscription-tab">
    <h2><?php echo esc_html__('My Subscription', 'subscription-system'); ?></h2>
    <?php if ($subscription) : ?>
        <div class="subscription-details-card">
            <h3><?php echo esc_html__('Subscription Details', 'subscription-system'); ?></h3>
            <div class="subscription-info">
                <p>
                    <strong><?php echo esc_html__('Plan:', 'subscription-system'); ?></strong> 
                    <?php echo esc_html($subscription->plan_name); ?>
                </p>
                <p>
                    <strong><?php echo esc_html__('Status:', 'subscription-system'); ?></strong> 
                    <span class="subscription-status <?php echo esc_attr(strtolower($subscription->status)); ?>">
                        <?php echo esc_html(ucfirst($subscription->status)); ?>
                    </span>
                </p>
                <p>
                    <strong><?php echo esc_html__('Amount:', 'subscription-system'); ?></strong> 
                    <?php echo esc_html($currency . ' ' . number_format($subscription->amount, 2)); ?>
                </p>
                <p>
                    <strong><?php echo esc_html__('Current Period:', 'subscription-system'); ?></strong> 
                    <?php 
                    // Get the start date
                    $start_date = date_i18n(get_option('date_format'), strtotime($subscription->start_date));
                    
                    // Calculate the end date based on duration
                    $duration = isset($subscription->duration) ? intval($subscription->duration) : 30;
                    $end_timestamp = strtotime("+" . $duration . " days", strtotime($subscription->start_date));
                    $end_date = date_i18n(get_option('date_format'), $end_timestamp);
                    
                    // Get duration label
                    $duration_label = '';
                    switch ($duration) {
                        case 30:
                            $duration_label = __('Monthly', 'subscription-system');
                            break;
                        case 90:
                            $duration_label = __('Quarterly', 'subscription-system');
                            break;
                        case 180:
                            $duration_label = __('Biannually', 'subscription-system');
                            break;
                        case 365:
                            $duration_label = __('Annually', 'subscription-system');
                            break;
                        default:
                            // translators: %d: number of days between subscription renewals
                            $duration_label = sprintf(__('Every %d days', 'subscription-system'), $duration);
                    }
                    
                    echo esc_html($start_date . ' – ' . $end_date);
                    echo ' <span class="renewal-type">(' . esc_html($duration_label) . ')</span>';
                    ?>
                </p>
                
                <?php if ($features_column_exists && !empty($subscription->features)) : 
                    $features = maybe_unserialize($subscription->features);
                    if (is_array($features) && !empty($features)) : ?>
                    <div class="plan-features">
                        <strong><?php echo esc_html__('Plan Features:', 'subscription-system'); ?></strong>
                        <ul class="features-list">
                            <?php foreach ($features as $feature) : ?>
                                <li><span class="dashicons dashicons-yes"></span> <?php echo esc_html($feature); ?></li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                    <?php endif; ?>
                <?php endif; ?>
                
                <?php if (strtolower($subscription->status) === 'active') : ?>
                    <div class="subscription-actions">
                        <?php if ($show_pause_button) : ?>
                        <button class="subscription-system-btn subscription-system-btn-danger ss-pause-subscription" data-id="<?php echo esc_attr($subscription->id); ?>">
                            <?php echo esc_html__('PAUSE SUBSCRIPTION', 'subscription-system'); ?>
                        </button>
                        <?php endif; ?>
                        
                        <button class="subscription-system-btn subscription-system-btn-danger ss-cancel-subscription" data-id="<?php echo esc_attr($subscription->id); ?>">
                            <?php echo esc_html__('CANCEL SUBSCRIPTION', 'subscription-system'); ?>
                        </button>
                    </div>
                <?php elseif (strtolower($subscription->status) === 'paused') : ?>
                    <div class="subscription-actions">
                        <button class="subscription-system-btn subscription-system-btn-success ss-resume-subscription" data-id="<?php echo esc_attr($subscription->id); ?>">
                            <?php echo esc_html__('RESUME SUBSCRIPTION', 'subscription-system'); ?>
                        </button>
                        
                        <button class="subscription-system-btn subscription-system-btn-danger ss-cancel-subscription" data-id="<?php echo esc_attr($subscription->id); ?>">
                            <?php echo esc_html__('CANCEL SUBSCRIPTION', 'subscription-system'); ?>
                        </button>
                    </div>
                <?php elseif (strtolower($subscription->status) === 'cancelled' || strtolower($subscription->status) === 'canceled') : ?>
                    <div class="subscription-message">
                        <p><?php echo esc_html__('Your subscription has been cancelled.', 'subscription-system'); ?></p>
                        <a href="<?php echo esc_url(get_permalink(get_option('pricing_plans_page'))); ?>" class="subscription-system-btn subscription-system-btn-primary">
                            <?php echo esc_html__('View Plans', 'subscription-system'); ?>
                        </a>
                    </div>
                <?php endif; ?>
                
                <div class="subscription-history-link">
                    <a href="?tab=history" class="view-history-link">
                        <span class="dashicons dashicons-list-view"></span>
                        <?php echo esc_html__('View Subscription History', 'subscription-system'); ?>
                    </a>
                </div>
            </div>
        </div>
        
    <?php else : ?>
        <div class="no-subscription">
            <p><?php echo esc_html__('You don\'t have any active subscriptions.', 'subscription-system'); ?></p>
            <a href="<?php echo esc_url(get_permalink(get_option('pricing_plans_page'))); ?>" class="subscription-system-btn subscription-system-btn-primary">
                <?php echo esc_html__('View Plans', 'subscription-system'); ?>
            </a>
        </div>
    <?php endif; ?>
    
    <script>
    jQuery(document).ready(function($) {
        // Remove the old event handlers since we're using the ones in subscription-system-public.js
    });
    </script>
    
    <style>
    /* Enhanced subscription tab styling */
    .subscription-tab {
        max-width: 900px;
        margin: 0 auto;
    }
    
    .subscription-tab h2 {
        font-size: 28px;
        color: #23282d;
        margin-bottom: 25px;
        position: relative;
        padding-bottom: 15px;
        border-bottom: 2px solid #f0f0f1;
    }
    
    .subscription-tab h2:after {
        content: '';
        position: absolute;
        bottom: -2px;
        left: 0;
        width: 60px;
        height: 2px;
        background-color: #2271b1;
    }
    
    .subscription-details-card {
        background-color: #fff;
        border-radius: 8px;
        box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
        padding: 30px;
        margin-bottom: 30px;
        border: 1px solid #e5e5e5;
    }
    
    .subscription-details-card h3 {
        margin-top: 0;
        padding-bottom: 15px;
        border-bottom: 1px solid #e5e5e5;
        font-size: 20px;
        color: #23282d;
    }
    
    .subscription-info {
        margin-top: 20px;
    }
    
    .subscription-info p {
        margin-bottom: 15px;
        font-size: 15px;
        line-height: 1.6;
    }
    
    .subscription-info strong {
        display: inline-block;
        min-width: 150px;
        color: #23282d;
    }
    
    .plan-features {
        margin-top: 25px;
        padding-top: 20px;
        border-top: 1px solid #f0f0f1;
    }
    
    .features-list {
        margin: 15px 0 0 0;
        padding: 0;
        list-style: none;
    }
    
    .features-list li {
        margin-bottom: 10px;
        padding-left: 25px;
        position: relative;
    }
    
    .features-list .dashicons {
        position: absolute;
        left: 0;
        top: 2px;
        color: #0a7d33;
    }
    
    .subscription-actions {
        margin-top: 30px;
        display: flex;
        gap: 15px;
    }
    
    .subscription-system-btn-warning {
        background-color: #fff8e6;
        color: #b78105;
        border: 1px solid #ffeeba;
        font-weight: bold;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        padding: 12px 24px;
    }
    
    .subscription-system-btn-warning:hover {
        background-color: #fff5d8;
        color: #b78105;
        box-shadow: 0 2px 5px rgba(183, 129, 5, 0.15);
    }
    
    .subscription-system-btn-success {
        background-color: #e3f9e5;
        color: #0a7d33;
        border: 1px solid #a3e9b7;
        font-weight: bold;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        padding: 12px 24px;
    }
    
    .subscription-system-btn-success:hover {
        background-color: #d1f7d7;
        color: #0a7d33;
        box-shadow: 0 2px 5px rgba(10, 125, 51, 0.15);
    }
    
    .subscription-message {
        margin-top: 20px;
        padding: 20px;
        background-color: #f9f9f9;
        border-radius: 4px;
        border: 1px solid #e5e5e5;
    }
    
    .subscription-message p {
        margin-bottom: 15px;
    }
    
    .subscription-history-link {
        margin-top: 30px;
        padding-top: 20px;
        border-top: 1px solid #f0f0f1;
    }
    
    .view-history-link {
        display: inline-flex;
        align-items: center;
        color: #2271b1;
        text-decoration: none;
        font-weight: 500;
        transition: color 0.2s ease;
    }
    
    .view-history-link:hover {
        color: #135e96;
        text-decoration: underline;
    }
    
    .view-history-link .dashicons {
        margin-right: 5px;
        font-size: 18px;
        width: 18px;
        height: 18px;
    }
    
    .no-subscription {
        text-align: center;
        padding: 50px 30px;
        background-color: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        border: 1px solid #e5e5e5;
    }
    
    .no-subscription p {
        margin-bottom: 20px;
        font-size: 16px;
        color: #646970;
    }
    
    /* Responsive adjustments */
    @media (max-width: 768px) {
        .subscription-tab h2 {
            font-size: 22px;
        }
        
        .subscription-details-card {
            padding: 20px;
        }
        
        .subscription-info strong {
            min-width: 120px;
        }
        
        .subscription-actions {
            flex-direction: column;
            gap: 10px;
        }
        
        .subscription-system-btn {
            width: 100%;
            text-align: center;
        }
    }
    </style>
</div> 