<?php
/**
 * Admin subscription plans 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 esc_html_e('Subscription Plans', 'subscription-system'); ?></h1>
        <button id="ss-add-plan" class="button button-primary"><?php esc_html_e('Add New Plan', 'subscription-system'); ?></button>
    </div>
    
    <?php if (isset($success_message)) : ?>
        <div class="ss-admin-message success">
            <?php echo esc_html($success_message); ?>
        </div>
    <?php endif; ?>
    
    <?php if (isset($error_message)) : ?>
        <div class="ss-admin-message error">
            <?php echo esc_html($error_message); ?>
        </div>
    <?php endif; ?>
    
    <form method="post">
        <?php wp_nonce_field('ss_save_plans', 'ss_nonce'); ?>
        
        <div class="ss-admin-plans">
            <?php foreach ($plans as $plan_id => $plan) : ?>
                <div class="ss-admin-plan">
                    <div class="ss-admin-plan-header">
                        <h3 class="ss-admin-plan-title"><?php echo esc_html($plan['name']); ?></h3>
                        <button type="button" class="button ss-remove-plan"><?php esc_html_e('Remove', 'subscription-system'); ?></button>
                    </div>
                    
                    <div class="ss-admin-form-row">
                        <label for="plans[<?php echo esc_attr($plan_id); ?>][name]"><?php esc_html_e('Plan Name', 'subscription-system'); ?></label>
                        <input type="text" id="plans[<?php echo esc_attr($plan_id); ?>][name]" name="plans[<?php echo esc_attr($plan_id); ?>][name]" value="<?php echo esc_attr($plan['name']); ?>" required>
                    </div>
                    
                    <div class="ss-admin-form-row">
                        <label for="plans[<?php echo esc_attr($plan_id); ?>][price]"><?php esc_html_e('Price', 'subscription-system'); ?></label>
                        <input type="number" id="plans[<?php echo esc_attr($plan_id); ?>][price]" name="plans[<?php echo esc_attr($plan_id); ?>][price]" value="<?php echo esc_attr($plan['price']); ?>" step="0.01" min="0" required>
                    </div>
                    
                    <div class="ss-admin-form-row">
                        <label for="plans[<?php echo esc_attr($plan_id); ?>][duration]"><?php esc_html_e('Duration (days)', 'subscription-system'); ?></label>
                        <input type="number" id="plans[<?php echo esc_attr($plan_id); ?>][duration]" name="plans[<?php echo esc_attr($plan_id); ?>][duration]" value="<?php echo esc_attr($plan['duration']); ?>" min="0" required>
                        <p class="description"><?php esc_html_e('Enter 0 for unlimited duration.', 'subscription-system'); ?></p>
                    </div>
                    
                    <div class="ss-admin-form-row">
                        <label for="plans[<?php echo esc_attr($plan_id); ?>][features]"><?php esc_html_e('Features', 'subscription-system'); ?></label>
                        <textarea id="plans[<?php echo esc_attr($plan_id); ?>][features]" name="plans[<?php echo esc_attr($plan_id); ?>][features]" rows="5"><?php echo esc_textarea(implode("\n", $plan['features'])); ?></textarea>
                        <p class="description"><?php esc_html_e('Enter one feature per line.', 'subscription-system'); ?></p>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        
        <div class="ss-admin-form-row">
            <button type="submit" name="ss_save_plans" class="button button-primary"><?php esc_html_e('Save Plans', 'subscription-system'); ?></button>
        </div>
    </form>
    
    <!-- Template for new plan -->
    <script type="text/template" id="ss-plan-template">
        <div class="ss-admin-plan">
            <div class="ss-admin-plan-header">
                <h3 class="ss-admin-plan-title"><?php esc_html_e('New Plan', 'subscription-system'); ?></h3>
                <button type="button" class="button ss-remove-plan"><?php esc_html_e('Remove', 'subscription-system'); ?></button>
            </div>
            
            <div class="ss-admin-form-row">
                <label for="plans[{plan_id}][name]"><?php esc_html_e('Plan Name', 'subscription-system'); ?></label>
                <input type="text" id="plans[{plan_id}][name]" name="plans[{plan_id}][name]" value="<?php esc_attr_e('New Plan', 'subscription-system'); ?>" required>
            </div>
            
            <div class="ss-admin-form-row">
                <label for="plans[{plan_id}][price]"><?php esc_html_e('Price', 'subscription-system'); ?></label>
                <input type="number" id="plans[{plan_id}][price]" name="plans[{plan_id}][price]" value="9.99" step="0.01" min="0" required>
            </div>
            
            <div class="ss-admin-form-row">
                <label for="plans[{plan_id}][duration]"><?php esc_html_e('Duration (days)', 'subscription-system'); ?></label>
                <input type="number" id="plans[{plan_id}][duration]" name="plans[{plan_id}][duration]" value="30" min="0" required>
                <p class="description"><?php esc_html_e('Enter 0 for unlimited duration.', 'subscription-system'); ?></p>
            </div>
            
            <div class="ss-admin-form-row">
                <label for="plans[{plan_id}][features]"><?php esc_html_e('Features', 'subscription-system'); ?></label>
                <textarea id="plans[{plan_id}][features]" name="plans[{plan_id}][features]" rows="5"><?php esc_attr_e("Feature 1\nFeature 2\nFeature 3", 'subscription-system'); ?></textarea>
                <p class="description"><?php esc_html_e('Enter one feature per line.', 'subscription-system'); ?></p>
            </div>
        </div>
    </script>
</div> 