jQuery(document).ready(function(){
	invoice_mediauploader();
	
	
	/******Check Page Availability*************/
	
	jQuery('.check_availability_page_btn').click(function(e){
		e.preventDefault();
		dap_member_show_loader();
		
		var page_type = jQuery(this).attr('data-id');
		var page_name = jQuery('#'+page_type).val();
		
		jQuery('.success_'+page_type).hide();
		jQuery('.error_'+page_type).hide();
		
		if(page_name != '') {
			jQuery(this).text("Checking..."); 
			jQuery.post(ajaxurl, {
					action: 'dapcheckPageAvailabilityAjax',
					page_name: page_name,
			}, function(response) {
				dap_member_hide_loader(); 
				var result = response.split('||');
				var status = result[0];
				var pageid =  result[1];
				if (status == 'yes') {
					jQuery('.success_msg').hide();
					jQuery('.error_msg').show();
				} else if (status == 'no') {
					jQuery('.success_msg').show();
					jQuery('.error_msg').hide();
				}
				jQuery('.check_availability_page_btn').text("Check Availability");
				/*setTimeout(function(){ 
					jQuery('.success_'+page_type).hide();
					jQuery('.error_'+page_type).hide();
				 }, 3000);*/
				
			});
				
		}else{
			dap_member_sweet_alert_message('Please enter a name. Name cannot be empty.')
			dap_member_hide_loader(); 	
		}
	});
	
	
	jQuery('.create_shortcode_btn').click(function(){
		var invoice_data_page = jQuery('#create_invoice_page').val();	
		var invoice_data_id = jQuery('#invoice_page_url').find(":selected").val();		

		if(invoice_data_page == ''){
			invoice_data_page = invoice_data_id;	
		}

		if(invoice_data_page == ''){
			dap_member_sweet_alert_message('Please select a URL');
			
			dap_member_hide_loader();
			return false;	
		}
		
		dap_member_show_loader();
		
		var type = 'invoice';
		var template = jQuery('#itemplate').val();	
		var page_template = jQuery('#ptemplate').val();		
		var view_invoice_link = jQuery('#view_invoice_link').val();		
		var admin_email = jQuery('#adminemail').val();		
		var company_name = jQuery('#companyname').val();		
		var company_url = jQuery('#companyurl').val();		
		var no_transaction_msg = jQuery('#no_transaction_msg').val();		
		var logo = jQuery('.photo_url').val();	
			
		var edit_id = jQuery('#saved_id').val();		
		if(edit_id != ''){
			invoice_data_id = jQuery('#page_id').val();	
		}
		
		if(admin_email == ''){
			dap_member_hide_loader();
			swal('Please enter Admin Email.');return;
		}
		if(company_name == ''){
			dap_member_hide_loader();
			swal('Please enter Company Name.');return;
		}
		if(company_url == ''){
			dap_member_hide_loader();
			swal('Please enter Company URL.');return;
		}
		if(logo == ''){
			dap_member_hide_loader();
			swal('Please Upload Logo Image or Enter Logo Image URL.');return;
		}
		
		jQuery.post(ajaxurl, {
			action: 'dapSaveMemberInvoiceAjax',
			edit_id: edit_id,
			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,
		}, function(response) {
			dap_member_hide_loader();
			response = JSON.parse(response);		
			jQuery('#saved_id').val(response.id);
			jQuery('#page_id').val(response.page_id);
			if(response.edit_id){
				jQuery('input[name="edit_id"]').val(response.edit_id);
			}
			if(response.short_code_html){
				jQuery('.get_shortcode').html(response.short_code_html);
				jQuery('.shortcode_div').show();
				
			}
			
			if(response.error){
				jQuery('.success_msg').hide();
				jQuery('.error_msg').show();	
			}
				
		})	
	});



jQueryDM('#invoice_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:'invoice',
		}, 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('#invoice_page_url').val('');
				return ;
			}else if(response == 'no'){
				
				jQueryDM('input[name="page_id"]').val(page_id);
			}
	
		});
	});

});	

function dap_member_invoice_preview(tmp_no){

		dap_member_show_loader();
		jQueryDM.post(ajaxurl, {
			action: 'dap_member_invoice_preview',
			tmp_no:tmp_no,
			
		}, function(response) {
			response = JSON.parse(response);	
			dap_member_hide_loader();
			jQueryDM('#previewTemplateModal').modal('show');
			jQueryDM('#previewTemplateModal').find('.template_preview_body').html(response.html);
			jQueryDM('#previewTemplateModal').find('.template_preview_body').append("<link rel='stylesheet'   href='"+response.csspath+"' >");
			
		    console.log(response);	
	   
		});

	
}
function invoice_mediauploader(){
	
	var mediaUploader;
	jQuery(document).on('click','.photo_upload_outer 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_upload_outer').find('.photo_url').val(attachment.url);
		});
		mediaUploader.open();
	});
} 
