<?php
require_once('source/session.php');
require_once('source/error.php');
require_once('source/dbconfig.php');
require_once('source/function.php');
require_once('source/restrict_admin.php');

if(isset($_POST['submit']))
{
    try {
        
        $fullname           = strip_tags($_POST['fullname']);
        $email              = strip_tags($_POST['email']);
        $gender             = strip_tags($_POST['gender']);
        $age                = strip_tags($_POST['age']);
        $class              = strip_tags($_POST['class']);
        $subclass           = strip_tags($_POST['subclass']);
        $phone              = strip_tags($_POST['phone']);
        
        $pass               = "3333";
        $passwd             = "3333";
        $passwrd            = md5(md5($salt . md5($passwd)) . $salt);
        $password           = md5(md5($salt . md5($pass)) . $salt);
        
        // Handle file upload
        $name = "default.jpg"; // Default image
        
        if(isset($_FILES['photo']) && $_FILES['photo']['error'] == UPLOAD_ERR_OK) {
            // Create directories if they don't exist
            if(!file_exists("profile_pic")) {
                mkdir("profile_pic", 0777, true);
            }
            
            // Generate unique filename
            $photo_name_random = md5(uniqid(rand(), true));
            $extension = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
            $photo_url = $photo_name_random . '.' . $extension;
            $location = "profile_pic/" . $photo_url;
            
            // Move uploaded file
            if(move_uploaded_file($_FILES['photo']['tmp_name'], $location)) {
                $name = $photo_url;
            }
        }
        
        if($passwrd != $password) {
            $_SESSION['msg'] = "incorrect";
            redirect('add_students.php');
        }
        
        // Check if email (admission number) already exists
        $STH = $DBH->prepare("SELECT count(*) FROM tbl_user WHERE email = ?");
        $STH->execute(array($email));
        $count_email = $STH->fetchColumn();
        
        if($count_email > 0) {
            $_SESSION['msg'] = "email";
            redirect('add_students.php');
        }
        
        // Insert new student
        sql($DBH,
            "INSERT INTO tbl_user (profile_pic, fullname, email, password, gender, age, registration_no, class, subclass, phone, registration_date, hash, status, teacher)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
            array($name, $fullname, $email, $password, $gender, $age, $email, $class, $subclass, $phone, $DATE_NOW, $token, "Active", "false"));
        
        // Send email notification
        $verify = "$pass";
        $subject = 'STUDENT REGISTRATION - VERBUM DEI INT\'L COLLEGE';
        
        $message = '<html>
                        <head>
                          <title>Student Registration</title>
                        </head>
                        <body>
                          <p>Dear Admin,</p>
                          <p>A new student has been registered with the following details:</p>
                          <p><b>Name:</b> '.$fullname.'</p>
                          <p><b>Admission Number:</b> '.$email.'</p>
                          <p><b>Class:</b> '.$class.' '.$subclass.'</p>
                          <p>The default access key is: '.$verify.'</p>
                          <p>Student can login at: https://portal.vda.com.ng/student-login/</p>
                        </body>
                    </html>';
        
        $headers[] = 'MIME-Version: 1.0';
        $headers[] = 'Content-type: text/html; charset=iso-8859-1';
        $headers[] = 'To: <'.$email.'>';
        $headers[] = "From: VERBUM DEI INT'L COLLEGE <info@portal.vda.com.ng>";
        
        mail($email, $subject, $message, implode("\r\n", $headers));

        $_SESSION['msg'] = "signup";
        redirect('add_students.php');
        exit;
    }
    catch(PDOException $e) { 
        file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
        $_SESSION['msg'] = "error";
        redirect('add_students.php');
    }
}
else
{
    $_SESSION['msg'] = "error";
    redirect('add_students.php');
}