jQuery("document").ready(function () {
	
	invoice_mediauploader();
 
 
	jQuery('.delete_member_pages').click(function(e){
		e.preventDefault();
		var id = jQuery(this).attr('data-id'); 
		swal({
		  title: "Are you sure?",
		  text: "Do you really want to Delete?",
		  icon: "warning",
		  buttons: true,
		  dangerMode: true,
		})
		.then((willDelete) => {
			if (willDelete) {
				jQuery.post(ajaxurl, {
						action: 'dapDeleteMemberPageAjax',
						id: id,
				}, function(response) {
					swal('Deleted Successfully!');
					location.reload();
				});
			}else{
				return false;	
			}
		});
	});
	
	jQuery('#dap_member_page_table').DataTable({
			"language": {
				"emptyTable": "No Shortcodes Found"
			},
			"order": [[ 0, "desc" ]],
			"fnInitComplete": function (){			 
				dap_member_hide_loader(); 
				jQuery('#dap_member_page_table').show();				 
			}
	}); 
	
	
	 jQuery('.create_member').change(function() {
		var selValue = jQuery(".create_member:checked").val();
		
		if(selValue == 'no'){
			jQuery('#create_member_no').show();
			jQuery('#create_member_yes').hide();
					
		}else if(selValue == 'yes'){
			jQuery('#create_member_no').hide();
			jQuery('#create_member_yes').show();
		}
	});
	
	
	jQuery('.Select_with_check input[type="checkbox"]').click(function(){
		console.log(jQuery(this).val());
		var type = jQuery(this).val();
		if(jQuery(this).prop("checked") == true){
			 jQuery('#member_'+type+'_outer').show();
			
		}
		else if(jQuery(this).prop("checked") == false){
			 jQuery('#member_'+type+'_outer').hide();
			
		}
	});
	
	jQuery('#content_page_url').on('change', function() {		 
		var selval = jQuery(this).val();
		jQuery('#content_page_id').val(selval);	 
	});
	
	
/*	jQuery('.create_shortcode_btn').on('click',function(){
		
		jQuery("#create_member_yes .Select_with_check input[type='checkbox'][name='member_type']").each(function(){
			
			if(jQuery(this).prop("checked") == true){
				
				if(jQuery(this).val() == "login"){					
					
				 
				}else if(jQuery(this).val() == "content"){					
					
				  
				}else if(jQuery(this).val() == "profile"){				
					
				}
			}
			jQuery('.shortcode_div').show();
		}); 
		
	});*/
	
	/******Create page*******/
	jQuery('.create_page_btn').click(function(e) {
		e.preventDefault();
		return;

			var post_id = jQuery(this).attr("id");
			
			var post_title = jQuery('.page_name').val();
			post_title = jQuery.trim(post_title);
			if (post_title != '') {
				 jQuery('.create_page_btn').text("Creating...") 
				jQuery.post(ajaxurl, {
					action: 'dapMemberGenerateWpPage',
					post_title: post_title,
				}, function(response) {
					response = JSON.parse(response); 
					jQuery("#"+post_id+"_id").val(response.post_id); 
					if (response.status == 'yes') {
						jQuery('.success_msg').hide();
						jQuery('.error_msg').show();
					} else if (response == 'no') {
						jQuery('.error_msg').hide();
						jQuery('.success_msg').show();
					}
					jQuery('.create_page_btn').text("Click to Create");
					setTimeout(function(){ 
						jQuery('.success_msg').hide();
						jQuery('.error_msg').hide();
					 }, 3000);
					
				});
			} else {
				alert('Please enter a name. Checkout name cannot be empty.');
			}

	});
	
	
	/******Check Page Availability*************/
	
	jQuery('.check_availability_page_btn').click(function(e){
		e.preventDefault();
				
		var page_type = jQuery(this).attr('id');
		var page_name = jQuery('#create_'+page_type).val();
		
		jQuery('.success_'+page_type).hide();
		jQuery('.error_'+page_type).hide();
		 
		if(page_name == '' || page_name == null || typeof page_name == undefined) {
			swal('Please enter a name. Name cannot be empty.')
			return false;
		}	
		jQuery(this).text("Checking...");
		dap_member_show_loader(); 
		jQuery.post(ajaxurl, {
				action: 'dapcheckPageAvailabilityAjax',
				page_name: page_name,
		}, function(response) {
			
			var result = response.split('||');
			var status = result[0];
			var pageid =  result[1];

			dap_member_hide_loader(); 
			console.log(result);
			if (status == 'yes') {
				jQuery('.success_create_'+page_type).hide();
				jQuery('.error_create_'+page_type).show();
			} else if (status == 'no') {
				jQuery('.error_create_'+page_type).hide();
				jQuery('.success_create_'+page_type).show();
				jQuery('#'+page_type+'_id').val(pageid);
			}
			jQuery('.check_availability_page_btn').text("Check Availability");
			 	
			/*setTimeout(function(){ 
				jQuery('.success_'+page_type).hide();
				jQuery('.error_'+page_type).hide();
			 }, 3000);*/
			
		});
				
		 
	});
	
	/******Create shortcode*******/
	jQuery('.create_shortcode_btn').click(function(e) {
			e.preventDefault();
			dap_member_show_loader();
			
			jQuery('.error_invoice_logo_image').hide();
			jQuery('.error_invoice_company_url').hide();
			jQuery('.error_invoice_company_name').hide();
			jQuery('.error_invoice_admin_email').hide();
			var addin_config_login = jQuery('#addin_config_login').val();
			var addin_config_content = jQuery('#addin_config_content').val();
			var addin_config_profile = jQuery('#addin_config_profile').val();
			var addin_config_affiliate = jQuery('#addin_config_affiliate').val();
			var addin_config_invoice = jQuery('#addin_config_invoice').val();
			var addin_config_cancellation = jQuery('#addin_config_cancellation').val();
			
			var member_types = [];
			var login_data_page = '';
			var login_data_id = '';
			var j = 0;
			jQuery('input[name=member_type]').each(function(){
				if(jQuery(this).prop('checked') == true){
					
					member_types.push(jQuery(this).val());
					var type = jQuery(this).val();
					var page = jQuery('#create_'+type+'_page').val();
					
					if(page == ''){
						page = jQuery('#'+type+'_page_url').val();	
					}
					
					if(page == ''){
						swal('Please select a URL');
						dap_member_hide_loader();
						return false;	
					}
					
					if(type == 'login'){
						login_data_page = jQuery('#create_'+type+'_page').val();	
						login_data_id = jQuery('#'+type+'_page_url').find(":selected").val();		
						
						jQuery.post(ajaxurl, {
								action: 'dapSaveMemberLoginAjax',
								login_data_page: login_data_page,
								login_data_id: login_data_id,
								addin_config_login: addin_config_login,
						}, function(response) {
							response = JSON.parse(response);		
							dap_member_hide_loader();
							if(response.error){
								jQuery('.error_create_'+type+'_page').show();
								jQuery('.success_create_'+type+'_page').hide();
								jQuery('.'+type+'_shortcode_outer').html('');
								return true;
							}	
							jQuery('.login_shortcode_outer').html(response.short_code_html);
							jQuery('.shortcode_div').show();
							
							
						})	
					}else if(type == 'content'){
						content_data_page = jQuery('#create_'+type+'_page').val();						 
						content_data_id = jQuery('#content_page_url').val();		
						
						jQuery.post(ajaxurl, {
							action: 'dapSaveMemberContentAjax',
							content_data_page: content_data_page,
							content_data_id: content_data_id,
							addin_config_content: addin_config_content,
						}, function(response) {
							response = JSON.parse(response);		
							dap_member_hide_loader();
							if(response.error){
								jQuery('.error_create_'+type+'_page').show();
								jQuery('.success_create_'+type+'_page').hide();
								jQuery('.mycontent_shortcode_outer').html('');
								return true;
							}	
							jQuery('.mycontent_shortcode_outer').html(response.short_code_html);
							jQuery('.shortcode_div').show();
							
						})	
					}else if(type == 'affiliate'){
						affiliate_data_page = jQuery('#create_'+type+'_page').val();	
						affiliate_data_id = jQuery('#'+type+'_page_url').find(":selected").val();		
						
						jQuery.post(ajaxurl, {
							action: 'dapSaveMemberAffiliateAjax',
							affiliate_data_page: affiliate_data_page,
							affiliate_data_id: affiliate_data_id,
							addin_config_affiliate: addin_config_affiliate,
						}, function(response) {
							response = JSON.parse(response);		
							dap_member_hide_loader();
							if(response.error){
								jQuery('.error_create_'+type+'_page').show();
								jQuery('.success_create_'+type+'_page').hide();
								jQuery('.'+type+'_shortcode_outer').html('');
								return true;
							}	
							jQuery('.affiliate_shortcode_outer').html(response.short_code_html);
							jQuery('.shortcode_div').show();
							
						})	
					}else if(type == 'invoice'){
						var invoice_data_page = jQuery('#create_invoice_page').val();	
						var invoice_data_id = jQuery('#invoice_page_url').find(":selected").val();		
						
						var template = 'template1';	
						var page_template = 'template1';		
						var view_invoice_link = 'Y';		
						var admin_email = jQuery('#invoice_admin_email').val();		
						var company_name = jQuery('#invoice_company_name').val();		
						var company_url = jQuery('#invoice_company_url').val();		
						var no_transaction_msg = 'Sorry, no transactions found';	
						var logo = jQuery('.photo_url').val();	
						var callInvoiceAjax = 'yes';
						
						if(invoice_data_page == '' && invoice_data_id == ''){
							jQuery('.error_invoice_no_page_selected').show();
							callInvoiceAjax = 'no';
						}						
						if(admin_email == ''){
							jQuery('.error_invoice_admin_email').show();
							callInvoiceAjax = 'no';
						}
						if(company_name == ''){
							jQuery('.error_invoice_company_name').show();
							callInvoiceAjax = 'no';
						}
						if(company_url == ''){
							jQuery('.error_invoice_company_url').show();
							callInvoiceAjax = 'no';
						}
						if(logo == ''){
							jQuery('.error_invoice_logo_image').show();
							callInvoiceAjax = 'no';
						}
						if(callInvoiceAjax == 'yes'){
							jQuery.post(ajaxurl, {
								action: 'dapSaveMemberInvoiceAjax',
								invoice_data_page: invoice_data_page,
								invoice_data_id: invoice_data_id,
								template: template,
								page_template: page_template,
								view_invoice_link: view_invoice_link,
								admin_email: admin_email,
								company_name: company_name,
								company_url: company_url,
								no_transaction_msg: no_transaction_msg,
								logo: logo,
								addin_config_invoice: addin_config_invoice,
							}, function(response) {
								response = JSON.parse(response);	
								dap_member_hide_loader();
								if(response.error){
									jQuery('.error_create_'+type+'_page').show();
									jQuery('.success_create_'+type+'_page').hide();
									jQuery('.'+type+'_shortcode_outer').html('');
									return true;
								}		
								jQuery('.invoice_shortcode_outer').html(response.short_code_html);
								jQuery('#invoice_saved_id').val(response.id);
								jQuery('#invoice_page_id').val(response.page_id);
								jQuery('.shortcode_div').show();	
									
							})	
						}
					}else if(type == 'cancellation'){
						var create_cancellation_page = jQuery('#create_cancellation_page').val();	
						var cancellation_page_url = jQuery('#cancellation_page_url').find(":selected").val();		
						
						if(cancellation_page_url == ''){
							cancellation_page_url = jQuery('#cancellation_page_id').val();	
						}
						
						var id = jQuery('#cancellation_saved_id').val();	
						var template = 'template1';	
						var show_all_transactions = 'N';	
						var product_type = 'ALL';		
						var contact_us = '';		
						var contact_us_link = '';		
						var cancellation_data_id = jQuery('#'+type+'_page_url').find(":selected").val();		
						var show_cancel = 'Y';		
						var cancel_text = 'CANCELLED';	
						var success_cancel_msg = 'Subscription cancellation completed Successfully!';	
						var failed_cancel_msg = 'Sorry, could not cancel the subscription.	';	
						var cancel_cancel_msg = 'Are you sure you want to cancel subscription?';	
						var pause_resume_select = 'no';	
						var dateformat = 'YYYY-MM-DD';	
						var bgcolor = '#f9f9f9';	
						var payment = 'PAYPAL';	
						var showrenewal = 'Y';	
						var templatetextcolor = '#000';	
						var cancelimage = '';	
						var cancelcontact = '';	
						var cancelcontacturl = '';	
						var cancelbuttonstyle = 'padding-top: 6px;background: #dc3545 ;border-color: #333333;color: #ffffff;font-size: 11px;border-width: 0px;border-style: none;border-radius: 0px; font-weight:600;width:90px;padding-bottom: 6px;';	
						var cancelbuttonstyleNew = '6;#17a2b8;none;#ffffff;11;0;none;0;600;90;6';	
						var cancelbuttontext = 'CANCEL';	
						var upgradelink = '';	
						var upgradetext = 'UPGRADE';	
						var upgradetextcolor = 'blue';	
						var canceltext = 'CANCEL';	
						var canceltextcolor = 'blue';	
						var cancelledtextcolor = 'red';	
						var notransfoundmsg = 'Sorry, no active transactions found';	
						var showpause = 'Y';	
						var pausesuccess = 'Subscription paused successfully.';	
						var pausefail = 'Sorry,could not pause the subscription.';	
						var pauseconfirm = 'Are you sure you pause the subscription?';	
						var pauseurl = '';	
						var pausebuttonstyle = 'padding-top: 6px;background: #1e7e34 ;border-color: none;color: #ffffff;font-size: 11px;border-width: 0;border-style: none;border-radius: 0px; font-weight:600;width:90px;padding-bottom: 6px;';	
						var pausebuttonstyleNew = '6;#1e7e34;none;#ffffff;11;0;none;0;600;90;6';	
						var pausebuttontext = 'PAUSE';	
						var resumesuccess = 'Subscription resumed successfully.';	
						var resumefail = 'Sorry,could not Resume the subscription.';	
						var resumeconfirm = 'Are you sure you resume the subscription?';	
						var resumeurl = '';	
						var resumebuttonstyle = 'padding-top: 6px;background: #17a2b8 ;border-color: none;color: #ffffff;font-size: 11px;border-width: 0px;border-style: none;border-radius: 0px; font-weight:600;width:90px;padding-bottom: 6px;';	
						var resumebuttonstyleNew = '6;#17a2b8;none;#ffffff;11;0;none;0;600;90;6';	
						var resumebuttontext = 'RESUME';	
						
						var callInvoiceAjax = 'yes';
						
						if(create_cancellation_page == '' && cancellation_page_url == ''){
							jQuery('.error_cancellation_no_page_selected').show();
							callInvoiceAjax = 'no';
						}
						
						if(callInvoiceAjax == 'yes'){
							jQuery.post(ajaxurl, {
								action: 'dapSaveMemberCancelAjax',
								id: id,
								template: template,
								show_all_transactions: show_all_transactions,
								product_type: product_type,
								contact_us: contact_us,
								contact_us_link: contact_us_link,
								cancellation_page_url: cancellation_page_url,
								cancellation_data_id: cancellation_data_id,
								create_cancellation_page: create_cancellation_page,
								show_cancel: show_cancel,
								cancelled_text: cancel_text,
								success_cancel_msg: success_cancel_msg,
								failed_cancel_msg: failed_cancel_msg,
								cancel_cancel_msg: cancel_cancel_msg,
								pause_resume_select: pause_resume_select,
								dateformat: dateformat,
								bgcolor: bgcolor,
								payment: payment,
								showrenewal: showrenewal,
								showrenewal: showrenewal,
								templatetextcolor: templatetextcolor,
								cancelimage: cancelimage,
								cancelcontact: cancelcontact,
								cancelcontacturl: cancelcontacturl,
								cancelbuttonstyle: cancelbuttonstyle,
								cancelbuttontext: cancelbuttontext,
								upgradelink: upgradelink,
								upgradetext: upgradetext,
								upgradetextcolor: upgradetextcolor,
								canceltext: canceltext,
								canceltextcolor: canceltextcolor,
								cancelledtextcolor: cancelledtextcolor,
								notransfoundmsg: notransfoundmsg,
								showpause: showpause,
								pausesuccess: pausesuccess,
								pausefail: pausefail,
								pauseconfirm: pauseconfirm,
								pauseurl: pauseurl,
								pausebuttonstyle: pausebuttonstyle,
								pausebuttontext: pausebuttontext,
								resumesuccess: resumesuccess,
								resumefail: resumefail,
								resumeconfirm: resumeconfirm,
								resumeurl: resumeurl,
								resumebuttonstyle: resumebuttonstyle,
								resumebuttontext: resumebuttontext,
								cancelbuttonstyleNew: cancelbuttonstyleNew,
								pausebuttonstyleNew: pausebuttonstyleNew,
								resumebuttonstyleNew: resumebuttonstyleNew,
								addin_config_cancellation: addin_config_cancellation,
							}, function(response) {
								response = JSON.parse(response);		
								dap_member_hide_loader();
								if(response.error){
									jQuery('.error_create_'+type+'_page').show();
									jQuery('.success_create_'+type+'_page').hide();
									jQuery('.'+type+'_shortcode_outer').html('');
									return true;
								}	
								jQuery('.cancellation_shortcode_outer').html(response.short_code_html);
								jQuery('#cancellation_saved_id').val(response.id);
								jQuery('#cancellation_page_id').val(response.page_id);
								jQuery('.shortcode_div').show();	
									
							})	
						}
					}else if(type == 'profile'){
						var create_profile_page = jQuery('#create_profile_page').val();	
						var profile_page_url = jQuery('#profile_page_url').find(":selected").val();		
						
						jQuery.post(ajaxurl, {
							action: 'dapSaveMemberProfileAjax',
							create_profile_page: create_profile_page,
							profile_page_url: profile_page_url,
							addin_config_profile: addin_config_profile,
						}, function(response) {
							response = JSON.parse(response);		
							dap_member_hide_loader();
							if(response.error){
								jQuery('.error_create_'+type+'_page').show();
								jQuery('.success_create_'+type+'_page').hide();
								jQuery('.'+type+'_shortcode_outer').html('');
								return true;
							}	
							jQuery('.profile_shortcode_outer').html(response.short_code_html);
							jQuery('.shortcode_div').show();	
								
						})	
					}
					
					j++;
				}
			});
			
		/*	
			if(j > 0){
				all_member_types =  member_types.join("|");
			}else{
				alert('Please select the member type');
				return false;	
			}
			*/
			
	});
	
	
	
});

function invoice_mediauploader(){
	
	var mediaUploader;
	jQuery(document).on('click','input[type="button"][name="photo"]',function() {
		 
		if (mediaUploader) {
			mediaUploader.open();
			return;
		}
		mediaUploader = wp.media.frames.file_frame = wp.media({
			title: 'Choose Image',
			button: {
				text: 'Choose Image'
			},
			multiple: false
		});
		mediaUploader.on('select', function() {
			
			attachment = mediaUploader.state().get('selection').first().toJSON();
			jQuery('.photo_url').val(attachment.url);
		});
		mediaUploader.open();
	});
} 

function dap_select_member_page(){
	
	
}
