<?php
    include'session.php';
	
    if($userid==0 || $userid==" ")
    {
        header('location:../login.php');
        exit();
    }
    $mnum = $_POST['mobileNo1'];
    $cMnum = $_POST['mobileNo2'];
    $amount = $_POST['amount'];
    
	$opcodequery="SELECT * FROM operator WHERE OwnApiCode='$opOwnCode'";
	$opcoderesult=mysqli_query($con,$opcodequery);
	$opcodedata=mysqli_fetch_array($opcoderesult);
	
	$OwnApiCode=$opcodedata['OwnApiCode'];
	$opCodeMyApi2=$opcodedata['My_API2'];
	$opcode=$opcodedata['Value'];
	//$myApiOpCode=$opcodedata['MyApiValue'];
	$MyApi2OpCode=$opcodedata['My_API2'];
	$KwikApiOpCode=$opcodedata['KwikAPI'];
	$UniqueApiOpCode=$opcodedata['UniqueAPI'];
	$DiziPayOpCode=$opcodedata['DiziPayApi'];
	$OzzyOpCode=$opcodedata['Ozzy_Api'];
	$PP_ApiOpCode=$opcodedata['PP_Api'];
	$Dizi_KwikOpCode=$opcodedata['Dizi_Kwik'];
	$columName=$opcodedata['Commision_Column'];
	$myApiOpCode=$MyApi2OpCode;
	$opName=$opcodedata['Operator'];
	
	$UseApi1=$opcodedata['UseAPI'];
	$UseApi2=$opcodedata['UseAPI2'];
			
	$opserviceId=$opcodedata['Serviceid'];
	
	if($OwnApiCode==4)
	{
	    $stv=1;
	}
	else
	{
	    $stv=0;
	}
	
	$mnumLength=strlen($mnum);
	
	/*
	$Parentid=$dataUser['Parentid'];
	$Grandparentid=$dataUser['Grandparentid'];
	
	$UserComm=getComm($userid,$opName);
	$ParentComm=getComm($Parentid,$opName);
	$GrandparentComm=getComm($Grandparentid,$opName);
	*/
	
	if(empty($opName) || $opName=="--Select Operator--")
	{
	    $error_msg="Please Select Any Operator first !";
	    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>";
	}
	elseif(empty($mnum) || empty($cMnum) || empty($amount))
	{
	    $error_msg="Fill All Input Feilds Please !";
	    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>";
	}
	elseif($opserviceId==1 && $mnumLength!=10 || $opserviceId==3 && $mnumLength!=10)
	{
	    $error_msg="Mobile Number Must Be In 10 Digit Only !";
	    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>";
	}
	elseif($amount>$usrBal)
	{
	    $error_msg="Insufficiant Wallet Balance !";
	    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>";
	}
	elseif($mnum!=$cMnum)
	{
	    $error_msg="Please Insert Mobile/DTH No. Correctly !";
	    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>";
	}
	elseif($amount<10)
	{
	    $error_msg="Amount Is Less Than 10 !";
	    echo "<div class='msg-div'>
			        <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>";
	}
	elseif(0<getDupRech($mnum,$amount,$opName,$date))
	{
	    $error_msg="Same Recharge Not Allowed At Same Day !";
	    echo "<div class='msg-div'>
			       <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>";
	}
	else
	{
	    /*
	    $UserCommAmt=$amount*$UserComm/100;
	    $DebitAmount=$amount-$UserCommAmt;
	
	    $actualParentComm=$ParentComm-$UserComm;
	    $ParentCommAmt=$amount*$actualParentComm/100;

	    $actualGrandParentComm=$GrandparentComm-$ParentComm;
	    $GrandParentCommAmt=$amount*$actualGrandParentComm/100;
	    */
	    $DebitAmount=$amount;
	    $usrNewBal=$usrBal-$DebitAmount;
	    
	                                
	    $insertRech="INSERT INTO recharge(Userid,Rechargedate,Time,PTime,Agentname,Balance,Companyname,Mobile,Amount,Debitamount)
		VALUES ('$userid','$date','$time','$PTime','$usrName','$usrNewBal','$opName','$mnum','$amount','$DebitAmount')";
		if(mysqli_query($con,$insertRech))
		{
		    $newrechid=mysqli_insert_id($con);
		    
		    $insertWallet="INSERT INTO MLM_Wallet (Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Remarks,Date,Time,APBU)
			VALUES ('$userid','$usrBal','-$DebitAmount','$usrNewBal','RECHARGE','SELF','$newrechid','DEBIT FOR RECHARGE','$date','$time','$userid')";
			if(mysqli_query($con,$insertWallet))
			{
			    $ewalletid=mysqli_insert_id($con);
			    
		        $updateRech="UPDATE recharge SET Ewalletid='$ewalletid' WHERE Id='$newrechid'";
				mysqli_query($con,$updateRech);
											    
				$UpdateBal="UPDATE MLM_Users SET Balance='$usrNewBal' WHERE Userid='$userid'";
				mysqli_query($con,$UpdateBal);
				
				/*
				$quParComm="INSERT INTO commwallet(Userid,Balance,Date,Time,Rechargeid)
				VALUES ('$Parentid','$ParentCommAmt','$date','$time','$newrechid')";
				mysqli_query($con,$quParComm);
												
				$quGparComm="INSERT INTO commwallet(Userid,Balance,Date,Time,Rechargeid)
				values('$Grandparentid','$GrandParentCommAmt','$date','$time','$newrechid')";
				mysqli_query($con,$quGparComm);
				*/
				
				$quGetUsrOp="SELECT * FROM CommSlab WHERE Userid='$userid' AND OpName='$opName'";
			    $reGetUsrOp=mysqli_query($con,$quGetUsrOp);
			    $dataGetUsrOp=mysqli_fetch_array($reGetUsrOp);
			    $UsrPersApi1=$dataGetUsrOp['API1'];
			    $UsrPersApi2=$dataGetUsrOp['API2'];
			    
				
				if($UsrPersApi1!="OFF")
				{
					if($UsrPersApi1=="By_Default" || $UsrPersApi1=="ON")
					{
						if($UseApi1!="OFF")
						{
							$UseApiName=$UseApi1;
						}
						else
						{
							$UseApiName=$UseApi2;
						}
					}
					else
					{
						$UseApiName=$UsrPersApi1;
					}
				}
				else
				{
					if($UsrPersApi2=="By_Default" || $UsrPersApi2=="ON")
					{
						if($UseApi1!="OFF")
						{
							$UseApiName=$UseApi1;
						}
						else
						{
							$UseApiName=$UseApi2;
						}
					}
					else
					{
						$UseApiName=$UsrPersApi2;
					}
				}
				
				$apiFile="RechFile_".$UseApiName.".php";						
				
				include $apiFile;			    
		                            
			}
			else
			{
			   $error_msg="Error No. 2 !";
	            echo "<div class='msg-div'>
			       <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>"; 
			}
		}
		else
		{
		    $error_msg="Error No. 1 !";
	        echo "<div class='msg-div'>
			       <form method='post'>
				        <h3>".$error_msg."</h3><br>
				        <i class='fa fa-ban' aria-hidden='true' id='NotokIcon'></i><br><br>
				        <button type='submit' class='btnPopup'>OK</button><br><br>
			        </form>
		        </div>";
		}
	}

?>