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

//require_once( str_replace('//','/',dirname(__FILE__).'/') .'../../../../wp-config.php');
global $wpdb, $fbbttm_helperobj;

check_ajax_referer( 'fbbttm_nonce_action', 'security' );

$fbbttm_pay_currency = $fbbttm_stripe_api = $fbbttm_stripe_scretkey = $fbbttm_payment_select=  $fbbttm_paypalid = $fbbttm_paypalapi = $fbbttm_payment_email = $fbbttm_api_username =$fbbttm_payment_mode = $fbbttm_s_adminmail = $fbbttm_s_subject = $fbbttm_paypal_method_status = $fbbttm_stripe_method_status = $fbbttm_shop_page = $fbbttm_cart_page = $fbbttm_checkout_page = $fbbttm_myaccount_page = $fbbttm_order_received = '';
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery 
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
if (isset($_POST['fbbttm_pay_currency'])) {
    $fbbttm_pay_currency = sanitize_text_field(wp_unslash($_POST['fbbttm_pay_currency']));
}

if (isset($_POST['fbbttm_stripe_api'])) {
    $fbbttm_stripe_api = sanitize_text_field(wp_unslash($_POST['fbbttm_stripe_api']));
}
if (isset($_POST['fbbttm_stripe_scretkey'])) {
    $fbbttm_stripe_scretkey = sanitize_text_field(wp_unslash($_POST['fbbttm_stripe_scretkey']));
}
if (isset($_POST['fbbttm_payment_select'])) {
    $fbbttm_payment_select = sanitize_text_field(wp_unslash($_POST['fbbttm_payment_select']));
}
if (isset($_POST['fbbttm_paypalid'])) {
    $fbbttm_paypalid = sanitize_text_field(wp_unslash($_POST['fbbttm_paypalid']));
}
if (isset($_POST['fbbttm_paypalapi'])) {
    $fbbttm_paypalapi = sanitize_text_field(wp_unslash($_POST['fbbttm_paypalapi']));
}
if (isset($_POST['fbbttm_payment_email'])) {
    $fbbttm_payment_email = sanitize_text_field(wp_unslash($_POST['fbbttm_payment_email']));
}
if (isset($_POST['fbbttm_api_username'])) {
    $fbbttm_api_username = sanitize_text_field(wp_unslash($_POST['fbbttm_api_username']));
}
if (isset($_POST['fbbttm_payment_mode'])) {
    $fbbttm_payment_mode = sanitize_text_field(wp_unslash($_POST['fbbttm_payment_mode']));
}

if (isset($_POST['fbbttm_s_adminmail'])) {
    $fbbttm_s_adminmail = sanitize_text_field(wp_unslash($_POST['fbbttm_s_adminmail']));
}
if (isset($_POST['fbbttm_s_subject'])) {
    $fbbttm_s_subject = sanitize_text_field(wp_unslash($_POST['fbbttm_s_subject']));
}	

if (isset($_POST['fbbttm_paypal_method_status'])) {
    $fbbttm_paypal_method_status = sanitize_text_field(wp_unslash($_POST['fbbttm_paypal_method_status']));
}	

if (isset($_POST['fbbttm_stripe_method_status'])) {
    $fbbttm_stripe_method_status = sanitize_text_field(wp_unslash($_POST['fbbttm_stripe_method_status']));
}		

if (isset($_POST['fbbttm_shop_page'])) {
    $fbbttm_shop_page = sanitize_text_field(wp_unslash($_POST['fbbttm_shop_page']));
}	

if (isset($_POST['fbbttm_cart_page'])) {
    $fbbttm_cart_page = sanitize_text_field(wp_unslash($_POST['fbbttm_cart_page']));
}

if (isset($_POST['fbbttm_checkout_page'])) {
    $fbbttm_checkout_page = sanitize_text_field(wp_unslash($_POST['fbbttm_checkout_page']));
}

if (isset($_POST['fbbttm_myaccount_page'])) {
    $fbbttm_myaccount_page = sanitize_text_field(wp_unslash($_POST['fbbttm_myaccount_page']));
}

if (isset($_POST['fbbttm_order_received'])) {
    $fbbttm_order_received = sanitize_text_field(wp_unslash($_POST['fbbttm_order_received']));
}

$security = 0;

if ( ! isset( $_POST[ 'fbbttm_meta_box_noncename' ] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash($_POST['fbbttm_meta_box_noncename'] ) ), 'fbbttm_meta_box_nonce' ) ) {
			  $security = 1;
			  $msg=['success'=>'Sorry, Your request can not be processed due to security reason.'];
			  //phpcs:ignore WordPress.WP.AlternativeFunctions.wp_json_encode_wp_json_encode 
			  echo wp_json_encode($msg);
			  die;
 }

if($security == 0){
global $wpdb, $table_fbbttm_setting, $fbbttm_result;

$table_fbbttm_setting = $wpdb->prefix . 'fbbttm_setting';
   
   $fbbttm_pay_currency =  $fbbttm_tax_per =  $fbbttm_vat_gst_tin =$fbbttm_stripe_method_status = $fbbttm_stripe_api =$fbbttm_stripe_scretkey =$fbbttm_payment_select = $fbbttm_paypal_method_status = $fbbttm_paypalid = $fbbttm_paypalapi = $fbbttm_payment_email = $fbbttm_api_username = $fbbttm_payment_mode = $fbbttm_s_adminmail = $fbbttm_s_subject = $fbbttm_email_header_image = $fbbttm_restaurant_name = $fbbttm_restaurant_address = $fbbttm_restaurant_address2 = $fbbttm_restaurant_city = $fbbttm_restaurant_country =$fbbttm_restaurant_state = $fbbttm_restaurant_zipcode = $fbbttm_restaurant_phone = $fbbttm_shop_page = $fbbttm_cart_page = $fbbttm_checkout_page = $fbbttm_myaccount_page = $fbbttm_order_received = '';
	
	if(isset($_POST['fbbttm_pay_currency']))
	{
	$fbbttm_pay_currency = sanitize_text_field(wp_unslash($_POST['fbbttm_pay_currency']));
	}
	if(isset($_POST['fbbttm_tax_per']))
	{
	$fbbttm_tax_per = sanitize_text_field(wp_unslash($_POST['fbbttm_tax_per']));
	}
	if(isset($_POST['fbbttm_vat_gst_tin']))
	{
	$fbbttm_vat_gst_tin = sanitize_text_field(wp_unslash($_POST['fbbttm_vat_gst_tin']));
	}
	if(isset($_POST['fbbttm_stripe_method_status']))
	{
	$fbbttm_stripe_method_status = sanitize_text_field(wp_unslash($_POST['fbbttm_stripe_method_status']));
	}
	if(isset($_POST['fbbttm_stripe_api']))
	{
	$fbbttm_stripe_api = sanitize_text_field(wp_unslash($_POST['fbbttm_stripe_api']));
	}
	if(isset($_POST['fbbttm_stripe_scretkey']))
	{
	$fbbttm_stripe_scretkey = sanitize_text_field(wp_unslash($_POST['fbbttm_stripe_scretkey']));
	}
	if(isset($_POST['fbbttm_payment_select']))
	{
	$fbbttm_payment_select = sanitize_text_field(wp_unslash($_POST['fbbttm_payment_select']));
	}
	if(isset($_POST['fbbttm_paypal_method_status']))
	{
	$fbbttm_paypal_method_status = sanitize_text_field(wp_unslash($_POST['fbbttm_paypal_method_status']));
	}
	if(isset($_POST['fbbttm_paypalid']))
	{
	$fbbttm_paypalid = sanitize_text_field(wp_unslash($_POST['fbbttm_paypalid']));
	}
	if(isset($_POST['fbbttm_paypalapi']))
	{
	$fbbttm_paypalapi = sanitize_text_field(wp_unslash($_POST['fbbttm_paypalapi']));
	}
	if(isset($_POST['fbbttm_payment_email']))
	{
	$fbbttm_payment_email = sanitize_text_field(wp_unslash($_POST['fbbttm_payment_email']));
	}
	if(isset($_POST['fbbttm_api_username']))
	{
	$fbbttm_api_username = sanitize_text_field(wp_unslash($_POST['fbbttm_api_username']));
	}
	if(isset($_POST['fbbttm_payment_mode']))
	{
	$fbbttm_payment_mode = sanitize_text_field(wp_unslash($_POST['fbbttm_payment_mode']));
	}

	if(isset($_POST['fbbttm_s_adminmail']))
	{
	$fbbttm_s_adminmail = sanitize_text_field(wp_unslash($_POST['fbbttm_s_adminmail']));
	}
	if(isset($_POST['fbbttm_s_subject']))
	{
	$fbbttm_s_subject = sanitize_text_field(wp_unslash($_POST['fbbttm_s_subject']));
	}	
	if(isset($_POST['fbbttm_email_header_image']))
	{
	$fbbttm_email_header_image = sanitize_text_field(wp_unslash($_POST['fbbttm_email_header_image']));
	}
	if(isset($_POST['fbbttm_restaurant_name']))
	{
	$fbbttm_restaurant_name = sanitize_text_field(wp_unslash($_POST['fbbttm_restaurant_name']));
	}

	if(isset($_POST['fbbttm_restaurant_address']))
	{
	$fbbttm_restaurant_address = sanitize_text_field(wp_unslash($_POST['fbbttm_restaurant_address']));
	}
	if(isset($_POST['fbbttm_restaurant_address2']))
	{
	$fbbttm_restaurant_address2 = sanitize_text_field(wp_unslash($_POST['fbbttm_restaurant_address2']));
	}
	if(isset($_POST['fbbttm_restaurant_city']))
	{
	$fbbttm_restaurant_city = sanitize_text_field(wp_unslash($_POST['fbbttm_restaurant_city']));
	}
	if(isset($_POST['fbbttm_restaurant_country']))
	{
	$fbbttm_restaurant_country = sanitize_text_field(wp_unslash($_POST['fbbttm_restaurant_country']));
	}
	if(isset($_POST['fbbttm_restaurant_state']))
	{
	$fbbttm_restaurant_state = sanitize_text_field(wp_unslash($_POST['fbbttm_restaurant_state']));
	}
	if(isset($_POST['fbbttm_restaurant_zipcode']))
	{
	$fbbttm_restaurant_zipcode = sanitize_text_field(wp_unslash($_POST['fbbttm_restaurant_zipcode']));
	}
	if(isset($_POST['fbbttm_restaurant_phone']))
	{
	$fbbttm_restaurant_phone = sanitize_text_field(wp_unslash($_POST['fbbttm_restaurant_phone']));
	}
	if(isset($_POST['fbbttm_shop_page']))
	{
	$fbbttm_shop_page = sanitize_text_field(wp_unslash($_POST['fbbttm_shop_page']));
	}
	if(isset($_POST['fbbttm_cart_page']))
	{
	$fbbttm_cart_page = sanitize_text_field(wp_unslash($_POST['fbbttm_cart_page']));
	}
	if(isset($_POST['fbbttm_checkout_page']))
	{
	$fbbttm_checkout_page = sanitize_text_field(wp_unslash($_POST['fbbttm_checkout_page']));
	}
	if(isset($_POST['fbbttm_myaccount_page']))
	{
	$fbbttm_myaccount_page = sanitize_text_field(wp_unslash($_POST['fbbttm_myaccount_page']));
	}
	if(isset($_POST['fbbttm_order_received']))
	{
	$fbbttm_order_received = sanitize_text_field(wp_unslash($_POST['fbbttm_order_received']));
	}


	



	$general_setting = esc_html__( 'general_settings', 'foodboxbooker' );
	$payment_setting = esc_html__( 'payment_settings', 'foodboxbooker' );
	
	$email_settings = esc_html__( 'email_settings', 'foodboxbooker' );
	$notification_settings = esc_html__( 'notification_settings', 'foodboxbooker' );
	$advance_settings = esc_html__( 'advance_settings', 'foodboxbooker' );
	

	
	$data = array(
		array(
			'fbbttm_setting_name'  => 'fbbttm_restaurant_name', 
			'fbbttm_setting_value' => $fbbttm_restaurant_name,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_restaurant_address', 
			'fbbttm_setting_value' => $fbbttm_restaurant_address,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_restaurant_address2', 
			'fbbttm_setting_value' => $fbbttm_restaurant_address2,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_restaurant_city', 
			'fbbttm_setting_value' => $fbbttm_restaurant_city,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_restaurant_country', 
			'fbbttm_setting_value' => $fbbttm_restaurant_country,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_restaurant_state', 
			'fbbttm_setting_value' => $fbbttm_restaurant_state,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_restaurant_zipcode', 
			'fbbttm_setting_value' => $fbbttm_restaurant_zipcode,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_restaurant_phone', 
			'fbbttm_setting_value' => $fbbttm_restaurant_phone,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_twilo_select', 
			'fbbttm_setting_value' => $fbbttm_twilo_select,
			'fbbttm_setting_type'  => $notification_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_acc_sid', 
			'fbbttm_setting_value' => $fbbttm_s_acc_sid,
			'fbbttm_setting_type'  => $notification_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_auth_token', 
			'fbbttm_setting_value' => $fbbttm_s_auth_token,
			'fbbttm_setting_type'  => $notification_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_sms_phnumber', 
			'fbbttm_setting_value' => $fbbttm_s_sms_phnumber,
			'fbbttm_setting_type'  => $notification_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_wp_phnumber', 
			'fbbttm_setting_value' => $fbbttm_s_wp_phnumber,
			'fbbttm_setting_type'  => $notification_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_pay_currency', 
			'fbbttm_setting_value' => $fbbttm_pay_currency,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_tax_per', 
			'fbbttm_setting_value' => $fbbttm_tax_per,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_vat_gst_tin', 
			'fbbttm_setting_value' => $fbbttm_vat_gst_tin,
			'fbbttm_setting_type'  => $general_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_stripe_method_status', 
			'fbbttm_setting_value' => $fbbttm_stripe_method_status,
			'fbbttm_setting_type'  => $payment_setting,
		),	
		array(
			'fbbttm_setting_name'  => 'fbbttm_stripe_api', 
			'fbbttm_setting_value' => $fbbttm_stripe_api,
			'fbbttm_setting_type'  => $payment_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_stripe_scretkey', 
			'fbbttm_setting_value' => $fbbttm_stripe_scretkey,
			'fbbttm_setting_type'  => $payment_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_paypal_method_status', 
			'fbbttm_setting_value' => $fbbttm_paypal_method_status,
			'fbbttm_setting_type'  => $payment_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_payment_select', 
			'fbbttm_setting_value' => $fbbttm_payment_select,
			'fbbttm_setting_type'  => $payment_setting,
		),
	    array(
			'fbbttm_setting_name'  => 'fbbttm_paypalid',
			'fbbttm_setting_value' => $fbbttm_paypalid,
			'fbbttm_setting_type'  => $payment_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_paypalapi',
			'fbbttm_setting_value' => $fbbttm_paypalapi,
			'fbbttm_setting_type'  => $payment_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_payment_email', 
			'fbbttm_setting_value' => $fbbttm_payment_email,
			'fbbttm_setting_type'  => $payment_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_api_username', 
			'fbbttm_setting_value' => $fbbttm_api_username,
			'fbbttm_setting_type'  => $payment_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_payment_mode', 
			'fbbttm_setting_value' => $fbbttm_payment_mode,
			'fbbttm_setting_type'  => $payment_setting,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_name', 
			'fbbttm_setting_value' => $fbbttm_s_name,
			'fbbttm_setting_type'  => $messages_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_mail', 
			'fbbttm_setting_value' => $fbbttm_s_mail,
			'fbbttm_setting_type'  => $messages_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_phone', 
			'fbbttm_setting_value' => $fbbttm_s_phone,
			'fbbttm_setting_type'  => $messages_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_date', 
			'fbbttm_setting_value' => $fbbttm_s_date,
			'fbbttm_setting_type'  => $messages_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_time', 
			'fbbttm_setting_value' => $fbbttm_s_time,
			'fbbttm_setting_type'  => $messages_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_service', 
			'fbbttm_setting_value' => $fbbttm_s_service,
			'fbbttm_setting_type'  => $messages_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_success', 
			'fbbttm_setting_value' => $fbbttm_s_success,
			'fbbttm_setting_type'  => $messages_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_error', 
			'fbbttm_setting_value' => $fbbttm_s_error,
			'fbbttm_setting_type'  => $messages_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_sendername', 
			'fbbttm_setting_value' => $fbbttm_s_sendername,
			'fbbttm_setting_type'  => $email_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_sendermail', 
			'fbbttm_setting_value' => $fbbttm_s_sendermail,
			'fbbttm_setting_type'  => $email_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_adminmail', 
			'fbbttm_setting_value' => $fbbttm_s_adminmail,
			'fbbttm_setting_type'  => $email_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_email_header_image', 
			'fbbttm_setting_value' => $fbbttm_email_header_image,
			'fbbttm_setting_type'  => $email_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_s_subject', 
			'fbbttm_setting_value' => $fbbttm_s_subject,
			'fbbttm_setting_type'  => $email_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_shop_page', 
			'fbbttm_setting_value' => $fbbttm_shop_page,
			'fbbttm_setting_type'  => $advance_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_cart_page', 
			'fbbttm_setting_value' => $fbbttm_cart_page,
			'fbbttm_setting_type'  => $advance_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_checkout_page', 
			'fbbttm_setting_value' => $fbbttm_checkout_page,
			'fbbttm_setting_type'  => $advance_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_myaccount_page', 
			'fbbttm_setting_value' => $fbbttm_myaccount_page,
			'fbbttm_setting_type'  => $advance_settings,
		),
		array(
			'fbbttm_setting_name'  => 'fbbttm_order_received', 
			'fbbttm_setting_value' => $fbbttm_order_received,
			'fbbttm_setting_type'  => $advance_settings,
		),
	);




$id = '';
$query = $wpdb->prepare(
	//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared 
	"SELECT * FROM $table_fbbttm_setting where fbbttm_setting_id != %d ORDER BY fbbttm_setting_id DESC LIMIT 30",$id);

$fbbttm_result = $wpdb->get_results(
//phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared 
	$query	, OBJECT);

if (!empty($fbbttm_result)) {
 	foreach ($data as $value) {
  	  $post_fbbttm_setting_name = $value['fbbttm_setting_name'];
  	  $post_fbbttm_setting_value = $value['fbbttm_setting_value'];

  	  $fbbttm_settingdata = $fbbttm_helperobj->fbbttm_comman_single_tableinfo($table_fbbttm_setting,'fbbttm_setting_name',$post_fbbttm_setting_name);

	  		if(!empty($fbbttm_settingdata))
	  		{
	  			foreach ($fbbttm_settingdata as $setddata)
	  			{
	  				$fbbttm_setting_id = $setddata->fbbttm_setting_id;
	  				$wpdb->update($table_fbbttm_setting, 
	  					array('fbbttm_setting_value'=>$post_fbbttm_setting_value), 
	  					array('fbbttm_setting_id'=>$fbbttm_setting_id));

	  			}

	  		}
	  		else
	  		{

	  			$wpdb->insert( $table_fbbttm_setting, $value );

	  		}

  	}

  	
	$msg=['success'=>'data updated successfully'];
	//phpcs:ignore WordPress.WP.AlternativeFunctions.wp_json_encode_wp_json_encode
	echo wp_json_encode($msg);
  }
  else {
  
	foreach ($data as $value) {	
		$wpdb->insert(
			$table_fbbttm_setting,
			$value
		);
	}
	$msg=['success'=>'data inserted successfully'];
	//phpcs:ignore WordPress.WP.AlternativeFunctions.wp_json_encode_wp_json_encode
	echo wp_json_encode($msg);
  }
 } 
?>