<?php
/**
 * Provide a admin area view for the plugin settings
 *
 * @link       https://yourwebsite.com
 * @since      1.0.0
 *
 * @package    Subscription_System
 * @subpackage Subscription_System/admin/partials
 */

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

// Define tabs
$tabs = array(
    'general'        => __('General', 'subscription-system'),
    'payment'        => __('Payment', 'subscription-system'),
    'communication'  => __('Communication', 'subscription-system'),
    'pages'          => __('Pages Settings', 'subscription-system'),
    'subscribe_modal' => __('Subscribe Button & Modal', 'subscription-system'),
);

// Initialize current tab with proper sanitization
$current_tab = 'general'; // Default to 'general'
if (isset($_GET['tab'])) {
    $tab = sanitize_text_field(wp_unslash($_GET['tab']));
    // Validate that the tab exists in our allowed tabs
    if (array_key_exists($tab, $tabs)) {
        $current_tab = $tab;
    }
}
?>

<div class="subscription-system-admin-page">
    <div class="wrap">
        <h1 class="wp-heading-inline"><?php echo esc_html__('Subscription System Settings', 'subscription-system'); ?></h1>
        <hr class="wp-header-end">
        
        <?php settings_errors('subscription_messages'); ?>
        
        <div class="nav-tab-wrapper wp-clearfix">
            <?php foreach ($tabs as $tab => $name) : ?>
                <a href="?post_type=subscription&page=subscription-settings&tab=<?php echo esc_attr($tab); ?>" 
                   class="nav-tab <?php echo $current_tab === $tab ? 'nav-tab-active' : ''; ?>">
                    <?php echo esc_html($name); ?>
                </a>
            <?php endforeach; ?>
        </div>
        
        <div class="metabox-holder">
            <div class="postbox">
                <div class="postbox-header">
                    <h2 class="hndle"><?php echo esc_html($tabs[$current_tab] . ' Settings', 'subscription-system'); ?></h2>
                </div>
                <div class="inside">
                    <form method="post" action="options.php">
                        <?php
                        settings_fields('subs_sys_settings');
                        do_settings_sections('subscription-settings-' . $current_tab);
                        ?>
                        
                    </form>
                    
                    <?php
                    // Include the respective settings display file based on the current tab
                    switch ($current_tab) {
                        case 'general':
                            include 'general-settings-display.php';
                            break;
                        case 'payment':
                            include 'payment-settings-display.php';
                            break;
                        case 'communication':
                            include 'communication-settings-display.php';
                            break;
                        case 'pages':
                            include 'pages-settings-display.php';
                            break;
                        case 'subscribe_modal':
                            include 'subscribe-modal-settings-display.php';
                            break;
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
/* Additional styling for settings page */
.form-table th {
    padding: 20px 10px 20px 0;
    width: 200px;
}

.form-table td {
    padding: 15px 10px;
}

.form-table select {
    min-width: 300px;
}

.submit-wrapper {
    border-top: 1px solid #ddd;
    padding-top: 15px;
    margin-top: 15px;
}

.nav-tab-wrapper {
    margin-bottom: 0;
    border-bottom: 1px solid #c3c4c7;
}

.nav-tab {
    background: #f0f0f1;
    border: 1px solid #c3c4c7;
    border-bottom: none;
    color: #646970;
}

.nav-tab:hover {
    background: #f6f7f7;
    color: #135e96;
}

.nav-tab-active, 
.nav-tab-active:hover {
    background: #fff;
    color: #000;
    border-bottom: 1px solid #fff;
}

.postbox {
    border: 1px solid #c3c4c7;
    box-shadow: 0 1px 1px rgba(0,0,0,.04);
    background: #fff;
}

.postbox-header {
    border-bottom: 1px solid #c3c4c7;
    background: #f6f7f7;
}

.postbox .hndle {
    padding: 8px 12px;
    margin: 0;
    font-size: 14px;
    color: #1d2327;
}

.postbox .inside {
    padding: 0 12px 12px;
    margin: 0;
    background: #fff;
}

.form-table {
    margin-top: 15px;
}

/* Style for the notice in payment tab */
.notice.inline {
    margin: 15px 0 5px;
}

/* Style for the submit button */
.wp-core-ui .button-primary {
    background: #2271b1;
    border-color: #2271b1;
    color: #fff;
}

.wp-core-ui .button-primary:hover {
    background: #135e96;
    border-color: #135e96;
    color: #fff;
}

/* Style for checkboxes and inputs */
input[type=checkbox]:checked::before {
    color: #2271b1;
}

input[type=text]:focus, 
select:focus {
    border-color: #2271b1;
    box-shadow: 0 0 0 1px #2271b1;
}
</style> 