<?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'];
	$data['payment_id']     = $_REQUEST['payment_id'];
	$response =  json_decode($obj->call_API2("API_payment_list.php", $data));
	error_reporting(0);
    //var_dump($response->paymentList);
	$data = $response->paymentList;
	//var_dump($data);
?>

	 <div class="card">
		<div class="card-body">
		  <h4 class="card-title  offset-md-3">Add Payment Received</h4>
		  <form id="frmPaymentInfo" method="post">
			 <div class="row">
				<div class="form-group offset-md-3 col-md-2">
				  <label>Member ID:*</label>
				  <input type="text" name="memberID" id="memberID" class="form-control" maxlength=9 value="<?php echo $data->refer_id;?>"/>
				  <small id="txtMemberIDHelp" class="form-text text-muted"></small>
				</div>
				<div class="form-group col-md-3">
				  <label>Member Name:*</label>
				  <input type="text" name="memberName" id="memberName" class="form-control" readonly value="<?php echo $_REQUEST['name'];?>"/>
				  <small id="txtMemberNameHelp" class="form-text text-muted"></small>
				</div>
				<div class="form-group col-md-3">
				  <label>Email ID:*</label>
				  <input type="text" name="emailID" id="emailID" class="form-control"  value="<?php echo $data->email_id;?>"/>
				  <small id="txtEmailIDHelp" class="form-text text-muted"></small>
				</div>
			 </div>
			 <div class="row">
				<div class="form-group offset-md-3 col-md-3">
				  <label>Order No:*</label>
				  <input type="text" name="orderID" id="orderID" class="form-control"  value="<?php echo $data->order_id;?>"/>
				  <small id="orderIDHelp" class="form-text text-muted"></small>
				</div>
				<div class="form-group col-md-3">
				  <label>Total Amount:*</label>
				  <input type="text" name="totalAmount" id="totalAmount" class="form-control" value="<?php echo $data->total_amount;?>" />
				  <small id="txttotalAmountHelp" class="form-text text-muted"></small>
				</div>
			 </div>
			 <div class="row">
				<div class="form-group offset-md-3 col-md-3">
				  <label>Transaction Number:*</label>
				  <input type="text" name="transactionNo" id="transactionNo" class="form-control" value="<?php echo $data->txn_no;?>" />
				  <small id="txtTransactionNoHelp" class="form-text text-muted"></small>
				</div>
				<div class="form-group col-md-3">
				  <label>Cheque Number:*</label>
				  <input type="text" name="chequeNo" id="chequeNo" class="form-control"  value="<?php echo $data->cheque_no;?>"/>
				  <small id="txtChequeNoHelp" class="form-text text-muted"></small>
				</div>
			 </div>
 			 <div class="row">
				   <div class="form-group offset-md-3 col-md-3">
					  <label>Bank Name:</label>
						<select name="cmbBankName" id="cmbBankName" class="form-control" required>
							<option value="Abhyudaya Co-operative Bank">Abhyudaya Co-operative Bank</option>
							<option value="Allahabad Bank">Allahabad Bank</option>
							<option value="Andhra Bank">Andhra Bank</option>
							<option value="Axis Bank">Axis Bank</option>
							<option value="Bandhan Bank">Bandhan Bank</option>
							<option value="Bank Of Baroda">Bank Of Baroda</option>
							<option value="Bank of India">Bank of India</option>
							<option value="Bank of Maharashtra">Bank of Maharashtra</option>
							<option value="Canara Bank">Canara Bank</option>
							<option value="Central Bank of India">Central Bank of India</option>
							<option value="Citibank">Citibank</option>
							<option value="City Union Bank">City Union Bank</option>
							<option value="Corporation Bank">Corporation Bank</option>
							<option value="DBS Bank">DBS Bankk</option>
							<option value="Deutsche Bank">Deutsche Bank</option>
							<option value="DCB Bank">DCB Bank</option>
							<option value="Federal Bank">Federal Bank</option>
							<option value="HSBC">HSBC</option>
							<option value="ICICI">ICICI</option>
							<option value="IDBI">IDBI</option>
							<option value="Indian Bank">Indian Bank</option>
							<option value="Indian Overseas Bank">Indian Overseas Bank</option>
							<option value="IndusInd">IndusInd</option>
							<option value="ING Vysya Bank">ING Vysya</option>
							<option value="Jammu and Kashmir Bank">Jammu and Kashmir Bank</option>
							<option value="Karnataka Bank Ltd">Karnataka Bank Ltd</option>
							<option value="Karur Mahindra">Kotak Mahindra</option>
							<option value="Laxmi Vilas Bank">Laxmi Vilas Bank</option>
							<option value="Karnataka Bank Ltd">Karnataka Bank Ltd</option>
							<option value="Punjab National Bank">Punjab National Bank</option>
							<option value="Punjab & Sind Bank">Punjab & Sind Bank</option>
							<option value="RBL Bank">RBL Bank</option>
							<option value="Saraswat Co-operative Bank">Saraswat Co-operative Bank</option>
							<option value="Shamrao Vitthal Co-operative Bank">Shamrao Vitthal Co-operative Bank</option>
							<option value="South Indian Bank">South Indian Bank</option>
							<option value="Standard Chartered">Standard Chartered</option>
							<option value="State Bank of Bikaner & Jaipur">State Bank of Bikaner & Jaipur</option>
							<option value="State Bank of Hyderabad">State Bank of Hyderabad</option>
							<option value="State Bank of India">State Bank of India</option>
							<option value="State Bank of Mysore">State Bank of Mysore</option>
							<option value="State Bank of Patiala">State Bank of Patiala</option>
							<option value="State Bank of Travancore">State Bank of Travancore</option>
							<option value="State Bank of Mysore">State Bank of Mysore</option>
							<option value="Syndicate Bank">Syndicate Bank</option>
							<option value="Tamilnad Mercantile Bank Ltd">Tamilnad Mercantile Bank Ltd.</option>
							<option value="UCO Bank">UCO Bank</option>
							<option value="Union Bank of India">Union Bank of India</option>
							<option value="United Bank of India">United Bank of India</option>
							<option value="Vijaya Bank">Vijaya Bank</option>
							<option value="Yes Bank Ltd">Yes Bank Ltd</option>
							<option value="Others">Others</option>
						</select>
					</div>
					<div class="form-group col-md-3" id="bankNameRow" hidden>
					  <label>Enter Bank Name:*</label>
					  <input type="text" name="bankName" id="bankName" class="form-control"  />
					  <small id="txtBankNameHelp" class="form-text text-muted"></small>
			        </div>
			 </div>
			 <div class="row">
			   <div class="form-group offset-md-3 col-md-2">
				  <label>Processing Fees:*</label>
				  <input type="text" name="processingFees" id="processingFees" class="form-control"  value="<?php echo $data->processing_fees;?>" />
				  <small id="txtProcessingFeesHelp" class="form-text text-muted"></small>
				</div>
				<div class="form-group col-md-2">
				  <label>Amount Received:*</label>
				  <input type="text" name="amountReceived" id="amountReceived" class="form-control"  value="<?php echo $data->amount_received;?>"/>
				  <small id="txtAmountReceivedHelp" class="form-text text-muted"></small>
				</div>
				<div class="form-group col-md-2">
					 <label>Payment Date:*</label>
					 <input class="form-control" name="paymentDate" id="paymentDate" data-inputmask="'alias': 'date'" value="<?php echo $data->payment_date;?>">
					 <small id="paymentDateHelp" class="form-text text-muted"></small>
				 </div>
			 </div><p>&nbsp;</p>
 			 <div class="row offset-md-3">
			    <input type="text" name="paymentID" id="paymentID" class="form-control col-md-2" style="display:none" value="<?php echo $data->id;?>"/>
				<button type="button" class="btn btn-primary" name="btnPaymentData" id="btnPaymentData">Submit</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);?>;

    var bankName ='<?php echo $data->bank_name;?>';
	$('#cmbBankName').val(bankName);



	 $("#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);

						  }
						
						});
				   }

				
			  }
    });


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

				if($(this).val()=="Others")
				{
					$("#bankNameRow").removeAttr("hidden");
    				$("#bankName").attr("required", "required");
				}

				else
				{
					$("#bankNameRow").attr("hidden", "hidden");
	   				$("#bankName").removeAttr("required");
    			}
			
	});



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

				$("#frmPaymentInfo").validate({
				  rules: {
					memberID: {
					  required: true,
					  digits: true,
					  minlength: 9
					},
					emailID: {
					  required: true,
					  email: true
					},
					orderID: {
					  required: true,
					},
					totalAmount: {
					  required: true,
					  number:true
					},
				    transactionNo: {
					  required: true,
					},
				    chequeNo: {
					  required: true,
					},
				    processingFees: {
					  required: true,
					},
					amountReceived: {
					  required: true,
					},
					paymentDate: {
					  required: true,
					}
				    
				  },
				  messages: {
					memberID:{
					  required: 'Please enter a 9 digit valid member id.',
					  digits:   'Please enter a 9 digit valid member id.',
					  minlength: 'Please enter a 9 digit valid member id.'
					},
			        emailID: "Please enter a valid email address",
					orderID: {
					  required: "Please enter the order number!",
					},
					totalAmount: {
					  required: "Please enter the payment amount!",
  					  number: "Please enter a valid amount!",
					},
					transactionNo: {
					  required: "Please enter the transaction number!",
					},
					chequeNo: {
					  required: "Please enter the Cheque/DD number!",
					},
					processingFees: {
					  required: "Please enter the processing fees!",
					},
					amountReceived: {
					  required: "Please enter the amount received!",
					},
				    paymentDate: {
					  required: "Please enter the date of payment!",
					}
				  },
				  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($("#frmPaymentInfo").valid())
			   {
					$("#frmPaymentInfo").submit();
			   }

			   else
				   return false;

			});


	  $("#frmPaymentInfo").on('submit',(function(e){

			
		    if($("#memberName").val()=="Invalid Member ID!")
				 return false;

        	$("#bankName").val($("#cmbBankName").val());

			 e.preventDefault();
			 $("#loader").show();
			 $("#btnPaymentData").hide();

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

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

				  if (data.success){
					alert(data.message);
					location.href='payment-list.php';
				  }

				  else{
					alert(data.message);
				  } 

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


		}));



</script>

