<?php
	session_start();
	if($_SESSION['subadm'])
	{
		$userid=1111;
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
	
	date_default_timezone_set("asia/Kolkata");
	$date= Date("Y-m-d");
    $time= Date("h:i:s A");
    
	if(isset($_POST['submit']))
	{
	    $msg="FAILED";
	    $rehit=$_POST['rehit'];
	    
		$queryPR="select * from recharge where Status='PENDING' and ReHit='$rehit' and Rechargedate='$date'";
		$resultPR=mysqli_query($con,$queryPR);
		while($data=mysqli_fetch_array($resultPR))
		{
	        $id=$data['Id'];
			$userid=$data['Userid'];
			$debitamount=$data['Debitamount'];
			$ewalletid=$data['Ewalletid'];
			$oper=$data['Companyname'];
			$mobile=$data['Mobile'];
			$amount=$data['Amount'];
			$Partnerid=$data['Partnerid'];
			
			$query2="select * from retailer where Userid='$userid'";
			$result2=mysqli_query($con,$query2);
			$databal=mysqli_fetch_array($result2);
			$CallBackUrl=$databal['CallBackUrl'];
			$ApiActive=$databal['ApiActive'];
			
			$query1="select * from wallet where Userid='$userid' order by Id desc";
			$result1=mysqli_query($con,$query1);
			$data1=mysqli_fetch_array($result1);
			$oldbal=$data1['Balance'];
			$newbal=$oldbal+$debitamount;
		
			$query2="insert into wallet(Userid,Openingbalance,Credit,Balance,Date,Time,Transactiontype,Paynrechid,Operator,Mobile,Amount,Status)
			values('$userid','$oldbal','$debitamount','$newbal','$date','$time','Recharge_Refund','$id','$oper','$mobile','$amount','FAILED')";
			if(mysqli_query($con,$query2))
			{
				$query12="update retailer set Balance='$newbal' where Userid='$userid'";
				mysqli_query($con,$query12);
			
				$query3="select * from wallet where Userid='$userid' order by Id desc";
				$result3=mysqli_query($con,$query3);
				$data3=mysqli_fetch_array($result3);
				$cewalletid=$data3['Id'];
			
				$query4="update recharge set Creditewalletid='$cewalletid', Operatorid='$msg', AdminOpId='F', ReHit='0', Balance='$newbal', Creditamount='$debitamount', Status='FAILED' where Id='$id' ";
				mysqli_query($con, $query4);
			
				$query5="delete from commwallet where Rechargeid='$id'";
				mysqli_query($con,$query5);
				
				if($ApiActive==1)
				{
					$ch = curl_init();
				
					curl_setopt($ch, CURLOPT_URL, "$CallBackUrl?partnertid=$Partnerid&status='FAILED'&orderid=$id&opid=$msg&errmsg=$msg");
					curl_setopt($ch, CURLOPT_POST, false );
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
					curl_setopt($ch, CURLOPT_TIMEOUT, 0);
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
			
					curl_close($ch);
				}
				
			}
		
		}
		
		echo "<center><h3>All Transaction Failed Successfully !</h3></center>";
	}

?>
<html>
    <head>
       <style>
        #msg{width:200px; height:40px; font-size:22px; margin:10px;}
       </style>
    </head>
    <body>
        <form method="post">
        <select name="rehit" id="msg">
            <option value="1">FAILDED</option>
            <option value="0">PENDINGED</option>
        </select>
        <input type="submit" name="submit" id="msg">
        </form>
    </body>
</html>