<?php
/**
 * Admin subscriptions list view.
 *
 * @since      1.0.0
 * @package    Subscription_System
 */

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

<div class="wrap ss-admin-container">
    <div class="ss-admin-header">
        <h1 class="ss-admin-title"><?php _e('All Subscriptions', 'subscription-system'); ?></h1>
        <a href="<?php echo admin_url('admin.php?page=subscription-system-add'); ?>" class="button button-primary"><?php _e('Add New', 'subscription-system'); ?></a>
    </div>
    
    <div class="ss-admin-filters">
        <form method="get">
            <input type="hidden" name="page" value="subscription-system">
            
            <select name="status">
                <option value=""><?php _e('All Statuses', 'subscription-system'); ?></option>
                <option value="active" <?php selected(isset($_GET['status']) && $_GET['status'] === 'active'); ?>><?php _e('Active', 'subscription-system'); ?></option>
                <option value="cancelled" <?php selected(isset($_GET['status']) && $_GET['status'] === 'cancelled'); ?>><?php _e('Cancelled', 'subscription-system'); ?></option>
                <option value="expired" <?php selected(isset($_GET['status']) && $_GET['status'] === 'expired'); ?>><?php _e('Expired', 'subscription-system'); ?></option>
            </select>
            
            <select name="plan_id">
                <option value=""><?php _e('All Plans', 'subscription-system'); ?></option>
                <?php foreach ($plans as $plan_id => $plan) : ?>
                    <option value="<?php echo esc_attr($plan_id); ?>" <?php selected(isset($_GET['plan_id']) && $_GET['plan_id'] === $plan_id); ?>><?php echo esc_html($plan['name']); ?></option>
                <?php endforeach; ?>
            </select>
            
            <button type="submit" class="button"><?php _e('Filter', 'subscription-system'); ?></button>
        </form>
    </div>
    
    <?php if (empty($subscriptions)) : ?>
        <p><?php _e('No subscriptions found.', 'subscription-system'); ?></p>
    <?php else : ?>
        <table class="ss-admin-table">
            <thead>
                <tr>
                    <th><?php _e('ID', 'subscription-system'); ?></th>
                    <th><?php _e('User', 'subscription-system'); ?></th>
                    <th><?php _e('Email', 'subscription-system'); ?></th>
                    <th><?php _e('Plan', 'subscription-system'); ?></th>
                    <th><?php _e('Status', 'subscription-system'); ?></th>
                    <th><?php _e('Start Date', 'subscription-system'); ?></th>
                    <th><?php _e('End Date', 'subscription-system'); ?></th>
                    <th><?php _e('Actions', 'subscription-system'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($subscriptions as $subscription) : ?>
                    <tr>
                        <td><?php echo esc_html($subscription->id); ?></td>
                        <td>
                            <?php
                            $user = get_user_by('id', $subscription->user_id);
                            if ($user) {
                                echo '<a href="' . esc_url(get_edit_user_link($user->ID)) . '">' . esc_html($user->display_name) . '</a>';
                            } else {
                                echo esc_html($subscription->user_id);
                            }
                            ?>
                        </td>
                        <td><?php echo esc_html($subscription->email); ?></td>
                        <td>
                            <?php
                            $plan = isset($plans[$subscription->plan_id]) ? $plans[$subscription->plan_id]['name'] : $subscription->plan_id;
                            echo esc_html($plan);
                            ?>
                        </td>
                        <td>
                            <select class="ss-status-select" data-id="<?php echo esc_attr($subscription->id); ?>">
                                <option value="active" <?php selected($subscription->status, 'active'); ?>><?php _e('Active', 'subscription-system'); ?></option>
                                <option value="cancelled" <?php selected($subscription->status, 'cancelled'); ?>><?php _e('Cancelled', 'subscription-system'); ?></option>
                                <option value="expired" <?php selected($subscription->status, 'expired'); ?>><?php _e('Expired', 'subscription-system'); ?></option>
                            </select>
                        </td>
                        <td><?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($subscription->start_date))); ?></td>
                        <td>
                            <?php
                            if ($subscription->end_date) {
                                echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($subscription->end_date)));
                            } else {
                                echo '—';
                            }
                            ?>
                        </td>
                        <td class="ss-admin-actions">
                            <?php if ($subscription->status === 'active') : ?>
                                <a href="#" class="button ss-admin-action" data-action="cancel" data-id="<?php echo esc_attr($subscription->id); ?>"><?php _e('Cancel', 'subscription-system'); ?></a>
                            <?php endif; ?>
                            <a href="#" class="button ss-admin-action" data-action="delete" data-id="<?php echo esc_attr($subscription->id); ?>"><?php _e('Delete', 'subscription-system'); ?></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <?php if ($total_pages > 1) : ?>
            <div class="ss-admin-pagination">
                <?php
                echo paginate_links(array(
                    'base' => add_query_arg('paged', '%#%'),
                    'format' => '',
                    'prev_text' => __('&laquo;', 'subscription-system'),
                    'next_text' => __('&raquo;', 'subscription-system'),
                    'total' => $total_pages,
                    'current' => $page
                ));
                ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>
</div> 