<?php

    include('dbconn.php');

    $year = date("Y");
    $copyrights = "Hak Cipta Terpelihara © ";
    $copyrights_name = " Pusat Sains Negara.";

    session_start();
    $msg="";

    $title = "Pusat Sains Negara - Sistem Pembelian Tiket Atas Talian";

   if($_SERVER["REQUEST_METHOD"] == "POST") {

       if (isset($_POST['resetpw'])) {
           $loguname = mysqli_real_escape_string($conn,$_POST['username']);
           $logemail = mysqli_real_escape_string($conn,$_POST['email']);
           
           $sql = "SELECT * FROM $tblaccounts WHERE Username = '$loguname' and Email = '$logemail'";
        
           $result = mysqli_query($conn,$sql);
           $row = mysqli_fetch_array($result,MYSQLI_ASSOC);
           
           $count = mysqli_num_rows($result);
           
           if($count === 1)
              {
                  $tmpusername = $row["Username"];
                  $tmppassword = $row["Password"];
                  
                  $strchar = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
                  $strnewpword = substr(str_shuffle($strchar), 0, 8);
                  
                  $sql = "UPDATE $tblaccounts SET Password = '$strnewpword' WHERE Username LIKE '$tmpusername'";
                  
                  if ($conn->query($sql) === TRUE)
                  {
                      
                  }
                  else
                  {
                     
                  }
                  
                  $_SESSION['regemail'] = $row["Email"];

                  $headers  = 'MIME-Version: 1.0' . "\r\n";
                  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

                  $sendername = "no-reply Set Semula Katalaluan";
                  $from = "no-reply@psn.gov.my";
                  $to = $_SESSION['regemail'];

                  $subject = "Set Semula Katalaluan!";

                  $headers .= 'From: no-reply@psn.gov.my '.$from."\r\n".'X-Mailer: PHP/' . phpversion();

                  $message = '<html><body>';
                  $message .= '<h3 style="color:#000;">Set Semula Katalaluan!</h3>';
                  $message .= '<p style="color:#000;"></p>';
                  $message .= '<p style="color:#000;"></p>';
                  $message .= '<p style="color:#000;">Tuan/Puan/Cik,</p>';
                  $message .= '<p style="color:#000;">Kata laluan anda telah ditetapkan semula.</p>';
                  $message .= '<p style="color:#000;"></p>';
                  $message .= '<p style="color:#000;">ID Pengguna : '.$tmpusername.'</p>';
                  $message .= '<p style="color:#000;">Katalaluan : '.$strnewpword.'</p>';
                  $message .= '<p style="color:#000;"></p>';
                  $message .= '<p style="color:#000;"><b>Please do not reply to this email. This mailbox is not monitored and you will not receive a response.</b></p>';
                  $message .= '<p style="color:#000;"></p>';
                  $message .= '<p style="color:#000;">Please contact PUSAT SAINS NEGARA at : <br>No Tel : 03-2089 3400<br>Email : eticketing@psn.gov.my</p>';

                  $message .= '</body></html>';

                  if(mail($to, $subject, $message, $headers)){
                      $msg = "Unable to send email. Please try again.";
                      echo "<script>alert('" . $msg . "');</script>";
                  } else{
                      $msg = "Your mail has been sent successfully.";
                      echo "<script>alert('" . $msg . "');</script>";
                      
                  }
              }

            else
              {
                  $msg = "Invalid Username/Email Details!";
                  echo "<script>alert('" . $msg . "');</script>";
              }
       }
   }
?>


<!DOCTYPE html>
<html lang="en" class="h-100">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, shrink-to-fit=9">
		<meta name="description" content="PSN cPanel">
		<meta name="author" content="PSN cPanel">
		<title><?php echo $title ?></title>
		
		<!-- Favicon Icon -->
        <link rel="icon" type="image/png" href="images/psn/logo.png">
		
		<!-- Stylesheets -->
        <?php include 'plugincss.php' ?>

	</head>

<body>				
	<div class="form-wrapper">
		<div class="app-form">
			<div class="app-form-sidebar">
				<div class="sidebar-sign-logo">
					
				</div>
				<div class="sign_sidebar_text">
					<center><h1>PUSAT SAINS NEGARA</h1></center>
				</div>
			</div>
			<div class="app-form-content">
				<div class="container">
					<div class="row justify-content-center">
                        <div class="col-lg-10 col-md-10">
                            <div class="app-top-items">
                                <a href="index.html">
                                    <div class="sign-logo" id="logo">
                                        <img src="images/logo.svg" alt="">
                                        <img class="logo-inverse" src="images/dark-logo.svg" alt="">
                                    </div>
                                </a>
                                <div class="app-top-right-link">
                                    <a class="sidebar-register-link" href="index.php"><i class="fa-regular fa-circle-left me-2"></i>Kembali ke Log Masuk</a>
                                </div>
                            </div>
                        </div>

						<div class="col-xl-7 col-lg-6 col-md-7">
                            <center><img src="images/psn/light-logo.png" alt="PSN" width="100%" /></center>
							<div class="registration">
                                    <form class="row g-3 needs-validation"  method="post" action="" novalidate>
									<h2 class="registration-title" style="align:center">Lupa Katalaluan</h2>
									<div class="form-group mt-5">
										<label class="form-label">Name Pengguna</label>
										<input class="form-control h_50" type="username" id ="username" name="username" value="">
									</div>
									<div class="form-group mt-4">
										<div class="field-password">
											<label class="form-label">Emel</label>
										</div>
                                        
										<div class="loc-group position-relative">
											<input class="form-control h_50" type="email" id="email" name="email">
										</div>
									</div>
									<button class="main-btn btn-hover w-100 mt-4" type="submit" id="resetpw" name="resetpw">Set Semula Katalaluan <i class="fa-regular fa-paper-plane"></i></button>
								</form>
								
							</div>							
						</div>
					</div>
				</div>
				<div class="copyright-footer">
					<?php echo $copyrights.$year.$copyrights_name ?>
				</div>
			</div>			
		</div>
	</div>

</body>
</html>
