<?php
// Start the session
session_start();
?>


 <!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
    <title>Parents : login</title>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
  
  <style>
.error {color: #FF0000;}
body {
  background-color: aqua;
}
</style>

  
  </head>
  <body class="p-3 m-0 border-0 bd-example m-0 border-0">
      
      
      <?php
            $cellnumErr = $pass1Err = "";
            
            
            $cellnum = $pass1 = "";
            
            $counter = 0;
      
      
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
          
     
     //codes for cellnum
  if (empty($_POST["cellnum"])) {
    $cellnumErr = "Cell number is required";
  } else {
    $cellnum = test_input($_POST["cellnum"]);
    $celllen = strlen($cellnum);
    $cell = "9999999999";
    $cell = strlen($cell);
    
    if ($celllen === $cell){
        $counter++;
    }else {
        $cellnumErr = "Cell number must be 10 digits";
    }
    
  }
     
      //codes for password 1
  if (empty($_POST["pass1"])) {
    $pass1Err = "Password is required";
  } else {
    $pass1 = test_input($_POST["pass1"]);
    $counter++;
  }
     
     
     
     
  }
      
    function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}  
      
      
      
   
      ?>
      
      

    <!-- Example Code -->
 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    
       <center><h3>Login Details : PARENTS</h3></center>
        <div class="form-floating mb-3">
  <input type="text" class="form-control" name="cellnum" id="cellnum" placeholder="Cell number" value="<?php echo $cellnum;?>" required minlength="10" maxlength="10" >
  <span class="error">* <?php echo $cellnumErr;?></span>
  
  <label for="cellnum">Cell number</label>
</div>
  
  
  <div class="form-floating mb-3">
  <input type="password" class="form-control" name="pass1" id="pass1" placeholder="Password" value="<?php echo $pass1;?>" required autofocus minlength="3" maxlength="30">
  <span class="error">* <?php echo $pass1Err;?></span>
  <br>
  <label for="surname">Password</label>
</div>
  
  <div class="d-grid gap-2 col-6 mx-auto">
  <button type="submit" class="btn btn-primary" type="button">Login</button>
  
</div>
  
</form> 
<!-- End Example Code -->

    <?php
    if ($counter === 2 ){
        
        
        //salting
        
        $saltpass = "masiutefoalby is a programmer".$pass1;
        
        //hassed
       $hashedpass = hash('sha256', $saltpass);
      
        
        //connecting to the database
        
        $servername = "localhost";
        $username = "commemay_sono";
        $password = "Masiu@5$5$5$";
        $dbname = "commemay_csc";

        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
              die("Connection failed: " . $conn->connect_error);
        }
        
        
        $sql = "SELECT * FROM csc WHERE cellnum = ?  AND pass = ? ";
        $stmt = $conn->prepare($sql); 
        $stmt->bind_param("ss", $cellnum,$hashedpass);
        $stmt->execute();
        $result = $stmt->get_result();
        
        
        if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
         
 //setting the session variables
        
        $_SESSION["num"]= $row["num"];
        $_SESSION["title"]= $row["title"];
        $_SESSION["surname"]= $row["surname"];
        $_SESSION["cellnum"]= $row["cellnum"];
        $_SESSION["whatsapp"]= $row["whatsapp"];
        $_SESSION["email"]= $row["email"];
        $_SESSION["to1"]= $row["to1"];
        $_SESSION["to2"]= $row["to2"];
        $_SESSION["to3"]= $row["to3"];
        $_SESSION["to4"]= $row["to4"];
        $_SESSION["to5"]= $row["to4"];
        $_SESSION["to6"]= $row["to6"];
        $_SESSION["date"]= $row["date"];
        
        
        //variable for edu portal
        
        $_SESSION["user"]= "Parent";
        
        echo ("<script LANGUAGE='JavaScript'>
        window.location.href='http://www.commenta.co.za/parentportal.php';
    </script>");
    
        }
    
        }   else {
         echo ("<script LANGUAGE='JavaScript'>
        window.alert('Incorrect username/password. Please try again.');
        window.location.href='http://www.commenta.co.za/myparentslogin.php';
        </script>");
             
        
    }
       


    $conn->close();
        
          }



    ?>


  </body>
</html>