
var $jq = jQuery.noConflict();

$jq(document).ready(function(){
	
	dapwp_list_of_copy_post_url();
	$jq('#dap_manage_pay_per_post_table').show();
	$jq('#dap_manage_pay_per_post_table').DataTable({
			"order": [[ 0, "desc" ]],
	}); 
	
	//dap_customize_button_style_option_set_defaultvalues();
	
	 //buybutton
	$jq('#dm_btn_wid').bootstrapSlider().change(function() {
		$jq('#buybutton').css('width', this.value + 'px');
	});
	
	$jq('#dm_btn_height').bootstrapSlider().change(function() {
		$jq('#buybutton').css('padding-top', this.value + 'px');
		$jq('#buybutton').css('padding-bottom', this.value + 'px');
	});
		
	$jq('#dm_backgroud_color_div').colorpicker().on('changeColor', function() {
		$jq('#buybutton').css('background-color', $jq(this).colorpicker('getValue'));
	});	
	
	$jq('#dm_border_wid').bootstrapSlider().change(function() {
		$jq('#buybutton').css('border-width', this.value + 'px');
	});
	
	$jq('#dm_border_style').change(function(){
		var style = $jq(this).val();
		$jq('#buybutton').css('border-style', style);
	});
	
	$jq('#dm_border_color_div').colorpicker().on('changeColor', function() {
		$jq('#buybutton').css('border-color', $jq(this).colorpicker('getValue'));
	});
	$jq('#dm_border_rad').bootstrapSlider().change(function() {
		$jq('#buybutton').css('border-radius', this.value + 'px');
	});
	$jq('#dm_font_size').bootstrapSlider().change(function() {
		$jq('#buybutton').css('font-size', this.value + 'px');
	});
	$jq('#dm_font_weight').change(function(){
		var weight = $jq(this).val();
		$jq('#buybutton').css('font-weight', weight);
	});
	$jq('#dm_font_family').change(function(){
		var family = $jq(this).val();
		if(family ==2){
			$jq('#buybutton').css('font-family', "'Oswald', sans-serif");
		}else if(family ==3){
			$jq('#buybutton').css('font-family', "'Open Sans', sans-serif");
		}else if(family ==4){
			$jq('#buybutton').css('font-family', "'Poppins', sans-serif");
		}else{
			$jq('#buybutton').css('font-family', "'Roboto', sans-serif");
		}
		
	});
	
	 //rentbutton
	$jq('#dm_btn_wid1').bootstrapSlider().change(function() {
		$jq('#rentbutton').css('width', this.value + 'px');
	});
	
	$jq('#dm_btn_height1').bootstrapSlider().change(function() {
		$jq('#rentbutton').css('padding-top', this.value + 'px');
		$jq('#rentbutton').css('padding-bottom', this.value + 'px');
	});
		
	$jq('#dm_backgroud_color1_div').colorpicker().on('changeColor', function() {
		$jq('#rentbutton').css('background-color', $jq(this).colorpicker('getValue'));
	});	
	
	$jq('#dm_border_wid1').bootstrapSlider().change(function() {
		$jq('#rentbutton').css('border-width', this.value + 'px');
	});
	
	$jq('#dm_border_style1').change(function(){
		var style = $jq(this).val();
		$jq('#rentbutton').css('border-style', style);
	});
	
	$jq('#dm_border_color1_div').colorpicker().on('changeColor', function() {
		$jq('#rentbutton').css('border-color', $jq(this).colorpicker('getValue'));
	});
	$jq('#dm_border_rad1').bootstrapSlider().change(function() {
		$jq('#rentbutton').css('border-radius', this.value + 'px');
	});
	$jq('#dm_font_size1').bootstrapSlider().change(function() {
		$jq('#rentbutton').css('font-size', this.value + 'px');
	});
	$jq('#dm_font_weight1').change(function(){
		var weight = $jq(this).val();
		$jq('#rentbutton').css('font-weight', weight);
	});
	$jq('#dm_font_family1').change(function(){
		var family = $jq(this).text();
		if(family ==2){
			$jq('#rentbutton').css('font-family', "'Oswald', sans-serif");
		}else if(family ==3){
			$jq('#rentbutton').css('font-family', "'Open Sans', sans-serif");
		}else if(family ==4){
			$jq('#rentbutton').css('font-family', "'Poppins', sans-serif");
		}else{
			$jq('#rentbutton').css('font-family', "'Roboto', sans-serif");
		}
	});
	 
	 //membershipbutton
	$jq('#dm_btn_wid2').bootstrapSlider().change(function() {
		$jq('#membershipbutton').css('width', this.value + 'px');
	});
	
	$jq('#dm_btn_height2').bootstrapSlider().change(function() {
		$jq('#membershipbutton').css('padding-top', this.value + 'px');
		$jq('#membershipbutton').css('padding-bottom', this.value + 'px');
	});
		
	$jq('#dm_backgroud_color2_div').colorpicker().on('changeColor', function() {
		$jq('#membershipbutton').css('background-color', $jq(this).colorpicker('getValue'));
	});	
	
	$jq('#dm_border_wid2').bootstrapSlider().change(function() {
		$jq('#membershipbutton').css('border-width', this.value + 'px');
	});
	
	$jq('#dm_border_style2').change(function(){
		var style = $jq(this).val();
		$jq('#membershipbutton').css('border-style', style);
	});
	
	$jq('#dm_border_color2_div').colorpicker().on('changeColor', function() {
		$jq('#membershipbutton').css('border-color', $jq(this).colorpicker('getValue'));
	});
	$jq('#dm_border_rad2').bootstrapSlider().change(function() {
		$jq('#membershipbutton').css('border-radius', this.value + 'px');
	});
	 $jq('#dm_font_size2').bootstrapSlider().change(function() {
		$jq('#membershipbutton').css('font-size', this.value + 'px');
	});
	$jq('#dm_font_weight2').change(function(){
		var weight = $jq(this).val();
		$jq('#membershipbutton').css('font-weight', weight);
	});
	$jq('#dm_font_family2').change(function(){
		var family = $jq(this).text();
		if(family ==2){
			$jq('#membershipbutton').css('font-family', "'Oswald', sans-serif");
		}else if(family ==3){
			$jq('#membershipbutton').css('font-family', "'Open Sans', sans-serif");
		}else if(family ==4){
			$jq('#membershipbutton').css('font-family', "'Poppins', sans-serif");
		}else{
			$jq('#membershipbutton').css('font-family', "'Roboto', sans-serif");
		}
		 
	});
	
	
	// margin_between_buttons
	$jq('#dm_margin_between_btn').bootstrapSlider().change(function() {
		//console.log(this.value);
	});
	
	/********Save data********************/
	$jq(document).on('click', '.save_data',function() {			 
	   save_data();
	});
	
	 
	    
});
 
 
 
 

 /********Save data of the template********************/
function save_data(){ 			  
	     
		/*buy Btn options */
		var buy_btn_html =  $jq('.buybutton_outer').html();
		var dm_btn_wid =  $jq('#dm_btn_wid').val();
		var dm_btn_height =  $jq('#dm_btn_height').val();
		var dm_backgroud_color =  $jq('#dm_backgroud_color').val();		 
		var dm_border_style =  $jq('#dm_border_style').val();
		var dm_border_wid =  $jq('#dm_border_wid').val();
		var dm_border_color =  $jq('#dm_border_color').val();
		var dm_border_rad =  $jq('#dm_border_rad').val();
		var dm_font_size =  $jq('#dm_font_size').val();
		var dm_font_weight =  $jq('#dm_font_weight').val();
		var dm_font_family =  $jq('#dm_font_family').val();
		
		/*Rent Btn options */
		var rent_btn_html =  $jq('.rentbutton_outer').html();
		var dm_btn_wid1 =   $jq('#dm_btn_wid1').val();
		var dm_btn_height1 =   $jq('#dm_btn_height1').val();
		var dm_backgroud_color1 =    $jq('#dm_backgroud_color1').val();		 
		var dm_border_style1 =   $jq('#dm_border_style1').val();
		var dm_border_wid1 =   $jq('#dm_border_wid1').val();
		var dm_border_color1 =   $jq('#dm_border_color1').val();
		var dm_border_rad1 =   $jq('#dm_border_rad1').val();
		var dm_font_size1 =  $jq('#dm_font_size1').val();
		var dm_font_weight1 =  $jq('#dm_font_weight1').val();
		var dm_font_family1 =  $jq('#dm_font_family1').val();
		
		/*Membership Btn options */
		var membership_btn_html =  $jq('.membershipbutton_outer').html();
		var dm_btn_wid2 =   $jq('#dm_btn_wid2').val();
		var dm_btn_height2 =   $jq('#dm_btn_height2').val();
		var dm_backgroud_color2 =  $jq('#dm_backgroud_color2').val();		 
		var dm_border_style2 =   $jq('#dm_border_style2').val();
		var dm_border_wid2 =  $jq('#dm_border_wid2').val();
		var dm_border_color2 =   $jq('#dm_border_color2').val();
		var dm_border_rad2 =   $jq('#dm_border_rad2').val();
		var dm_font_size2 =  $jq('#dm_font_size2').val();
		var dm_font_weight2 =  $jq('#dm_font_weight2').val();
		var dm_font_family2 =  $jq('#dm_font_family2').val();
		
		// margin_between_btn 
			var dm_margin_between_btn = $jq('#dm_margin_between_btn').val();

		// buttons alignment
			var dm_buttom_alignment = $jq('#dm_buttom_alignment').val();

		
	    var buy_btn_option = dm_btn_wid+'||'+dm_btn_height+'||'+dm_backgroud_color+'||'+dm_border_style+'||'+dm_border_wid+'||'+dm_border_color+'||'+dm_border_rad+'||'+dm_font_size+'||'+dm_font_weight+'||'+dm_font_family+'||'+dm_margin_between_btn+'||'+dm_buttom_alignment;
	    var rent_btn_option = dm_btn_wid1+'||'+dm_btn_height1+'||'+dm_backgroud_color1+'||'+dm_border_style1+'||'+dm_border_wid1+'||'+dm_border_color1+'||'+dm_border_rad1+'||'+dm_font_size1+'||'+dm_font_weight1+'||'+dm_font_family1+'||'+dm_margin_between_btn+'||'+dm_buttom_alignment;
	    var membership_btn_option = dm_btn_wid2+'||'+dm_btn_height2+'||'+dm_backgroud_color2+'||'+dm_border_style2+'||'+dm_border_wid2+'||'+dm_border_color2+'||'+dm_border_rad2+'||'+dm_font_size2+'||'+dm_font_weight2+'||'+dm_font_family2+'||'+dm_margin_between_btn+'||'+dm_buttom_alignment;
	    
 		$jq( '.save_data').text("Saving...");	
		$jq.post(ajaxurl, {
			action: 'savePayPerPostButtonCustomizerAjax',
			buy_btn_option:buy_btn_option,
			rent_btn_option:rent_btn_option,
			membership_btn_option:membership_btn_option,
			buy_btn_html:buy_btn_html,
			rent_btn_html:rent_btn_html,
			membership_btn_html:membership_btn_html,
		}, function(response) {
			$jq( '.save_data').text("Save");
			$jq( '.saved_warning').show("slow");
			// console.log(response); 
			setTimeout(function(){ $jq('.saved_warning').hide("slow"); }, 3000);
		});	
	
	
}

 
		
 
function dap_customize_button_style_option_set_defaultvalues(){	 	  
 
			var dm_btn_wid = $jq('#buybutton').css('width');
			dm_btn_wid = dm_btn_wid.split("px");
			var dm_btn_wid = dm_btn_wid[0];
			dm_btn_wid = parseInt(dm_btn_wid);
			$jq('#dm_btn_wid').val(dm_btn_wid);
			
			var dm_btn_height = $jq('#buybutton').css('padding-top');
			dm_btn_height = dm_btn_height.split("px");
			var dm_btn_height = dm_btn_height[0];
			dm_btn_height = parseInt(dm_btn_height);
			$jq('#dm_btn_height').val(dm_btn_height);
			
			var dm_border_wid = $jq('#buybutton').css('border-width');
			dm_border_wid = dm_border_wid.split("px");
			var dm_border_wid = dm_border_wid[0];
			dm_border_wid = parseInt(dm_border_wid);
			$jq('#dm_border_wid').val(dm_border_wid);
			
			var dm_border_rad = $jq('#buybutton').css('border-radius');
			dm_border_rad = dm_border_rad.split("px");
			var dm_border_rad = dm_border_rad[0];
			dm_border_rad = parseInt(dm_border_rad);
			$jq('#dm_border_rad').val(dm_border_rad);
			
			var dm_border_style = $jq('#buybutton').css('border-style');
			$jq('#dm_border_style').val(dm_border_style);
			 
			var dm_bdr_color_div = $jq('#buybutton').css('border-color');
			$jq('#dm_bdr_color_div').colorpicker('setValue', dm_bdr_color_div);	
			
			var dm_backgroud_color_div = $jq('#buybutton').css('background-color');
			$jq('#dm_backgroud_color_div').colorpicker('setValue', dm_backgroud_color_div);	
			
}
 
 
 function dapwp_update_post_status(post_id){
	 
	 var dapwp_post_status_checkbox = $jq('.dapwp_post_status_checkbox_'+post_id).prop('checked');
	 if(dapwp_post_status_checkbox){
		 dapwp_post_status_checkbox = 'Y';
	 }else {
		 dapwp_post_status_checkbox = 'N';
	 }
	jQuery.post(ajaxurl, {
			action: 'dapPayPerPostEnableAndDisableActionAjax',
			dapwp_post_status_checkbox: dapwp_post_status_checkbox,
			post_id: post_id,
	}, function(response) {
		
	});
	 
 }

 function delete_dapwp_pay_per_post(post_id = 0){
	 //console.log(post_id);
	 swal({
		title: "",
		text: "Are you sure you want to delete?",
		type: "warning",
		showCancelButton: true,
		showCloseButton: true,
		confirmButtonColor: "#da3935",
		confirmButtonText: "Yes, Delete!",
		customClass: 'dapwp_sweet_alert_message_class ',
	}).then((result) => {
		if (result.value) {
						$jq.post(ajaxurl, {
							action: 'dapPayPerPostEnableAndDisableActionAjax',
							post_id:post_id,
						}, function(response) {
							var dap_manage_pay_per_post_table = $jq('#dap_manage_pay_per_post_table').DataTable();
							dap_manage_pay_per_post_table.row('.dapwp_post_tr_'+post_id).remove().draw();
							swal("" ,"Deleted successfully" ,"success");
						});
		}
	});				
 }

function dapwp_list_of_copy_post_url(){
	$jq('.dapwp_selected_post_list input[type="checkbox"]').on('click',function(){
		$jq('.allSelectedProductsPreview').html('');
		var html = '';
		$jq('.dapwp_selected_post_list input[type="checkbox"]:checked').each(function(){
			var post_link = jQuery(this).val();
			html += '<li>'+post_link+'</li>';
			
		});
		$jq('.allSelectedProductsPreview').append(html);
	});
}

function dapwp_copy_post(){
	var source_post_id = $jq('.source_post_id').val();
	var source_page_enter_url = $jq('.source_page_enter_url').val();
	if(source_post_id == 0 && source_page_enter_url == '' ){
		swal('Please select main post.');
		return false;
	}
	var no_of_select_post = $jq('.dapwp_selected_post_list input[type="checkbox"]:checked').length;
	if(no_of_select_post == 0){
		swal('Please select posts to which you want to copy');
		return false;
	}
	var distination_post_ids = {};
	var i_counter = 0;
	$jq('.dapwp_selected_post_list input[type="checkbox"]:checked').each(function(){
		distination_post_ids[i_counter++] = $jq(this).attr('data-post-id');
	});
	console.log(distination_post_ids);
	
	$jq('.pp-post-loader').show();
	
	$jq.post(ajaxurl, {
		action: 'dapPayPerPostCopyActionAjax',
		source_post_id:source_post_id,
		source_page_enter_url:source_page_enter_url,
		distination_post_ids:distination_post_ids,
	}, function(response) {
		response = $jq.parseJSON(response); 	
		$jq('.pp-post-loader').hide();	
		if(response.success){
			swal("" ,"Copied successfully" ,"success");
		}else if(response.error){
			swal("" ,response.error ,"error");
		}
		
		
	});
}
