<?php
session_start();

if (!isset($_SESSION['unohs']) || $_SESSION['unohs'] == null) {
    header("location:index.php?msg=unauthorized");
    exit;
}

include("conn.php");

if (isset($_POST['newupi'])) {
    $upiid = mysqli_real_escape_string($conn, $_POST['newupi']);
    $admin_id = $_SESSION['unohs'];

    if (!empty($upiid)) {
        $hashed = md5($upiid);
        $sql_q = "UPDATE nirvahaka_shonu SET guptapada='$hashed' WHERE unohs='$admin_id'";
        $chk = mysqli_query($conn, $sql_q);

        if ($chk) {
            echo '<script>alert("✅ Password Updated Successfully.");</script>';
        } else {
            echo '<script>alert("❌ Failed to update password.");</script>';
        }
    } else {
        echo '<script>alert("⚠️ Password cannot be empty.");</script>';
    }
}
?>

<?php include 'header.php'; ?>

<div class="main-panel">
  <div class="content-wrapper">
    <div class="row">
      <div class="col-sm-12 mb-4 mb-xl-0">
        <h4 class="font-weight-bold text-dark">Update Admin Password</h4>
      </div>
    </div> 

    <div class="row">
      <form action="#" id="upiform" method="post" autocomplete="off">
        <div class="d-flex align-items-center">	
          <input name="newupi" type="password" placeholder="Enter New Password" class="flex-grow-1 cool-input" style="height: 40px;" required />
        </div>
        <div class="d-flex align-items-center mt-3">
          <button type="submit" class="btn btn-primary cool-button mr-2">Update</button>
        </div>
      </form>
    </div>		  		  					  		  		  

  </div>
</div>

<script>
  if (window.history.replaceState) {
    window.history.replaceState(null, null, window.location.href);
  }
</script>
</body>
</html>
