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


	// Function to get permalink by page ID
	function fbbttm_get_custom_permalink_by_page_id($page_id) {
		    if (get_post_status($page_id)) {
		        return get_permalink($page_id);
		    } else {
		        return 'Invalid page ID.';
		    }
	}
		

	// Example usage of the function
	function fbbttm_page_usage_of_custom_var() {
	  

	    global $fbbttm_helperobj,$wpdb,$fbbttm_settingdata,$fbbttm_settingvalarray,$fbbttm_tabledataobj;


	    $table_fbbttm_setting = FBBTTM_TB_SETTING;
    	
    	// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery 
    	// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching  


		$fbbttm_settingvalarray  = $wpdb->get_results(
			// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			"SELECT * FROM $table_fbbttm_setting ORDER BY fbbttm_setting_id DESC LIMIT 50", ARRAY_A);

		$fbbttm_settingdata = array();
		for($i=0;$i<count($fbbttm_settingvalarray);$i++)
		{

			$key = $fbbttm_settingvalarray[$i]['fbbttm_setting_name'];
			$value = $fbbttm_settingvalarray[$i]['fbbttm_setting_value'];
			$fbbttm_settingdata[$key] = $value;

		}

	    $fbbttm_tabledataobj->fbbttm_set_countryinfo(FBBTTM_COUNTRIES);
	    $fbbttm_tabledataobj->fbbttm_set_stateinfo(FBBTTM_STATES);
	    $fbbttm_tabledataobj->fbbttm_set_currency_info(FBBTTM_CURRENCIES);

	    /*shop page link*/
		$shoppage = $fbbttm_helperobj->fbbttm_get_single_commonquery('fbbttm_shop_page');
		
		if(!empty($shoppage))
		{
			$pageid = $shoppage[0]['fbbttm_setting_value'];
		}
		else{

			$pageid = get_option( 'fbbttm_fbbttm-shop_page_id' );
		}

		$shoppagelink = fbbttm_get_custom_permalink_by_page_id($pageid);

		/*cart page link*/

		$cartpage = $fbbttm_helperobj->fbbttm_get_single_commonquery('fbbttm_cart_page');
		$cartpageid ='';
		if(!empty($cartpage))
		{
			$cartpageid = $cartpage[0]['fbbttm_setting_value'];
		}
		else{

			$cartpageid = get_option( 'fbbttm_fbbttm-cart_page_id' );
		}

		$cartpagelink = fbbttm_get_custom_permalink_by_page_id($cartpageid);


		/*checkout page link*/

		$checkoutpage = $fbbttm_helperobj->fbbttm_get_single_commonquery('fbbttm_checkout_page');
		$checkoutpageid = '';
		if(!empty($checkoutpage))
		{
			$checkoutpageid = $checkoutpage[0]['fbbttm_setting_value'];
		}
		else{

			$checkoutpageid = get_option( 'fbbttm_fbbttm-checkout_page_id' );
		}

		$checkoutpagelink = fbbttm_get_custom_permalink_by_page_id($checkoutpageid);


		/*myaccount page link*/
		$myaccountpage = $fbbttm_helperobj->fbbttm_get_single_commonquery('fbbttm_myaccount_page');
		$myaccountpageid   ='';
		
		if(!empty($myaccountpage))
		{
			$myaccountpageid = $myaccountpage[0]['fbbttm_setting_value'];
		}
		else{

			$myaccountpageid = get_option( 'fbbttm_fbbttm-myaccount_page_id' );
		}

		$myaccountpagelink = fbbttm_get_custom_permalink_by_page_id($myaccountpageid);


		/*myaccount page link*/

		$orderrecivedpage = $fbbttm_helperobj->fbbttm_get_single_commonquery('fbbttm_order_received');
		
		$orderpageid   ='';	
		if(!empty($orderrecivedpage))
		{
			$orderpageid = $orderrecivedpage[0]['fbbttm_setting_value'];
		}
		else{

			$orderpageid = get_option( 'fbbttm_fbbttm-order-received_page_id' );
		}

		$orderrecivedpagelink = fbbttm_get_custom_permalink_by_page_id($orderpageid);


		/*Tax percent*/

		$taxdata = $fbbttm_helperobj->fbbttm_get_single_commonquery('fbbttm_tax_per');



		
		
		$taxpercent   ='';	
		if(!empty($fbbttm_settingdata) && $fbbttm_settingdata['fbbttm_tax_per'])
		{	
			$taxpercent = $fbbttm_settingdata['fbbttm_tax_per'];
		}
		else{

			$taxpercent = '10';
		}


		/*Currency*/

		$currencydata = $fbbttm_helperobj->fbbttm_get_single_commonquery('fbbttm_pay_currency');

		$currency   ='';	
		if(!empty($fbbttm_settingdata) && $fbbttm_settingdata['fbbttm_pay_currency'] != 'Select currency')
		{
			$currency = $fbbttm_settingdata['fbbttm_pay_currency'];
		}
		else{

			$currency = 'USD';
		}

		$currencydata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo(FBBTTM_CURRENCIES,'code',$currency);

		$symbol = '$';
		if(!empty($currencydata))
		{
			if(!empty($currencydata[0]->symbol)){
				$symbol = $currencydata[0]->symbol;
			}
		}
		

		define('FBBTTM_SHOP_PAGE_LINK', $shoppagelink);
		define('FBBTTM_CART_PAGE_LINK', $cartpagelink);
		define('FBBTTM_CHECKOUT_PAGE_LINK', $checkoutpagelink);
		define('FBBTTM_MYACCOUNT_PAGE_LINK', $myaccountpagelink);
		define('FBBTTM_ORDERRECIEVED_PAGE_LINK', $orderrecivedpagelink);

		define('FBBTTM_DEFAULT_TAX_PERCENT', $taxpercent);
		define( 'FBBTTM_CURRENCY', $currency );
		define( 'FBBTTM_CURRENCY_SYMBOL', $symbol );

		

	  }	
   
	add_action('wp_loaded', 'fbbttm_page_usage_of_custom_var');

	

	function fbbttm_add_inline_dynamic_css(){

		global $fbbttm_helperobj,$wpdb,$fbbttm_settingdata,$fbbttm_settingvalarray;

		$css = '';
		
		// Fetching dynamic-style.php output and store in a variable
		ob_start();
		//include get_template_directory().'/css/dynamic-style.php';


		if(!empty($fbbttm_settingdata) && isset($fbbttm_settingdata['fbbttm_color_option_status'])) 
		{
			$status =  esc_attr($fbbttm_settingdata['fbbttm_color_option_status']); 
		}
		else {
			$status = '';
		}

		if(!empty($fbbttm_settingdata) && isset($fbbttm_settingdata['fbbttm_button_bg_color'])) 
		{
			$fbbttm_buttonbg_color =  esc_attr($fbbttm_settingdata['fbbttm_button_bg_color']); 
		}
		else {
			$fbbttm_buttonbg_color = '';
		}


			if(!empty($fbbttm_settingdata) && isset($fbbttm_settingdata['fbbttm_button_text_color'])) 
			{
				$fbbttm_button_text_color =  esc_attr($fbbttm_settingdata['fbbttm_button_text_color']); 
			}
			else 
			{
				$fbbttm_button_text_color = '';
			}


			$fbbttm_button_bg_var = "--fbbttm-button-bg-color:inherit";
			$fbbttm_button_text_var = "--fbbttm-button-text-color:inherit";	

			if($status=='yes')
			{
				if(!empty($fbbttm_button_text_color))
				{
					$fbbttm_button_text_var = "--fbbttm-button-text-color:$fbbttm_button_text_color;";	
				}

				if(!empty($fbbttm_buttonbg_color))
				{
					$fbbttm_button_bg_var = "--fbbttm-button-bg-color:$fbbttm_buttonbg_color;";	
				}
			}

		$css  .= ':root{'.$fbbttm_button_text_var. $fbbttm_button_bg_var.'}';
		
		// spirobiz main style
		wp_add_inline_style( 'foodboxbooker-front', $css );
	}


	add_action( 'wp_enqueue_scripts', 'fbbttm_add_inline_dynamic_css', 20 );


?>