(function () {
    "use strict";
    var showResults;
    jQuery('#search-box').keyup(function () {
        var searchText;
        searchText = jQuery('#search-box').val();
        return showResults(searchText);
    });
    showResults = function (searchText) {
        jQuery('tbody tr').hide();
        return jQuery('tbody tr:Contains(' + searchText + ')').show();
    };
    jQuery.expr[':'].Contains = jQuery.expr.createPseudo(function (arg) {
        return function (elem) {
            return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
        };
    });
}.call(this));

jQuery(document).ready(function() {
	"use strict";
	 if (jQuery('#fbbttm-hiddencid').length) {
			var hdncid = jQuery('#fbbttm-hiddencid').val();
		 	if(hdncid!=''){
		 	 	setTimeout(function(){ 
			 	 	jQuery('#fbbttm_restaurant_country').trigger("change");
			 	}, 2000);	
		 	}
	 }

	 jQuery('#fbbttm_restaurant_country').change(function(){

	  	var ajaxurl = jQuery('#hidnajaxurl').val();

 	 	  	var id = jQuery(this).val();
	 	  	//alert(id);
	 	  	var hdnsid = jQuery('#fbbttm-hiddencid').val();

			 jQuery.ajax({
			    method: 'POST',
			    dataType: 'json',
			    url: ajax_object.ajax_url, 
			    data: { 
					action:'get_state_as_country',
					id:id,
					hdnsid:hdnsid
    			}, 
    			success: function(response) {	
    				//return false;
					if(response.stateoption){

						jQuery('#fbbttm_restaurant_state').html(response.stateoption);

					}
				}
  			});
		return false;	

 	  });

	
	jQuery('#submitsetting').on('click', function(e){
		//alert('button click');
	    e.preventDefault();
		//var data = jQuery("#submit").serialize();

		var ajaxurl = jQuery('#hidnajaxurl').val();
		var fbbttm_s_acc_sid = jQuery('#fbbttm_s_acc_sid').val();
		var fbbttm_s_auth_token = jQuery('#fbbttm_s_auth_token').val();
		var fbbttm_s_sms_phnumber = jQuery('#fbbttm_s_sms_phnumber').val();
		var fbbttm_s_wp_phnumber = jQuery('#fbbttm_s_wp_phnumber').val();
		var fbbttm_restaurant_name = jQuery('#fbbttm_restaurant_name').val();
		var fbbttm_restaurant_address = jQuery('#fbbttm_restaurant_address').val();
		var fbbttm_restaurant_address2 = jQuery('#fbbttm_restaurant_address2').val();
		var fbbttm_restaurant_city = jQuery('#fbbttm_restaurant_city').val();
		var fbbttm_restaurant_country = jQuery('#fbbttm_restaurant_country').val();
		var fbbttm_restaurant_state = jQuery('#fbbttm_restaurant_state').val();
		var fbbttm_restaurant_zipcode = jQuery('#fbbttm_restaurant_zipcode').val();
		var fbbttm_restaurant_phone = jQuery('#fbbttm_restaurant_phone').val();
		
		var fbbttm_pay_currency = jQuery('#fbbttm_pay_currency').val();
		var fbbttm_tax_per = jQuery('#fbbttm_tax_per').val();
		var fbbttm_vat_gst_tin = jQuery('#fbbttm_vat_gst_tin').val();

		var fbbttm_stripe_method_status = jQuery('input[name="fbbttm_stripe_method_status"]:checked').val();
		var fbbttm_stripe_api = jQuery('#fbbttm_stripe_api').val();
		var fbbttm_stripe_scretkey = jQuery('#fbbttm_stripe_scretkey').val();
		var fbbttm_paypalid = jQuery('#fbbttm_stripe_api').val();
	    var fbbttm_payment_select = jQuery('input[name="fbbttm_payment_select"]:checked').val();
	    var fbbttm_paypal_method_status = jQuery('input[name="fbbttm_paypal_method_status"]:checked').val();
	    var fbbttm_paypalid = jQuery('#fbbttm_s_paypalid').val();
	    var fbbttm_paypalapi = jQuery('#fbbttm_s_paypalapi').val();
	    var fbbttm_payment_email = jQuery('#fbbttm_s_paymentemail').val();
	    var fbbttm_api_username = jQuery('#fbbttm_s_apiusername').val();
	    var fbbttm_payment_mode = jQuery('input[name="fbbttm_payment_mode"]:checked').val();
	
	
		var fbbttm_s_sendername = jQuery('#fbbttm_s_sendername').val();
		var fbbttm_s_sendermail = jQuery('#fbbttm_s_sendermail').val();
		var fbbttm_s_adminmail = jQuery('#fbbttm_s_adminmail').val();
		var fbbttm_s_subject = jQuery('#fbbttm_s_subject').val();
		var fbbttm_email_header_image = jQuery('#fbbttm_email_header_image').val();

		var fbbttm_shop_page = jQuery('#fbbttm_shop_page').val();
		var fbbttm_cart_page = jQuery('#fbbttm_cart_page').val();
		var fbbttm_checkout_page = jQuery('#fbbttm_checkout_page').val();
		var fbbttm_myaccount_page = jQuery('#fbbttm_myaccount_page').val();
		var fbbttm_order_received = jQuery('#fbbttm_order_received').val();


		var fbbttm_meta_box_noncename = jQuery('#fbbttm_meta_box_noncename').val();

	    jQuery.ajax({
	    method: 'POST',
	    dataType: 'json',
	  	url: ajax_object.ajax_url, 
	    data: { 
		     	 security: ajax_object.nonce,
		     	 action: 'fbbttm_setting_datainsert',
				 fbbttm_s_acc_sid:fbbttm_s_acc_sid,
				 fbbttm_s_auth_token:fbbttm_s_auth_token,
				 fbbttm_s_sms_phnumber:fbbttm_s_sms_phnumber,
				 fbbttm_s_wp_phnumber:fbbttm_s_wp_phnumber,
				 fbbttm_restaurant_name:fbbttm_restaurant_name, 
				 fbbttm_restaurant_address:fbbttm_restaurant_address,
				 fbbttm_pay_currency:fbbttm_pay_currency,
				 fbbttm_tax_per:fbbttm_tax_per,
				 fbbttm_vat_gst_tin:fbbttm_vat_gst_tin,
				 fbbttm_restaurant_address:fbbttm_restaurant_address,
				 fbbttm_restaurant_address2:fbbttm_restaurant_address2,
			 	 fbbttm_restaurant_city:fbbttm_restaurant_city,
				 fbbttm_restaurant_country:fbbttm_restaurant_country,
				 fbbttm_restaurant_state:fbbttm_restaurant_state,
				 fbbttm_restaurant_zipcode:fbbttm_restaurant_zipcode,
				 fbbttm_restaurant_phone:fbbttm_restaurant_phone,
				 fbbttm_shop_page:fbbttm_shop_page,
				 fbbttm_cart_page:fbbttm_cart_page,
				 fbbttm_checkout_page:fbbttm_checkout_page,
				 fbbttm_myaccount_page:fbbttm_myaccount_page,
				 fbbttm_order_received:fbbttm_order_received,
			   	 fbbttm_stripe_method_status:fbbttm_stripe_method_status,
				 fbbttm_stripe_api:fbbttm_stripe_api,
				 fbbttm_stripe_scretkey:fbbttm_stripe_scretkey,
			     fbbttm_payment_select:fbbttm_payment_select,
			     fbbttm_paypal_method_status:fbbttm_paypal_method_status,
				 fbbttm_paypalid:fbbttm_paypalid,
			     fbbttm_paypalapi:fbbttm_paypalapi,
			     fbbttm_payment_email:fbbttm_payment_email,
			     fbbttm_api_username:fbbttm_api_username,
			     fbbttm_payment_mode:fbbttm_payment_mode,
			     fbbttm_s_sendername:fbbttm_s_sendername,
			     fbbttm_s_sendermail:fbbttm_s_sendermail,
			     fbbttm_s_adminmail:fbbttm_s_adminmail,
			     fbbttm_s_subject:fbbttm_s_subject,
			     fbbttm_email_header_image:fbbttm_email_header_image,
			     fbbttm_meta_box_noncename:fbbttm_meta_box_noncename
   		 }, 
	    success: function(response) {		

			jQuery('#result').text(response.success).fadeIn('slow');
			jQuery('#result').delay(8000).fadeOut('slow');

	    }
 	 });
  });




jQuery('#fbbttm_f_id').on('change', function() {
			var fooditemprice =  jQuery(this).find(":selected").attr('data-price') ;
			jQuery('#fbbttm_fitem_price').val(fooditemprice);	
			jQuery("#fbbttm_total_day").trigger("change");
	});

	jQuery("#fbbttm_package_special_price").on('change keyup paste', function() {
		jQuery("#fbbttm_total_day").trigger("change");
	});	

	jQuery("#fbbttm_total_day").on('change keyup paste', function() {
	   
		var daysval = jQuery(this).val();
		var fooditemprice =  jQuery('#fbbttm_fitem_price').val();
		var special_price = jQuery('#fbbttm_package_special_price').val();
		var packge_price ='';

		if(special_price!='' && special_price!=0)
			{
				packge_price = daysval * special_price;
			}
		else{
				packge_price = daysval * fooditemprice;
			}	


		jQuery('#fbbttm_package_price').val(packge_price);	
			

		console.log(packge_price);	
	});

jQuery('.fbbttm-button').click(function(){
	jQuery('tbody tr').removeClass('active');
	jQuery('tbody tr .fbbttm-checkbox__original').attr('checked', false);
	jQuery(this).closest('tr').addClass('active');
	jQuery('tbody tr.active .fbbttm-checkbox__original').attr('checked', true);
	return true;
});

	
	jQuery("#packageinsert").on("submit", function(e){
		e.preventDefault();
		//alert('test sdfdsfdsf');
		


		var redirecturl = jQuery('#redirecturl').val();

		//jQuery('#packageinsert_submit').prop('disabled', true);

		let formData = new FormData(this);
	    // Add additional data if needed
        formData.append('action', 'fbbttm_package_add'); // WordPress AJAX action
        formData.append('security', ajax_object.nonce); // Nonce for security

        jQuery.ajax({
            url: ajax_object.ajax_url, // admin-ajax.php
            type: 'POST',
             dataType: 'json',
            data: formData,
            processData: false, // Do not process data (required for FormData)
            contentType: false, // Do not set content-type header (required for FormData)
            success: function (response) {

            	jQuery('.fbbttm__error').html(' ');

                if (response.success) {

                	jQuery('#result').html(response.success).fadeIn('slow');

                	setTimeout(function() {
                          window.location.href=redirecturl;
                    }, 1500);
                    
                } else {

                	jQuery.each(response.error_message, function(key,valueObj){
							jQuery('.fbbttm__error.'+key).html(valueObj).fadeIn('slow');;  
						});

                	if(response.php_error!=''){

                		jQuery('#php_error').html(response.php_error).fadeIn('slow');

                	}
                    
                }
            },
            error: function () {
                alert('AJAX error occurred.');
            }
        });
		return false;

	});
});

