<?php
    include'connection.php';
	include'../Retailers/functions.php';

$reqId=$_GET['reqId'];
$amount=$_GET['amount'];
$Txnnumber=$_GET['Txnnumber'];
$BankId=$_GET['BankId'];
$reqType=$_GET['reqType'];
$uid=$_GET['UsrId'];
$adminId=1111;

    $quGetDupReq="SELECT * FROM paymentreq WHERE Id=$reqId";
	$reGetDupReq=mysqli_query($con,$quGetDupReq);
	$dataGetDupReq=mysqli_fetch_array($reGetDupReq);
	$DupReqStatus=$dataGetDupReq['Active'];
	
	if($DupReqStatus==0)
	{
	    $respmsg="Approved By Admin";
	    $status="APPROVED";
	    $Admin_Op_Bal=getBal($adminId);
	    $Admin_Cl_Bal=$Admin_Op_Bal-$amount;
	    
	    $clintOpBal=getBal($uid);
	    $clintClBal=$clintOpBal+$amount;
	    
	    $query1="UPDATE paymentreq SET Responsemsg='$respmsg', RespTime='$time', Bankcharge='0', Status='$status', Active='1', APBU='$adminId' WHERE Id=$reqId";
	    mysqli_query($con,$query1);
		
		$query2="INSERT INTO wallet (Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Remarks,Date,Time,APBU)
		    VALUES ('$adminId','$Admin_Op_Bal','-$amount','$Admin_Cl_Bal','PAYMENT_APPROVED','$uid','$reqId','PAYMENT_APPROVED','$date','$time','$adminId')";
		mysqli_query($con,$query2);
		
		$query3="INSERT INTO wallet (Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Remarks,Date,Time,APBU)
			VALUES ('$uid','$clintOpBal','$amount','$clintClBal','PAYMENT_APPROVED','$adminId','$reqId','Approved By Admin','$date','$time','$adminId')";
		mysqli_query($con,$query3);
		
		$query4="UPDATE admin SET Balance='$Admin_Cl_Bal' WHERE Userid='$adminId'";
		mysqli_query($con,$query4);
		
		$sub=substr($uid,"0","2");
		if($sub==50)
		{
			$query5="UPDATE retailer SET Balance='$clintClBal' WHERE Userid='$uid'";
			mysqli_query($con,$query5);
		}
		elseif($sub==20)
		{	
			$query6="UPDATE distributer SET Balance='$clintClBal' WHERE Userid='$uid'";
			mysqli_query($con,$query6);
		}
							    
		
		if($reqType=="BANKING")
		{
		    $quBank="SELECT Bankname FROM bank WHERE Id='$BankId'";
			$reBank=mysqli_query($con,$quBank);
			$dataBank=mysqli_fetch_array($reBank);
			$Bankname=$dataBank['Bankname'];
				                            
			$InsertTxn="INSERT INTO Bank_Account_Txn(Date,Time,Bank_Id,Bank_Name,Txn,Txn_Id,BankRefNo,Disc,Txn_For)
				VALUES ('$date','$time','$BankId','$Bankname','$amount','$reqId','$Txnnumber','Payment_Approved','$uid')";
			mysqli_query($con,$InsertTxn);
				                
			$SettleUpdate="UPDATE paymentreq SET SettleStatus='SETTELED', SettleDate='$date' WHERE Id=$reqId";
			mysqli_query($con,$SettleUpdate);
		}
	}
    
    header("Location: topup_reports.php");
?>