jQueryDM(document).ready(function(){
	
	dap_member_tiny_editor();
    dap_afflilate_show_section_option(); 
    dap_afflilate_option_customizer(); 
	dap_affiliate_link_to_field_keyup();
	dap_member_affiliate_existing_page_select();
    dap_member_affilliate_tabs_Validation();	
});

function dap_member_affiliate_name_tab_validation(){
	
	var validation_status = false;
	$parent_selector = '.dap_member_content';
	post_title = jQueryDM($parent_selector).find('input[name="affiliate_name"]').val();
	login_page_url = jQueryDM($parent_selector).find('select[name="affiliate_page_url"]').val();
	
	if(post_title == '' && login_page_url == ''){
		$message = "Please Enter a Name for your Affiliate Page";
		dap_member_sweet_alert_message($message);
		validation_status = true;
		return validation_status;
	}
	page_id = jQueryDM($parent_selector).find('input[name="page_id"]').val();
	if(post_title != '' && page_id == ''){
		$message = "Please 'Click To Create' button";
		dap_member_sweet_alert_message($message);
		validation_status = true;
		return validation_status;
		
	}
	
	var seleted_template = jQueryDM($parent_selector).find('input[name="template_selected_name"]').val();
    if(seleted_template ==''){
		validation_status = true;
		$message = "Please select template.";
		dap_member_sweet_alert_message($message);
		return validation_status;
	} 
	 
	return validation_status;
}


function dap_member_affilliate_tabs_Validation(){
	
		jQueryDM('#dap_member_affiliate_tabs li.nav-item').on('click','a', function (e) {
		if(e.target == e.currentTarget) {
			var current_tab_id = jQueryDM(e.target).attr("href"); // activated tab
		}else {
			var current_tab_id = jQueryDM(this).parents('li').find('a').attr("href"); // activated tab
		}	
		
		
		if(current_tab_id == '#dap_member_affiliated_general'){
			dap_member_show_tab_by_id(current_tab_id);
			return ;
		}
		
		if(dap_member_affiliate_name_tab_validation()){
			current_tab_id = '#dap_member_affiliated_general';
			dap_member_show_tab_by_id(current_tab_id);
			return ;
		}
		if(current_tab_id == '#dap_member_affiliated_shortdetails'){
			
			edit_id = jQueryDM('.dap_member_content').find('input[name="affiliate_edit_id"]').val();
			if(edit_id == ''){
				$message = "Please Save Template";
				dap_member_sweet_alert_message($message);
				
				return ;
			}
		}
		
		dap_member_show_tab_by_id(current_tab_id);
		});
}


function dap_member_affiliate_existing_page_select(){
	
	jQueryDM('#affiliate_page_url').on('change',function(){
		page_id = jQueryDM(this).val();
		dap_member_show_loader();
		jQueryDM.post(ajaxurl, {
			action: 'dap_checked_page_id_and_page_type_used',
			page_id:page_id,
			page_type : 'affiliate'
		}, function(response) {
			
			dap_member_hide_loader();
			if(response == 'yes'){
				$message = "This URL is already in use. Please select a different URL.";
				dap_member_sweet_alert_message($message);
				jQueryDM('#affiliate_page_url').val('');
				return ;
			}else if(response == 'no'){
				
				jQueryDM('input[name="page_id"]').val(page_id);
			}
	
		});
	});
}

function dap_member_affiliate_create_page_btn($obj){
	
	$parent_selector = '.dap_member_content';
	
	post_title = jQueryDM($parent_selector).find('input[name="affiliate_name"]').val();
	
	if(post_title == ''){
		$message = "Please Enter a Name for your Affiliate Page";
		dap_member_sweet_alert_message($message);
		return ;
	}
	
	dap_member_show_loader();
	jQueryDM($obj).text('Creating...');
	jQueryDM.post(ajaxurl, {
		action: 'dapMemberGenerateWpPage',
		post_title:post_title,
	}, function(response) {
		dap_member_hide_loader();
		jQueryDM($obj).text('Click to Create');
		$parent_selector = '.page_response';
		
		response = JSON.parse(response);
	
		jQueryDM($parent_selector).removeClass('text-success text-danger');
		if(response.status == 'yes'){
			$message = "Sorry, This Name is already taken. Please Try with another one.";
			jQueryDM($parent_selector).text($message).addClass('text-danger');
			return ;
		}else if(response.status == 'no'){
			$message = "Login Affiliate created successfully.";
			jQueryDM($parent_selector).text($message).addClass('text-success');
			jQueryDM('.dap_member_content input[name="page_id"]').val(response.post_id);
			return ;
		}
		
	    	
	});
	

}
function dap_affiliate_customize_temp(template_name , template_number_name){
	
	dap_member_show_loader();
	jQueryDM.post(ajaxurl, {
		action: 'dap_member_get_template',
		template_name:template_name,
		template_number_name: template_number_name,
	}, function(response) {
		response = JSON.parse(response);
		
		jQueryDM('.choices-template.row li').removeClass('activeCustomize');
		jQueryDM('.choices-template.row').find('li.onecolumn_'+template_number_name).addClass('activeCustomize');
		jQueryDM('input[name="template_selected_name"]').val(template_number_name);
		jQueryDM('.customize_html_outer').html(response.html); 
		jQueryDM('.dap_affiliate_template_outer_link').html('<link  rel="stylesheet" href="'+response.css_file_path+'">'); 
		dap_member_tiny_editor();
	 	dap_member_goto_next_tab('#dap_member_affiliated_preview');
	 	
	 	//set default values of color picker
	    if(response.default_colorpicker_values){
			var data = response.default_colorpicker_values;
			
			for (var key in data) {
				jQueryDM('#'+key).colorpicker('setValue',data[key]);
			}
		}	
		
		if(response.default_numaric_values){
			var data = response.default_numaric_values;
			
			for (var key in data) {
				jQueryDM('#'+key).bootstrapSlider('setValue',data[key]);
			}
		}
		
		if(response.default_style_values){
			var data = response.default_style_values;
			
			for (var key in data) {
				jQueryDM('#'+key).val(data[key]);
			}
		}
		
		if(response.default_checkedbox_values){
			var data = response.default_checkedbox_values;
			
			for (var key in data) {
				jQueryDM('input[name="'+key+'"]').prop('checked',data[key]).change();
			}
		}
	 	
	 	dap_member_hide_loader();
	 	
	 	
	});	
	
}

function dap_affiliate_preview_temp(template_name , template_number_name){
	
	
	
	dap_member_show_loader();
	jQueryDM.post(ajaxurl, {
		action: 'dap_member_get_template',
		template_name:template_name,
		template_number_name: template_number_name,
	}, function(response) {
		
		response = JSON.parse(response);
		
		jQueryDM('#dap_affiliate_preview_wrapper_modal .modal-body').html(response.html).append('<link rel="stylesheet"  href="'+response.css_file_path+'">');
		jQueryDM('#dap_affiliate_preview_wrapper_modal').modal('show');  
		dap_member_hide_loader();
	    	
	});
	
}

function dap_affiliate_link_to_field_keyup(){
	
	jQueryDM(document).on('keyup','input[name="affiliate_link_to_field"]',function(){		
		data_url = jQueryDM('.affiliate_link_to').attr('data-url');
		jQueryDM('.affiliate_link_to').text(data_url+jQueryDM(this).val());
	});
	
}
function dap_afflilate_show_section_option(){
     
    jQueryDM('.afflilate_show_section_option input[type="checkbox"]').on('change',function(){
	    dataid = jQueryDM(this).attr('data-id');
       if(jQueryDM(this).prop('checked')){
		   jQueryDM('.'+dataid).show();
	   }else{
		   jQueryDM('.'+dataid).hide();
	   }
    })
}



function dap_affiliate_BoxShadow(colorval = '') {
	
	var hor_lnth = parseFloat(jQueryDM('#affi_hor_lnth').val());
	var ver_lnth = parseFloat(jQueryDM('#affi_ver_lnth').val());
	var blur_radius = parseFloat(jQueryDM('#affi_blur_radius').val());
	var sprd_radius = parseFloat(jQueryDM('#affi_spread_radius').val());
	var shad_clr = jQueryDM('#affi_shad_clr').val();
	hor_lnth = hor_lnth + 'px';
	ver_lnth = ver_lnth + 'px';
	blur_radius = blur_radius + 'px';
	sprd_radius = sprd_radius + 'px';
	var box_shadow = hor_lnth + ' ' + ver_lnth + ' ' + blur_radius + ' ' + sprd_radius + ' ' + shad_clr;
	var p_selector = '.dap_member_affiliate_inner';
	jQueryDM(p_selector).css('-webkit-box-shadow', box_shadow);
	jQueryDM(p_selector).css('-moz-box-shadow', box_shadow);
	jQueryDM(p_selector).css('box-shadow', box_shadow);
}

function dap_afflilate_option_customizer(){
	
	// template outer style 
	var p_selector = '.dap_member_affiliate_inner';
	
	// External Shadow Customizer
	jQueryDM('#affi_hor_lnth').bootstrapSlider().change(function() {
		dap_affiliate_BoxShadow();
	});
	jQueryDM('#affi_ver_lnth').bootstrapSlider().change(function() {
		dap_affiliate_BoxShadow();
	});
		jQueryDM('#affi_blur_radius').bootstrapSlider().change(function() {
		dap_affiliate_BoxShadow();
	});
		jQueryDM('#affi_spread_radius').bootstrapSlider().change(function() {
		dap_affiliate_BoxShadow();
	});
	jQueryDM('#affi_shad_clr,#affi_shad_clr_div').colorpicker().on('changeColor', function() {
		
		dap_affiliate_BoxShadow(jQueryDM(this).colorpicker('getValue'));
	});
	
	// Template option 
	
	
	jQueryDM('#dm_affiliate_temp_aligment').on('change',function() {
		jQueryDM('.customize_html_outer').find('.dap_member_affi_temp_outer').css('text-align',jQueryDM(this).val());
	}); 
	jQueryDM('#affi_tmp_wid').bootstrapSlider().change(function() {
		jQueryDM(p_selector).css('max-width', this.value + 'px');
	});
	
	jQueryDM('#affi_tmp_bdr_wid').bootstrapSlider().change(function() {
		jQueryDM(p_selector).css('border-width', this.value + 'px');
	});
	
	jQueryDM('#affi_tmp_br_radius').bootstrapSlider().change(function() {
		jQueryDM(p_selector).css('border-radius', this.value + 'px');
	});
	
	jQueryDM('#affi_tmp_border_color,#affi_tmp_border_color_div').colorpicker().on('changeColor', function() {
		
		jQueryDM(p_selector).css('border-color', jQueryDM(this).colorpicker('getValue'));
	});
	
	jQueryDM('#affi_tmp_bdr_style').change(function(){
		jQueryDM(p_selector).css('border-style', jQueryDM(this).val());
	});
	
	jQueryDM('#affi_tmp_padd_top').bootstrapSlider().change(function() {
		jQueryDM(p_selector).css('padding-top', this.value + 'px');
	});
	jQueryDM('#affi_tmp_padd_bottom').bootstrapSlider().change(function() {
		jQueryDM(p_selector).css('padding-bottom', this.value + 'px');
	});
	
	// Background Color

    jQueryDM('#affi_tmp_affi_background_div,#affi_tmp_affi_background').colorpicker().on('changeColor', function() {
		
		jQueryDM('.enter_name_outer.aff_link_outer').css('background-color', jQueryDM(this).colorpicker('getValue'));
	});
	
	jQueryDM('#affi_tmp_performace_background_div,#affi_tmp_performace_background').colorpicker().on('changeColor', function() {
		
		jQueryDM('.enter_name_outer.aff_performace_summary_outer').css('background-color', jQueryDM(this).colorpicker('getValue'));
	});
	
	
	jQueryDM('#affi_tmp_payment_background_div,#affi_tmp_payment_background').colorpicker().on('changeColor', function() {
		
		jQueryDM('.enter_name_outer.aff_payment_details_outer').css('background-color', jQueryDM(this).colorpicker('getValue'));
	});
	
	jQueryDM('#affi_tmp_earnnings_background_div,#affi_tmp_earnnings_background').colorpicker().on('changeColor', function() {
		
		jQueryDM('.enter_name_outer.aff_earnings_details_outer').css('background-color', jQueryDM(this).colorpicker('getValue'));
	});
	
	jQueryDM('#affi_tmp_traffic_background_div,#affi_tmp_traffic_background').colorpicker().on('changeColor', function() {
		
		jQueryDM('.enter_name_outer.aff_traffic_statistics_outer').css('background-color', jQueryDM(this).colorpicker('getValue'));
	});
	
	
}


function dap_save_affiliate_info(go_tab = ''){
	
	
	template_html = jQueryDM('.customize_html_outer').html();
	
	
	//Display Options
	aff_link_show = jQueryDM('input[name="aff_link_outer"]').prop('checked');
	aff_performace_summary_show = jQueryDM('input[name="aff_performace_summary_outer"]').prop('checked');
	aff_payment_details_show = jQueryDM('input[name="aff_payment_details_outer"]').prop('checked');
	aff_earnings_details_show = jQueryDM('input[name="aff_earnings_details_outer"]').prop('checked');
	aff_traffic_statistics_show = jQueryDM('input[name="aff_traffic_statistics_outer"]').prop('checked');
	aff_coupon_details_outer = jQueryDM('input[name="aff_coupon_details_outer"]').prop('checked');
	dispaly_opitons = aff_link_show+'||'+aff_performace_summary_show+'||'+aff_payment_details_show+'||'+aff_earnings_details_show+'||'+aff_traffic_statistics_show;
	
	//External Shadow Customizer
    affi_spread_radius = jQueryDM('#affi_spread_radius').val();
    affi_blur_radius = jQueryDM('#affi_blur_radius').val();
    affi_hor_lnth = jQueryDM('#affi_hor_lnth').val();
    affi_ver_lnth = jQueryDM('#affi_ver_lnth').val();
    affi_shad_clr = jQueryDM('#affi_shad_clr').val();
	external_shadow_customizer  = affi_spread_radius+'||'+affi_blur_radius+'||'+affi_hor_lnth+'||'+affi_ver_lnth+'||'+affi_shad_clr;
	
	//Template Style
     affi_tmp_wid = jQueryDM('#affi_tmp_wid').val();
     affi_tmp_bdr_style = jQueryDM('#affi_tmp_bdr_style').val();
     affi_tmp_bdr_wid = jQueryDM('#affi_tmp_bdr_wid').val();
     affi_tmp_br_radius = jQueryDM('#affi_tmp_br_radius').val();
     affi_tmp_border_color = jQueryDM('#affi_tmp_border_color').val();
     affi_tmp_padd_top = jQueryDM('#affi_tmp_padd_top').val();
     affi_tmp_padd_bottom = jQueryDM('#affi_tmp_padd_bottom').val();
     dm_affiliate_temp_aligment = jQueryDM('#dm_affiliate_temp_aligment').val();
     template_Style = affi_tmp_wid+'||'+affi_tmp_bdr_style+'||'+affi_tmp_bdr_wid+'||'+affi_tmp_br_radius+'||'+affi_tmp_border_color+'||'+affi_tmp_padd_top+'||'+affi_tmp_padd_bottom+'||'+dm_affiliate_temp_aligment;
	
	//Background Color
      
      affi_tmp_affi_background = jQueryDM('#affi_tmp_affi_background').val();
      affi_tmp_performace_background = jQueryDM('#affi_tmp_performace_background').val();
      affi_tmp_payment_background = jQueryDM('#affi_tmp_payment_background').val();
      affi_tmp_earnnings_background = jQueryDM('#affi_tmp_earnnings_background').val();
      affi_tmp_traffic_background = jQueryDM('#affi_tmp_traffic_background').val();
	 background_color = affi_tmp_affi_background+'||'+affi_tmp_performace_background+'||'+affi_tmp_payment_background+'||'+affi_tmp_earnnings_background+'||'+affi_tmp_traffic_background;
	 
	 styling_options = dispaly_opitons+'|||'+external_shadow_customizer+'|||'+template_Style+'|||'+background_color+'|||'+aff_coupon_details_outer;
	 edit_id =  jQueryDM('input[name="affiliate_edit_id"]').val();
	 template_selected_name =  jQueryDM('input[name="template_selected_name"]').val();
	 page_id = jQueryDM('input[name="page_id"]').val();
	  
	 dap_member_show_loader();
		jQueryDM.post(ajaxurl, {
			action: 'save_dap_member_affiliate_page_save',
			styling_options:styling_options,
			template_html:template_html,
			edit_id:edit_id,
			template_selected_name:template_selected_name,
			page_id:page_id,
		}, function(response) {
			dap_member_hide_loader();
			response = JSON.parse(response);
			if(response.success){
				jQueryDM('input[name="affiliate_edit_id"]').val(response.edit_id);
				$title = "Saved";
				$message = '<div class="dap_member_popup_msg">'+response.html+"</div>";
				$type = "success";
				
				jQueryDM('.dap_member_login_details .shortcode_display').html(response.short_code_html);
				jQueryDM('.dap_member_login_details .shortcode_page_link').html(response.page_url_html);
				jQueryDM('.dap_member_content #affiliate_page_url,.dap_member_content input[name="affiliate_name"],.dap_member_affiliate_create_page_btn').attr('disabled','disabled');  
				if(go_tab == ''){
				 dap_member_goto_next_tab('#dap_member_affiliated_shortdetails');	
				 return ;
				}
				
				
			}else if(response.error){
					$type = 'error';
					$title = 'Oops';
					$message = response.error;
			}
			 dap_member_sweet_alert_popup_with_msg_type($title,$message,$type);
		});	
	 
	 
}
