<?php
session_start();
include 'includes/database.php';
include 'includes/functions.php';
$conn= new Functions();

if(!empty($_POST['name'])
    && !empty($_POST['class'])
    && !empty($_POST['section']) && !empty($_POST['sex'])
    && !empty($_POST['username']) && !empty($_POST['reg_no'])){
    $name = preg_replace('/[^\w\s]+/', '', $_POST['name']);
    $name = trim(preg_replace('/\s+/', ' ', $name));
    $sex = $_POST['sex'];
    $reg_no = $_POST['reg_no'];
    $country = $_POST['country'];
    $state = $_POST['state'];
    $section = $_POST['section'];
    $class = $_POST['class'];
    $email = $_POST['email'];
    $dob = $_POST['dob'];
    $gender = $_POST['sex'];
    $religion = $_POST['religion'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $photo_name = $_FILES['photo']['name'];
    $photo_tmpname = $_FILES['photo']['tmp_name'];
    $photo_size = $_FILES['photo']['size'];
    $username = preg_replace('/[^\w\s]+/', '', $_POST['username']);
    $username = trim(preg_replace('/\s+/', ' ', $username));
    $generated_pin = rand(10000, 99999);

    $photofilenameAr = explode('.', $photo_name);
    $photo_extension = end($photofilenameAr);
    $photo_ext = strtolower($photo_extension);

    //check if image was selected
    if(!empty($photo_name)) {
        if ($photo_size > 2000000) {
            echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fa fa-ban'></i>  Photo size exceeded. Max size is 2MB.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
           </p>";
           return false;
        }
        if ($photo_ext == 'png' || $photo_ext == 'jpg' || $photo_ext == 'jpeg' || $photo_ext == 'svg') {
            $strgPath = "upload/" . $photo_name;
            if (file_exists($strgPath)) {
                echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fa fa-ban'></i>  Photo with the same name already exists.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
           </p>";
            } else {
                $photo_upload = move_uploaded_file($photo_tmpname, $strgPath);
            }
        } else {
            echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fa fa-ban'></i> Only png, jpg, jpeg or svg formats are allowed. Photo did not upload.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
           </p>";

        }
    }

    //perform validations for email
    //check for email availability
    $sql = "SELECT * FROM student WHERE username =:username AND username != ''";
    $conn->query($sql);
    $conn->bind(":username", $username);
    if($conn->rowCount() > 0){
        echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
         <i class='fa fa-ban'></i> Username already exists.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
           </p>";
        return false;
    }
    $sql = "SELECT * FROM student WHERE name =:name AND name != ''";
    $conn->query($sql);
    $conn->bind(":name", $name);
    if($conn->rowCount() > 0){
        echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
         <i class='fa fa-ban'></i> Name already exists.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
           </p>";
        return false;
    }
      //check if Reg no already exists
       $sql = "SELECT * FROM student WHERE registerNO =:reg_no AND registerNO != ''";
    $conn->query($sql);
    $conn->bind(":reg_no", $reg_no);
    if($conn->rowCount() > 0){
        echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
         <i class='fa fa-ban'></i> Registration no already exists.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
           </p>";
        return false;
    }
    //check for email availability
    $sql = "SELECT * FROM student WHERE email =:email AND email != ''";
    $conn->query($sql);
    $conn->bind(":email", $email);
    if($conn->rowCount() > 0){
        echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fa fa-ban'></i> Email already exists.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
           </p>";
        return false;
    }else {

        //process  inserting of data into database
        $sql = "INSERT INTO student (name, sex, classesid, sectionid, username, country, 
            dob, religion, email, phone, address, state, registerNO, photo, exam_pin) 
          VALUES(:name, :sex, :classesid, :sectionid, :username, :country, 
            :dob, :religion, :email, :phone, :address, :state, :register_no, :photo, :exam_pin)";
        $conn->query($sql);
        $conn->bind(":name", $name);
        $conn->bind(":sex", $sex);
        $conn->bind(":classesid", $class);
        $conn->bind(":sectionid", $section);
        $conn->bind(":username", $username);
        $conn->bind(":country", $country);
        $conn->bind(":dob", $dob);
        $conn->bind(":religion", $religion);
        $conn->bind(":email", $email);
        $conn->bind(":phone", $phone);
        $conn->bind(":address", $address);
        $conn->bind(":state", $state);
        $conn->bind(":register_no", $reg_no);
        $conn->bind(":photo", $photo_name);
        $conn->bind(":exam_pin", $generated_pin);

        $send = $conn->execute();
        if ($send) {
            echo "<p class='alert alert-success alert-dismissible fade show' role='alert'>
                    <i class='fa fa-check-circle'></i> Student $name was added successfully.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='3; student'>
           </p>";
        } else {
            echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fa fa-ban'></i> An error occurred while adding data.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='4; student'>
           </p>";
        }
    }


}else{
    echo "<p class='alert alert-danger alert-dismissible fade show text-center' role='alert'>
              <i class='fa fa-ban'></i>
               Fields marked (*) are required.
                  <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
                             <span aria-hidden='true'>×</span>
                  </button>
           </p>";
}



