<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Display Settings meta settings
function eapsgt_display_settings() {
	global $post;
	wp_nonce_field( "display-settings-field", "nonce2" );
	$popup_trigger = esc_html(get_post_meta( $post->ID, "eapsgt-trigger", true ));
	if ( empty( $popup_trigger ) ) {
		$popup_trigger = "time";
	}
	$delay_value = esc_html(get_post_meta( $post->ID, "eapsgt-delay_value", true ));
	if ( ! isset( $delay_value ) || ! $delay_value ) {
		$delay_value = 1;
	};
	$scroll_value = esc_html(get_post_meta( $post->ID, "eapsgt-scroll_value", true ));
	if ( ! isset( $scroll_value ) || ! $scroll_value ) {
		$scroll_value = 10;
	};

	$page_list_checked = get_post_meta( $post->ID, "_eapsgt-page_list", true );
    $page_list_checked = empty($page_list_checked) ? array() : $page_list_checked ;
    $page_list_checked = array_map( 'esc_attr', $page_list_checked );

    $post_list_checked = get_post_meta( $post->ID, "_eapsgt-post_list", true );
    $post_list_checked = empty($post_list_checked) ? array() : $post_list_checked ;
    $post_list_checked = array_map( 'esc_attr', $post_list_checked );

	$popup_frequency = esc_html(get_post_meta( $post->ID, "eapsgt-frequency", true ));
	if ( empty( $popup_frequency ) ) {
		$popup_frequency = "firstTimeOnly";
	}
	$frequency_value = esc_html(get_post_meta( $post->ID, "eapsgt-frequency_value", true ));
	if ( ! isset( $frequency_value ) || ! $frequency_value ) {
		$frequency_value = 1;
	};

	$pages = get_pages();
	$args  = array(
		'numberposts' => - 1
	);
	$posts = get_posts( $args );
	?>
    <input class="eapsgt-slider_value" type="hidden" trigger_value="<?php echo esc_attr($popup_trigger) ?>"
           delay_value="<?php echo esc_attr($delay_value) ?>" scroll_value="<?php echo esc_attr($scroll_value) ?>">
    <div class="eapsgt-display-settings__admin">
        <div class="eapsgt-display-settings__option">
            <table class="eapsgt-detailtbl">
                <tr>
                    <th>
                        <div class="eapsgt-options"><?php esc_html_e( "Trigger", 'easy-animated-popup' ); ?></div>
                        <div id="eapsgt-on_timmer_des"
                             class="eapsgt-des <?php if ( $popup_trigger !== 'time' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>"><?php esc_html_e( "Set the delay in seconds when popup will be shown"
                                , 'easy-animated-popup' ); ?>
                        </div>
                        <div id="eapsgt-on_scroll_des"
                             class="eapsgt-des <?php if ( $popup_trigger !== 'scroll' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>"><?php esc_html_e( "Specify the percent of scoll when popup will be shown"
                                , 'easy-animated-popup' ); ?>
                        </div>
                        <div id="eapsgt-on_exit_des"
                             class="eapsgt-des <?php if ( $popup_trigger !== 'exit' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>"><?php esc_html_e( "Show the popup before leaving the current page"
                                , 'easy-animated-popup' ); ?>
                        </div>
                    </th>
                    <td>
                        <input type="radio" id="eapsgt-on_timmer_trigger" name="eapsgt-trigger"
                               value="time" <?php if ( $popup_trigger == "time" ) {
                            echo esc_attr("checked");
                        } ?>>
                        <span><label class="eapsgt-label_trigger"
                                     for="eapsgt-on_timmer_trigger"><?php esc_html_e( "On Timer", 'easy-animated-popup' ); ?></span></label>
                        <br>
                        <div id="eapsgt-on_timmer_help"
                             class="eapsgt-help <?php if ( $popup_trigger !== 'time' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>">
                            <div class="eapsgt-slider" id="eapsgt-delay_slider"></div>
                            <input class="eapsgt-value_input" type="number"
                                   id="eapsgt-delay_slider_value" name="eapsgt-delay_value"
                                   value="<?php echo esc_attr($delay_value); ?>" min="1"
                                   max="120"/> <?php esc_html_e( "sec", 'easy-animated-popup' ); ?>
                        </div>

                        <input type="radio" id="eapsgt-on_scroll_trigger" name="eapsgt-trigger"
                               value="scroll" <?php if ( $popup_trigger == "scroll" ) {
                            echo esc_attr("checked");
                        } ?>>
                        <span><label class="eapsgt-label_trigger"
                                     for="eapsgt-on_scroll_trigger"><?php esc_html_e( "On Scroll", 'easy-animated-popup' ); ?></span></label>
                        <br>
                        <div id="eapsgt-on_scroll_help"
                             class="eapsgt-help <?php if ( $popup_trigger !== 'scroll' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>">
                            <div class="eapsgt-slider" id="eapsgt-scroll_slider"></div>
                            <input class="eapsgt-value_input" type="number"
                                   id="eapsgt-scroll_slider_value" name="eapsgt-scroll_value"
                                   value="<?php echo esc_attr($scroll_value); ?>" min="10" max="100"/> %<br>
                        </div>
                        <input type="radio" id="eapsgt-on_exit_trigger" name="eapsgt-trigger"
                               value="exit" <?php if ( $popup_trigger == "exit" ) {
                            echo esc_attr("checked");
                        } ?>>
                        <span><label class="eapsgt-label_trigger"
                                     for="eapsgt-on_exit_trigger"><?php esc_html_e( "On Exit", 'easy-animated-popup' ); ?></span></label>
                        <br>
                    </td>
                </tr>
                <tr>
                    <th>
                        <div class="eapsgt-options"><?php esc_html_e( "Target page", 'easy-animated-popup' ); ?></div>
                        <div><?php esc_html_e( "Popup will appear on the page(s) you choose", 'easy-animated-popup' ); ?></div>
                        <div class="eapsgt-yellow" id="eapsgt-page_check_warning"><span
                                    class="eapsgt-warn eapsgt-warning"></span><?php esc_html_e( "You should check at least one post", 'easy-animated-popup' ); ?>
                        </div>
                    </th>
                    <td>
                        <div class="eapsgt-list_checkbox" id="eapsgt-pages_list">
                            <input type="text" id="eapsgt_page_filter" placeholder="Search by page title">
                            <button type="button"
                                    class="eapsgt-page_check_all eapsgt-icon-btn eapsgt-add-btn">
                                <div class="eapsgt-page_check-icon eapsgt-add-icon"></div>
                                <div class="eapsgt-page_check-btn-txt eapsgt-btn-txt"><?php esc_html_e( "Check all", 'easy-animated-popup' ); ?></div>
                            </button>
                            <br>
                            <div class="eapsgt-list_posts">
                                <?php
                                foreach ( $pages as $page ) {
                                    $pageTitle = $page->post_title;
                                    $pageId    = $page->ID;
                                    ?>
                                    <input class="eapsgt-page_check" type="checkbox"
                                           id="eapsgt_page_list<?php echo esc_attr($pageId) ?>"
                                           name="eapsgt-page_list[]"
                                           value="<?php echo esc_attr($pageId) ?>" <?php echo ( in_array( $pageId, $page_list_checked ) ) ? 'checked="checked"' : ''; ?> />
                                    <label for="eapsgt_page_list<?php echo esc_attr($pageId) ?>"><span><?php echo esc_html($pageTitle) ?></span><br></label>
                                    <?php
                                }
                                ?>
                            </div>
                        </div>

                    </td>
                </tr>
                <tr>
                    <th>
                        <div class="eapsgt-options"><?php esc_html_e( "Target post", 'easy-animated-popup' ); ?></div>
                        <div><?php esc_html_e( "Popup will appear in the post(s) you choose", 'easy-animated-popup' ); ?></div>
                        <div class="eapsgt-yellow" id="eapsgt-post_check_warning"><span
                                    class="eapsgt-warn eapsgt-warning"></span><?php esc_html_e( "You should check at least one post", 'easy-animated-popup' ); ?>
                        </div>
                    </th>
                    <td>
                        <div class="eapsgt-list_checkbox" id="eapsgt-posts_list">
                            <input type="text" id="eapsgt_post_filter" placeholder="Search by post title">
                            <button type="button"
                                    class="eapsgt-post_check_all eapsgt-icon-btn eapsgt-add-btn">
                                <div class="eapsgt-post_check-icon eapsgt-add-icon"></div>
                                <div class="eapsgt-post_check-btn-txt eapsgt-btn-txt"><?php esc_html_e( "Check all", 'easy-animated-popup' ); ?></div>
                            </button>
                            <br>
                            <div class="eapsgt-list_posts">
                                <?php
                                foreach ( $posts as $post ) {
                                    $postTitle = $post->post_title;
                                    $postId    = $post->ID;
                                    ?>
                                    <input class="eapsgt-post_check" type="checkbox"
                                           id="eapsgt_post_list<?php echo esc_attr($postId) ?>"
                                           name="eapsgt-post_list[]"
                                           value="<?php echo esc_attr($postId) ?>" <?php echo ( in_array( $postId, $post_list_checked ) ) ? 'checked="checked"' : ''; ?> />
                                    <label for="eapsgt_post_list<?php echo esc_attr($postId) ?>"><span><?php echo esc_html($postTitle) ?></span><br></label>
                                    <?php
                                }
                                ?>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <th>
                        <div class="eapsgt-options"><?php esc_html_e( "Frequency", 'easy-animated-popup' ); ?></div>
                        <div id="eapsgt-first_time_des"
                             class="eapsgt-frequency-des <?php if ( $popup_frequency !== 'firstTimeOnly' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>">
                            <?php esc_html_e( "The popup will only appear once", 'easy-animated-popup' ); ?>
                        </div>
                        <div id="eapsgt-everytime_des"
                             class="eapsgt-frequency-des <?php if ( $popup_frequency !== 'everytime' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>">
                            <?php esc_html_e( "The popup will appear each time you load the page", 'easy-animated-popup' ); ?>
                        </div>
                        <div id="eapsgt-loop_des"
                             class="eapsgt-frequency-des <?php if ( $popup_frequency !== 'loop' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>">
                            <?php esc_html_e( "The popup will appear according to the cycle you set (by date)", 'easy-animated-popup' ); ?>
                        </div>
                    </th>
                    <td>
                        <input id="eapsgt-first_time_frequency" type="radio"
                               name="eapsgt-frequency"
                               value="firstTimeOnly" <?php if ( $popup_frequency == "firstTimeOnly" ) {
                            echo esc_attr("checked");
                        } ?>>
                        <label class="eapsgt-label_trigger"
                               for="eapsgt-first_time_frequency"><span><?php esc_html_e( "First time only", 'easy-animated-popup' ); ?></span></label><br>
                        <input id="eapsgt-everytime_frequency" type="radio"
                               name="eapsgt-frequency"
                               value="everytime" <?php if ( $popup_frequency == "everytime" ) {
                            echo esc_attr("checked");
                        } ?>>
                        <label class="eapsgt-label_trigger"
                               for="eapsgt-everytime_frequency"><span><?php esc_html_e( "Everytime", 'easy-animated-popup' ); ?></span></label><br>
                        <input id="eapsgt-loop_frequency" type="radio" name="eapsgt-frequency"
                               value="loop" <?php if ( $popup_frequency == "loop" ) {
                            echo esc_attr("checked");
                        } ?>>
                        <label class="eapsgt-label_trigger"
                               for="eapsgt-loop_frequency"><span><?php esc_html_e( "Loop", 'easy-animated-popup' ); ?></span></label><br>
                        <div id="eapsgt-loop_help"
                             class="eapsgt-help-frequency <?php if ( $popup_frequency !== 'loop' ) {
                                 echo esc_attr('eapsgt-hidden');
                             } ?>">
                            <?php esc_html_e( "After", 'easy-animated-popup' ); ?><input value="<?php echo esc_attr($frequency_value); ?>"
                                                                                        class="eapsgt-value_input"
                                                                                        type="number"
                                                                                        name="eapsgt-frequency_value"
                                                                                        min="1"> <?php esc_html_e( "day(s)", 'easy-animated-popup' ); ?>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>
	<?php
}

// Add filter for Display settings
add_filter( 'eapsgt_tab_display', 'eapsgt_display_settings' );
