<?php
include('header.php');
session_start();
if (!isset($_SESSION['userid']) || $_SESSION['userid'] !== "ADMIN") {
    echo "<script>window.location.href = 'index.php';</script>";
    exit();
}
if(isset($_GET['apid']) && $_GET['approve'] == 1 ){
     $apid = base64_decode(mysqli_real_escape_string($conn,$_GET['apid']));
    $select = mysqli_query($conn,"SELECT * FROM qrtxn WHERE txnid='$apid' AND status='pending'");
    if(mysqli_num_rows($select)==1){
    $data = mysqli_fetch_assoc($select);
    $amt = $data['amount'];
    $pupdate =  mysqli_query($conn,"UPDATE payments SET status='success' WHERE order_id='$apid' ");
    $update = mysqli_query($conn,"UPDATE users SET balance=balance+$amt WHERE username='".$data['username']."'");
    if($update && $pupdate){
        ?>
         <script>
            $(function(){
                Swal.fire(
                    'Balance Added Successfully', 
                    '',
                    'success'
                )
            })
            setTimeout(() => {
                window.location='BalanceTransfer.php';
            }, 1000);
            </script>
        <?php
    }else{
        ?>
                <script>
                    $(function(){
                        Swal.fire(
                            'Not Added', 
                            '',
                            'error'
                        )
                    })
                  
                </script>
                <?php
    }
    }
}else if(isset($_GET['rejid']) && $_GET['reject'] == 1 ){
    $apid = base64_decode(mysqli_real_escape_string($conn,$_GET['rejid']));
   $select = mysqli_query($conn,"SELECT * FROM payments WHERE order_id='$apid' ");
   if(mysqli_num_rows($select)==1){
   $data = mysqli_fetch_assoc($select);
   $update = mysqli_query($conn,"UPDATE payments SET status='rejected' WHERE order_id='$apid'");
   if($update){
       ?>
        <script>
           $(function(){
               Swal.fire(
                   'Reject Successfully', 
                   '',
                   'success'
               )
           })
           setTimeout(() => {
               window.location='BalanceTransfer.php';
           }, 1000);
           </script>
       <?php
   }else{
       ?>
               <script>
                   $(function(){
                       Swal.fire(
                           'Something Wrong', 
                           '',
                           'error'
                       )
                   })
                 
               </script>
               <?php
   }
   }
}
if(isset($_POST['ahkweb_add_user']) && $_POST['ahkweb_add_user'] =="ahkwebsolutions_adduser"){
        $email = mysqli_real_escape_string($conn,$_POST['emailid']);
        $amount = mysqli_real_escape_string($conn,$_POST['amount']);
      
      $res = mysqli_query($conn,"UPDATE usertable SET walletamount=walletamount+'$amount' WHERE emailid='$email' ");
      
      if($res){
      ?>
                <script>
                    $(function(){
                        Swal.fire(
                            'Balance Added Successfully', 
                            '',
                            'success'
                        )
                    })
                  
                </script>
                <?php
      }else{
      ?>
                <script>
                    $(function(){
                        Swal.fire(
                            'Opps',
                            'Balance Not Added',
                            'error'
                        )
                    })
                </script>
                <?php
        
      }
        
    }
if(isset($_POST['mobile']) && $_POST['transfer'] == 'true'){
    $mobile = mysqli_real_escape_string($conn,$_POST['mobile']);
    $sl = mysqli_query($conn,"SELECT * FROM usertable WHERE  phone='$mobile'");
    if(mysqli_num_rows($sl)==1){
        $ud = mysqli_fetch_assoc($sl);
        ?>
       	

																																															

            
                
                             
                          <div class="payment-form">
                           <h4 >Conform User Details</h4>
                          
                              <form action="" method="POST">
                                  <div class="row">
                                      <div class="col-md-6">
                                          <div class="row">
                                              <div class="col-md-6">
                                                  <div class="form-group">
                                                      <label>Full name of User:</label>
                                                      <input type="text" class="form-control" value="<?php echo $ud['name']; ?>" readonly>
                                                  </div>
                                              </div>
                                              <div class="col-md-6">
                                                  <div class="form-group">
                                                      <label>Old Wallet balance:</label>
                                                      <input type="text" class="form-control" value="<?php echo $ud['walletamount']; ?>" readonly >
                                                  </div>
                                              </div>
                                              <input type="hidden" name="ahkweb_add_user" value="ahkwebsolutions_adduser">
                                              <input type="hidden" name="emailid" value="<?php echo $ud['emailid']; ?>">
                                              <div class="col-md-6">
                                                  <div class="form-group">
                                                      <label>Add Wallet Amount:</label>
                                                      <input required name="amount" value="" type="number" placeholder="Enter Amount" class="form-control">
                                                  </div>
                                              </div>
                                          </div>
                                          
                                          <div class="text-end">
                                              <button type="submit" class="btn btn-primary">Transfer</button>
                                          </div>
                                      </div>
                                  </div>
                              </form>
                        
                            </div>
  <?php
    }else{
        ?>
        <div class="payment-form">
                                  <h4 class="card-title">Balance Transfer ENTER USER MOBILE NO ONLY</h4>
                              
                             
                                  <form action="" method="POST">
                                      <div class="row">
                                          <div class="col-md-6">
                                              <div class="row">
                                                  <div class="col-md-6">
                                                    <p class="text-danger">No Data Found try Again by User Mobile No!!</p>
                                                      <div class="form-group">
                                                          <label>ENTER USER MOBILE NO ONLY</label>
                                                          <input type="number" class="form-control" maxlength="10" name="mobile"  placeholder="Enter beneficiary Mobile no" required >
                                                      </div>
                                                  </div>
                                                  <input type="hidden" name="transfer" value="true">
                                                  
                                              </div>
                                              
                                              <div class="text-end">
                                                  <button type="submit" class="btn btn-danger">Fetch Balance</button>
                                              </div>
                                          </div>
                                      </div>
                                  </form>
                              </div>
                          
      <?php
    }
    
    
    

}else{
    ?>
    <div class="payment-form">
                                <h4 class="card-title">Balance Transfer </h4>
                           
                                <form action="" method="POST">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label>ENTER USER MOBILE NO ONLY:</label>
                                                        <input type="number" class="form-control" maxlength="10" name="mobile"  placeholder="Enter beneficiary Mobile no" required>
                                                    </div>
                                                </div>
                                                <input type="hidden" name="transfer" value="true">
                                                
                                            </div>
                                            
                                            <div class="text-end">
                                                <button type="submit" class="btn btn-success">Fetch Balance</button>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        
    <?php
}
?>
              
        
             
																																						
	
   <style>
        .payment-form {
            max-width: 90%;
            margin: auto;
            background: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgb(0 0 0 / 80%);
        }

        .payment-form input {
            width: calc(100% - 20px);
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .payment-form button {
            width: 100%;
            padding: 10px;
            background: #28a745;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .payment-form button:hover {
            background: #218838;
        }

        .qr-popup {
            display: none;
            position: fixed;
            top: 70px;
            left: 50%;
            transform: translateX(-50%);
            width: 300px;
            background: rgb(255, 255, 255);
            justify-content: center;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            color: #181818;
            border: 5px solid #28a745;
            /* Add a 5px border with green color */
            box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
            /* Optional: Add a shadow for better styling */
        }

        .qr-popup img {
            max-width: 200px;
            margin: 10px auto;
        }

        .qr-popup button {
            margin-top: 10px;
            padding: 10px;
            background: #dc3545;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
<?php 
include('userfooter.php');
?>
