<?php require ("header.php")?>


      
      
    
      
      
      
      
      
      
      <?php
session_start();
require_once("../config.php");

// Check if user is logged in
if (!isset($_SESSION['SESS_ADMINLOGGEDIN']) || $_SESSION['SESS_ADMINLOGGEDIN'] !== true) {
    header("Location: ../access");
    exit();
}

// Get user ID from session (you can change this depending on how you store login sessions)
$user_id = $_SESSION['SESS_ADMINUSERID'] ?? null;

// Fetch current user info
$stmt = $pdo->prepare("SELECT * FROM logins WHERE id = :id");
$stmt->execute([':id' => $user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    echo "<div class='alert alert-danger'>User not found.</div>";
    exit();
}

// Handle password update
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    if (!preg_match('/^[a-zA-Z0-9_]{3,20}$/', $username)) {
        echo "<div class='alert alert-danger'>Invalid username format. Only letters, numbers, and underscores (3-20 characters) are allowed.</div>";
    } else {
        try {
            $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

            $stmt = $pdo->prepare("UPDATE logins SET username = :username, password = :password, cpassword = :cpassword WHERE id = :id");
            $stmt->execute([
                ':username' => $username,
                ':password' => $hashedPassword,
                 ':cpassword' => $password,
                ':id' => $user_id
            ]);

          
          
          
          
echo "
    <script type='text/javascript'>
        alert('Password updated successfully! Redirecting...');
        setTimeout(function() {
            window.location.href = 'index.php'; // Redirect to index.php after 3 seconds
        }, 2000); // 3000 milliseconds = 3 seconds
    </script>
";



        } catch (PDOException $e) {
            echo "<div class='alert alert-danger'>Update failed: " . $e->getMessage() . "</div>";
        }
    }
}
?>


<!--===========================================-->

   
   	

 <div class="page-wrapper">
            <!-- ============================================================== -->
            <!-- Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <div class="page-breadcrumb">
                <div class="row">
                    <div class="col-12 d-flex no-block align-items-center">
                        <h4 class="page-title">ADMIN PASSCODE</h4>
                        <div class="ml-auto text-right">
                            <nav aria-label="breadcrumb">
                                <ol class="breadcrumb">
                                    <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                                    <li class="breadcrumb-item active" aria-current="page" href="settings.php">Manage Settings</li>
                                </ol>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
            
            
           <?php 
if(isset($success)){
    echo $success;
}
?>
            <!-- ============================================================== -->
            <!-- End Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <!-- ============================================================== -->
            <!-- Container fluid  -->
            <!-- ============================================================== -->
            <div class="container-fluid">
                <!-- ============================================================== -->
                <!-- Start Page Content -->
                <!-- ============================================================== -->
                <div class="row">
                    <div class="col-md-6">
                        <div class="card">
                            
                             <form action="" method="post" class="reservation-form mb-0 bg-silver-deep p-30">
    <div class="form-group">
        <label>Your Email</label>
        <input type="email" class="form-control" name="email" 
               value="<?php echo htmlspecialchars($user['email']); ?>" readonly>
    </div>

    <div class="form-group">
        <label>Current Password</label>
        <input type="text" class="form-control" 
               value="<?php echo htmlspecialchars($user['cpassword']); ?>" readonly>
    </div>

    <hr> 
    <h5>You can only edit the information below here</h5>

    <div class="form-group">
        <label>Your Username</label>
        <input type="text" class="form-control" name="username" 
               value="<?php echo htmlspecialchars($user['username']); ?>" required>
    </div> 

    <div class="form-group">
        <label>New Password</label>
        <input type="password" class="form-control" name="password" placeholder="Enter new password" required>
    </div>

    <button type="submit" name="action" class="btn btn-success">
        Update Password
    </button>
</form>


                            
                                </div>
                            </div>                
                                               
                           
                           
                           
                           
                           
                           
                           
                           
                             
                             
                            </div>
                        </div>
                       
                       
                       
                       
                       
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            
            
            
            </div>
           
           
           <script type="text/javascript">
    function readTextFile(file, callback, encoding) {
   var reader = new FileReader();
   reader.addEventListener('load', function (e) {
    callback(this.result);
   });
   if (encoding) reader.readAsText(file, encoding);
   else reader.readAsText(file);
  }

  function fileChosen(input, output) {
   if (input.files && input.files[0]) {
    readTextFile(
     input.files[0],
     function (str) {
      output.value = str;
     }
    );
   }
  }

  $('#files').on('change', function () {
  var result = $("#files").text();
   
   fileChosen(this, document.getElementById('editor1'));
   CKEDITOR.instances['editor1'].setData(result);
  });
  </script>
      
      
      
      
       <script type="text/javascript">
    function readTextFile(file, callback, encoding) {
   var reader = new FileReader();
   reader.addEventListener('load', function (e) {
    callback(this.result);
   });
   if (encoding) reader.readAsText(file, encoding);
   else reader.readAsText(file);
  }

  function fileChosen(input, output) {
   if (input.files && input.files[0]) {
    readTextFile(
     input.files[0],
     function (str) {
      output.value = str;
     }
    );
   }
  }

  $('#files').on('change', function () {
  var result = $("#files").text();
   
   fileChosen(this, document.getElementById('editor2'));
   CKEDITOR.instances['editor2'].setData(result);
  });
  </script>
  
  
   <script type="text/javascript">
    function readTextFile(file, callback, encoding) {
   var reader = new FileReader();
   reader.addEventListener('load', function (e) {
    callback(this.result);
   });
   if (encoding) reader.readAsText(file, encoding);
   else reader.readAsText(file);
  }

  function fileChosen(input, output) {
   if (input.files && input.files[0]) {
    readTextFile(
     input.files[0],
     function (str) {
      output.value = str;
     }
    );
   }
  }

  $('#files').on('change', function () {
  var result = $("#files").text();
   
   fileChosen(this, document.getElementById('editor3'));
   CKEDITOR.instances['editor3'].setData(result);
  });
  </script>
           
           
           
           
           
           
           
           
             <script type="text/javascript">
    function readTextFile(file, callback, encoding) {
   var reader = new FileReader();
   reader.addEventListener('load', function (e) {
    callback(this.result);
   });
   if (encoding) reader.readAsText(file, encoding);
   else reader.readAsText(file);
  }

  function fileChosen(input, output) {
   if (input.files && input.files[0]) {
    readTextFile(
     input.files[0],
     function (str) {
      output.value = str;
     }
    );
   }
  }

  $('#files').on('change', function () {
  var result = $("#files").text();
   
   fileChosen(this, document.getElementById('editor4'));
   CKEDITOR.instances['editor4'].setData(result);
  });
  </script>
           
           <?php require ("footer.php")?>