<?php

/**
 * Admin Settings Page Template
 * * Implements an extensible administrative interface that:
 * - Supports modular feature addition through action hooks
 * - Maintains clean separation between free and premium features
 * - Uses WordPress Settings API for configuration management
 * - Provides tab-based navigation for complex functionality sets
 * * Extension Points:
 * - attrua_before_admin_settings: Pre-content hook for notifications/headers
 * - attrua_admin_tabs: Tab rendering hook for all tabs (core and premium)
 * - attrua_tab_content_{tab_id}: Content rendering hook for specific tab
 * - attrua_after_admin_settings: Post-content hook for additional sections
 *
 * @package Attributes\Admin\Display
 * @since   1.2.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Determine if premium features are available
 * Premium plugin will filter this to true
 */
$has_premium_features = apply_filters('attributes_has_premium_features', false);

// Determine the active tab
$active_tab = isset($_GET['tab']) ? sanitize_key($_GET['tab']) : 'login';

if (isset($_GET['tab'])) {
    if (isset($_GET['_wpnonce']) && wp_verify_nonce(sanitize_key($_GET['_wpnonce']), 'attrua_tab_nonce')) {
        $active_tab = sanitize_key($_GET['tab']);
    }
}

$post_type = '';

$icon_url = ATTRUA_URL . 'assets/img/attrua-50.png';
?>

<div class="attrua-masthead">
    <div class="attrua-masthead-container">
        <div class="attrua-masthead-logo-container">
            <img class="attrua-masthead-logo" src="<?php echo esc_url($icon_url); ?>" alt="Attributes WP logo" width="50"> <?php echo esc_html(get_admin_page_title()); ?> <span class="attrua-version-number"><sup><?php echo esc_html(ATTRUA_VERSION); ?></sup><sub></sub></span>
        </div>
    </div>
</div>

<div class="attrua-wrapper attrua-login-page-wrapper">
    <?php
    /**
     * Pre-content hook for notifications or header content
     * 
     * @param bool $has_premium_features Whether premium features are available
     */
    do_action('attrua_before_admin_settings', $has_premium_features);
    ?>

    <nav class="attrua-nav-tabs">
        <a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=attributes-user-access&tab=login'), 'attrua_tab_nonce')); ?>" class="attrua-nav-tab <?php echo $active_tab === 'login' ? 'login attrua-nav-tab-active' : 'login'; ?>">
            <i class="ti ti-login"></i> <?php esc_html_e('Login', 'attributes-user-access'); ?>
        </a>

        <?php
        /**
         * Render admin tabs for all features
         * Core and Premium plugins implement this to add their tabs
         * 
         * @param string $active_tab Currently active tab
         */
        do_action('attrua_admin_tabs', $active_tab);
        ?>
    </nav>

    <div class="attrua-tab-content">
        <?php if ($active_tab === 'login'): ?>
            <div class="description">
                <div>
                    <h3><?php esc_html_e('Page Management', 'attributes-user-access'); ?></h3>
                    <?php echo esc_html__('Create and manage a custom login page. Use the provided shortcode to display the login form on your page.', 'attributes-user-access'); ?>
                </div>
                <div>
                    <h3><?php esc_html_e('Redirection', 'attributes-user-access'); ?></h3>
                    <?php echo esc_html__('Toggle redirection to automatically send users to your custom login page instead of the default WordPress login page.', 'attributes-user-access'); ?>
                </div>
            </div>

            <div id="attrua-pages-settings" class="attrua-settings-section">
                <form method="post" action="options.php" class="attrua-settings-form">
                    <?php settings_fields('attrua_pages_group'); ?>
                    <?php do_settings_sections('attributes-settings'); ?>

                    <table class="form-table attrua-pages-table" role="presentation">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Page Title', 'attributes-user-access'); ?></th>
                                <th><?php esc_html_e('Page Slug', 'attributes-user-access'); ?></th>
                                <th><?php esc_html_e('Shortcode', 'attributes-user-access'); ?></th>
                                <th><?php esc_html_e('Actions', 'attributes-user-access'); ?></th>
                                <th><?php esc_html_e('Redirect', 'attributes-user-access'); ?></th>
                            </tr>
                        </thead>
                        <?php do_action('attrua_render_page_rows', $has_premium_features); ?>
                    </table>
                </form>

                <?php wp_nonce_field('attrua_settings_action', '_wpnonce'); ?>
            </div>
        <?php endif; ?>

        <?php
        /**
         * Render tab-specific content
         * This action is specific to the current active tab
         * 
         * @param bool $has_premium_features Whether premium features are available
         */
        do_action("attrua_tab_content_{$active_tab}", $has_premium_features);
        ?>

        <?php
        /**
         * Post-content hook for additional sections
         * Premium plugin can add marketing or additional features
         * 
         * @param bool $has_premium_features Whether premium features are available
         */
        do_action('attrua_after_admin_settings', $has_premium_features);
        ?>
    </div>
</div>

<div class="attrua-footerline">
    <p>
        <a href="https://www.attributeswp.com" target="_blank">www.attributeswp.com</a>
    </p>
</div>