/* 
* Version: 1.0
*/
jQuery(document).ready(function(e) {
	// Get tab index of booking
	if(getSearchParams('bt') == 'booking'){
		o365_js_obj.active_tab = jQuery( "ul.TabbedPanelsTabGroup li#msbooking_payment_details" ).index( );		
	}

	// Get tab index of revoke token
	if(getSearchParams('bt') == 'revoketoken'){
		o365_js_obj.active_tab = jQuery( "ul.TabbedPanelsTabGroup li#o365_revoke_token_tab" ).index( );
		console.log(o365_js_obj.active_tab);
		console.log(getSearchParams('bt'));
	}

	// Get tab index of user
	if(getSearchParams('bt') == 'user'){
		o365_js_obj.active_tab = jQuery( "ul.TabbedPanelsTabGroup li#o365_userauth_tab_title" ).index( );
	}

	// change Url on tab click
	jQuery(".TabbedPanelsTab").click(function (e) {
		
		let tab_id = jQuery(this).attr("id");
		if( tab_id == undefined || tab_id == null ) {
			window.history.pushState({}, "Title", o365_js_obj.site_url + "/wp-admin/admin.php?page=o365_settings");
		}

		if(tab_id == 'o365_revoke_token_tab') {
			window.history.pushState({}, "Title", o365_js_obj.site_url + "/wp-admin/admin.php?page=o365_settings&bt=revoketoken");
		}
		if(tab_id == 'msbooking_payment_details') {
			window.history.pushState({}, "Title", o365_js_obj.site_url + "/wp-admin/admin.php?page=o365_settings&bt=booking");
		}
		if(tab_id == 'o365_userauth_tab_title') {
			window.history.pushState({}, "Title", o365_js_obj.site_url + "/wp-admin/admin.php?page=o365_settings&bt=user");
		}
	});

	if(o365_js_obj.active_tab)
	{
		var tab_id = parseInt(o365_js_obj.active_tab);
		var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1", {defaultTab:tab_id});
	}
	else
	{
		var TabbedPanels1 = new Spry.Widget.TabbedPanels("TabbedPanels1", {defaultTab:0});
	}
	
	jQuery("#save_settings_get_authentication").click(function(e) {
		var client_id =jQuery("#client_id").val();
		var client_secret = jQuery("#client_secret").val();
		var tenant_name = jQuery("#tenant_name").val();
		var client_object = jQuery("#client_object").val();
		var o365_display_user_id = jQuery("#o365_display_user_id").val();
		var o365_dynamic_crm_url = jQuery("#o365_dynamic_crm_url").val();
		var shared_settings = (jQuery("#shared_settings").is(":checked")) ? jQuery("#shared_settings").val() : '';
		var b2b_shared_settings = (jQuery("#b2b_shared_settings").is(":checked")) ? jQuery("#b2b_shared_settings").val() : '';
		var delete_settings = (jQuery("#delete_settings").is(":checked")) ? jQuery("#delete_settings").val() : '';
		var log_errors = (jQuery("#log_errors").is(":checked")) ? jQuery("#log_errors").val() : '';
		var logging_to_file = (jQuery("#logging_to_file").is(":checked")) ? jQuery("#logging_to_file").val() : '';
		var o365_username =jQuery("#o365_username").val();
		var o365_password =jQuery("#o365_password").val();
		var o365_yammer_ci = jQuery('#o365_yammer_ci').val();
		var o365_yammer_cs = jQuery('#o365_yammer_client_secret').val();

		// var o365_auth_version =jQuery("#o365_auth_version").val();
		var o365_national_endpoints = jQuery('#o365_national_endpoints').val();
		var o365_graph_endpoints = jQuery('#o365_graph_endpoints').val();
		
		if( client_id == "" )
		{
			show_error_message( "Plesae input the Application ID." )
		}
		else if( client_secret == "" )
		{
			show_error_message( "Plesae input the Application Key." )
		}
		else if( tenant_name == "" )
		{
			show_error_message( "Plesae input the tenant name." )
		}
		else
		{
			show_progress_loader()
			var data = {
                action: 'o365_setings_page_save_data',
                'client_id': client_id,
				'client_secret': client_secret,
				'tenant_name': tenant_name,
				'shared_settings':shared_settings,
				'b2b_shared_settings':b2b_shared_settings,
				'delete_settings': delete_settings,
				'log_errors': log_errors,
				'logging_to_file': logging_to_file,
				'o365_username': o365_username,
				'o365_password': o365_password,
				'client_object': client_object,
				'o365_display_user_id': o365_display_user_id,
				"o365_dynamic_crm_url": o365_dynamic_crm_url,
				'o365_yammer_ci': o365_yammer_ci,
				'o365_yammer_cs': o365_yammer_cs,
				// "o365_auth_version": o365_auth_version,
				"o365_national_endpoints": o365_national_endpoints,
				"o365_graph_endpoints": o365_graph_endpoints
            };
            jQuery.post(ajaxurl, data, function( response_stringified ){
				console.log( response_stringified );
				if( response_stringified != "success")
				{
					 window.location.href = response_stringified;
				}
				else
				{
					window.location.href = response_stringified;
				}
            }).fail(function(){
				hide_progress_loader();
            });
		}
	});
	function show_progress_loader()
	{
		jQuery("#please-wait-bg").show();
	}
	function hide_progress_loader()
	{
		jQuery("#please-wait-bg").hide();
	}
	function show_error_message( message )
	{
		alert( message );
	}
	jQuery('.ques_img1').hover(function(){
        jQuery('.question_block').show();
    }, function() {
       jQuery('.question_block').hide();
	});
	
	// Delete token Button in the base plugin revoke token tab
	jQuery(".o365_base_plugin_refresh_token_button").click(function(e){
		var access_token = jQuery.trim( jQuery(this).attr('data-access_token') );
		var refresh_token = jQuery.trim( jQuery(this).attr('data-refresh_token') );
		var expiry_time = jQuery.trim( jQuery(this).attr('data-expiry_time') );
		
		var data = { 'action':'o365_base_plugin_refresh_token_button', 'access_token':access_token, 'refresh_token':refresh_token, 'expiry_time':expiry_time};
		jQuery.post( ajaxurl, data , function( response, txtStatus, jqXHR ){
			if(response.success){
				alert('An access token has been deleted. Please reload the page for a new token.');
				location.reload();
			}
		})
		.fail(function(){
			alert("ERROR");
		});
	});

	
});

function getSearchParams(k){
	var p={};
	location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){p[k]=v})
	return k?p[k]:p;
   }
