jQuery(document).ready(function(){
	
	
	jQuery('.add_subaccount_btn').click(function(){
		var productId = jQuery(this).closest('.subaccountMainOuter').attr('data-id');
		jQuery('.displayed_modal').val(productId);
		jQuery('.showHideCommon').hide();
		jQuery('.productListDropDown').hide();
		jQuery('#modalRegisterForm').find('#productDropDown_'+productId).first().show();
		jQuery('#modalRegisterForm').appendTo("body") ;
		jQuery('.modalAddNewUser').trigger('click');
	});
	
	jQuery('.editable').each(function(){
		jQuery(this).attr('contenteditable', false);
	});
	
	jQuery('.edit_subaccount_btn').click(function(e){
		e.preventDefault();
		
		dap_member_show_loader();
		
		var user_id = jQuery(this).attr('data-key');
		var prod_id = jQuery(this).attr('data-id');
		var main_id = jQuery('input[name=subaccounUserId]').val();
		
		var productId = jQuery(this).closest('.subaccountMainOuter').attr('data-id');
		jQuery('#modalRegisterForm').appendTo("body") ;
		jQuery.post(admin_ajaxurl, {
			action: "dap_user_details",
			user_id: user_id,
		},function(response) {
			response = JSON.parse(response);
			dap_member_hide_loader();
			if(response.error == ''){
				var id = response.user.id;
				var first_name = response.user.first_name;	
				var last_name = response.user.last_name;
				var user_name = response.user.user_name;
				var email = response.user.email;
				var address1 = response.user.address1;
				var address2 = response.user.address2;
				var city = response.user.city;
				var state = response.user.state;
				var zip = response.user.zip;
				var country = response.user.country;

				
				jQuery('#modalRegisterForm').find('#productDropDown_'+productId).first().val(prod_id);
				jQuery('#modalRegisterForm').find('input[name=u_first_name]').val(first_name);
				jQuery('#modalRegisterForm').find('input[name=u_last_name]').val(last_name);
				jQuery('#modalRegisterForm').find('input[name=u_email]').val(email);
				jQuery('#modalRegisterForm').find('input[name=u_address1]').val(address1);
				jQuery('#modalRegisterForm').find('input[name=u_address2]').val(address2);
				jQuery('#modalRegisterForm').find('input[name=u_city]').val(city);
				jQuery('#modalRegisterForm').find('input[name=u_state]').val(state);
				jQuery('#modalRegisterForm').find('input[name=u_zip]').val(zip);
				jQuery('#modalRegisterForm').find('input[name=u_country]').val(country);
				
				jQuery('.dap_user_id').val(id);
				jQuery('#modalRegisterForm').find('.productListDropDown').hide();
				jQuery('#modalRegisterForm').find('#productDropDown_'+productId).first().show();
				jQuery('#modalRegisterForm').find('.createSubaccountButton').html('Save');
				jQuery('.modalAddNewUser').trigger('click');	
						
			}else{
				swal(response.error);	
			}
		});
		
		
	});
	
	jQuery('.delete_subaccount_btn').click(function(e){
		e.preventDefault();
		
		var user_id = jQuery(this).attr('data-key');
		var prod_id = jQuery(this).attr('data-id');
		var main_id = jQuery('input[name=subaccounUserId]').val();
		
		swal({
			title: delete_confirmation,
			text: "",
			icon: "warning",
			buttons: true,
			dangerMode: true,
		})
		.then((willDelete) => {
			if (willDelete) {
				dap_member_show_loader();
				jQuery.post(admin_ajaxurl, {
					action: "dap_delete_user",
					user_id: user_id,
					prod_id: prod_id,
					main_id: main_id,
				},function(response) {
					swal(delete_success);
					location.reload();
				});
			} else {
				return;
			}
		});
		
		
		
	}); 
		
	jQuery('.createSubaccountButton').click(function(e){
		e.preventDefault();
		var  ori_text = jQuery('.createSubaccountButton').html();
		
		jQuery('.createSubaccountButton').html('Please wait..');
		
		//dap_member_show_loader();
		var pId = jQuery('.displayed_modal').val();
		var id = jQuery('.dap_user_id').val();
		var userId = jQuery('input[name=subaccounUserId]').val();
		var productId = jQuery('.productListDropDown:visible').val();
		var first_name = jQuery('input[name=u_first_name]').val();
		var last_name = jQuery('input[name=u_last_name]').val();
		var email = jQuery('input[name=u_email]').val();
		var address1 = jQuery('input[name=u_address1]').val();
		var address2 = jQuery('input[name=u_address2]').val();
		var city = jQuery('input[name=u_city]').val();
		var state = jQuery('input[name=u_state]').val();
		var zip = jQuery('input[name=u_zip]').val();
		var country = jQuery('input[name=u_country]').val();
		
		var fieldName = '';
		var fieldValue = '';
			
		var productUserField = jQuery('#productUserField').val();
		productUserField = JSON.parse(productUserField);
		var usersList = '';
		if(productUserField != null){
			usersList = productUserField[pId];
		}
		
		if(jQuery('.addtional_info_outer').is(":visible")){
			jQuery('.custom_add_fields').each(function(){
				fieldName += jQuery('*[id^="custom_"]').attr('name')+'|';	
				fieldValue += jQuery('*[id^="custom_"]').val()+',';			
			})
			
		}		
		
		if(first_name == ''){
			swal('Please Enter First Name');
			jQuery('.createSubaccountButton').html(ori_text);
			jQuery('.createSubaccountButton').find('.waves-ripple').remove();
			return false;
		}else if(email == ''){
			swal('Please Enter Email');
			jQuery('.createSubaccountButton').html(ori_text);
			jQuery('.createSubaccountButton').find('.waves-ripple').remove();
			return false;
		}
		
		
		jQuery.post(admin_ajaxurl, {
			action: "dap_create_new_user",
			id: id,
			pId: pId,
			first_name: first_name,
			last_name: last_name,
			email: email,
			address1: address1,
			address2: address2,
			city: city,
			state: state,
			zip: zip,
			country: country,
			userId: userId,
			productId: productId,
			fieldValue: fieldValue,
			fieldName: fieldName,
			usersList: usersList,
		},function(response) {
			if(response == 'limit_reached'){
				jQuery('.createSubaccountButton').html(ori_text);
				jQuery('.createSubaccountButton').find('.waves-ripple').remove();
				swal(limit_reached);	
			}else{
				swal(success_msg);	
				location.reload();
			}	
			
			//dap_member_hide_loader();
		});
				
	});


	/*****bulk account start********/

	jQuery(document).on('click', '.add_subaccount_bulk_btn', function(e){
		e.preventDefault();

		var outerData = jQuery(this).closest('.subaccountMainOuter');
		var dataId = jQuery(this).closest('.subaccountMainOuter').attr('data-id');
		var products = jQuery('#productDropDown_'+dataId).html();

		jQuery(outerData).find('.bulkImportHtmlOuter').show();

		jQuery(outerData).find('.bulkImportHtmlOuter .productListDropDown').html(products);
		jQuery(outerData).find('.bulkImportHtmlOuter .productListDropDown').attr('id', 'productDropDown_'+dataId);
		jQuery(outerData).find('.bulkImportHtmlOuter .productListDropDown').show();
	});

	jQuery(document).on('click', '.close_grp_bulk_import', function(e){
		e.preventDefault();
		var outerData = jQuery(this).closest('.subaccountMainOuter');
		
		jQuery(outerData).find('.bulkImportHtmlOuter').hide();

		jQuery(outerData).find('.bulkImportHtmlOuter .productListDropDown').hide();
	});

	jQuery(document).on('click', '.createSubaccountBulkButton', function(e){
		e.preventDefault();

		

		var outerData = jQuery(this).closest('.subaccountMainOuter');
		var pId = jQuery(this).closest('.subaccountMainOuter').attr('data-id');


		var b_data = jQuery(outerData).find('#b_data').val();
		var b_f1 = jQuery(outerData).find('#b_f1').val();
		var b_f2 = jQuery(outerData).find('#b_f2').val();
		var b_f3 = jQuery(outerData).find('#b_f3').val();
		var id = jQuery('.dap_user_id').val();
		var userId = jQuery('input[name=subaccounUserId]').val();

		if(b_data == ''){
			swal(bulkNoAddUserMsg);
			return false;
		}

		if(b_f1 == 'select' || b_f1 == ''){
			swal(no_field_select_msg);
			return false;	
		}

		var productId = jQuery(outerData).find('.bulkImportHtmlOuter .productListDropDown').val();
		jQuery.post(admin_ajaxurl, {
			action: "dap_create_bulk_user",
			userId: userId,
			pId: pId,
			productId: productId,
			b_f1: b_f1,
			b_f2: b_f2,
			b_f3: b_f3,
			b_data: b_data,
			
		},function(response) {
			response = JSON.parse(response);
			//dap_member_hide_loader();
			if(response.msg == 'limit_reached'){
				swal(limit_reached);	
			}else if(response.msg == 'created_some'){
				var new_limit_reached_bulk = limit_reached_bulk.replace("%%USERS%%", response.num);
				swal(new_limit_reached_bulk)
				.then((value) => {
				  location.reload();
				});
			}else{
				swal(success_msg);	
				location.reload();
			}	
		});
	});

	/*****bulk account end********/
	
	
});
