<?php

if ( ! defined( 'ABSPATH' ) ) { exit; }

add_action( 'wp_ajax_fbbttm_package_add', 'fbbttm_package_addlist' );
add_action( 'wp_ajax_nopriv_fbbttm_package_add', 'fbbttm_package_addlist' );

function fbbttm_package_addlist()
  {

		global $wpdb;
		check_ajax_referer( 'fbbttm_nonce_action', 'security' );
		$fbbttm_table_packagelist = $wpdb->prefix . 'fbbttm_packagelist';

		$fbbttm_package_name =  $fbbttm_f_id  = $fbbttm_fitem_price = $fbbttm_package_special_price = $fbbttm_total_day =$fbbttm_package_price = $fbbttm_package_description = $fbbttm_pack_id =  $fbbttm_packge_image_al ='';

		if(isset($_POST['fbbttm_package_name'])){
		$fbbttm_package_name = sanitize_text_field(wp_unslash($_POST['fbbttm_package_name']));
		}
		if(isset($_POST['fbbttm_f_id'])){
		$fbbttm_f_id = sanitize_text_field(wp_unslash($_POST['fbbttm_f_id']));
		}
		if(isset($_POST['fbbttm_fitem_price'])){
		$fbbttm_fitem_price = sanitize_text_field(wp_unslash($_POST['fbbttm_fitem_price']));
		}
		if(isset($_POST['fbbttm_package_special_price'])){
		$fbbttm_package_special_price = sanitize_text_field(wp_unslash($_POST['fbbttm_package_special_price']));
	    }
	    if(isset($_POST['fbbttm_total_day'])){
		$fbbttm_total_day = sanitize_text_field(wp_unslash($_POST['fbbttm_total_day']));
	    }
	    if(isset($_POST['fbbttm_package_price'])){
		$fbbttm_package_price = sanitize_text_field(wp_unslash($_POST['fbbttm_package_price']));
		}
		if(isset($_POST['fbbttm_package_description'])){
		$fbbttm_package_description = sanitize_text_field(wp_unslash($_POST['fbbttm_package_description']));
	    }
	    if(isset($_POST['fbbttm_pack_id'])){
		$fbbttm_pack_id = sanitize_text_field(wp_unslash($_POST['fbbttm_pack_id']));
	    }
	    if(isset($_POST['fbbttm_packge_image_al'])){
		$fbbttm_packge_image_al = sanitize_text_field(wp_unslash($_POST['fbbttm_packge_image_al']));
		}



		$fbbttm_table_setting = $wpdb->prefix . 'fbbttm_setting';

		$fbbttm_setting_id  = '';

		$pricecondition ='0';
		$fbbttmday = 0;
		$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;
		 }

		 if(!empty($fbbttm_package_special_price)){

			if($fbbttm_package_special_price !='0' &&  $fbbttm_package_special_price >= $fbbttm_fitem_price  )
			{
				$pricecondition = 1;
			}
			}

		if(!empty($fbbttm_pack_id) && $fbbttm_f_id !='null'){
		
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching 
			$fbbttm_resultpacklist  = $wpdb->get_results(
				//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
				    $wpdb->prepare(
				    	//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
				        "SELECT * FROM $fbbttm_table_packagelist WHERE `fbbttm_pack_id` != %d AND `fbbttm_f_id` = %d AND `fbbttm_total_day` = %d ORDER BY fbbttm_pack_id DESC LIMIT 1"
				
				        ,
				        $fbbttm_pack_id, $fbbttm_f_id, $fbbttm_total_day
				    ), 
				    ARRAY_A
				);
		}
		else if($fbbttm_f_id !='null')
		{

			$fbbttm_resultpacklist = $wpdb->get_results(
			    $wpdb->prepare(
			    	//phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			        "SELECT * FROM $fbbttm_table_packagelist WHERE `fbbttm_f_id` = %d AND `fbbttm_total_day` = %d ORDER BY fbbttm_pack_id DESC LIMIT 1",
			        $fbbttm_f_id, $fbbttm_total_day
			    ), 
			    ARRAY_A	);


		}	

		if($fbbttm_packge_image_al!='')
			{
				$imageuploadurl = $fbbttm_packge_image_al;
			}
			else
			{
				$imageuploadurl ='';
			}







			// phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotValidated 
			if (isset($_FILES['packge_image']) && $_FILES['packge_image']['error'] === UPLOAD_ERR_OK) {

			// Include the necessary WordPress file handling functions
    	require_once ABSPATH . 'wp-admin/includes/file.php';

			$upload_dir = wp_upload_dir();

			
			$imagepath = $upload_dir['path'];
			$imageurl = $upload_dir['url'];



	   // Get the uploaded file's name and extension
    $fileName = sanitize_file_name($_FILES['packge_image']['name']);
    $fileTmpPath = sanitize_text_field($_FILES['packge_image']['tmp_name']);
    $fileType = sanitize_mime_type($_FILES['packge_image']['type']);
    $fileSize = intval($_FILES['packge_image']['size']);
    $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

	    // Specify allowed file types
	    $allowedFileExtensions = ['jpg', 'jpeg', 'png', 'gif','webp'];

	    // Check if the file has a valid extension
    if (in_array($fileExtension, $allowedFileExtensions)) {

        // Use WordPress's file upload handling
        $upload_overrides = ['test_form' => false]; // Disable form validation
        $upload_result = wp_handle_upload($_FILES['packge_image'], $upload_overrides);

        if (isset($upload_result['file'])) {
            // Success: File uploaded
            $imageuploadurl = esc_url($upload_result['url']);
        } else {
            // Error during upload
            $error_msg['image-error'] = esc_html__('Error uploading the file: ', 'foodboxbooker') . esc_html($upload_result['error']);
        }
    } else {
        // Invalid file type error
        $extval = implode(', ', $allowedFileExtensions);
        $error_msg['image-error'] = sprintf(
            /* translators: 1: Allowed file extensions */
            esc_html__('Invalid file type. Allowed types are: %1$s', 'foodboxbooker'),
            esc_html($extval)
        );
    }
	} else {

		if($imageuploadurl==''){
		 $error_msg['image-error'] = esc_html__( 'No file uploaded or there was an upload error.', 'foodboxbooker' );
		}


	}

	if(!empty($fbbttm_package_name) && (!empty($fbbttm_f_id) || $fbbttm_f_id !='null')  && !empty($fbbttm_fitem_price) &&  $pricecondition == 0 && $fbbttmday == 0  && empty($fbbttm_resultpacklist) && !empty($fbbttm_total_day) && $security == 0 && $imageuploadurl !=''){

			$data = array(
				'fbbttm_package_name'=> $fbbttm_package_name,
				'fbbttm_f_id'=> $fbbttm_f_id,
				'fbbttm_package_image'=>$imageuploadurl,
				'fbbttm_fitem_price'=> $fbbttm_fitem_price,
				'fbbttm_special_price'=> $fbbttm_package_special_price,
				'fbbttm_total_day'=> $fbbttm_total_day,
				'fbbttm_package_price'=> $fbbttm_package_price,
				'fbbttm_package_description'=> $fbbttm_package_description
			);

	
		if(!empty( $fbbttm_pack_id))	{
			$wpdb->update($fbbttm_table_packagelist,  $data , array('fbbttm_pack_id'=>$fbbttm_pack_id));	
			$mes = esc_html__('Package updated successfully','foodboxbooker');
		}else
		{
	  		$wpdb->insert($fbbttm_table_packagelist,  $data );
			$mes = esc_html__( 'Package has been added successfully.', 'foodboxbooker' );
		}
		
	   $message ['success'] = '<div class="fbbttm_success fbbttm-popconfirm"><span>'.$mes.'</span></div>'; 
			
		} else {

			
			
	
		    $er_msg ='There is some error!!';

		    if( $security == 1){

			 $er_msg =  esc_html__('Sorry, Your request can not be processed due to security reason.', 'foodboxbooker');
	 		}

		    if(!empty($fbbttm_resultpacklist))
		    {
		    	$er_msg =esc_html__( 'This package is already available same fooditeam with same package day and same price', 'foodboxbooker' );;
		    }

			$message['php_error']= '<div class="fbbttm_error fbbttm-popconfirm"><span>'.$er_msg.'</span></div>';



			if(empty($fbbttm_package_name)) {
				$error_msg['packname-error'] = esc_html__( 'Please enter package name', 'foodboxbooker' );
			}
			if(empty($fbbttm_f_id) || $fbbttm_f_id =='null') {
				$error_msg['fooditem-error'] = esc_html__( 'Please select food item', 'foodboxbooker' );
			}

			if(empty($fbbttm_total_day)) {
				$error_msg['fbbttmday-error'] = esc_html__( 'Please enter package day', 'foodboxbooker' );
			}
			

			if($fbbttm_package_special_price!='0' && !empty($fbbttm_package_special_price) )
			{
				if($fbbttm_package_special_price >= $fbbttm_fitem_price )
				{
					$error_msg['spname-error'] = esc_html__( 'Please enter special price less than food item price', 'foodboxbooker' );
				}	
			}		

			if($fbbttmday==1){
				$error_msg['fbbttmday-error'] = esc_html__( 'Please enter a proper day', 'foodboxbooker' );
			}
			$message['error_message']=$error_msg;
		 }
		 echo wp_json_encode($message);
		 die;
  }



  
  add_action( 'wp_ajax_fbbttm_setting_datainsert', 'fbbttm_setting_datainsert' );
add_action( 'wp_ajax_nopriv_fbbttm_setting_datainsert', 'fbbttm_setting_datainsert' );

function fbbttm_setting_datainsert()
  {

  	global $wpdb, $fbbttm_helperobj;

  	$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){

	 	$fbbttm_table_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_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_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 $fbbttm_table_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($fbbttm_table_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($fbbttm_table_setting, 
	  					array('fbbttm_setting_value'=>$post_fbbttm_setting_value), 
	  					array('fbbttm_setting_id'=>$fbbttm_setting_id));

	  			}

	  		}
	  		else
	  		{
	  			$wpdb->insert( $fbbttm_table_setting, $value );
	  		}
	 		}

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

	echo wp_json_encode($msg);

die;
}


  	
  	
  }





?>