<?php
/**
 * Cookie Consent & Policy Module
 */

if (!defined('ABSPATH')) {
    exit;
}

class Nenghui_Cookie_Consent {
    private $options;

    public function __construct() {
        add_action('admin_menu', array($this, 'add_plugin_page'));
        add_action('admin_init', array($this, 'page_init'));
        add_action('wp_footer', array($this, 'render_frontend_assets'));
    }

    public function add_plugin_page() {
        add_options_page(
            'Cookie Consent & Policies', 
            'Cookie Consent', 
            'manage_options', 
            'nenghui-cookie-consent', 
            array($this, 'create_admin_page')
        );
    }

    public function create_admin_page() {
        $this->options = get_option('nenghui_cookie_options');
        ?>
        <div class="wrap">
            <h1>Cookie同意与政策设置</h1>
            <form method="post" action="options.php">
                <?php
                settings_fields('nenghui_cookie_group');
                do_settings_sections('nenghui-cookie-consent');
                submit_button();
                ?>
            </form>
        </div>
        <?php
    }

    public function page_init() {
        register_setting(
            'nenghui_cookie_group', 
            'nenghui_cookie_options'
        );

        add_settings_section(
            'nenghui_cookie_section_main', 
            'Cookie同意与政策设置', 
            null, 
            'nenghui-cookie-consent'
        );

        add_settings_field(
            'cookie_notice_text', 
            'Cookie同意通知文本', 
            array($this, 'cookie_notice_text_callback'), 
            'nenghui-cookie-consent', 
            'nenghui_cookie_section_main'
        );

        add_settings_field(
            'cookie_accept_label', 
            '“同意”按钮文本', 
            array($this, 'cookie_accept_label_callback'), 
            'nenghui-cookie-consent', 
            'nenghui_cookie_section_main'
        );

        add_settings_field(
            'cookie_decline_label', 
            '“拒绝”按钮文本', 
            array($this, 'cookie_decline_label_callback'), 
            'nenghui-cookie-consent', 
            'nenghui_cookie_section_main'
        );

        add_settings_section(
            'nenghui_cookie_section_pages', 
            '相关政策页面设置', 
            null, 
            'nenghui-cookie-consent'
        );

        add_settings_field(
            'privacy_policy_page', 
            '隐私政策页面 (Privacy Policy)', 
            array($this, 'privacy_policy_page_callback'), 
            'nenghui-cookie-consent', 
            'nenghui_cookie_section_pages'
        );

        add_settings_field(
            'cookie_policy_page', 
            'Cookie政策页面 (Cookie Policy)', 
            array($this, 'cookie_policy_page_callback'), 
            'nenghui-cookie-consent', 
            'nenghui_cookie_section_pages'
        );
    }

    public function cookie_notice_text_callback() {
        $text = isset($this->options['cookie_notice_text']) ? $this->options['cookie_notice_text'] : 'We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.';
        echo '<textarea name="nenghui_cookie_options[cookie_notice_text]" rows="3" class="large-text">' . esc_textarea($text) . '</textarea>';
    }

    public function cookie_accept_label_callback() {
        $text = isset($this->options['cookie_accept_label']) ? $this->options['cookie_accept_label'] : 'Accept';
        echo '<input type="text" name="nenghui_cookie_options[cookie_accept_label]" value="' . esc_attr($text) . '" class="regular-text">';
    }

    public function cookie_decline_label_callback() {
        $text = isset($this->options['cookie_decline_label']) ? $this->options['cookie_decline_label'] : 'Decline';
        echo '<input type="text" name="nenghui_cookie_options[cookie_decline_label]" value="' . esc_attr($text) . '" class="regular-text">';
    }

    public function privacy_policy_page_callback() {
        $selected = isset($this->options['privacy_policy_page']) ? $this->options['privacy_policy_page'] : '';
        wp_dropdown_pages(array(
            'name' => 'nenghui_cookie_options[privacy_policy_page]',
            'selected' => $selected,
            'show_option_none' => '选择页面'
        ));
    }

    public function cookie_policy_page_callback() {
        $selected = isset($this->options['cookie_policy_page']) ? $this->options['cookie_policy_page'] : '';
        wp_dropdown_pages(array(
            'name' => 'nenghui_cookie_options[cookie_policy_page]',
            'selected' => $selected,
            'show_option_none' => '选择页面'
        ));
    }

    public function render_frontend_assets() {
        $options = get_option('nenghui_cookie_options');
        $notice_text = isset($options['cookie_notice_text']) ? $options['cookie_notice_text'] : 'We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.';
        $accept_label = isset($options['cookie_accept_label']) ? $options['cookie_accept_label'] : 'Accept';
        $decline_label = isset($options['cookie_decline_label']) ? $options['cookie_decline_label'] : 'Decline';
        
        $cookie_url = isset($options['cookie_policy_page']) && !empty($options['cookie_policy_page']) ? get_permalink($options['cookie_policy_page']) : '#';
        ?>
        
        <!-- Cookie Consent Banner -->
        <div id="nh-cookie-banner" class="nh-cookie-banner" style="display: none;">
            <div class="nh-cookie-content">
                <p><?php echo esc_html($notice_text); ?></p>
                <div class="nh-cookie-links">
                     <?php if($cookie_url !== '#'): ?><a href="<?php echo esc_url($cookie_url); ?>" class="nh-policy-link">Cookie Policy</a><?php endif; ?>
                </div>
            </div>
            <div class="nh-cookie-actions">
                <button id="nh-cookie-accept" class="nh-cookie-btn nh-accept"><?php echo esc_html($accept_label); ?></button>
                <button id="nh-cookie-decline" class="nh-cookie-btn nh-decline"><?php echo esc_html($decline_label); ?></button>
            </div>
        </div>

        <style>
            .nh-cookie-banner {
                position: fixed;
                bottom: 20px;
                left: 20px;
                max-width: 400px;
                background: #fff;
                padding: 20px;
                box-shadow: 0 5px 20px rgba(0,0,0,0.15);
                border-radius: 8px;
                z-index: 9999;
                font-family: inherit;
                border-left: 5px solid #0056b3;
            }
            .nh-cookie-content p {
                margin: 0 0 10px;
                font-size: 14px;
                color: #333;
                line-height: 1.5;
            }
            .nh-cookie-links {
                margin-bottom: 15px;
                font-size: 12px;
            }
            .nh-cookie-links a {
                color: #0056b3;
                text-decoration: underline;
                margin-right: 10px;
                cursor: pointer;
            }
            .nh-cookie-actions {
                display: flex;
                gap: 10px;
            }
            .nh-cookie-btn {
                flex: 1;
                border: none;
                padding: 10px 15px;
                border-radius: 4px;
                cursor: pointer;
                font-size: 14px;
                font-weight: 600;
                transition: all 0.3s ease;
                text-align: center;
            }
            .nh-cookie-btn.nh-accept {
                background: #0056b3;
                color: white;
            }
            .nh-cookie-btn.nh-accept:hover {
                background: #004494;
            }
            .nh-cookie-btn.nh-decline {
                background: #f1f1f1;
                color: #666;
            }
            .nh-cookie-btn.nh-decline:hover {
                background: #e1e1e1;
                color: #333;
            }
        </style>

        <script>
            document.addEventListener('DOMContentLoaded', function() {
                var cookieBanner = document.getElementById('nh-cookie-banner');
                var acceptBtn = document.getElementById('nh-cookie-accept');
                var declineBtn = document.getElementById('nh-cookie-decline');

                // Check for cookie consent
                if (!localStorage.getItem('nh_cookie_consent')) {
                    cookieBanner.style.display = 'block';
                }

                // Accept button
                acceptBtn.addEventListener('click', function() {
                    localStorage.setItem('nh_cookie_consent', 'accepted');
                    cookieBanner.style.display = 'none';
                });

                // Decline button
                declineBtn.addEventListener('click', function() {
                    localStorage.setItem('nh_cookie_consent', 'declined');
                    cookieBanner.style.display = 'none';
                });
            });
        </script>
        <?php
    }
}

new Nenghui_Cookie_Consent();
