<?php
include "header.php";
$charge=2;

// URL of the PHP page

if(isset($_POST['addw'])){
  
   $amount=$_POST['amount'];
    if($amount>=50)
  $amount=  round($amount); 
 echo $amountc=$amount+(($amount*$charge)/100);
 echo $amounts= round($amountc);
$url = 'https://upi.clusterwebsolution.com/api/create-order';

// Data to be sent in the POST request
$data = array(
    'customer_mobile' => '1234567890',
    'user_token' => '64f06fc9c26552c69d7ed4eba3459e1e',
    'amount' => $amounts,
    'order_id' => '123456'.time(),
    'redirect_url' => 'https://upi.clusterwebsolution.com/success',
    'remark1' => $userdata['user_token'],
    'remark2' => $amount,
);

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute cURL session and store the response
echo $response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    echo 'cURL Error: ' . curl_error($ch);
}

// Close cURL session
curl_close($ch);


// Decode the JSON response
echo $jsonResponse = json_decode($response, true);

// Check if decoding was successful
if ($jsonResponse !== null) {
    error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
ini_set('display_errors', TRUE);
        $paymentUrl = $jsonResponse['result']['payment_url'];
        ob_start();
    echo '<script  type="text/javascript"> window.top.location.href="'.$paymentUrl.'"; </script>';
    
    ob_end_flush();
    exit;
    
    
    
} else {
    echo 'Failed to decode JSON response.';
}

}
?>
<div class="main-panel">
				<div class="content">
					<div class="container-fluid">
						<h4 class="page-title">Payout and Transactions</h4>  	<h4 class="page-title">Wallet Ballance: ₹ <span> <?php echo $userdata['wbal']; ?></span></p></h4> 
						<div class="row row-card-no-pd">
							<div class="col-md-12">
							<form method="post" class="row mb-4">
							
								<div class="col-md-4 mb-2">
									<label>Minimum ₹ 50 Add Money</label>
									<input type="number" id="search_input" onchange="changeHandler(this)" name="amount" m placeholder="Enter Amount" class="form-control">	<label>Payout Charge</label>
									<? echo $charge;?>%
								</div>
                                <script>
  function changeHandler(val)
  {
    if (Number(val.value) > 50)
    {
      val.value = val.value
    }else{
        val.value = 50;
    }
  }
</script>
								<div class="col-md-2 mb-2">
									<label>&nbsp;</label>
									<button type="submit" id="search" name="addw" class="btn btn-primary btn-block">Add Money</button>
								</div>
							</form>	
							<div class="table-responsive">
								<table class="table table-sm table-hover table-bordered table-head-bg-primary" id="dataTable" width="100%">
										<thead>
											<tr>
												<th>#</th>
												<th> Ac Name</th>
												<th>TransactionId</th>
												<th>Date Time</th>
													<th>type</th>
												<th>Ac OR UPI </th>
												<th>IFC</th>
											
												<th>Order ID</th>
												<th>Amount</th>
												<th>Status</th>
												
											</tr>
										</thead>
										<tbody>
<?php
if($userdata['role'] == 'User'){
 $token = $userdata['user_token'];

$query = "SELECT * FROM reports where user_token='$token'";
$query_run = mysqli_query($conn, $query);


if ($query_run) {
    while ($row = mysqli_fetch_assoc($query_run)) {
        
      
        
        echo "<tr>";
        echo "<td>" . htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['Accountname'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['transactionId'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['date'], ENT_QUOTES, 'UTF-8') . "</td>";

        ?>
        <td class="badge badge-dark" style="width:70px;"><?php echo htmlspecialchars($row['type']); ?></td>
        <?php
        echo "<td>" . htmlspecialchars($row['Accountnop'], ENT_QUOTES, 'UTF-8') . "</td>";
        ?>
        <?php
        echo "<td>" . htmlspecialchars($row['ifsc'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>" . htmlspecialchars($row['order_id'], ENT_QUOTES, 'UTF-8') . "</td>";
echo "<td>₹" . htmlspecialchars($row['amount'], ENT_QUOTES, 'UTF-8') . "</td>";


        //$sts = $row['status'];
        if($row['status']=="Success"){
            $sts = 'Success';
            $cls = "badge badge-success";
        }else{
            $sts = 'Sending';
            $cls = "badge badge-warning";
        }
        echo "<td><button class='$cls'>" . $sts . "</button></td>";
        echo "</tr>";
    }
} else {
    echo "Error in query: ";
    //echo "Error in query: " . mysqli_error($conn); 
}
}

//admin ke lie echo 

else{
    $token = $userdata['user_token'];
if(isset($_GET['id'])){
    $ids=$_GET['id'];
     $ssss=$_GET['ssss'];
     
    $query2="UPDATE reports SET status = '$ssss' WHERE reports.id = '$ids'";
    $query_run = mysqli_query($conn, $query2);

}

$query = "SELECT * FROM reports";
$query_run = mysqli_query($conn, $query);

if ($query_run) {
    while ($row = mysqli_fetch_assoc($query_run)) {
        
      
        
        echo "<tr>";
       echo "<td>" . htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8') . "</td>";
       echo "<td>" . htmlspecialchars($row['Accountname'], ENT_QUOTES, 'UTF-8') . "</td>";
       echo "<td>" . htmlspecialchars($row['transactionId'], ENT_QUOTES, 'UTF-8') . "</td>";
       echo "<td>" . htmlspecialchars($row['date'], ENT_QUOTES, 'UTF-8') . "</td>";

        ?>
        <td class="badge badge-dark" style="width:70px;"><?php echo htmlspecialchars($row['type']); ?></td>
        <?php
        echo "<td>" . htmlspecialchars($row['Accountnop'], ENT_QUOTES, 'UTF-8') . "</td>";
        echo "<td>" . htmlspecialchars($row['ifsc'], ENT_QUOTES, 'UTF-8') . "</td>";
        echo "<td>" . htmlspecialchars($row['order_id'], ENT_QUOTES, 'UTF-8') . "</td>";
        echo "<td>₹" . htmlspecialchars($row['amount'], ENT_QUOTES, 'UTF-8') . "</td>";

        //$sts = $row['status'];
        if($row['status']=="Success"){
            $sts = 'Success';
            $sss='Pending';
            $cls = "badge badge-success";
        }else{
             $sss = 'Success';
            $sts = 'Pending';
            $cls = "badge badge-warning";
        }
        echo "<td><a href='?id=".$row['id']."&ssss=$sss'><button class='$cls'>" . $sts . "</button></a></td>";
        echo "</tr>";
    }
} else {
    echo "Error in query: ";
    //echo "Error in query: " . mysqli_error($conn); 
}
}
?>
											
										</tbody>
									</table>
							</div>
							</div>
						</div>
					</div>
				</div>
</div>
</div>
</div>
</body>
<script src="assets/js/plugin/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
<script src="assets/js/core/popper.min.js"></script>
<script src="assets/js/core/bootstrap.min.js"></script>
<script src="assets/js/plugin/bootstrap-notify/bootstrap-notify.min.js"></script>
<script src="assets/js/plugin/bootstrap-toggle/bootstrap-toggle.min.js"></script>
<script src="assets/js/plugin/jquery-mapael/jquery.mapael.min.js"></script>
<script src="assets/js/plugin/jquery-mapael/maps/world_countries.min.js"></script>
<script src="assets/js/plugin/jquery-scrollbar/jquery.scrollbar.min.js"></script>
<script src="assets/js/ready.min.js"></script>
<script src="assets/js/rechpay.js?1697912979"></script>
<script type="text/javascript">
function utr_search(utr_number){
if(getCurentFileName()=="transactions"){	
if(utr_number.length==12){
search_txn('2023-10-01','2023-10-21','',utr_number);
}else{
Swal.fire('Enter Valid UTR Number!');	
}
}else{
location.href ='transactions';
}
}
</script>
</html>
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.1/css/dataTables.bootstrap5.min.css"/>
<script src="https://cdn.datatables.net/1.13.1/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.1/js/dataTables.bootstrap5.min.js"></script>
<script>
$(document).ready(function () {
    $("#dataTable").DataTable();
});
</script>

<link href="https://code.jquery.com/ui/1.12.1/themes/cupertino/jquery-ui.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" ></script>
<script>
$(document).ready(function () {
$( ".datepicker" ).datepicker({
  dateFormat: "dd-mm-yy"
});
});
</script>
<script src="assets/js/merchant.js?1697912979"></script>				
<script type="text/javascript">
$(document).ready(function () {
//$("#search").click();
});	
</script>