<?php


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

if ( ! class_exists( 'APBGF_Form_Setting' ) ) {
	
	class APBGF_Form_Setting {        
		/**
		 * Instance
		 *
		 * @var $instance
		 */
		private static $instance = null;

		/**
		 * Init the Dependencies.
		 */
		private function __construct() {

			//adding gfb setting menu for each gf form.
			add_filter('gform_form_settings_menu', array( $this, 'apbgf_form_settings_menu_tab' ));
			//adding setting page for gfb setting menu.
			add_action('gform_form_settings_page_apbgf_settings_page', array( $this, 'apbgf_form_settings_menu_page' ));
			//saving form setting page values
			add_action('admin_init', array( $this, 'apbgf_save_form_settings_menu' ));

			add_action('gform_field_standard_settings', array( $this, 'apbgf_appointment_calendar_fields' ), 10);
			//add_action('gform_editor_js', array( $this, 'apbgf_appointment_calendar_fields_script' ));          
			add_action('admin_enqueue_scripts',array( $this, 'apbgf_enqueue_admin_script') );

		}

		public function apbgf_enqueue_admin_script() {
		    $default_location = APBGF_Location::all_location();
		    $first_location_id = ($default_location instanceof WP_Post) ? $default_location->ID : '';

		    // Register your admin script
		    wp_register_script(
		        'apbgf-editor-script',
		        esc_url( APBGF_ADMIN_JS_URL ) . 'apbgf-editor-script.js', // Adjust path as needed
		        array('jquery'), // Dependencies
		        '1.0',
		        true // Load in footer
		    );

		    wp_localize_script('apbgf-editor-script', 'apbgf_admin_data', array(
		        'first_location_id' => $first_location_id,
		    ));

		    wp_enqueue_script('apbgf-editor-script');
		}

		public function apbgf_appointment_calendar_fields( $position ) {          

			if ( 50 == $position ) {
				// echo $position . '<br>';
				?>
				<li class="apbgf_location_setting field_setting apbgf_field_setting" style="display:none;">
					<?php
					$all_locations = APBGF_Location::all_location();                        
				
						$first_location_id = esc_attr($all_locations->ID);
					?>
						<input type="hidden" name="apbgf_location" id="apbgf_location" value="<?php echo esc_attr( $first_location_id ); ?>">
		
					<input type="hidden" name="apbgf_location_hide" id="apbgf_location_hide" value="yes">
				</li>

				<li class="apbgf_categories_setting field_setting apbgf_field_setting">

					<label for="apbgf_service_category">
						<?php esc_html_e('Select Service Categories', 'appointment-and-booking-for-gravity-forms'); ?><br>
						<?php
						$all_categories = APBGF_Service::all_categories();

						if (is_array($all_categories) && count($all_categories) > 0) {
							?>
							<select name="apbgf_service_category[]" id="apbgf_service_category" class="select2 apbgf_service_category" multiple="multiple" onchange="SetFieldProperty('apbgf_service_category', Array.from(this.selectedOptions).map(option => parseInt(option.value)));">
								<option value="0"><?php esc_html_e('All Service Categories', 'appointment-and-booking-for-gravity-forms'); ?></option>
								<?php
								foreach ($all_categories as $category) {
									echo '<option value="' . esc_attr($category->term_id) . '">' . esc_html($category->name) . '</option>';
								}
								?>
							</select>
							<?php
						}

						?>
					</label>

					<input type="checkbox" name="apbgf_service_category_hide" id="apbgf_service_category_hide" value="yes" onclick="SetFieldProperty('apbgf_service_category_hide', this.checked);">
					<label for="apbgf_service_category_hide">
						<?php esc_html_e('Hide Categories field from front-end', 'appointment-and-booking-for-gravity-forms'); ?>
					</label>
				</li>

				<li class="apbgf_service_setting field_setting apbgf_field_setting">
					<label for="apbgf_service">
						<?php esc_html_e('Select Service', 'appointment-and-booking-for-gravity-forms'); ?><br>
						<?php
						$all_service = APBGF_Service::all_service();                        
						if ( is_array( $all_service ) && count( $all_service ) > 0 ) {
							?>
							<select name="apbgf_service" id="apbgf_service" class="apbgf_service" onchange="SetFieldProperty( 'apbgf_service', this.value );">
								<option value="all"><?php esc_html_e('All Service', 'appointment-and-booking-for-gravity-forms'); ?></option>
								<?php
								foreach ($all_service as $value) {
									echo '<option value="' . esc_attr($value->ID) . '" >' . esc_attr($value->post_title) . '</option>';
								}
								?>
							</select>
							<?php
						}
						?>
					</label>

					<input type="checkbox" name="apbgf_service_hide" id="apbgf_service_hide" value="yes" onclick="SetFieldProperty('apbgf_service_hide', this.checked);">
					<label for="apbgf_service_hide">
						<?php esc_html_e('Hide Service field from front-end', 'appointment-and-booking-for-gravity-forms'); ?>
					</label>
				</li>

				<li class="apbgf_staff_setting field_setting apbgf_field_setting">
					<label for="apbgf_staff">
						<?php esc_html_e('Select Staff', 'appointment-and-booking-for-gravity-forms'); ?><br>
						<?php
						$all_staff = APBGF_Staff::all_staff();                        
						if ( is_array( $all_staff ) && count( $all_staff ) > 0 ) {
							?>
							<select name="apbgf_staff" id="apbgf_staff" class="apbgf_staff" onchange="SetFieldProperty( 'apbgf_staff', this.value );">
								<option value="all"><?php esc_html_e('All Staff', 'appointment-and-booking-for-gravity-forms'); ?></option>
								<?php
								foreach ($all_staff as $value) {
									$user_id = get_post_meta( $value->ID, 'wp_user', true );                
									$userdata = get_userdata( $user_id );
									if ( $userdata ) {
										$staff_name = APBGF_Staff::get_staff_display_name( $value->ID );
										if ( ! empty( $staff_name ) ) {
											echo '<option value="' . esc_attr($value->ID) . '" >' . esc_html($staff_name) . '</option>';
										}
									}
								}
								?>
							</select>
							<?php
						}
						?>
					</label>

					<input type="checkbox" name="apbgf_staff_hide" id="apbgf_staff_hide" value="yes" onclick="SetFieldProperty('apbgf_staff_hide', this.checked);">
					<label for="apbgf_staff_hide">
						<?php esc_html_e('Hide Staff field from front-end', 'appointment-and-booking-for-gravity-forms'); ?>
					</label>
				</li>
				<?php
			}
		}

		public function apbgf_appointment_calendar_fields_script() {
			$default_location = APBGF_Location::all_location();
			$first_location_id = ( $default_location instanceof WP_Post ) ? esc_js($default_location->ID) : '';


		}

		public function apbgf_save_form_settings_menu() {

			if ( ! rgpost('apbgf_form_id') ) {
				return;
			}
			if (isset($_POST['apbgf_setting_message_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['apbgf_setting_message_nonce'] ) ), 'apbgf_setting_message_action' ) ) {
				if ( isset( $_POST['apbgf_setting'] ) ) {
					$form_id = rgpost('apbgf_form_id');
					$settings = get_option('apbgf_form_settings_' . $form_id); 

					if ( false == $settings ) {
						$settings = (array) $settings;
					}

					$settings['registration_field'] = array(
						'fname' => trim( rgpost('fname') ),
						'lname' => trim( rgpost('lname') ),
						'email' => trim( rgpost('email') ),
						'phone' => trim( rgpost('phone') )
					);

					update_option('apbgf_form_settings_' . $form_id, $settings);
				}
			}
		}

		public function apbgf_form_settings_menu_page() {

			$form_id = RGForms::get('id');
			$form   = \GFAPI::get_form($form_id);
			$all_fields = array();
			foreach ( $form['fields'] as $field ) {
				if ( $field->is_conditional_logic_supported() ) {
					$inputs = $field->get_entry_inputs();

					if ( $inputs ) {

						// $choices = array();

						foreach ( $inputs as $input ) {
							if ( rgar( $input, 'isHidden' ) ) {
								continue;
							}
							$all_fields[] = array(
								'value' => $input['id'],
								'label' => GFCommon::get_label( $field, $input['id'], true ) . ' Name'
							);
						}                       

					} else {
						$all_fields[] = array(
							'value' => $field->id,
							'label' => GFCommon::get_label( $field )
						);
					}

				}
			}

			$settings = get_option('apbgf_form_settings_' . $form_id);           
			if ( false == $settings ) {   
				$settings = (array) $settings;

				$settings['registration_field'] = array(
					'fname' => '',
					'lname' => '',
					'email' => '',
					'phone' => ''                   
				);
			}

			GFFormSettings::page_header();
			require_once APBGF_ADMIN_VIEW_PATH . 'apbgf-form-setting.php';
			GFFormSettings::page_footer();
		}

		public function apbgf_form_settings_menu_tab( $menu_items ) {

			$menu_items[] = array(
				'name' => 'apbgf_settings_page',
				'icon' => 'dashicons dashicons-calendar-alt',
				'label' => esc_html__('GFB Setting', 'appointment-and-booking-for-gravity-forms')
			);

			return $menu_items;
		}


		/**
		 * Singleton Class Instatnce.
		 */
		public static function get_instance() {
			if ( null === self::$instance ) {
				self::$instance = new APBGF_Form_Setting();
			}

			return self::$instance;
		}
	}

	APBGF_Form_Setting::get_instance();
}
