<?php
	session_start();
	if($_SESSION['adm'])
	{
		$userid= $_SESSION['adm'];
	}
	else
	{
		header('location:../login.php');
	}
?>
<?php
	include'connection.php';
	include'../Retailers/functions.php';
	$id=$_GET['id'];
	date_default_timezone_set("asia/Kolkata");
	$date= Date("Y-m-d");
	$time= Date("h:i:s A");
	if(isset($_POST['submit']))
	{
	    $msg=$_POST['msg'];
	    $solved=$_POST['solved'];
	    
    	$query="SELECT * FROM recharge WHERE Id=$id";
	    $result=mysqli_query($con,$query);
	    $data=mysqli_fetch_array($result);
	
	    $uid=$data['Userid'];
	    $debitamount=$data['Debitamount'];
	    $ewalletid=$data['Ewalletid'];
	    $oper=$data['Companyname'];
	    $mobile=$data['Mobile'];
	    $amount=$data['Amount'];
	    $Partnerid=$data['Partnerid'];
	    $Status=$data['Status'];
	    
	    if($Status=="FAILED")
	    {
	        echo "<h1 align='center'><font color='red'> &#x2714; Transaction Failed Already !</font></h1><br>";
	        exit();
	    }
	    $query2="SELECT * FROM retailer WHERE Userid='$uid'";
		$result2=mysqli_query($con,$query2);
		$databal=mysqli_fetch_array($result2);
		$CallBackUrl=$databal['CallBackUrl'];
		$ApiActive=$databal['ApiActive'];
	    
	    $UsrOpBal=getBal($uid);
	    $UsrClBal=$UsrOpBal+$debitamount;
	
	    $query2="INSERT INTO wallet(Userid,OpBal,Txn,ClBal,TxnType,TxnWith,TxnId,Date,Time,APBU)
	    values('$uid',$UsrOpBal,'$debitamount','$UsrClBal','Recharge_Refund','SELF','$id','$date','$time','$userid')";
	    if(mysqli_query($con,$query2))
	    {   
		    $ewalletid=mysqli_insert_id($con);
		    
	    	$query12="UPDATE retailer SET Balance='$UsrClBal' WHERE Userid='$uid'";
		    mysqli_query($con,$query12);
		
		    $query4="UPDATE recharge SET Creditewalletid='$ewalletid', Operatorid='$msg', AdminOpId='F', ReHit='0', Balance='$UsrClBal', Creditamount='$debitamount', Status='FAILED' WHERE Id='$id' ";
		    mysqli_query($con, $query4);
		
		    $query5="DELETE FROM commwallet WHERE Rechargeid='$id'";
		    mysqli_query($con,$query5);
		    
		    if(!empty($solved))
		    {
		        $UpdateComp="UPDATE complain SET Solvedate='$date', SolveTime='$time', Responsemsg='Refunded', Status='Solved', Active='1' WHERE Rechargeid='$id'";
		        mysqli_query($con,$UpdateComp);
		        echo "<h1 align='center'><font color='blue'> &#x2714; Complain Closed Successfully !</font></h1><br>";
		    }
		    
		    if($ApiActive==1)
	        {
	            $status="FAILED";
	            
        		$url = "$CallBackUrl?partnertid=$Partnerid&status=$status&orderid=$id&opid=$msg&errmsg=$msg";
                $curl = curl_init();
            
                curl_setopt_array($curl, array(
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_CONNECTTIMEOUT => 60,
                CURLOPT_TIMEOUT => 60,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache"
                    ),
                ));

                $response = curl_exec($curl);
                $hhtpCode =curl_getinfo($curl, CURLINFO_HTTP_CODE);
                $err = curl_error($curl);

                curl_close($curl);
                
                if ($err) {
                        //$do=mysql_query("INSERT INTO kwk_cbhis (oid,uid,status_code,msg,url)VALUES(paste your values)");
                } else {
                    //$do=mysql_query("INSERT INTO kwk_cbhis (oid,uid,status_code,msg,url)VALUES(paste your values)");
                }
                    $insertCBH="INSERT INTO cbhis (oid,uid,status_code,msg,url)VALUES('$id','$uid','$hhtpCode','$response','$url')";
                    mysqli_query($con,$insertCBH);
	       }
		    
		    echo "<h1 align='center'><font color='#008000'> &#x2714; Your Recharge failed Successfully !</font></h1><br>";
	    }
		
		
		
	}

?>
<html>
    <head>
       <style>
        #msg{width:200px; height:40px; font-size:22px;}
       </style>
    </head>
    <body>
        <form method="post">
        <table>
            <tr>
                <th><h2>Print Message</h2></th>
                <th><h2>Complain Close</h2></th>
                <th><h2>Action</h2></th>
            </tr>
            <tr>
                <td>
                    <select name="msg" id="msg">
                        <option>Failed !</option>
                        <option>Invalid Number Or Operator !</option>
                        <option>Invalid Number Or Amount !</option>
                        <option>Invalid Amount !</option>
                        <option>Operator Down !</option>
                        <option>Refund !</option>
                        <option>You can only select other than prime plan for recharge.	</option>
                    </select>
                </td>
                <td><input type="checkbox" value="solved" name="solved" id="msg"></td>
                <td><input type="submit" name="submit" id="msg"></td>
            </tr>
        </table>
        </form>
    </body>
</html>