<?php 	include_once('../../config.php');   	
		include_once(ABS_PATH.'/admin/login_check.php');    
		
		
		
		    $_POST['note'] = 'NA';
		    if(isset($_POST['update']))
            {    
                        $stmt=$conn->prepare("select  * from    UI__USDTRest where id=:id ");  
                        $stmt->bindValue(':id', $_REQUEST['id'] , PDO::PARAM_STR);
                        $stmt->execute(); 
                        $AllUsers = $stmt->fetch(PDO::FETCH_OBJ); 
                        
                        
                        
                                 
                        if($_POST['Status']==1)
                        {
                        
                                $URL  = 'https://daemon.takshitsolutions.com/api/app/v1/get-token?projectName=VIJAYx9A4ZkP2w7VfM1r8JdQ3L6tB0HxG7H8&userName='.$AllUsers->custId;  
                                $crl = curl_init();
                                curl_setopt($crl, CURLOPT_URL, $URL);
                                curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                                curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);  
                                $response = curl_exec($crl); 
                                $Data = json_decode($response);    
                              
                                
                                $FrmAddress = $_POST['FrmAddress'];
                                $PriVt  = $_POST['privatKey'];
                                $ToaddRess = $AllUsers->address;
                                $Amt = $AllUsers->AMMT;
                                        
                                $data_string = '{ 
                                "coinname":"USDT",
                                "fromaddress":"'.$FrmAddress.'",
                                "privatekey":"'.$PriVt.'",
                                "toaddress":"'.$ToaddRess.'",
                                "amount":"'.$Amt.'",
                                "contractaddress":"TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t",
                                "network":"Tron",
                                "decimals":"6"
                                }'; 
                               
                                
                                $ch = curl_init();
                                curl_setopt($ch, CURLOPT_URL,'https://daemon.takshitsolutions.com/api/app/v1/transfer-token');
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                                curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
                                curl_setopt($ch, CURLOPT_POST, 1);
                                $headers = array();
                                $headers[] = "Accept: application/json";
                                $headers[] = "Authorization: Bearer ".$Data->data;
                                $headers[] = "Content-Type: application/json";
                                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                                $result = curl_exec($ch); 
                                $DecodD = json_decode($result);  
                                
                                if($DecodD->isError=="false")
                                {    
                                    $stmt=$conn->prepare("update       UI__USDTRest  set status=1,processDate=:processDate where where id=:id ");  
                                    $stmt->bindValue(':processDate', date("d-M-Y H:i") , PDO::PARAM_STR);
                                    $stmt->bindValue(':id', $_REQUEST['id'] , PDO::PARAM_STR);
                                    $stmt->execute(); 
                                } 
                                     
                        }else
                        {
                            
                            
                            
                    				        $_POST['userid'] = $AllUsers->custId;
                    				        $_POST['fund'] = $AllUsers->AMMT;
                    				                         				        
                				        
                                            $stmt=$conn->prepare("select  amt,availableFund from  UI__Fundtransfer   where userid=:cuurentId order by id desc");  
                                            $stmt->bindValue(':cuurentId', $_POST['userid'] , PDO::PARAM_STR);
                                            $stmt->execute();  
                                            
                                            if($stmt->rowCount() > 0)
                                            {
                                               $AllUsers = $stmt->fetch(PDO::FETCH_OBJ); 
                                               $totalAvaib = $AllUsers->availableFund+$_POST['fund'];  
                                            }else
                                                 $totalAvaib = $_POST['fund']; 
                                            
                                			$stmt=$conn->prepare("insert into  UI__Fundtransfer (userid,Tdate,amt,availableFund,refunded) values(:userid,:Tdate,:amt,:availableFund,1)    "); 
                                            $stmt->bindValue(':userid',$_POST['userid'], PDO::PARAM_STR); 
                                            $stmt->bindValue(':Tdate',date("d-M-Y H:i"), PDO::PARAM_STR); 
                                            $stmt->bindValue(':amt',$_POST['fund'], PDO::PARAM_STR);  
                                            $stmt->bindValue(':availableFund', $totalAvaib , PDO::PARAM_STR);  
                                            $stmt->execute(); 
                                             
                                            
                                            $stmt=$conn->prepare("select *  from UI__DailyROIGene    where custId=:sId     ");   
                                            $stmt->bindValue(':sId',  $_POST['userid'] , PDO::PARAM_INT);  
                                            $stmt->execute(); 
                                            $XUI__DailyROIGene = $stmt->fetchAll(PDO::FETCH_OBJ);  
                                            
                                            $Tamnt = 0;
                                            foreach($XUI__DailyROIGene as $xx)
                                            {
                                            $Tamnt +=$xx->amount;
                                            }      
                                            
                                            
                                            $stmt=$conn->prepare("select *  from UI__Bns    where Rid =:sId   ");   
                                            $stmt->bindValue(':sId',  $_POST['userid'] , PDO::PARAM_INT);  
                                            $stmt->execute(); 
                                            $UI__Bns = $stmt->fetchAll(PDO::FETCH_OBJ); 
                                            
                                            $LevelI = 0;
                                            foreach($UI__Bns as $dddm)
                                            {
                                            $LevelI +=$dddm->Tamt;
                                            }
                                            
                                            
                                            $stmt=$conn->prepare("select Amt   from UI__Tpm where TopUpId=:refId   ");   
                                            $stmt->bindValue(':refId',  $_POST['userid'] , PDO::PARAM_INT);  
                                            $stmt->execute(); 
                                            $MdirectB = $stmt->fetchAll(PDO::FETCH_OBJ);  
                                            
                                            $directIncome = 0;
                                            foreach($MdirectB as $dmm)
                                            $directIncome += $dmm->Amt;                
                                            
                                            
                                            
                                            
                                            $stmt=$conn->prepare("select *  from UI__WithDraw    where userid=:sId and (accept=1 ||accept=2)  ");   
                                            $stmt->bindValue(':sId', $_POST['userid'] , PDO::PARAM_INT);  
                                            $stmt->execute(); 
                                            $UI__WithDraw = $stmt->fetchAll(PDO::FETCH_OBJ); 
                                            
                                            
                                            $totalWithdrwa = 0;
                                            foreach($UI__WithDraw as $tl)
                                            {
                                            $totalWithdrwa +=$tl->amt;
                                            }
                                            
                                              
                                            $stmt=$conn->prepare("select availableFund from UI__Fundtransfer  where  userid=:userid  order by id desc  ");   
                                            $stmt->bindValue(':userid',  $_POST['userid'] , PDO::PARAM_INT);  
                                            $stmt->execute(); 
                                            $UI__Fundtransfer = $stmt->fetch(PDO::FETCH_OBJ);   
                                            
                                               
                                            $PAYWAllet =  ($Tamnt+$LevelI+$directIncome+$UI__Fundtransfer->availableFund)-($totalWithdrwa);      
                                            $stmt=$conn->prepare("insert into  UI__DebitCredit (custId,addDel,availableBal,Tdate,type,Intype,byw) values(:custId,:addDel,:availableBal,:Tdate,:type,'Refunded','NA')"); 
                                            $stmt->bindValue(':custId',$_POST['userid'], PDO::PARAM_STR); 
                                            $stmt->bindValue(':addDel',$_POST['fund'], PDO::PARAM_STR); 
                                            $stmt->bindValue(':availableBal',$PAYWAllet, PDO::PARAM_STR);  
                                            $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);  
                                            $stmt->bindValue(':type', 1 , PDO::PARAM_STR);   
                                            $stmt->execute();     
                                            $LastIntId = $conn->lastInsertId();
                                            
                                            $stmt=$conn->prepare("select id from UI__Rgst  where  cuurentId=:cuurentId    ");   
                                            $stmt->bindValue(':cuurentId',  $_POST['userid'] , PDO::PARAM_STR);  
                                            $stmt->execute(); 
                                            $UIid = $stmt->fetch(PDO::FETCH_OBJ);   
                                            
                                            $amount = password_hash(($PAYWAllet),PASSWORD_DEFAULT);  
                                            $stmt =  $conn->prepare("update _pkCc__ set bl1=:bl1,tm=:tm where Uxd=:Uxd "); 
                                            $stmt->bindValue(':bl1', $amount, PDO::PARAM_STR); 
                                            $stmt->bindValue(':tm', date("d-M-Y H:i"), PDO::PARAM_STR); 
                                            $stmt->bindValue(':Uxd', $UIid->id, PDO::PARAM_STR);  
                                            $stmt->execute();   
                                            
                                            $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                                            $stmt->bindValue(':cuurentId',  $_POST['userid'] , PDO::PARAM_STR);  
                                            $stmt->execute();
                                            $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);     	
                                            
                                            
                                            $stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
                                            $stmt->bindValue(':type',   11 , PDO::PARAM_STR);   
                                            $stmt->bindValue(':bank',  "Refunded" , PDO::PARAM_STR);   
                                            $stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
                                            $stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
                                            $stmt->bindValue(':amountPaid', $_POST['fund']  , PDO::PARAM_STR); 
                                            $stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
                                            $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                                            $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
                                            $stmt->bindValue(':custId', $_POST['userid'], PDO::PARAM_STR);  
                                            $stmt->bindValue(':cdId', $LastIntId  , PDO::PARAM_STR); 
                                            $stmt->bindValue(':Topay',  $_POST['userid'] , PDO::PARAM_STR); 
                                            $stmt->bindValue(':TopayName',  $_POST['userid'] , PDO::PARAM_STR);     
                                            $stmt->execute();  
                                            
                                                
                                                $stmt=$conn->prepare("update       UI__USDTRest  set status=2,processDate=:processDate where where id=:id ");  
                                                $stmt->bindValue(':processDate', date("d-M-Y H:i") , PDO::PARAM_STR);
                                                $stmt->bindValue(':id', $_REQUEST['id'] , PDO::PARAM_STR);
                                                $stmt->execute(); 
                                            
                        } 
                        die;
                        
                        header("Location: USDTedit.php");
                        die;
            }     
                        
            $stmt=$conn->prepare("select  * from   UI__USDTRest where id=:id ");  
            $stmt->bindValue(':id', $_REQUEST['id'] , PDO::PARAM_STR);
            $stmt->execute(); 
            $AllUsers = $stmt->fetch(PDO::FETCH_OBJ); 
?>  
<?php  include ABS_PATH.'/admin/header.php';	?>  

<script>
function del_record(val)
{

	var r = confirm("Do you really want to delete record !");
	if (r == true) {    
	$.ajax({
	type: 'POST',
	url: "../ajax/default.php",
	//async: false,			 
	data: {del_city_names:1,val:val},	  
	success: function(ajaxresult){	     
		location.reload();	 	
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) { 
	alert('Error');
	}
	});
	
	}	
	
}

</script>
		 
		<div class="col-md-10">  
  			<div class="content-box-large">
  				<div class="panel-heading">
					<div class="panel-title"><h3>update Request    </h3></div>
					
					<div align="right">  <input type="button"    class="btn btn-warning" name="btnback" id="btnback" value=" Back " onClick="javascript:location.href='UsdtwalletRst.php';" />
					</div>
					
				</div>
				
  				<div class="panel-body">
  					<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
						<thead>
							<tr>
								<th>Sr</th>
								<th>CustId</th> 
								<th>Name</th>  	  
								<th>Dollar</th>  
								<th>Type</th> 
								<th>Address</th>  
							</tr>
						</thead>
						<tbody>
							<?php 
                                    $stmt=$conn->prepare("select  * from   UI__USDTRest  where id=:id");    
                                    $stmt->bindValue(':id', $_REQUEST['id'] , PDO::PARAM_STR);
                                    $stmt->execute(); 
                                    $AllUsers = $stmt->fetchAll(PDO::FETCH_OBJ); 
                                    $sr = 1; 
                                 
								  foreach($AllUsers as $users)
								  {   
							   
                                    $stmt=$conn->prepare("select  * from   UI__Rgst where cuurentId=:custId ");  
                                    $stmt->bindValue(':custId', $users->custId , PDO::PARAM_STR);
                                    $stmt->execute();   
                                    $userName = $stmt->fetch(PDO::FETCH_OBJ); 

							?>
							<tr class="odd gradeX">
								<td><?php echo $sr++;?> </td>
								<td><?php echo $users->custId;?></td>
								<td><?php echo $userName->nm;?></td>
								<td><?php echo  round(($users->AMMT/$users->INRVAl),2);?>  </td> 
								<td><?php   if($users->PgTYpe==1) echo "TopUp";else echo "Withdrawl"; ?>  </td> 
								<td><?php   echo $users->address; ?>  </td>   
							</tr>  
							<?php } ?>
						</tbody>
					</table>
					
					
					
					   <?php 
					    if($AllUsers->status==0) {
					   ?> 
					    <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data" id="product_frm" autocomplete="off"> 
								 
								 
								 <div class="form-group">
								    <label for="inputEmail3" class="col-sm-2 control-label">Status</label>
								    <div class="col-sm-3">
								     <select  class="form-control" name="Status">
								         <option value="1" <?php if($AllUsers->status==1) echo "selected"; ?>>Approved</option>
								         <option value="0" <?php if($AllUsers->status==0) echo "selected"; ?>>Pending</option>
								         <option value="2" <?php if($AllUsers->status==2) echo "selected"; ?>>Rejected</option>
								     </select>
								    </div>
								 </div>
								 <hr>
								 
								 
								  <div class="form-group">
								    <label for="inputEmail3" class="col-sm-2 control-label">From Address</label>
								    <div class="col-sm-6">
								      <input type="text" name="FrmAddress"  class="form-control" readonly value="TQHD8d25cyrzdVPBynrPDhip5YVv28J17C"/>
								    </div>
								 </div>
								 <hr>
								 
							 	 <div class="form-group">
								    <label for="inputEmail3" class="col-sm-2 control-label">Private Key</label>
								    <div class="col-sm-6">
								      <input type="text" name="privatKey"  class="form-control" required/>
								    </div>
								 </div>
								 <hr>
								 
								
								 
								  <div class="form-group">
								    <label for="inputEmail3" class="col-sm-2 control-label">Note</label>
								    <div class="col-sm-3">
								      <input type="text" name="noteDt"  class="form-control" value="NA"/>
								    </div>
								 </div>
								 <hr>
								  
								  
								  <div class="form-group">
										<label for="inputEmail3" class="col-sm-2 control-label">Update</label>
										<div class="col-sm-10">
										  <input type="submit"    class="btn btn-primary"  name="update" value="Update Status">
										</div>
								  </div>   
								</form>
					<?php } ?>			
								
								
  				</div>
  			</div>



		  </div>
		</div>
    </div>

<?php  include ABS_PATH.'/admin/footer.php';	?>    