<?php

namespace Macymed\MacymedLinkTracker\Admin\Pages;

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

class Dashboard extends Page
{
    public static function init() {}
    public static function get_menu_structure(){
        $base_url = admin_url('admin.php?page=macymed-link-tracker-evolution');

        return [
            esc_html__( 'LINKTRACKER', 'macymed-link-tracker' ) => [
                [
                    'label' => esc_html__( 'Statistiques', 'macymed-link-tracker' ),
                    'url' => admin_url('admin.php?page=macymed-link-tracker-statistics') . '&tab=referer',
                    'tab' => 'referer',
                    'default' => 'none',
                ],
                [
                    'label' => esc_html__( 'Options', 'macymed-link-tracker' ),
                    'url' => admin_url('admin.php?page=macymed-link-tracker-settings') . '&tab=tracking',
                    'tab' => 'tracking',
                    'default' => 'none',
                ],
                [
                    'label' => esc_html__( 'Support', 'macymed-link-tracker' ),
                    'url' => admin_url('admin.php?page=macymed-link-tracker-evolution') . '&tab=contact',
                    'tab' => 'contact',
                    'default' => 'none',
                ],
                [
                    'label' => esc_html__( 'Outils', 'macymed-link-tracker' ),
                    'url' => admin_url('admin.php?page=macymed-link-tracker-tools') . '&tab=import_configuration',
                    'tab' => 'import_configuration',
                    'default' => 'none',
                ],
                // [
                //     'label' => 'Aide et Support',
                //     'url' => admin_url('admin.php?page=macymed-link-tracker-help') . '&tab=help',
                //     'tab' => 'help',
                // ],
                
                
                
                
                
                
            ],
            
        ];
    }
    public static function render()
    {
        global $wpdb;

        // Données nécessaires
        $license = get_option('macymed_tracker_license_key', 'Non activée');

        $table_name_main   = $wpdb->prefix . MACYMED_LINK_TRACKER_TABLE_NAME;
        $table_name_clicks = $wpdb->prefix . MACYMED_LINK_TRACKER_CLICKS_TABLE_NAME;

        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $total_links = $wpdb->get_var(("SELECT COUNT(*) FROM " . $table_name_main));
        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
        $click_count = $wpdb->get_var(("SELECT COUNT(*) FROM " .  $table_name_clicks));

?>
        <div class="macymed-content">

            <?php
            (self::renderTitle());
            ?>

            <div id="poststuff">
                <div id="post-body" class="metabox-holder columns-2">
                    <div id="post-body-content">
                        <div class="meta-box-sortables">
                            <div class="postbox">
                                <div class="inside">
                                    <?php
                                    do_action('macymed_link_tracker_dashboard_before_stats');
                                    ?>
                                    <p><strong><?php echo esc_html__('Nombre de liens enregistrés', 'macymed-link-tracker'); ?> :</strong> <?php echo intval($total_links); ?></p>
                                    <p><strong><?php echo esc_html__('Nombre de clics enregistrés', 'macymed-link-tracker'); ?> :</strong> <?php echo intval($click_count); ?></p>
                                    <?php
                                    do_action('macymed_link_tracker_dashboard_after_stats');
                                    ?>
                                </div>
                            </div>
                            <div class="postbox">
                                <h2 class="hndle"><span><?php echo esc_html__('Statistiques des clics', 'macymed-link-tracker'); ?></span></h2>
                                <div class="inside" id="chart-dashboard">
                                    <canvas  id="myChart"></canvas>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Section latérale -->
                    <div id="postbox-container-1" class="postbox-container">
                        <div class="meta-box-sortables">
                            <div class="postbox">
                                <h2 class="hndle"><span><?php echo esc_html__('Informations générales', 'macymed-link-tracker'); ?></span></h2>
                                <div class="inside">
                                    <p><?php echo esc_html__("Merci d'utiliser le plugin Macymed - Link Tracker !", 'macymed-link-tracker'); ?></p>

                                    <p><?php
                                        echo wp_kses(
                                            (__('Pour toute assistance, veuillez nous contacter à <a href="mailto:extensions@macymed.fr">extensions@macymed.fr</a>.', 'macymed-link-tracker')),
                                            [
                                                'a' => [
                                                    'class'        => [],
                                                    'target' => [],
                                                    'href'     => [],
                                                ],
                                            ]
                                        );
                                        ?>
                                    </p>
                                    <p><?php
                                        echo wp_kses(
                                            (__('Site : <a href="https://extensions.macymed.fr" target="_blank">extensions.macymed.fr</a>', 'macymed-link-tracker')),
                                            [
                                                'a' => [
                                                    'class'        => [],
                                                    'target' => [],
                                                    'href'     => [],
                                                ],
                                            ]
                                        );
                                        ?>
                                    </p>
                                </div>
                            </div>



                        </div>
                    </div>
                </div>
            </div>
        </div>
<?php
    }
}
