<?php

use Carbon_Fields\Container;
use Carbon_Fields\Field;

class DCBP_Admin
{

	private $plugin_name;
	private $version;
	public function __construct($plugin_name, $version)
	{
		$this->plugin_name = $plugin_name;
		$this->version = $version;
	}

	public function settings_ui_save() {
		// Verify the nonce to ensure the request is secure.
		if ( ! isset( $_POST['carbon_fields_container_dartamas_plug-in_nonce'] ) || 
			 ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['carbon_fields_container_dartamas_plug-in_nonce'] ) ), 'carbon_fields_container_dartamas_plug-in_nonce' ) ) {
			die( 'Security check failed' );
		}
	
		// Sanitize and validate the data received from the POST request.
		if ( isset( $_POST['carbon_fields_compact_input'] ) && is_array( $_POST['carbon_fields_compact_input'] ) ) {
			// Unsplash before sanitization
			$data = array_map( 'sanitize_text_field', wp_unslash( $_POST['carbon_fields_compact_input'] ) );
			
			// Validate that the '_dartamas_selected_pages' exists and is a string.
			if ( isset( $data['_dartamas_selected_pages'] ) && is_string( $data['_dartamas_selected_pages'] ) ) {
				$selected_page = sanitize_text_field( $data['_dartamas_selected_pages'] );
	
				// Explode the string to get the individual post IDs.
				$post_ids = explode( "|", $selected_page );
				$index = 0;
	
				// Iterate through each post ID and update the post meta.
				foreach ( $post_ids as $post_id ) {
					$post_id = intval( $post_id ); // Ensure the post ID is an integer.
	
					// Only update if the post ID is valid (greater than 0).
					if ( $post_id > 0 ) {
						update_post_meta( $post_id, '_my_meta_value_key', 'on' );
						update_post_meta( $post_id, '_my_meta_value_key_id', '_dartamas_selected_pages|||'. intval( $index ) .'|value' );
					}
					$index += 1;
				}
			}
		}
	}
	
	
	public function enqueue_scripts()
	{
		wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/DCBP-admin.js', array('jquery'), $this->version, false);
	}
	public function enqueue_styles()
	{

		wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/DCBP-admin.css', array(), $this->version, 'all');
	}
	public function available_options() {
		$wp_pages = get_pages();		
		$pages = array();

		foreach($wp_pages as $page){
			$pages[$page->ID] = $page->post_title;
		}		

		return $pages;
	}
	
	public function setup_settings_ui()
	{
		Container::make('theme_options', __('dartamas','dartamas'))
			->set_icon( constant("DCBP_IMG_URL").'/logo.png')
			->add_fields(array(
				Field::make( 'html', 'crb_information_text' )
    			->set_html( '<img src="'. constant("DCBP_IMG_URL").'/icon.png" width="100" height="100"></img>				
				<h2 style="font-size:1vw">'. __('dartamas_Plugin_Description','dartamas').'</h2> <br>
				<input type="button" id="btn_register" class="dartamas-btn" value="'. __('register in dartamas','dartamas').'">
				<hr>'),
			))

			
			->add_fields(array(
				Field::make('html', 'crb_dartamas_account_info')
					->set_html('<input type="radio" checked><lable>'.__('I have an account','dartamas').'</lable>')
					->set_help_text(__("Get your free API key within the <a href=\"https://dartamas.com/\">dartamas Web Site.</a>",'dartamas'))
					,
			))

			->add_fields(array(
				Field::make('text', 'dartamas_token', __('API KEY','dartamas'))
				->set_required(true)				
				->set_attribute( 'placeholder', __("api key placeholder",'dartamas'))

			))
			->add_fields(array(
				Field::make( 'html', 'crb_setting' )
				->set_html('<hr><h1>'.__('Options','dartamas').'</h1>'),				
				Field::make('checkbox', 'dartamas_selected_page_only', __('Enable dartamas only for the selected WP Pages','dartamas'))
					->set_default_value(true)					
			))
			->add_fields(array(
				Field::make('multiselect', 'dartamas_exclude_pages', __('Select Exclude Pages','dartamas'))
					->set_options(array( $this, 'available_options' ))
					->set_conditional_logic(array(
						array(
							'field' => 'dartamas_selected_page_only',
							'value' => true,
							'compare' => '='
						)
					))
			))			
			->add_fields(array(
				Field::make('multiselect', 'dartamas_selected_pages', __('Select Pages','dartamas'))
					->add_options(array( $this, 'available_options' ))
					->set_conditional_logic(array(
						array(
							'field' => 'dartamas_selected_page_only',
							'value' => false,
							'compare' => '='
						)
					))
			));

	}
	
	public function crb_load()
	{
		\Carbon_Fields\Carbon_Fields::boot();
	}
}
