<?php
	session_start();
	if($_SESSION['adm'])
	{
		$userid= $_SESSION['adm'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
	include'time.php';
	include'../Retailers/functions.php';
	
	ini_set('memory_limit', '-1');

	$id=$_GET['id'];
	
	if(empty($id))
	{
	    echo "<h3 align='center' style='color:red;'>Request Id Not Found !</h3>";
	}
	
	$query="SELECT * FROM paymentreq WHERE Id='$id'";
	$result=mysqli_query($con,$query);
	$data=mysqli_fetch_array($result);
	$amount=$data['Amount'];
	$Txnnumber=$data['Txnnumber'];
	$uid=$data['Userid'];
	$BankName=$data['Bankname'];
	$reqType=$data['reqType'];
	
	
			
	$status="APPROVED";
	
	if(isset($_POST['submit']))
	{
		$respmsg="Approved By Admin";
		$bankAmount=$_POST['amont'];
		$bankTxnNo=$_POST['txn'];
		$bankcharge=$_POST['bc'];
		$MyBankName=$_POST['BankName'];
		
		if(empty($bankcharge))
		{
			$bankcharge=0;
		}
		
		$actualamount=$amount-$bankcharge;
		
		$Admin_Op_Bal=getBal($userid);
		$flag=0;
		if($actualamount<=$Admin_Op_Bal)
		{
			$quGetDupReq="SELECT * FROM paymentreq WHERE Id=$id";
			$reGetDupReq=mysqli_query($con,$quGetDupReq);
			$dataGetDupReq=mysqli_fetch_array($reGetDupReq);
			$DupReqStatus=$dataGetDupReq['SettleStatus'];
			
			if($DupReqStatus!="SETTELED")
			{
			    if($amount>$bankAmount || $amount<$bankAmount)
			    {
			        echo "<center><h3 style='color:red;'>Request Amount Not Matched !</h3></center>";
			    }
			    else
			    {
			        $flag=1;
			    }
			    
			    if($flag==1)
			    {
				    $User_Op_Bal=getBal($uid);
	                
				    mysqli_query($con,"START TRANSACTION");
				        				
					if($reqType=="ADVANCE")
				    {
				        $BankId="SELECT * FROM bank WHERE Bankname='$MyBankName'";
				        $reBankId=mysqli_query($con,$BankId);
				        $dataBankId=mysqli_fetch_array($reBankId);
				        $BankId=$dataBankId['Id'];
				                            
				        $InsertTxn="INSERT INTO Bank_Account_Txn(Date,Time,Bank_Id,Bank_Name,Txn,Txn_Id,Disc,Txn_For)
				        VALUES ('$date','$time','$BankId','$MyBankName','$amount','$id','Payment_Setteled','$uid')";
				        if(mysqli_query($con,$InsertTxn))
				        {
				            $SettleUpdate="UPDATE paymentreq SET SettleStatus='SETTELED', Txnnumber='$bankTxnNo', Bankname='$BankName2' WHERE Id=$id";
				            if(mysqli_query($con,$SettleUpdate))
				            {
                                if(mysqli_query($con,"COMMIT"))
                                {
                                    echo "<h3 align='center' style='color:green; font-weight:bold;'>Payment Setteled Successfully !</h3>";  
                                }
                                else
                                {
                                    if(mysqli_query($con,"ROLLBACK"))
									{
				                        echo "<h3 align='center' style='color:red; font-weight:bold;'>Rollback For Not Commit !</h3>";
									}
                                }
				            }
				            else
				            {
				                if(mysqli_query($con,"ROLLBACK"))
								{
				                    echo "<h3 align='center' style='color:red; font-weight:bold;'>Rollback For Settlement Query !</h3>";
								}
								else
								{
									echo "error";
									mysqli_query($con,"ROLLBACK");
								}
							}
						}
					}
					elseif($reqType=="EXCHANGE")
					{
					    
					}
			    }
			}
			else
			{
				echo "<center><h3 align='center' style='color:red;'>This Request Proccesed Already !</h3></center>";
			}
		}
		else
		{
			echo "<center><h3 align='center' style='color:red;'>Insufficiant Balance !</h3></center>";
		}
		
	}
?>
<html>
<head>
<style>
    #inp{margin:15px; width:300px; height:50px; font-size:20px; padding:5px; border-radius:5px;}
</style>
</head>
<body>
	<form method="post">
	<table border="1" cellpadding="5" align="center" style="border-collapse:collapse;">
		<tr>
			<td><input type="text" placeholder="Enter Bank Charge..." name="bc" id="inp"></td>
		</tr>
		<?php
		if($reqType=="ADVANCE")
		{
		    echo "<tr>
			        <td>
			            <select name='BankName' id='inp'>
			                <option>--Select Bank--</option>";
			                
			                $GetBank="SELECT * FROM bank WHERE Status='Active'";
			                $reGetbank=mysqli_query($con,$GetBank);
			                while($dataGetbank=mysqli_fetch_array($reGetbank))
			                {
			                    echo "<option>".$dataGetbank['Bankname']."</option>"; 
			                }
			       
			        echo "</select>
			    </td>
		    </tr>
		    <tr>
			    <td><input type='text' placeholder='Enter Txn No...' name='txn' id='inp'></td>
		    </tr>";
		}
		?>
		<tr>
			<td><input type="number" placeholder="Enter Amount..." name="amont" id="inp"></td>
		</tr>
		<tr>
			<td><input type="submit" name="submit" value="Balance Success" id="inp"></td>
		</tr>
	</table>
	</form>
</body>
</html>