<?php

	include 'header.php';
	include 'navbar.php';
	include 'includes/admin_API_Config.php';

	/*$obj = new API_Config;
	$data = array();
	$data['admin_username'] = $_SESSION['admin_username'];
	$response = json_decode($obj->call_API("API_Product_List.php", $data));
	error_reporting(0);
	
	$firstName 				= $firstNameArray[$nameIndex];
		$lastName 				= $lastNameArray[$nameIndex];
		$mobile_number 			= '9999999999';
		$email_address			= $firstName."@gmail.com";

		$data_string='';

		$data_string='first_name='.$firstName;
		$data_string.='&last_name=Suri'.$lastName;
		$data_string.='&mobile_number='.$mobile_number;

		$data_string.='&email_address='.$email_address;
		$data_string.='&password=8839947c977cfd1d642e65d4c3cde5bd';
		$data_string.='&sponsor_id='.$_REQUEST['refer_id'];
		$data_string.='&sponsor_by='.$_REQUEST['refer_id'];
		$data_string.='&verified=y';
		$data_string.='&left_node=1';
		$data_string.='&right_node=0';
		$data_string.='&source=self';
		$data_string.='&dob=';
		$data_string.='&pan_card=';
		$data_string.='&nominee_name=';
		$data_string.='&relation_nominee=self';
		$data_string.='&ip_address=123.123.123.123';
		$data_string.='&country_code=+91';
		$data_string.='&account_type=2';*/

?>






	 <div class="card">
		<div class="card-body">
		  <h4 class="card-title">Add Member</h4>
		  <form id="frmAddMember" method="post">
			 <div class="row">
				<div class="col-md-2">
					<label>Member First Name:</label>
					<input class="form-control" name="firstName" id="firstName">
				</div>
				<div class="col-md-2">
					<label>Member Last Name:</label>
					<input class="form-control" name="lastName" id="lastName">
				</div>
				<div class="col-md-2">
					<label>Mobile Number:</label>
					<input class="form-control" name="mobileNumber" id="mobileNumber" maxlength="10">
				</div>
				<div class="col-md-2">
					<label>Email Address:</label>
					<input class="form-control" name="emailID" id="emailID">
				</div>
			 </div>
			 <p>&nbsp;</p>
			 <div class="row">
				<div class="col-md-2">
					<label>Member ID:</label>
					<input class="form-control" name="memberID" id="memberID" maxlength="9">
				</div>
				<div class="col-md-2">
					<label>Member Name:</label>
					<input class="form-control" name="memberName" id="memberName" readonly>
				</div>
			    <div class="col-md-2">
					<label>Leg placement:</label>
					<select name="cmbLeg" id="cmbLeg" class="form-control valid" required="" aria-invalid="false">
						<option value='1'>Left</option>
						<option value='2'>Right</option>
                    </select>
				</div>
				<div class="col-md-2">
					<label>Add PV to:</label>
					<select name="cmbPosition" id="cmbPosition" class="form-control valid" required="" aria-invalid="false">
						<option value="0">Not Specific</option>";
						<option value="1">Left</option>";
						<option value="2">Right</option>";
					</select>
				</div>
			 </div>
 			 <p>&nbsp;</p>
 			 <div class="row">
				<button type="button" class="btn btn-primary" name="btnPurchase" id="btnPurchase">Purchase</button>
			 </div>
			 <div class="dot-opacity-loader" id="loader">
				  <span></span>
				  <span></span>
				  <span></span>
              </div>
		  </form>
		</div>
	  </div>

<?php
	include 'footer.php';
?>



   <script type="text/javascript">

    var productDetails =  <?php echo json_encode($response->productList);?>;


	 $("#memberID").on("keypress keyup blur paste change",function (event) {
	   
           $(this).val($(this).val().replace(/[^\d].+/, ""));
            if ((event.which < 48 || event.which > 57)) {
                event.preventDefault();
            }
			else
			{
				if($("#memberID").val().length==9)
				{

					  $.post("../API/API_check_refer_idV2.php",
					  {
						refer_id: $("#memberID").val(),
						//ajax : 1,

					  },

					  function(data, status){
						if(status)
						{
						  if(data.status==1)
						  {
							 $("#memberName").val(data.user_info.name + "  " + data.user_info.last_name);
						  }
						  else
						  {
							 $("#memberName").val("Invalid Member ID!");

						  }

						}

						else
						  {
							alert("Cannot get a response from the server. Please try later!");
							$('#finish').attr('disabled', true);

						  }
						
						});
				   }

				
			  }
    });



	$("#cmbProduct").change(function(){

		$("#productPrice").val($(':selected', this).attr("price"));
		$("#productPV").val($(':selected', this).attr("pv"));


	});

	  
	 $("#btnPurchase").click(function(){

			 // validate signup form on keyup and submit
		 $("#frmAddMember").validate({
			  rules: {
				memberID:{
				  required: true,
				  minlength: 9,
				},
				paidByDeposit:{
				  required: true,
				},
				paidByCash:{
				  required: true,
				},
				paidByBBT:{
				  required: true,
				}
			  },
			  messages: {
				memberID:{
				required: 'Please enter the Member ID.',
				minlength: "Please enter 9 digit valid Member ID"
		    	},
				paidByDeposit:{
				  required: "Please enter the amount.",
				},
				paidByCash:{
				  required: "Please enter the amount.",
				},
				paidByBBT:{
				  required: "Please enter the amount.",
				}
			  },
			  errorPlacement: function(label, element) {
				label.addClass('mt-2 text-danger');
				label.insertAfter(element);
			  },
			  highlight: function(element, errorClass) {
				$(element).parent().addClass('has-danger')
				$(element).addClass('form-control-danger')
			  }

			
		  });

		  if($("#frmMemberPackage").valid())
			 $("#frmMemberPackage").submit();

	  });


	  $("#frmAddMember").on('submit',(function(e){
			
		    if($("#memberName").val()=="Invalid Member ID!")
				 return false;

			/*var formData = {
            'memberID'		:    $("#memberID").val(), 
			'product_id'	:    $("#cmbProduct").val(),
			'position'      :    $("#cmbPosition").val(),
			'paidByDeposit' :    $("#paidByDeposit").val(),
			'paidByCash'    :    $("#paidByCash").val(), 
		    'paidByBBT'     :    $("#paidByBBT").val() 
			};*/

			//var payment = parseFloat(formData.paidByDeposit) + parseFloat(formData.paidByCash) + parseFloat(formData.paidByBBT);

			var payment = parseFloat($("#paidByDeposit").val()) + parseFloat($("#paidByCash").val()) + parseFloat($("#paidByBBT").val());

			if(payment < parseFloat($("#productPrice").val()))
		    {
				alert("Amount paid cannot be less than purchase price!");
			 	return false;
			}
			 e.preventDefault();
			 $("#loader").show();
			 $("#btnPurchase").hide();

			 $.ajax({
				type        : 'POST', 
				url         : '', 
				data        : new FormData(this),
				dataType    : 'json', 
				encode      :  true,
				cache		: false,
				contentType : false,
				processData : false,
				})

				.done(function(data){
				  $("#loader").hide();
				  $("#btnPurchase").show();

				  if (data.success){
					alert(data.message);
					location.reload();
				  }

				  else{
					alert(data.message);
				  } 

				}) 
				
				.fail(function(data){
					$("#loader").hide();
					$("#btnPurchase").show();
					alert("Some error has occured. Please try again later!"); 
				});


		}));



</script>

