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

    $reqId     = $_GET['reqId'];

    $quOrder   = "SELECT * FROM order_req WHERE Id=$reqId";
	$reOrder   = mysqli_query($con,$quOrder);
	$dataOrder = mysqli_fetch_array($reOrder);
    $uid       = $dataOrder['Userid'];
    $ReqType   = $dataOrder['ReqType'];
    $Amount    = $dataOrder['Amount'];
    $BankId    = $dataOrder['BankId'];
	$Status    = $dataOrder['Status'];
    $TxnNo     = $dataOrder['TxnNo'];
    $Mode      = $dataOrder['Mode'];
    $Remarks   = $dataOrder['Remarks'];
	
	if($Status=="PENDING")
	{
	    $respmsg    = "Approved By Admin";
	    $AdminOpBal = getBal($userid);
	    $AdminClBal = $AdminOpBal-$Amount;
	    
	    $clintOpBal = getBal($uid);
	    $clintClBal = $clintOpBal+$Amount;
	    
	    $query1="UPDATE order_req SET Remarks='$Remarks', Status='APPROVED' WHERE Id=$reqId";
	    mysqli_query($con,$query1);
		
		$query2="INSERT INTO wallet (Date,Time,Userid,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith,Remarks)
		    VALUES ('$date','$time','$userid','$AdminOpBal','-$Amount','$AdminClBal','$reqId','PAYMENT_APPROVED','$uid','Approved By Admin')";
		mysqli_query($con,$query2);
		
		$query3="INSERT INTO wallet (Date,Time,Userid,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith,Remarks)
			VALUES ('$date','$time','$uid','$clintOpBal','$Amount','$clintClBal','$reqId','PAYMENT_APPROVED','$userid','Approved By Admin')";
		mysqli_query($con,$query3);
		
		
		if($ReqType=="BANKING") //Pending
		{
		    $BankOpBal = getBankBal($BankId);
			$BankClBal = $BankOpBal+$Amount;	                            
			$InsertTxn="INSERT INTO bank_wallet(BankId,Date,Time,OpBal,Txn,ClBal,TxnId,TxnType,TxnWith,BankRefNo,Remarks)
				VALUES ('$BankId','$date','$time','$BankOpBal','$Amount','$BankClBal','$reqId','PAYMENT_APPROVED','$uid','$TxnNo','Approved By Admin')";
			mysqli_query($con,$InsertTxn);
				                
			$SettleUpdate="UPDATE order_req SET S_Status='SETTELED', S_Date='$date', S_Time='$time' WHERE Id=$reqId";
			mysqli_query($con,$SettleUpdate);
		}
	}
    
    header("Location: topup_reports.php");
?>