<?php
/**
 * Admin Panel UI - Parts
 *
 * @version  1.2
 * @package  Any
 * @category Page Structure in Admin Panel
 * @author   wpdevelop
 *
 * @web-site https://wpbookingcalendar.com/
 * @email info@wpbookingcalendar.com
 *
 * @modified 2025-02-15
 */

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

/**
 * Show Top Nav bar
 *
 * @param array $args - parameters.
 *
 * @return void
 */
function wpbc_ui__top_nav( $args = array() ) {

	$defaults = array(
		'attr'                             => array(),
		'is_full_screen_user_mode_enabled' => true,
	);
	$params   = wp_parse_args( $args, $defaults );

	echo '<div class="wpbc_ui_el wpbc_ui_el__top_nav">';

	wpbc_ui__vert_left_bar__do_toggle();
	wpbc_ui_el__divider_vertical( array( 'class' => 'wpbc_ui_el__vertical_line wpbc_ui__top_nav__btn_show_left_vertical_nav_divider' ) );

	wpbc_ui__top_nav__dropdown__wpbc();

	do_action( 'wpbc_ui_el__top_nav__content_start', $params['page_tag'], $params['active_page_tab'], $params['active_page_subtab'] );


	?><div class="wpbc_ui_el__make_space"></div><?php  	// Spacer.

	do_action( 'wpbc_ui_el__top_nav__content_center', $params['page_tag'], $params['active_page_tab'], $params['active_page_subtab'] );

	// Load Top News Message and "Search by ID" fields.
	echo '<style type="text/css"> .wpbc_message_wrapper  { margin-left: 0px !important; } </style>';
	make_bk_action( 'wpbc_h1_header_content_end', $params['page_tag'], $params['active_page_tab'], $params['active_page_subtab'] );

	?><div class="wpbc_ui_el__make_space"></div><?php	// Spacer.

	do_action( 'wpbc_ui_el__top_nav__content_end', $params['page_tag'], $params['active_page_tab'], $params['active_page_subtab'] );

	wpbc_ui_el__divider_vertical();

	// Right sidebar toggle button.
	wpbc_ui__vert_right_bar__do_toggle();
	wpbc_ui_el__divider_vertical( array( 'class' => 'wpbc_ui_el__vertical_line wpbc_ui__top_nav__btn_show_right_vertical_nav_divider' ) );


	// Full Screen Buttons.
	wpbc_ui__top_nav__btn_full_screen( $params );
	wpbc_ui__top_nav__btn_normal_screen( $params );

	echo '</div>';
}



/**
 * Show element - "WPBC - Main Dropdown"
 *
 * @return void
 */
function wpbc_ui__top_nav__dropdown__wpbc() {

	$svg_size       = '22px';
	$svg_icon_style =  'margin:5px 5px 0 0;'; // 'background-position: 0 0;background-size: ' . $svg_size . ' ' . $svg_size . ';width: ' . $svg_size . ';height: ' . $svg_size . ';'; //.
	$svg_icon       = wpbc_get_svg_logo_for_background( '#555', '#e5e5e5', '1.0' );

	$wpbc_starter_pages       = function_exists( 'wpbc_get_published_activation_booking_pages' ) ? wpbc_get_published_activation_booking_pages() : array();
	$wp_post_booking_absolute = false;

	if (
		empty( $wpbc_starter_pages ) &&
		function_exists( 'wpbc_stp_wiz__is_exist_published_page_with_booking_form' )
	) {
		$wp_post_booking_absolute = wpbc_stp_wiz__is_exist_published_page_with_booking_form();
	}

	$el_arr = array(
		// 'title'        => 'Booking Calendar',
		// 'font_icon'    => 'wpbc-bi-calendar2-range',
		'title_html'      => '<span class="nav-tab-text" style="margin: -3px 0 0 5px;font-size: 16px;padding: 0;"><span style="position: absolute;font-size: 7px;margin-top: 13px;margin-left: 1px;">WP</span>Booking Calendar</span>',
		'svg_icon'        => $svg_icon,
		'svg_icon_style'  => $svg_icon_style,
		'style'           => 'display: flex;flex-flow:row nowrap;align-items: center;justify-content: flex-start; ',
		'container_style' => 'padding: 0 15px 0 10px;',
		'position'        => 'left',
		'has_down_arrow'  => true,
		'items'           => array(),
	);

	if ( ! empty( $wpbc_starter_pages ) && is_array( $wpbc_starter_pages ) ) {
		$el_arr['items'][] = array(
			'type'  => 'header',
			'title' => __( 'Visit Booking Pages', 'booking' ),
		);
		$el_arr['items'][] = array(
			'type'  => 'link',
			'title' => __( 'Visit Home Page', 'booking' ),
			'url'   => esc_url( home_url( '/' ) ),
		);

		foreach ( $wpbc_starter_pages as $wpbc_starter_page ) {
			if ( empty( $wpbc_starter_page['url'] ) ) {
				continue;
			}

			$wpbc_starter_page_title = __( 'Go to page with booking form', 'booking' );
			if ( ! empty( $wpbc_starter_page['button_title'] ) ) {
				$wpbc_starter_page_title = $wpbc_starter_page['button_title'];
			} elseif ( ! empty( $wpbc_starter_page['page_title'] ) ) {
				$wpbc_starter_page_title = $wpbc_starter_page['page_title'];
			}

			$el_arr['items'][] = array(
				'type'  => 'link',
				'title' => $wpbc_starter_page_title,
				'url'   => esc_url( $wpbc_starter_page['url'] ),
			);
		}
	} elseif ( ! empty( $wp_post_booking_absolute ) ) {
		$el_arr['items'][] = array(
			'type'  => 'header',
			'title' => __( 'Visit Booking Page', 'booking' ),
		);
		$el_arr['items'][] = array(
			'type'  => 'link',
			'title' => __( 'Visit Home Page', 'booking' ),
			'url'   => esc_url( home_url( '/' ) ),
		);
		$el_arr['items'][] = array(
			'type'  => 'link',
			'title' => __( 'Go to page with booking form', 'booking' ),
			'url'   => esc_url( $wp_post_booking_absolute ),
		);
	}
	$el_arr['items'][] = array(
		'type'  => 'header',
		'title' => __( 'Help', 'booking' ),
	);
	$el_arr['items'][] = array(
		'type'  => 'link',
		'title' => __( 'FAQ', 'booking' ),
		'url'   => 'https://wpbookingcalendar.com/faq/',
	);
	$el_arr['items'][] = array(
		'type'  => 'header',
		'title' => __( 'Support', 'booking' ),
	);
	$el_arr['items'][] = array(
		'type'  => 'link',
		'title' => __( 'Support Forum', 'booking' ),
		'url'   => 'https://wpbookingcalendar.com/support/',
	);
	$el_arr['items'][] = array(
		'type'  => 'link',
		'title' => __( 'Contact Support', 'booking' ),
		'url'   => 'mailto:support@wpbookingcalendar.com',
		'attr'  => array( 'style' => 'font-weight: 600;' ),
	);
	$el_arr['items'][] = array( 'type' => 'divider' );
	$el_arr['items'][] = array(
		'type'  => 'link',
		'title' => __( 'Release History', 'booking' ),
		'url'   => 'https://wpbookingcalendar.com/wn/',
	);
	$el_arr['items'][] = array(
		'type'  => 'link',
		'title' => __( 'What\'s New', 'booking' ) . ' <span style="float:right">' . esc_attr( WP_BK_VERSION_NUM ) . '</span>',
		'url'   => esc_url( admin_url( add_query_arg( array( 'page' => 'wpbc-about' ), 'index.php' ) ) ),
	);
//	$el_arr['items'][] = array(
//		'type'  => 'link',
//		'title' => __( 'About', 'booking' ),
//		'url'   => 'https://wpbookingcalendar.com/',
//	);


	$is_show_up = wpbc_is_show_up();

	if ( $is_show_up ) {
		$el_arr['items'][] = array( 'type' => 'divider' );
		$el_arr['items'][] = array(
			'type'  => 'link',
			'title' => ( class_exists( 'wpdev_bk_personal' ) )
				? __( 'View Upgrade Options', 'booking' )
				: __( 'Upgrade to Pro', 'booking' ),
			'url'   => wpbc_up_link(),
			'attr' => array(
				'class' => 'wpbc_ui_el_upgrade_button wpbc_button_light wpbc_button_green',
				'style' => 'color:#fff;font-weight: 600;',
			),
		);
	}

	wpbc_ui_el__dropdown_menu( $el_arr );
}


/**
 * Show element - "Full Screen"
 *
 * @return void
 */
function wpbc_ui__top_nav__btn_full_screen( $args = array() ) {

	$defaults = array(
		'is_full_screen_user_mode_enabled' => true,
	);
	$params   = wp_parse_args( $args, $defaults );

	$el_arr = array();

	$el_arr['onclick'] = 'wpbc_admin_ui__full_screen__do_on( this, ' . ( $params['is_full_screen_user_mode_enabled'] ? 'true' : 'false' ) . ' );';

	$el_arr['font_icon'] = 'wpbc-bi-arrows-fullscreen';
	$el_arr['hint']      = array(
		'title'    => __( 'Full Screen', 'booking' ),
		'position' => 'top',
	);

	$el_arr['container_class'] = 'wpbc_ui__top_nav__btn_full_screen';

	if ( ( ! wpbc_is_setup_wizard_page() ) && ( $params['is_full_screen_user_mode_enabled'] ) ) {

		// Params for saving user option.
		$user_cust_option = 'is_full_screen';
		$nonce_action     = $user_cust_option . '_nonce_act';

		$el_arr['attr'] = array(
			'data-wpbc-u-save-name'    => $user_cust_option,
			'data-wpbc-u-save-value'   => 'On',
			'data-wpbc-u-save-nonce'   => wp_create_nonce( $nonce_action ), // do not need to  make escaping the values because: wpbc_get_custom_attr() should handle escaping.
			'data-wpbc-u-save-user-id' => wpbc_get_current_user_id(),
			'data-wpbc-u-save-action'  => $nonce_action,
		);
		?><script type="text/javascript"><?php

			$is_full_screen = WPBC_User_Custom_Data_Saver::get_user_data_value( wpbc_get_current_user_id(), $user_cust_option );
			$is_full_screen = ( 'On' === wpbc_ui__get_full_screen_mode_from_cookie( $is_full_screen ) );

			echo wpbc_jq_ready_start();  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

			if ( $is_full_screen ) {
				echo " jQuery( '.wpbc_ui__top_nav__btn_full_screen,.wpbc_ui__top_nav__btn_normal_screen' ).toggleClass( 'wpbc_ui__hide' ); ";
			}
			echo ' wpbc_check_full_screen_mode(); ';
			echo wpbc_jq_ready_end();  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		?></script><?php
	}
	wpbc_ui_el__a( $el_arr );
}


/**
 * Show element - "Normal Screen"
 *
 * @return void
 */
function wpbc_ui__top_nav__btn_normal_screen( $args = array() ) {

	$defaults = array(
		'is_full_screen_user_mode_enabled' => true,
	);
	$params   = wp_parse_args( $args, $defaults );

	$el_arr = array();

	$el_arr['onclick'] = 'wpbc_admin_ui__full_screen__do_off( this, ' . ( $params['is_full_screen_user_mode_enabled'] ? 'true' : 'false' ) . ' );';

	$el_arr['title']     = '';
	$el_arr['font_icon'] = 'wpbc-bi-arrows-angle-contract';
	$el_arr['hint']      = array(
		'title'    => __( 'Exit Full Screen', 'booking' ),
		'position' => 'top',
	);
	$el_arr['container_class'] = 'wpbc_ui__top_nav__btn_normal_screen wpbc_ui__hide';

	if ( $params['is_full_screen_user_mode_enabled'] ) {
		// Params for saving user option.
		$user_cust_option = 'is_full_screen';
		$nonce_action     = $user_cust_option . '_nonce_act';
		$el_arr['attr']   = array(
			'data-wpbc-u-save-name'    => $user_cust_option,
			'data-wpbc-u-save-value'   => 'Off',
			'data-wpbc-u-save-nonce'   => wp_create_nonce( $nonce_action ), // do not need to  make escaping the values because: wpbc_get_custom_attr() should handle escaping.
			'data-wpbc-u-save-user-id' => wpbc_get_current_user_id(),
			'data-wpbc-u-save-action'  => $nonce_action,
		);
	}

	wpbc_ui_el__a( $el_arr );
}


/**
 * Get Full Screen mode from the immediate browser cookie, falling back to saved user meta value.
 *
 * @param string $saved_value Saved user-meta value.
 *
 * @return string 'On', 'Off', or empty string.
 */
function wpbc_ui__get_full_screen_mode_from_cookie( $saved_value = '' ) {

	$saved_value = in_array( $saved_value, array( 'On', 'Off' ), true ) ? $saved_value : '';

	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
	if ( ! isset( $_COOKIE['wpbc_admin_full_screen'] ) ) {
		return $saved_value;
	}

	// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
	$cookie_value = sanitize_text_field( $_COOKIE['wpbc_admin_full_screen'] );

	if ( in_array( $cookie_value, array( 'On', 'Off' ), true ) ) {
		return $cookie_value;
	}

	return $saved_value;
}


/**
 * Set the admin full screen class
 *
 * @param bool $classes Body classes.
 *
 * @return array
 */
function wpbc_check_full_screen_mode_on_loading( $classes ) {

	$is_full_screen = WPBC_User_Custom_Data_Saver::get_user_data_value( wpbc_get_current_user_id(), 'is_full_screen' );
	$is_full_screen = wpbc_ui__get_full_screen_mode_from_cookie( $is_full_screen );
	$is_full_screen = ( 'On' === $is_full_screen );

	if ( ( wpbc_is_this_plugin_page() ) && ( $is_full_screen ) && ( ! wpbc_is_setup_wizard_page() ) ) {
		$classes .= ' wpbc_admin_full_screen';
	}

	return $classes;
}
add_filter( 'admin_body_class', 'wpbc_check_full_screen_mode_on_loading' );
