<?php
defined('ABSPATH') || exit;

class Termin_Kalender_Admin_Page
{

    public function __construct()
    {
        add_action('admin_menu', [$this, 'add_calendar_admin_menu']);
    }

    public function add_calendar_admin_menu()
    {
        if (defined('TERMIN_KALENDER_PRO_ON')) {
            $page_title = 'Termin-Kalender PRO';
        } else {
            $page_title = 'Termin-Kalender';
        }
        $menu_title = $page_title;
        $capability = 'termin_kalender_edit';
        $menu_slug = 'termin-kalender';
        $callback = [$this, 'render_calendar_admin_menu_page'];
        $icon_url = 'dashicons-calendar';
        $position = 4;

        // Register the admin page and -  Add the hook suffix to the shared static property.
        Termin_Kalender_Plugin::$ter_kal_admin_pages_hook_suffixes[] = add_menu_page(
            $page_title,
            $menu_title,
            $capability,
            $menu_slug,
            $callback,
            $icon_url,
            $position
        );

    }

    function render_calendar_admin_menu_page()
    {
        $google_api_key = '';
        $google_kalender_id = '';
        $options = get_option('termin_kalender_options', []);

        if (defined('TERMIN_KALENDER_PRO_ON')) {
            if (!current_user_can('termin_kalender_edit')) {
                return '<h4 class="color_red" >' . esc_html__('Access denied, you need to have edit rights for Termin-Kalender', 'termin-kalender') . '</h4>';
            }
            $google_admin_calendar_enabled = $options['termin_kalender_google_admin_calendar'] ?? '';
            if ($google_admin_calendar_enabled == 'on') {
                $google_api_key = $options['termin_kalender_google_api_key'] ?? '';
                $google_kalender_id = $options['termin_kalender_google_kalender_id'] ?? '';
            }
        }


        $has_archive = false;
        if (defined('TERMIN_KALENDER_PRO_ON') && class_exists('Termin_Kalender_Event_Repository') && !Termin_Kalender_Event_Repository::is_legacy_mode()) {
            global $wpdb;
            $table = Termin_Kalender_Event_Repository::get_table_name();
            $archive_table = esc_sql($table . '_archive');
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
            $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$archive_table'");
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
            $table_count = $wpdb->get_var("SELECT COUNT(id) FROM $archive_table");
            if ($table_exists === $archive_table && $table_count > 0) {
                $has_archive = true;
            }
        }

        ?>
        <div class="ter-kal-block-kalender" data-g_key="<?php echo esc_attr($google_api_key); ?>"
            data-g_id="<?php echo esc_attr($google_kalender_id); ?>">
            <div class="admin_kalender wrap">
                <div style="display:flex; justify-content:space-between; align-items:flex-end;">
                    <div id="kategoriezeile" class="kategoriezeile"></div>
                    <?php if ($has_archive): ?>
                        <div style="margin-bottom: 20px;">
                            <label
                                style="background:var(--pro-bg, #f0f0f1); padding: 5px 12px; border-radius: 4px; border: 1px solid var(--pro-border, #ccc); font-weight: 500; display:flex; align-items:center; gap: 8px;">
                                <input type="checkbox" id="tk-show-archive" value="1">
                                <span class="dashicons dashicons-archive"
                                    style="font-size: 16px; width: 16px; height: 16px; margin-top:2px;"></span>
                                <?php esc_html_e('Load Archive', 'termin-kalender'); ?>
                            </label>
                        </div>
                    <?php endif; ?>
                </div>
                <div id="termin-kalender-admin-calendar"></div>
                <div id="event-dialog" class="event-dialog"
                    title="<?php esc_html_e('Appointment Details', 'termin-kalender'); ?>" style="display:none;">
                    <form id="event-form" class="event-form">
                        <label for="event-title"><?php esc_html_e('Appointment Title', 'termin-kalender'); ?>:</label>
                        <input type="text" id="event-title" name="event-title" class="regular-text event-title">
                        <div class="event-dates">
                            <div class="event-date">
                                <label for="event-start"><?php esc_html_e('Start', 'termin-kalender'); ?>:</label>
                                <input type="datetime-local" id="event-start" name="event-start"
                                    class="regular-text datetime-picker">
                            </div>
                            <div class="event-date">
                                <label for="event-end"><?php esc_html_e('End', 'termin-kalender'); ?>:</label>
                                <input type="datetime-local" id="event-end" name="event-end"
                                    class="regular-text datetime-picker">
                                <label for="event-until"
                                    style="display:none;"><?php esc_html_e('Until', 'termin-kalender'); ?>:</label>
                                <input type="datetime-local" id="event-until" name="event-until"
                                    class="regular-text datetime-picker" style="display:none;">
                            </div>
                            <div id="event-recurrings" class="event-date" style="display: none;">
                                <label for="event-recurring"><?php esc_html_e('Recurring: ', 'termin-kalender'); ?></label>
                                <select id="event-recurring" name="event-recurring" class="regular-text">
                                    <option value=""><?php esc_html_e('None', 'termin-kalender'); ?></option>
                                    <option value="WEEKLY"><?php esc_html_e('Weekly ', 'termin-kalender'); ?></option>
                                    <option value="MONTHLY"><?php esc_html_e('Monthly ', 'termin-kalender'); ?></option>
                                    <option value="YEARLY"><?php esc_html_e('Annual ', 'termin-kalender'); ?></option>
                                </select>
                            </div>
                            <div class="event-date show_allday" id="show_allday" style="display: none;">
                                <label for="event-allday"><?php esc_html_e('All day', 'termin-kalender'); ?>:</label>
                                <input type="checkbox" id="event-allday" name="event-allday" class="regular-text">
                            </div>
                            <input type="hidden" id="event-dtstart" name="event-dtstart">
                        </div>
                        <span id="allday-notice" class="ui_info" style="display:none;">
                            <?php esc_html_e('Allday is fixed. To change the Appointment length, deselect Allday.', 'termin-kalender'); ?>
                        </span>
                        <div class="day-checkboxes" id="day_check" style="display: none;">
                            <div class="form-check-label"><?php esc_html_e('Choose Days:', 'termin-kalender'); ?></div>
                            <label for="MO" class="form-check-label"><?php esc_html_e('Mon', 'termin-kalender'); ?>
                                <input type="checkbox" id="MO" class="form-check-input" name="days[]" value="MO">
                            </label>
                            <label for="TU" class="form-check-label"><?php esc_html_e('Tue', 'termin-kalender'); ?>
                                <input type="checkbox" id="TU" class="form-check-input" name="days[]" value="TU">
                            </label>
                            <label for="WE" class="form-check-label"><?php esc_html_e('Wed', 'termin-kalender'); ?>
                                <input type="checkbox" id="WE" class="form-check-input" name="days[]" value="WE">
                            </label>
                            <label for="TH" class="form-check-label"><?php esc_html_e('Thu', 'termin-kalender'); ?>
                                <input type="checkbox" id="TH" class="form-check-input" name="days[]" value="TH">
                            </label>
                            <label for="FR" class="form-check-label"><?php esc_html_e('Fri', 'termin-kalender'); ?>
                                <input type="checkbox" id="FR" class="form-check-input" name="days[]" value="FR">
                            </label>
                            <label for="SA" class="form-check-label"><?php esc_html_e('Sat', 'termin-kalender'); ?>
                                <input type="checkbox" id="SA" class="form-check-input" name="days[]" value="SA">
                            </label>
                            <label for="SU" class="form-check-label"><?php esc_html_e('Sun', 'termin-kalender'); ?>
                                <input type="checkbox" id="SU" class="form-check-input" name="days[]" value="SU">
                            </label>
                        </div>
                        <hr>
                        <div class="all_fields_inline" id="interval-ocurence">
                            <div class="fields_inline" id="freq_interval" style="display: none;">
                                <label for="interval" class=""><?php esc_html_e('Repeat every', 'termin-kalender'); ?></label>
                                <input type="number" name="interval" id="interval" value="1" min="1" max="52">
                                <span id="interval-unit"><?php esc_html_e('week(s)', 'termin-kalender'); ?></span>
                            </div>
                            <div class="fields_inline" id="bysetpos-field" style="display: none;">
                                <label for="bysetpos"><?php esc_html_e('Position in Month', 'termin-kalender'); ?></label>
                                <select name="bysetpos" id="bysetpos">
                                    <option value=""><?php esc_html_e('Every occurrence', 'termin-kalender'); ?></option>
                                    <option value="1"><?php esc_html_e('First', 'termin-kalender'); ?></option>
                                    <option value="2"><?php esc_html_e('Second', 'termin-kalender'); ?></option>
                                    <option value="3"><?php esc_html_e('Third', 'termin-kalender'); ?></option>
                                    <option value="4"><?php esc_html_e('Fourth', 'termin-kalender'); ?></option>
                                    <option value="-1"><?php esc_html_e('Last', 'termin-kalender'); ?></option>
                                </select>
                            </div>
                        </div>
                        <div id="excluding-dates" class="excluding-dates" name="excluding-dates" style="display: none;">
                            <button type="button" class="button bootstrap_button bootstrap_primary show_more" id="show_more"
                                data-text-show="<?php esc_attr_e('Add days to exclude from recurring', 'termin-kalender'); ?>"
                                data-text-hide="<?php esc_attr_e('Close', 'termin-kalender'); ?>">
                            </button>
                            <div id="show_more_div" style="display:none;">
                                <label
                                    for="date-chooser"><?php esc_html_e('Choose dates to exclude:', 'termin-kalender'); ?></label>
                                <input type="date" id="exclude-date-chooser" class="regular-text">
                                <button type="button" class="button bootstrap_button bootstrap_primary exclude-add-date"
                                    id="exclude-add-date"><?php esc_html_e('Add', 'termin-kalender'); ?></button>
                                <label for="excluding-days"
                                    class="ui_info"><?php esc_html_e('Excluded dates', 'termin-kalender'); ?>:</label>
                                <div id="excluding-days-display" class="regular-text excluding-days-display"></div>
                                <input type="hidden" id="excluding-days" name="excluding-days" value="">
                            </div>
                        </div>
                        <input id="event-byweekday" name="event-byweekday" type="hidden" value="">
                        <div id="event-recurrings_info_pro" class="ui_info">
                            <?php esc_html_e('Upgrade to Termin-Kalender PRO for repeat appointments and more.', 'termin-kalender'); ?>
                        </div>
                        <hr>
                        <div class="event-category">
                            <label for="event-category"><?php esc_html_e('Category', 'termin-kalender'); ?>:</label>
                            <select id="event-category" name="event-category" class="regular-text"></select>
                        </div>
                        <!-- Options will be populated by JavaScript -->
                        <div id="event_details_or_post">
                            <div class="bootstrap_button_line">
                                <button type="button" class="bootstrap_button bootstrap_primary"
                                    id="heading-button"><?php esc_html_e('Title', 'termin-kalender'); ?></button>
                                <button type="button" class="bootstrap_button bootstrap_primary"
                                    id="bold-button"><?php esc_html_e('Bold', 'termin-kalender'); ?></button>
                                <button type="button" class="bootstrap_button bootstrap_primary"
                                    id="italic-button"><?php esc_html_e('Italic', 'termin-kalender'); ?></button>&nbsp;&nbsp;
                                <button type="button" class="bootstrap_button bootstrap_info"
                                    id="underline-button"><?php esc_html_e('Underline', 'termin-kalender'); ?></button>
                                <button type="button" class="bootstrap_button bootstrap_info"
                                    id="strikethrough-button"><?php esc_html_e('Strike', 'termin-kalender'); ?></button>
                                <button type="button" class="bootstrap_button bootstrap_success"
                                    id="hr-button"><?php esc_html_e('Line Divider', 'termin-kalender'); ?></button>&nbsp;&nbsp;
                                <button type="button" class="bootstrap_button bootstrap_primary"
                                    id="quote-button"><?php esc_html_e('Quote', 'termin-kalender'); ?></button>
                                <button type="button" class="bootstrap_button bootstrap_primary"
                                    id="code-button"><?php esc_html_e('Code', 'termin-kalender'); ?></button>&nbsp;&nbsp;
                                <button type="button" class="bootstrap_button bootstrap_warning"
                                    id="list-button"><?php esc_html_e('List', 'termin-kalender'); ?></button>&nbsp;&nbsp;
                                <button type="button" class="bootstrap_button bootstrap_dark"
                                    id="link-button"><?php esc_html_e('Link', 'termin-kalender'); ?></button>
                                <button type="button" class="bootstrap_button bootstrap_dark"
                                    id="image-button"><?php esc_html_e('Image', 'termin-kalender'); ?></button>
                            </div>
                            <div id="show_event_details">
                                <label
                                    for="event-details"><?php esc_html_e('Appointment Details', 'termin-kalender'); ?>:</label>
                                <textarea id="event-details" name="event-details" class="regular-text" rows="2"></textarea>
                                <button type="button" class="bootstrap_primary show_preview" id="show_preview"
                                    style="display:none;">OK</button>
                                <div id="event-details-html" class="event-details-html" style="display:none;"></div>
                            </div>
                            <button type="button" class="bootstrap_primary" style="border: 1px solid #ADD5FF;"
                                id="create_event_post"><?php esc_html_e('Create Wordpress Event Post', 'termin-kalender'); ?></button><br>
                            <div id="post_info" style="display: none;">
                                <?php esc_html_e('Save to get a link to its post page, where you can edit it and display it on your site in post lists, archives, or grids using your theme or page builder.', 'termin-kalender'); ?>
                                <button type="button" class="bootstrap_warning" style="border: 1px solid #FF8A65;"
                                    id="back_to_calendar_only"><?php esc_html_e('Back to use calendar only', 'termin-kalender'); ?></button>
                            </div>
                            <div id="show_event_post" style="display: none;">
                                <p>
                                    <?php esc_html_e('Your post is ready', 'termin-kalender'); ?> :&nbsp;
                                    <span id="show_event_post_link"></span><br>
                                    <?php esc_html_e('The content is fully independent from your Appointment details.', 'termin-kalender'); ?>
                                </p>
                            </div>
                            <input type="hidden" id="create_post" name="create_post">
                            <input type="hidden" id="post_link" name="post_link">
                        </div>
                        <hr>
                        <div id="event-extensions_info_pro" class="ui_info">
                            <?php esc_html_e('Upgrade to Termin-Kalender PRO to display appointment locations and unlock more features.', 'termin-kalender'); ?>
                        </div>
                        <div id="event-extensions" style="display:none;">
                            <label for="event-location"><?php esc_html_e('Location', 'termin-kalender'); ?>: <b>&nbsp;<span
                                        id="event-location_link"></span></b></label>
                            <input type="text" id="event-location" name="event-location" class="regular-text">
                        </div>
                        <label for="event-notes"><?php esc_html_e('Notes', 'termin-kalender'); ?>:</label>
                        <textarea id="event-notes" name="event-notes" class="regular-text event-notes" rows="2"></textarea>
                        <input type="hidden" id="event-id" name="event-id">
                    </form>
                </div>

                <div id="category-dialog" title="<?php esc_html_e('Category Details', 'termin-kalender'); ?>"
                    style="display:none;">
                    <form id="category-form" class="category-form">
                        <label for="category-label"><?php esc_html_e('Category Label', 'termin-kalender'); ?>:</label>
                        <input type="text" id="category-label" name="category-label" class="regular-text"><br>

                        <div id="category-icon-label" class="form-check-label"><?php esc_html_e('Icon', 'termin-kalender'); ?>:
                        </div>
                        <div class="custom-select" id="category-icon-wrapper" role="combobox"
                            aria-labelledby="category-icon-label">
                            <div class="select-selected" id="category-icon-selected">Select Icon</div>
                            <div class="select-items tk-hidden" id="category-icon-items">
                                <!-- Options will be populated by JavaScript -->
                            </div>
                        </div>
                        <br>
                        <label
                            for="category-backgroundColor"><?php esc_html_e('Background Color', 'termin-kalender'); ?>:</label>
                        <input type="color" id="category-backgroundColor" name="category-backgroundColor"
                            class="regular-text"><br>
                        <label for="category-textColor"><?php esc_html_e('Text Color', 'termin-kalender'); ?>:</label>
                        <input type="color" id="category-textColor" name="category-textColor" class="regular-text"><br>
                        <input type="hidden" id="categoryID" name="categoryID">
                        <input type="hidden" id="category-icon" name="category-icon">
                    </form>
                </div>


                <?php include Termin_Kalender_Plugin::$plugin_dir_path . 'includes/admin_help_dialog_html.php'; ?>

            </div>
        </div>
        <?php
    }
}   // end class




