<?php

namespace Macymed\MacymedLinkTracker\Admin\Pages;

use Macymed\MacymedLinkTracker\Utils\Report;

if (! defined('ABSPATH')) {
    exit; // Sécurité
}

class Stats extends Page
{
    public static function init()
    {
    }



    public static function render()
    {
        // Vérification du nonce dans l'URL
        $nonce = isset($_GET['tab_nonce']) ? sanitize_text_field(wp_unslash($_GET['tab_nonce'])) : '';
        if ($nonce && !wp_verify_nonce($nonce, 'tab_navigation')) {
            wp_die(esc_html__('Invalid nonce or expired link.', 'macymed-link-tracker'));
        }

        // Récupérer l'onglet actif
        $active_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'global';

        // Générer un nonce pour les onglets
        $nonce = wp_create_nonce('tab_navigation');

        // Générer les URLs des onglets
        $tab_global = (add_query_arg(['tab' => 'global', 'tab_nonce' => $nonce], menu_page_url('macymed-link-tracker-stats', false)));
        $tab_advanced = (add_query_arg(['tab' => 'advanced', 'tab_nonce' => $nonce], menu_page_url('macymed-link-tracker-stats', false)));
        

        ?>
        <div class="wrap macymed_link_tracker_table">
            <?php
                    self::renderTitle(esc_html__('Stats', 'macymed-link-tracker'));
        ?>
            <h2 class="nav-tab-wrapper">

                <a href="<?php echo esc_url($tab_global); ?>" class="nav-tab <?php echo $active_tab == 'global' ? 'nav-tab-active' : ''; ?>"><?php echo esc_html__('Général', 'macymed-link-tracker'); ?></a>
                <a href="<?php echo esc_url($tab_advanced); ?>" class="nav-tab <?php echo $active_tab == 'advanced' ? 'nav-tab-active' : ''; ?>"><?php echo esc_html__('Avancé', 'macymed-link-tracker'); ?></a>
                <?php
                do_action('macymed_link_tracker_stats_tab_advanced', $active_tab, $nonce);
                ?>
                
            </h2>
            <div id="poststuff">
                <div id="post-body" class="metabox-holder columns-3">
                    <div id="post-body-content">
                        <div class="meta-box-sortables">
                            <?php
                        if ($active_tab == 'global') {
                            self::render_global_tab();
                        } elseif ($active_tab == 'advanced') {
                            self::render_advanced_tab();
                        } elseif ($active_tab == 'report') {
                            self::render_report_tab();
                        }
        ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php
    }

    public static function render_global_tab()
    {
        ?>
        <div class="postbox">
            <h2 class="hndle"><span><?php echo esc_html__('Vue globale', 'macymed-link-tracker'); ?></span></h2>
            <div class="inside">
                <label for="type-select"><?php echo esc_html__('Vue par', 'macymed-link-tracker'); ?>:</label>
                <select id="type-select">
                    <option value="daily"><?php echo esc_html__('Jours', 'macymed-link-tracker'); ?></option>
                    <option value="weekly"><?php echo esc_html__('Semaines', 'macymed-link-tracker'); ?></option>
                    <option value="monthly"><?php echo esc_html__('Mois', 'macymed-link-tracker'); ?></option>
                    <option value="yearly"><?php echo esc_html__('Années', 'macymed-link-tracker'); ?></option>
                </select>
                <button id="generate-chart" class="button"><?php echo esc_html__('Appliquer', 'macymed-link-tracker'); ?></button>
            </div>
        </div>

    <?php
            self::render_report_chart();
    }
    public static function render_report_chart($display_graph = true)
    {
        ?>
        <div class="postbox" id="report-chart" style="display: <?php echo $display_graph ? 'block' : 'none'; ?>">
            <h2 class="hndle"><span><?php echo esc_html__('Evolution', 'macymed-link-tracker'); ?></span></h2>
            <div class="inside">
                <canvas id="myChart"></canvas>
            </div>
        </div>

        
    <?php
    }
    public static function render_advanced_tab()
    {
        ?>
        <div class="postbox">
            <h2 class="hndle"><span><?php echo esc_html__('Critères avancés', 'macymed-link-tracker'); ?></span></h2>
            <div class="inside">
                <label for="type-select"><?php echo esc_html__('Vue par', 'macymed-link-tracker'); ?>:</label>
                <select id="type-select">
                    <option value="daily"><?php echo esc_html__('Jours', 'macymed-link-tracker'); ?></option>
                    <option value="weekly"><?php echo esc_html__('Semaines', 'macymed-link-tracker'); ?></option>
                    <option value="monthly"><?php echo esc_html__('Mois', 'macymed-link-tracker'); ?></option>
                    <option value="yearly"><?php echo esc_html__('Années', 'macymed-link-tracker'); ?></option>
                </select>
                <label for="start-date"><?php echo esc_html__('Début de période', 'macymed-link-tracker'); ?>:</label>
                <input type="date" id="start-date">
                <label for="end-date"><?php echo esc_html__('fin de période', 'macymed-link-tracker'); ?>:</label>
                <input type="date" id="end-date">
                <label for="search-text"><?php echo esc_html__('Mot clef', 'macymed-link-tracker'); ?>:</label>
                <input type="text" id="search-text">
                <button id="generate-chart" class="button"><?php echo esc_html__('Appliquer', 'macymed-link-tracker'); ?></button>
            </div>
        </div>
    <?php
            self::render_report_chart();
    }

    public static function render_report_tab()
    {
        do_action('macymed_link_tracker_stats_report_tab_before');
        do_action('macymed_link_tracker_stats_report_tab_pro_before', 'Génération de rapport');

        do_action('macymed_link_tracker_stats_report_tab_after');
        do_action('macymed_link_tracker_stats_report_tab_pro_after', function () {});
    }
}
