<?php
/**
 * AI Auto Tool - Post Exporter Plugin
 * Export posts and content
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Thoát nếu truy cập trực tiếp

if (!class_exists('Post_Exporter_Plugin')) {
    class Post_Exporter_Plugin extends rendersetting {
        public $active = false;
        public $active_option_name = 'Post_Exporter_Plugin_active';
        public $icon = '<i class="fa-solid fa-download"></i>';
        
        public function __construct() {
            $this->active = get_option($this->active_option_name, false);
            
            if ($this->active == 'true') {
                $this->init();
            }
            
            // AJAX handlers
            add_action('wp_ajax_update_active_option_canonical_' . $this->active_option_name, array($this, 'update_active_option_callback'));
        }
        
        public function init() {
            // Initialize post exporter functionality
            add_action('admin_menu', array($this, 'add_admin_menu'));
        }
        
        public function add_admin_menu() {
            add_submenu_page(
                'ai_auto_tool',
                'Post Exporter',
                'Post Exporter',
                'manage_options',
                'post-exporter',
                array($this, 'render_admin_page')
            );
        }
        
        public function render_admin_page() {
            echo '<div class="wrap">';
            echo '<h1>Post Exporter</h1>';
            echo '<p>Export posts and content functionality.</p>';
            echo '</div>';
        }
        
        public function update_active_option_callback() {
            check_ajax_referer('aiautotool_nonce', 'security');
            
            if (!current_user_can('manage_options')) {
                wp_die(__('Permission denied', 'ai-auto-tool'), '', array('response' => 403));
            }
            
            if (isset($_POST['active'])) {
                $active = sanitize_text_field($_POST['active']);
                update_option($this->active_option_name, $active, null, 'no');
                
                wp_send_json_success(array('active' => $active));
            }
            
            wp_send_json_error('Invalid request');
        }
        
        public function render_feature() {
            $box = new AutoToolBox(
                $this->icon . ' Post Exporter',
                'Export posts and content',
                admin_url('admin.php?page=aiautotool_integrations'),
                $this->active_option_name,
                $this->active,
                '',
                false
            );
            
            echo $box->generateHTML();
        }
    }
}
