<?php
session_start();
require_once('../db.php');

include('inc/session.php');

$user = $_SESSION['user'];
								
$ins = mysqli_query($conn,"SELECT * FROM users WHERE email='$user' ");
$data = mysqli_fetch_array($ins);
								
$email = $data['email'];
$rowpas = $data['pass'];
$bal = $data['bal'];
							
$deps = "SELECT * FROM deposit  ";
$payment = $conn->query($deps);

$inss = mysqli_query($conn,"SELECT * FROM administrator WHERE admin='$user' ");
$datas = mysqli_fetch_array($inss);
								
$ppp = $datas['token'];

include('../inc/logo.php');	
?>

<?php include('inc/header.php');?>

<body class="fixed-nav sticky-footer bg-dark" id="page-top">


  <!-- Navigation-->
 <?php include('inc/nav.php');?>
        
  <!-- Navigation Ends-->
      </ul>
    </div>
  </nav>
  <div class="content-wrapper">
  
    <div class="container-fluid">
      <!-- Breadcrumbs-->
     
      <div class="row">
        <div class="col-12">
          
     
     <h1 class="w3-xxxlarge w3-text-green" align="center"><b>Update User Account Details</b></h1>
       

      <!-- Example Bar Chart Card-->
       
     <?php 
// define variables and set to empty values
      require("function/functions.php");

if(isset($_GET['id'])){
      
      $id = $_GET['id']; 
    
    $getuser = mysqli_query($conn, "SELECT * FROM users WHERE accno='$id' ");
    $qryuser = mysqli_fetch_array($getuser);
    
    $receiver = $qryuser['email'];
    $phone2 = $qryuser['phone'];
    $level2 = $qryuser['level'];
    $fname2 = $qryuser['firstname'];
    $lname2 = $qryuser['lastname'];
    $bal2 = $qryuser['bal'];  
    $status2 = $qryuser['status'];
    $bvn2 = $qryuser['BVN'];
    $pass2 = $qryuser['pass'];
    $username2 = $qryuser['username'];
    $email = $qryuser['email'];
      
  }else{
      
      $receiver = "";
  }	 
	 	
	
$qryApi = mysqli_query($conn,"SELECT * FROM settings");
$apidata = mysqli_fetch_array($qryApi);

$sitename = $apidata['sitename'];
$sitelogo = $apidata['sitelogo'];
$admin = $apidata['email'];
$sitephone = $apidata['mobile']; 
$siturl = $_SERVER['SERVER_NAME'];
$request_dir = $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']);


		
	if(isset($_POST['create'])){
		
$fname = $_POST['fname'];
$lname = $_POST['lname'];
//$email = $_POST['email'];
$pass =  $_POST['pass'];
$phone = $_POST['phone'];
$status = $_POST['status'];
$old = $_POST['otp'];
$username = $_POST['username'];
$otp = md5($old);

$hash = password_hash($pass, PASSWORD_DEFAULT);

$block = "Block";
$activate = "Activate";
$del = "Delete";
$cur = "₦";
$log=$_SERVER['REMOTE_ADDR'];
$country = $_POST['country'];
$accno = mt_rand(10000,298983);

$ref = mt_rand(108367652312,398166660123);

$refby = "Admin";
$refbyid = "7175";
$refid = "7175";
$refwallet = 0;
$refcount = 0;
$reflink = 'https://'.$request_dir.'/register.php?refid=';
$refunverified = 0;
$refverified = 0;

$pincredit = 0;

$blockpro = '0';

if($status == 'Active'){ 
$text= "Please be informed that your $sitename account has been <b>ACTIVATED</b> and a Unique Account number <b>Assigned</b> to you. <br>
Login to your account to see your Unique Account number for wallet funding." ;	

$txt = "Dear $fname2 Your Account on $sitename has been ACTIVATED and a Unique Account Number ASSIGNED to you. Thank You for choosing Us. www.$siturl";
		
}elseif($status == 'Pending'){ 
$text= "Please be informed that your $sitename account Activation is still in <b>PROGRESS</b>. <br>
Please Be Patient with Us.";

}elseif($status == 'BVNError'){
$text= "Please be informed that your $sitename account Activation request has been <b>REJECTED</b> and your Account <b>NOT ACTIVATED</b>. <br>
<b>REASON:</b> The BVN you Supplied is Invalid, as it can not be Verified. Kindly Login to your account and correct it.";

}elseif($status == 'NameError'){ 
$text= "Please be informed that your $sitename account Activation request has been <b>REJECTED</b> and your Account <b>NOT ACTIVATED</b>. <br>
<b>REASON:</b> The Name You Supplied Didnt Matched with the Name on your BVN. Kindly Login to your account and correct it.";

}elseif($status == 'Upgrade'){
$text= "Please be informed that your $sitename account Activation request has been <b>REJECTED</b> and your Account <b>NOT ACTIVATED</b>. <br>
<b>REASON:</b> You Can only get a Unique Account Number for wallet Funding if you have Upgraded Your account Level. Kindly Login to your account and Upgrade to either Agent, Reseller or Portal Owner."; 

}else{
$text= "Please be informed that your $sitename account Activation is still in <b>PROGRESS</b>. <br>
Please Be Patient with Us.";}



if($otp == $ppp){


$check=mysqli_query($conn,"SELECT * FROM sme_data_transactions WHERE email='$email' OR phone='$phone' ");
   
    $nu = mysqli_num_rows($check);

$check=mysqli_query($conn,"SELECT * FROM users WHERE email='$email' OR phone='$phone2' ");//looking through existing usernames
   
    $num = mysqli_num_rows($check);
    
   


    
    
    //if ($nu >0){

if ($num >0){
    
//$sql3 = mysqli_query($conn,"UPDATE sme_data_transactions SET phone='$phone',ref='$hash',refer='$pass',email='$email',token='$username',customer='$fname $lname' WHERE email='$email' ");

$sql = mysqli_query($conn,"UPDATE users SET firstname='$fname',lastname='$lname',username='$username',phone='$phone',IPaddress='101.201.201' WHERE email='$email' OR phone='$phone2' ");



echo '<div class="alert alert-success"><strong>Great!</strong> Account <b>UPDATED</b>  Successfully</div>';
        

// send email

$from = "$sitename<info@$siturl>"; //the email address from which this is sent
$to = "$email"; //the email address you're sending the message to
$subject = "Account Update"; //the subject of the message

// To send HTML mail, the Content-type header must be set
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "Return-Path: $sitename<info@$siturl>\r\n";
$headers .= "Organization: $sitename\r\n";
 
// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

$message = "

<html>
<head>
<style>
.table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 50%;
}

td {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}


.button {
  background-color: #008CBA; /* Blue */
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  border-radius: 8px;
  cursor: pointer;
  
  -webkit-transition-duration: 0.4s; /* Safari */
  transition-duration: 0.4s;
}

.button:hover {
  background-color: #4CAF50; /* Green */
  color: white;
}

</style>
</head>

<body>

//<img src='https://vtuafrica.com.ng/portal/sitelogo/logo.png'>
<img src='https://".$request_dir."/sitelogo/".$apidata['sitelogo']."'>

<h3>Dear $fname2 $lname2,</h3>

Your Profile on $sitename was update by the Admin, as you requested.  <br> <br>
Please if you did not requested for your profile to be updated, kindly call $sitephone to report immediately.
Thank You for Choosing $sitename <br>



<strong>Your new details are as follows:</strong> <p>

<table class='table' >
  
  <tr>
    <td>Email Address</td>
    <td>$email</td>
  </tr>

  <tr>
    <td>First Name</td>
    <td>$fname</td>
  </tr>

 <tr>
    <td>Last Name</td>
    <td>$lname</td>
  </tr>

 <tr>
    <td>User Name</td>
    <td>$username</td>
  </tr>

 <tr>
    <td>Phone Number</td>
    <td>$phone</td>
  </tr>


  
</table> <p>

$sitename give you instant and automated recharge for Airtime, <br>
SME DATA, Direct Data, Gotv, Dstv, Startimes, Electric Token, Smile Internet, <br>
WAEC, JAMB, NECO, NABTEB Pins, Bulk SMS, Bank Transfer and lots more...<p>

<a href='https://$siturl'><button class='button'>Try it Now!</button></a> <br>


<p>
Our support team is standby to assist you wherever you need help.<p>
 

<p>Warm Regards, <p>

<strong>The Admin TEAM <br>
  $sitephone<br>
  $admin <br>
www.$siturl </strong>

</body><html>";

//now mail
$DoMail = mail($to,$subject,$message,$headers);


$qryApi = mysqli_query($conn,"SELECT * FROM api_setting");
$apidata = mysqli_fetch_array($qryApi);
$api = $apidata['smartkey'];
$senderID = $apidata['smsPhone'];

$query_rec = mysqli_query($conn,"SELECT * FROM billing");
$bil = mysqli_fetch_array($query_rec);
$bi = $bil['sms'];

//$txt = "Dear $fname2 $lname2 Your Account on $sitename has been UPGRADED from $acclevel to $accType. Thank You. www.$siturl";

$message = utf8_encode($txt);
$aa = urlencode($message);
$from = utf8_encode($senderID);
$sender = urlencode($from);

	   
	   
//$baseurl ="https://smartrecharge.ng/api/v1/http.php?api_key=$api&recipient=$phone2&message=$aa&sender=$sender&route=3";



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $baseurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

 $content1 =curl_exec($ch);
                         

  $content=substr($content1, 0, 2);
                                   
        curl_close($ch);

}else{
    
echo '<div class="alert alert-danger"><strong>Error!</strong> User does not exist.</div>';
}

        
	
	}else {
	
							
echo '<div class="alert alert-danger">Enter a valid OTP Received From SMS</div> '; 
	
}


}


if(isset($_POST['withdraw'])){
										
									

$email5 = $_POST['email'];


$sms = mt_rand(100000,61562822);  
$developer = "08064897610";



if(!empty($email5) ){

$check=mysqli_query($conn,"SELECT * FROM users2 WHERE email='$email' ");//looking through existing usernames
   
    $num = mysqli_num_rows($check);
    
    
   if($email5 == $developer){

echo '<div class="alert alert-success"><strong>Great!</strong> OTP Sent Successfully</div>';



}else{
echo '<div class="alert alert-danger"><strong>Invalid</strong> <b>Admin Phone Number</b> Entered.</div>';  }

}else{
echo '<div class="alert alert-danger">Enter Your Admin Phone Number.</div> ';  }
}

		?>
	
  
  <form action="" method="post" autocomplete = 'off'> 
  
  <!--<div class="form-group">
        <label><strong>FirstName</strong></label>
		 <input type="hidden" id="fname" name="fname" class="form-control"  >
		 <div id="charNum"></div>
		 
      </div> 
      
      
      <div class="form-group">
        <label><strong>LastName</strong></label>
		 <input type="hidden" id="lname" name="lname"    class="form-control"  >
		 <div id="charNum"></div>
		 
      </div> 
  
  
  
	
		 
      </div> -->
	 
	</div> 

<?php 
if($level2 === 'reseller'){
$accType = '<badge class="badge badge-pill badge-success">Portal Owner</badge>';
}elseif($level2 === 'agent'){
$accType = '<badge class="badge badge-pill badge-info">Agent/Reseller</badge>';
}elseif($level2 === 'special'){
$accType = '<badge class="badge badge-pill badge-success">Special User</badge>';
 }else{
 $accType = '<badge class="badge badge-pill badge-danger">Normal User</badge>'; }?>


      <font color="white"><strong>Enter Email Address or Phone Number</strong></font><br>
 <div class="form-group"><strong>You are about to ACTIVATE the Account Status of <font color="red"><?php echo $fname2; ?> <?php echo $lname2;?> </font><br>
Email Address: <?php echo $receiver; ?> <br>
Current Level: <?php echo $accType; ?> <br>
Current Status: <font color="red"><?php echo $status2; ?> </font> <br>
Current BVN: <font color="red"><?php echo $bvn2; ?> </font> <br>
Current Phone: <font color="red"><?php echo $phone2; ?> </font> <br>
Current Balance: <font color="red"><?php echo $bal2; ?></font></strong><br> <br>
       
 <!--<label><strong>Email Address:</strong></label>
		 <input type="text" id="email" name="email"  value="<?php echo $receiver; ?>"  class="form-control"  required>
		 <div id="charNum"></div>-->

    <div class="form-group">
<label><strong>First Name:</strong></label>
        <input type="text" id="fname" name="fname" value="<?php echo $fname2; ?>"  class="form-control" required>
		 <div id="charNum"></div>

 <div class="form-group">
<label><strong>Last Name:</strong></label>
        <input type="text" id="lname" name="lname" value="<?php echo $lname2; ?>"  class="form-control" required>
		 <div id="charNum"></div>

 <div class="form-group">
<label><strong>UserName:</strong></label>
        <input type="text" id="username" name="username" value="<?php echo $username2; ?>"  class="form-control" required>
		 <div id="charNum"></div>

<div class="form-group">
<label><strong>Phone Number:</strong></label>
        <input type="text" id="phone" name="phone" value="<?php echo $phone2; ?>"  class="form-control" required>
		 <div id="charNum"></div>

 <!-- <div class="form-group">
<label><strong>Password:</strong></label>
        <input type="text" id="pass" name="pass" value="<?php echo $pass2; ?>"  class="form-control" required>
		 <div id="charNum"></div>-->



	  
	 
<div class="form-group">
<label><strong>An OTP has been sent to Your Phone, Enter it below:  <a href="#"  data-toggle="modal" data-target="#exampleModal2"><font color="red">Resend OTP</font></a></strong></label>
        <input type="password" id="phone" name="otp"   class="form-control" required>
		 <div id="charNum"></div>	
	  

      <button type="submit" id="submit" name="create" class="btn btn-info">Update User</button>
    </form>  
 
             
            </div>
            
          </div>
          <!-- Card Columns Example Social Feed-->    
         </p>
        </div>
      </div>
    </div>
    <!-- /.container-fluid-->
    <!-- /.content-wrapper-->
    <?php include('inc/footer.php');?>

 <!--============================ Modal starts here=============================== -->
                                                <div class="modal fade" id="exampleModal2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel2" aria-hidden="true">
                                                    <div class="modal-dialog" role="document">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h4 class="modal-title" id="exampleModalLabel2">Resend OTP</h4>
                                                                <a href="#" class="close" data-dismiss="modal" aria-label="Close">
                                                                            <span aria-hidden="true">&times;</span>
                                                                        </a>
                                                                   
                                                                      
                                                                        
                                                            </div>
                                                                                                    

                                                            <div class="modal-body">                                                      
                                                           <p><form method="POST" action="" accept-charset="UTF-8">
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                 
                              <tr>
                                        <td>Phone</td>
                                        <td><input class="form-control"  name="email" type="text" value=""></td>
                                    </tr>
                                </table>
                            </div>
                            </p>
                                                            
                                                            <div class="modal-footer">
                                                                <a href="#" class="btn btn-secondary" data-dismiss="modal">Close</a>
                                                                <button type="submit" name="withdraw" class="btn btn-success">Resend OTP</button>
                                                            </div>
                                </form> 

                                <!--====================================Modal ends here============================-->