<?php
session_start();
require_once('mysqli_con.php');
$errors = array();
?>
<style>
.error {
  width: 92%;
  margin: 0px auto;
  padding: 10px;
  border: 1px solid #a94442;
  color: #a94442;
  background: 5px;
  text-align: left;
}
</style>

<style>
.success {
  width: 92%;
  margin: 0px auto;
  padding: 10px;
  border: 1px solid #a94442;
  color: #a94442;
  background: 5px;
  text-align: left;
}
</style>

<?php
if(isset($_POST['Register'])){
$userName = $_POST["username"];
$pass = $_POST["pw"];
$passw = $_POST["pw2"];

$sqlCheck = "SELECT * FROM adminlog WHERE AdminUsername = '$userName' LIMIT  1";
$result = mysqli_query($conn, $sqlCheck);
$existed = mysqli_fetch_assoc($result);

     if($existed ['AdminUsername'] === $userName){
       array_push($errors, "Email already exist. Please try another email. ");
       ?>
       <meta http-equiv="refresh" content="2;URL=registerAdmin.php" />
        <?php
        }else if ($pass != $passw) {
        array_push($errors, "The two password do not match. Please try again.");
        ?>
        <br>
        <meta http-equiv="refresh" content="2;URL=registerAdmin.php" />
        <?php   }


  if(count($errors) > 0){
  ?>

  <div class = "error">
  <?php
  foreach ($errors as $errors)
  echo $errors; ?>
  </div>

  <meta http-equiv="refresh" content="2;URL=registerAdmin.php" />
  <?php
}

else if(count($errors) == 0){

$sql = "INSERT INTO adminlog (AdminUsername,password)
VALUES ('$userName','$pass')";
mysqli_query ($conn,$sql);
?>
<div class = "success">
<?php
echo "Successful Registered ";
?>
</div>
    <meta http-equiv="refresh" content="2; URL='admin.php'" >
  <?php } else { ?>
  <?php
    echo "Please Register Again"?>
    <meta http-equiv="refresh" content="2; URL='registerAdmin.php'" >
<?php}}?>
