<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       https://www.ibsofts.com
 * @since      2.2.4
 *
 * @package    Boom_Fest
 * @subpackage Boom_Fest/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Boom_Fest
 * @subpackage Boom_Fest/admin
 * @author     iB Arts Pvt. Ltd. <support@ibarts.in>
 */

class Boom_Fest_Admin
{

	/**
	 * The ID of this plugin.
	 *
	 * @since    2.2.4
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    2.2.4
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    2.2.4
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct($plugin_name, $version)
	{

		$this->plugin_name = $plugin_name;
		$this->version = $version;
	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    2.2.4
	 */
	public function enqueue_styles()
	{

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Boom_Fest_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Boom_Fest_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */
		wp_enqueue_style($this->plugin_name, BOOM_FEST_URL . 'admin/css/boom-fest-admin.css', array(), $this->version, 'all');
		wp_enqueue_style('bootstrap-css', BOOM_FEST_URL . 'admin/css/bootstrap.min.css', array(), $this->version, 'all');
		wp_enqueue_style('chosen-css', BOOM_FEST_URL . 'admin/css/chosen.min.css', array(), $this->version, 'all');
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    2.2.4
	 */
	public function enqueue_scripts()
	{

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Boom_Fest_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Boom_Fest_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_script($this->plugin_name, BOOM_FEST_URL . 'admin/js/boom-fest-admin.js', array('jquery'), $this->version, false);
		wp_localize_script(
			$this->plugin_name,
			'ajax_object',
			array(
				'admin_url' => admin_url('admin-ajax.php'),
				'bf_admin_action_nonce' => wp_create_nonce('bf_admin_action_nonce'),
			)
		);



		wp_enqueue_script('bf_customfest', BOOM_FEST_URL . 'admin/js/boom-fest-customfest.js', array('jquery'), $this->version, false);
		wp_localize_script('bf_customfest', 'bf_ajax_object', array('admin_ajax_url' => admin_url('admin-ajax.php')));


		wp_enqueue_script('bootstrap-js', BOOM_FEST_URL . 'admin/js/bootstrap.bundle.min.js', array('jquery'), $this->version, false);
		wp_enqueue_script('chosen-js', BOOM_FEST_URL . 'admin/js/chosen.jquery.min.js', array('jquery'), $this->version, false);
	}

	/**
	 * Function for admin page menu setting
	 */

	public function bf_admin()
	{
		add_menu_page('Theme Setting', 'Boom Fest', 'administrator', 'boom-fest', array(__CLASS__, 'bf_admin_setting'), 'dashicons-bell');
		// Add submenu
		add_submenu_page(
			'boom-fest', // Parent slug (matches the main menu slug)
			'Our Products', // Page title
			'Our Products', // Submenu title
			'administrator', // Capability
			'our-products', // Submenu slug
			array(__CLASS__, 'our_products_callback') // Callback function
		);
	}

	/**
	 * Callback function for add_menu_page.
	 */

	public static function bf_admin_setting()
	{
		if (file_exists(BOOM_FEST_PATH . 'admin/partials/boom-fest-admin-display.php')) {
			require BOOM_FEST_PATH . 'admin/partials/boom-fest-admin-display.php';
		}
	}

	/**
	 * Callback function for add_submenu_page.
	 */
	public static function our_products_callback()
	{
		if (file_exists(BOOM_FEST_PATH . 'admin/partials/boom-fest-our-products.php')) {
			require BOOM_FEST_PATH . 'admin/partials/boom-fest-our-products.php';
		}
	}

	/**
	 * Ajax action function for admin side menu setting.
	 */
	public function bf_customfest()
	{
		// Check user capabilities to ensure only admins can execute this
		if (!current_user_can('manage_options')) {
			return;
		}
		global $wpdb;
		$table2 = $wpdb->prefix . 'boom_festive_activated';
		$festival = sanitize_text_field($_POST['festival']);
		$results = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'boom_festive_activated WHERE id = %d', 1));
		if ($results != null) {
			$wpdb->update($table2, array('festival' => $festival, 'celebration_type' => null, 'decoration_image' => null, 'font_style' => null), array('id' => 1));
			echo 'Festival has been updated!';
		} else {
			$result = $wpdb->query($wpdb->prepare('INSERT INTO ' . $wpdb->prefix . 'boom_festive_activated (festival) VALUES (%s)', 'new-year'));
			if ($result != false) {
				echo 'Festival has been saved!';
			} else {
				echo 'Some error occured!';
			}
		}
		die();
	}





	/**
	 * Ajax action function for admin side menu setting.
	 */

	public function bf_admin_action()
	{
		// Verify nonce for CSRF protection
		if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'bf_admin_action_nonce')) {
			wp_send_json_error('Invalid request. Nonce verification failed.', 403);
		}

		// Check user capabilities to ensure only admins can execute this
		if (!current_user_can('manage_options')) {
			wp_send_json_error('You do not have permission to perform this action.', 403);
		}

		global $wpdb;
		$table2 = $wpdb->prefix . 'boom_festive_activated';

		// Sanitize input fields
		$celebration_type = sanitize_text_field($_POST['celebration_type']);
		$decoration_image = sanitize_text_field($_POST['decoration_image']);
		$font_style = sanitize_text_field($_POST['font_style']);
		if (isset($_POST['pages']) && is_array($_POST['pages'])) {
			$pages = array_map('sanitize_text_field', $_POST['pages']);
		} else {
			$pages = array();
		}
		$pages = json_encode($pages);

		// Validate required fields
		if (empty($celebration_type) || empty($pages)) {
			wp_send_json_error('You must select a celebration type and pages.', 400);
		}

		// Check if an entry exists and update or insert settings in the database
		$results = $wpdb->get_results($wpdb->prepare(
			'SELECT * FROM ' . $wpdb->prefix . 'boom_festive_activated WHERE id = %d',
			1
		));

		if ($results != null) {
			$wpdb->update(
				$table2,
				array(
					'celebration_type' => $celebration_type,
					'decoration_image' => $decoration_image,
					'font_style' => $font_style,
					'pages' => $pages,
				),
				array('id' => 1)
			);
			wp_send_json_success('Decorations have been updated!');
		} else {
			$result = $wpdb->query($wpdb->prepare(
				'INSERT INTO ' . $wpdb->prefix . 'boom_festive_activated (celebration_type, decoration_image, font_style, pages) VALUES (%s, %s, %s, %s)',
				$celebration_type,
				$decoration_image,
				$font_style,
				$pages
			));
			if ($result != false) {
				wp_send_json_success('Decorations have been created!');
			} else {
				wp_send_json_error('An error occurred while saving decorations.', 500);
			}
		}
	}
}
