<?php
/**
 * WP Multi – Modul: Custom Admin-Links
 *
 * Automatisch aus wp-multi.php ausgelagert. Wird über den Loader in
 * wp-multi.php eingebunden (nur wenn das WP Multi Toolkit aktiv ist).
 */

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

/*
* Custom Link im Admin Sidebar hinzufügen
*/


// Die benutzerdefinierten Admin-Link-Seiten werden zentral in wp_multi_register_admin_menus() registriert.

// Callback-Funktion für das Anzeigen der benutzerdefinierten Seiten
function wp_stat_notice_custom_page() {
    $current_slug = isset($_GET['page']) ? sanitize_key($_GET['page']) : '';
    $custom_pages = get_option('wp_stat_notice_custom_pages', []);

    if (empty($custom_pages) || !is_array($custom_pages)) {
        wpmt_admin_page_open(__('Benutzerdefinierte Seite', 'wp-stat-notice'));
        wpmt_admin_notice('error', __('Keine benutzerdefinierten Seiten gefunden.', 'wp-stat-notice'));
        wpmt_admin_page_close();
        return;
    }

    foreach ($custom_pages as $page) {
        if ($page['slug'] === $current_slug) {
            wpmt_admin_page_open($page['title']);
            wpmt_admin_card_open();
            if (isset($page['url']) && filter_var($page['url'], FILTER_VALIDATE_URL)) {
                echo '<script>window.open("' . esc_url($page['url']) . '", "_blank");</script>';
                echo '<p>' . __('Die externe Seite wird in einem neuen Fenster geöffnet.', 'wp-stat-notice') . '</p>';
                echo '<p><a href="' . esc_url($page['url']) . '" target="_blank" class="wpm-btn wpm-btn--primary">' . esc_html__('Seite manuell öffnen', 'wp-stat-notice') . '</a></p>';
            } else {
                echo '<p>' . __('Dies ist eine benutzerdefinierte Seite im Admin-Bereich.', 'wp-stat-notice') . '</p>';
            }
            wpmt_admin_card_close();
            wpmt_admin_page_close();
            return;
        }
    }

    wpmt_admin_page_open(__('Benutzerdefinierte Seite', 'wp-stat-notice'));
    wpmt_admin_notice('error', __('Seite nicht gefunden.', 'wp-stat-notice'));
    wpmt_admin_page_close();
}

// Funktion zum Hinzufügen und Verwalten benutzerdefinierter Seiten
function wp_stat_notice_add_custom_page_form() {
    $dashicons = [
        'dashicons-admin-links', 'dashicons-admin-site', 'dashicons-admin-home',
        'dashicons-admin-plugins', 'dashicons-admin-users', 'dashicons-analytics',
        'dashicons-archive', 'dashicons-book', 'dashicons-calendar',
        'dashicons-camera', 'dashicons-cart', 'dashicons-cloud',
        'dashicons-clipboard', 'dashicons-clock', 'dashicons-cloud-upload',
        'dashicons-email', 'dashicons-heart', 'dashicons-laptop',
        'dashicons-lock', 'dashicons-phone', 'dashicons-rss',
        'dashicons-search', 'dashicons-settings', 'dashicons-share',
        'dashicons-tag', 'dashicons-thumbs-up', 'dashicons-welcome-learn-more',
        'dashicons-welcome-write-blog'
    ];

    ?>
    <div class="wrap">
        <h1><?php _e('Benutzerdefinierten Adminlink hinzufügen', 'wp-stat-notice'); ?></h1>

        <!-- Formular zum Hinzufügen -->
        <form method="post" action="">
            <?php wp_nonce_field('wp_stat_notice_add_page', 'wp_stat_notice_nonce'); ?>
            <table class="form-table">
                <tr>
                    <th scope="row"><label for="page_title"><?php _e('Titel der Seite', 'wp-stat-notice'); ?></label></th>
                    <td><input type="text" name="page_title" id="page_title" required class="regular-text"></td>
                </tr>
                <tr>
                    <th scope="row"><label for="page_url"><?php _e('URL der Seite (optional)', 'wp-stat-notice'); ?></label></th>
                    <td><input type="url" name="page_url" id="page_url" class="regular-text" placeholder="https://example.com"></td>
                </tr>
                <tr>
                    <th scope="row"><label for="page_slug"><?php _e('Slug der Seite', 'wp-stat-notice'); ?></label></th>
                    <td><input type="text" name="page_slug" id="page_slug" required class="regular-text"></td>
                </tr>
                <tr>
                    <th scope="row"><label for="page_icon"><?php _e('Dashicon auswählen', 'wp-stat-notice'); ?></label></th>
                    <td>
                        <select name="page_icon" id="page_icon" onchange="updateIconPreview(this)">
                            <?php foreach ($dashicons as $dashicon): ?>
                                <option value="<?php echo esc_attr($dashicon); ?>"><?php echo esc_html($dashicon); ?></option>
                            <?php endforeach; ?>
                        </select>
                        <div id="icon-preview" class="dashicons" style="font-size: 24px; margin-top: 10px;"></div>
                    </td>
                </tr>
            </table>
            <p><input type="submit" name="add_custom_page" class="button button-primary" value="<?php _e('Seite hinzufügen', 'wp-stat-notice'); ?>"></p>
        </form>

        <?php
        // Formularverarbeitung für Hinzufügen
        if (isset($_POST['add_custom_page']) && check_admin_referer('wp_stat_notice_add_page', 'wp_stat_notice_nonce')) {
            $title = sanitize_text_field($_POST['page_title']);
            $url = !empty($_POST['page_url']) ? esc_url_raw($_POST['page_url']) : '';
            $slug = sanitize_key($_POST['page_slug']);
            $icon = in_array($_POST['page_icon'], $dashicons, true) ? $_POST['page_icon'] : 'dashicons-admin-links';

            if (empty($title) || empty($slug)) {
                echo '<div class="notice notice-error"><p>' . __('Titel und Slug sind erforderlich.', 'wp-stat-notice') . '</p></div>';
            } else {
                $custom_pages = get_option('wp_stat_notice_custom_pages', []);
                $slugs = array_column($custom_pages, 'slug');
                if (in_array($slug, $slugs)) {
                    echo '<div class="notice notice-error"><p>' . __('Dieser Slug wird bereits verwendet.', 'wp-stat-notice') . '</p></div>';
                } else {
                    $custom_pages[] = ['title' => $title, 'url' => $url, 'slug' => $slug, 'icon' => $icon];
                    update_option('wp_stat_notice_custom_pages', $custom_pages);
                    echo '<div class="updated"><p>' . __('Benutzerdefinierte Seite wurde hinzugefügt!', 'wp-stat-notice') . '</p></div>';
                }
            }
        }

        // Verwaltung der Seiten
        $custom_pages = get_option('wp_stat_notice_custom_pages', []);
        if (!empty($custom_pages)) {
            echo '<h2>' . __('Verwaltung der benutzerdefinierten Seiten', 'wp-stat-notice') . '</h2>';
            echo '<table class="widefat fixed" cellspacing="0">';
            echo '<thead><tr><th>' . __('Titel', 'wp-stat-notice') . '</th><th>' . __('URL', 'wp-stat-notice') . '</th><th>' . __('Slug', 'wp-stat-notice') . '</th><th>' . __('Aktionen', 'wp-stat-notice') . '</th></tr></thead>';
            echo '<tbody>';
            foreach ($custom_pages as $index => $page) {
                echo '<tr>';
                echo '<td>' . esc_html($page['title']) . '</td>';
                echo '<td>' . esc_html($page['url'] ?: '-') . '</td>';
                echo '<td>' . esc_html($page['slug']) . '</td>';
                $delete_url = wp_nonce_url(admin_url('admin.php?page=wp-stat-notice-custom-page&delete=' . $index), 'wp_stat_notice_delete_page_' . $index);
                echo '<td><a href="' . esc_url(admin_url('admin.php?page=wp-stat-notice-custom-page&edit=' . $index)) . '">' . __('Bearbeiten', 'wp-stat-notice') . '</a> | ';
                echo '<a href="' . esc_url($delete_url) . '" onclick="return confirm(\'' . esc_js(__('Möchtest du diese Seite wirklich löschen?', 'wp-stat-notice')) . '\')">' . __('Löschen', 'wp-stat-notice') . '</a></td>';
                echo '</tr>';
            }
            echo '</tbody></table>';
        }

        // Bearbeiten
        if (isset($_GET['edit']) && isset($custom_pages[(int) $_GET['edit']])) {
            $edit_index = (int) $_GET['edit'];
            $edit_page = $custom_pages[$edit_index];

            echo '<h2>' . __('Seite bearbeiten', 'wp-stat-notice') . '</h2>';
            ?>
            <form method="post" action="">
                <?php wp_nonce_field('wp_stat_notice_edit_page', 'wp_stat_notice_nonce'); ?>
                <table class="form-table">
                    <tr>
                        <th scope="row"><label for="edit_page_title"><?php _e('Titel der Seite', 'wp-stat-notice'); ?></label></th>
                        <td><input type="text" name="edit_page_title" id="edit_page_title" value="<?php echo esc_attr($edit_page['title']); ?>" required class="regular-text"></td>
                    </tr>
                    <tr>
                        <th scope="row"><label for="edit_page_url"><?php _e('URL der Seite (optional)', 'wp-stat-notice'); ?></label></th>
                        <td><input type="url" name="edit_page_url" id="edit_page_url" value="<?php echo esc_attr($edit_page['url']); ?>" class="regular-text"></td>
                    </tr>
                    <tr>
                        <th scope="row"><label for="edit_page_slug"><?php _e('Slug der Seite', 'wp-stat-notice'); ?></label></th>
                        <td><input type="text" name="edit_page_slug" id="edit_page_slug" value="<?php echo esc_attr($edit_page['slug']); ?>" required class="regular-text"></td>
                    </tr>
                    <tr>
                        <th scope="row"><label for="edit_page_icon"><?php _e('Dashicon', 'wp-stat-notice'); ?></label></th>
                        <td>
                            <select name="edit_page_icon" id="edit_page_icon" onchange="updateIconPreview(this)">
                                <?php foreach ($dashicons as $dashicon): ?>
                                    <option value="<?php echo esc_attr($dashicon); ?>" <?php selected($edit_page['icon'], $dashicon); ?>><?php echo esc_html($dashicon); ?></option>
                                <?php endforeach; ?>
                            </select>
                            <div id="icon-preview" class="dashicons <?php echo esc_attr($edit_page['icon']); ?>" style="font-size: 24px; margin-top: 10px;"></div>
                        </td>
                    </tr>
                </table>
                <p><input type="submit" name="save_custom_page" class="button button-primary" value="<?php _e('Änderungen speichern', 'wp-stat-notice'); ?>"></p>
            </form>
            <?php

            if (isset($_POST['save_custom_page']) && check_admin_referer('wp_stat_notice_edit_page', 'wp_stat_notice_nonce')) {
                $title = sanitize_text_field($_POST['edit_page_title']);
                $url = !empty($_POST['edit_page_url']) ? esc_url_raw($_POST['edit_page_url']) : '';
                $slug = sanitize_key($_POST['edit_page_slug']);
                $icon = in_array($_POST['edit_page_icon'], $dashicons, true) ? $_POST['edit_page_icon'] : 'dashicons-admin-links';

                if (empty($title) || empty($slug)) {
                    echo '<div class="notice notice-error"><p>' . __('Titel und Slug sind erforderlich.', 'wp-stat-notice') . '</p></div>';
                } else {
                    $custom_pages[$edit_index] = ['title' => $title, 'url' => $url, 'slug' => $slug, 'icon' => $icon];
                    update_option('wp_stat_notice_custom_pages', $custom_pages);
                    echo '<div class="updated"><p>' . __('Seite erfolgreich bearbeitet!', 'wp-stat-notice') . '</p></div>';
                }
            }
        }

        // Löschen
        if (isset($_GET['delete']) && isset($custom_pages[(int) $_GET['delete']])) {
            check_admin_referer('wp_stat_notice_delete_page_' . (int) $_GET['delete']);
            $delete_index = (int) $_GET['delete'];
            unset($custom_pages[$delete_index]);
            $custom_pages = array_values($custom_pages);
            update_option('wp_stat_notice_custom_pages', $custom_pages);
            echo '<div class="updated"><p>' . __('Seite wurde gelöscht.', 'wp-stat-notice') . '</p></div>';
        }
        ?>
    </div>

    <script>
    function updateIconPreview(select) {
        const preview = select.nextElementSibling;
        preview.className = 'dashicons ' + select.value;
    }
    document.addEventListener('DOMContentLoaded', () => {
        document.querySelectorAll('#page_icon, #edit_page_icon').forEach(select => {
            updateIconPreview(select);
        });
    });
    </script>
    <?php
}

// Der Menüpunkt "Admin-Link hinzufügen" wird zentral in wp_multi_register_admin_menus() registriert.


