<?php
    include("inc/db.php");

    if(isset($_POST['usersubmit'])) {
    
        // Initialize variables
        $fname = mysqli_real_escape_string($db, $_POST['fname']); 
        $sname = mysqli_real_escape_string($db, $_POST['sname']); 
        $mat = mysqli_real_escape_string($db, $_POST['mat']);
        $program = mysqli_real_escape_string($db, $_POST['program']);
        $sessiond = mysqli_real_escape_string($db, $_POST['sessiond']);
        $school = mysqli_real_escape_string($db, $_POST['school']);
        $Dept = mysqli_real_escape_string($db, $_POST['Dept']);
        $leveld = mysqli_real_escape_string($db, $_POST['leveld']);
        $sex = mysqli_real_escape_string($db, $_POST['sex']);
        $dob = mysqli_real_escape_string($db, $_POST['dob']);
        $email = mysqli_real_escape_string($db, $_POST['email']);
        $phone = mysqli_real_escape_string($db, $_POST['phone']);
        $addressd = mysqli_real_escape_string($db, $_POST['addressd']);
        $pbirth = mysqli_real_escape_string($db, $_POST['pbirth']);
        $sorigin = mysqli_real_escape_string($db, $_POST['sorigin']);
        $lgovt = mysqli_real_escape_string($db, $_POST['lgovt']);
        $pgname = mysqli_real_escape_string($db, $_POST['pgname']);
        $pgnumb = mysqli_real_escape_string($db, $_POST['pgnumb']);
    
        // Check if the surname or email already exist
        $checkQuery = "SELECT * FROM portal WHERE sname = '$sname' OR mat = '$mat'";
        $checkResult = mysqli_query($db, $checkQuery);
    
        if(mysqli_num_rows($checkResult) > 0) {
            echo "<script>alert('Surname or matric number already exist. Please use a different surname or matric number.')</script>";
        } else {
            // Proceed with the insertion
            $insertQuery = "INSERT INTO portal (fname, sname, mat, program, sessiond, school, Dept, leveld, sex, dob, email, phone, addressd, pbirth, sorigin, lgovt, pgname, pgnumb) 
                            VALUES ('$fname', '$sname', '$mat', '$program', '$sessiond', '$school', '$Dept', '$leveld', '$sex', '$dob', '$email', '$phone', '$addressd', '$pbirth', '$sorigin', '$lgovt', '$pgname', '$pgnumb')";
            
            $insertResult = mysqli_query($db, $insertQuery);
            
            if($insertResult) {
                echo "<script>alert('Registration successful')</script>";
                header("location: index.php");
                exit();
            } else {
                echo "<script>alert('Registration failed. Please try again.')</script>";
            }  
        }
    }
    ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Portal</title>
    <link rel="stylesheet" href="bootstrap-4.6.2-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="styles.css">

    <link rel="shortcut icon" href="../Image/cisn-logo.png" type="image/x-icon" />
    <link rel="apple-touch-icon" href="../Image/cisn-logo.png">
</head>
<body>
<p class="title"></p>
            <p align="center" >
                <img src="images/cisn-logo.png" alt="" width="10%" height="10%" >
            </p>
            <h4 align="center">REGISTRATION</h4>
     <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <form action="register.php" method="POST" enctype="multipart/form-data">
                <h5 style="text-align:center;margin-bottom:40px;">Please Fill in Capital Letters</h5>
                <div class=" row form-group">
                <div class="col-md-6">
                    <label for="name"> First name</label>
                    <input type="text" name="fname" class="form-control" required>
                </div>
                <div class="col-md-6">
                    <label for="name">Surname</label>
                    <input type="text" name="sname" class="form-control" required>    
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                <label for="name">Matric Number</label>
                    <input type="text" name="mat" class="form-control" required>  
                </div>
                <div class="col-md-6">
                <label for="name"> Program type</label>
                   <select name="program" id="" class="form-control" required>
                    <option value="Select program type">Select program type</option>
                    <option value="ND Full TIme">Full Time</option>
                    <option value="ND Part Time">Part Time</option>
                    
                   </select>
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                <label for="name">Session</label>
                   <select name="sessiond" id="" class="form-control" required>
                    <option value="Select session">Select session</option>
                    <option value="2022/2023">2022/2023</option>
                    <option value="2023/2024">2023/2024</option>
                   </select>   
                </div>
                <div class="col-md-6">
                <label for="name">School</label>
                   <select name="school" id="" class="form-control" required>
                    <option value="Select School">Select School</option>
                    <option value="Science">Science</option>
                    <option value="Management">Management</option>
                    <option value="Engineering">Engineering</option>
                   </select>
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                <label for="name">Department:</label>
                   <select name="Dept" id="" class="form-control" required>
                    <option value="Select Department">Select Department</option>
                    <option value="Shipping MGT HND">Shipping MGT HND</option>
                    <option value="Shipping MGT NID">Shipping MGT NID</option>
                    <option value="Shipping MGT ASS">Shipping MGT ASS</option>
                    <option value="Shipping MGT ASS Sec">Shipping MGT ASS Sec</option>
                     <option value="Shipping PGD">Shipping PGD</option>
                      <option value="Engineering NID">Engineering NID</option>
                      <option value="Security MGT NID SEC">Security MGT NID SEC</option>
                   </select>   
                </div>
                <div class="col-md-6">
                <label for="name">Level:</label>
                   <select name="leveld" id="" class="form-control" required>
                    <option value="name0">Select Level</option>
                    <option value="HND 1 MGT">HND 1 MGT</option>
                    <option value="HND 2 MGT">HND 2 MGT</option>
                    <option value="HND 3 MGT">HND 3 MGT</option>
                    <option value="HND 4 MGT">HND 4 MGT</option>
                    
                      <option value="NID 1 MGT">NID 1 MGT</option>
                      <option value="NID 2 MGT">NID 2 MGT</option>
                      <option value="NID 3 MGT">NID 3 MGT</option>
                      <option value="NID 4 MGT">NID 4 MGT</option>
                      
                      <option value="ASS 1 MGT">ASS 1 MGT</option>
                      <option value="ASS 2 MGT">ASS 2 MGT</option>
                      <option value="ASS 3 MGT">ASS 3 MGT</option>
                      <option value="ASS 4 MGT">ASS 4 MGT</option>
                      
                      <option value="ASS 1 SEC">ASS 1 SEC</option>
                      <option value="ASS 2 SEC">ASS 2 SEC</option>
                      <option value="ASS 3 SEC">ASS 3 SEC</option>
                      <option value="ASS 4 SEC">ASS 4 SEC</option>
                      
                      <option value="PGD 1">PGD 1</option>
                      <option value="PGD 2">PGD 2</option>
                      
                      <option value="NID 1 ENG">NID 1 ENG</option>
                      <option value="NID 2 ENG">NID 2 ENG</option>
                      <option value="NID 3 ENG">NID 3 ENG</option>
                      <option value="NID 4 ENG">NID 4 ENG</option>
                      
                      <option value="NID 1 SEC">NID 1 SEC</option>
                      <option value="NID 2 SEC">NID 2 SEC</option>
                      <option value="NID 3 SEC">NID 3 SEC</option>
                      <option value="NID 1 SEC">NID 4 SEC</option>
                      
                      <option value="STS 1">STS 1</option>
                      <option value="STS 2">STS 2</option>
                      <option value="STS 3">STS 3</option>
                      <option value="STS 4">STS 4</option>
                   </select>
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                <label for="name">Sex</label>
                   <select name="sex" id="" class="form-control" required>
                    <option value="Select">Select</option>
                    <option value="MALE">Male</option>
                    <option value="FEMALE">Female</option>
                    
                   </select>   
                </div>
                <div class="col-md-6">
                <label for="name">Date of Birth</label>
                    <input type="date" name="dob" class="form-control" required>
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                <label for="name">Email</label>
                    <input type="email" name="email" class="form-control" required>    
                </div>
                <div class="col-md-6">
                <label for="name">Phone</label>
                    <input type="text" name="phone" class="form-control" required>
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                <label for="name">Residential Address</label><br>
                   <textarea cols="33" name="addressd" class="form-control" required></textarea>
                </div>
                <div class="col-md-6">
                <label for="name">Place of Birth</label>
                    <input type="text" name="pbirth" class="form-control" required>
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                <label for="name">State of Origin</label>
                    <input type="text" name="sorigin" class="form-control" required>    
                </div>
                <div class="col-md-6">
                <label for="name">Local Government</label>
                    <input type="text" name="lgovt" class="form-control" required>  
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                    <label for="name">Parent/Guardian Name</label>
                    <input type="text" name="pgname" class="form-control" required>
                </div>
                <div class="col-md-6">
                    <label for="name">Parent/Guardian Phone number</label>
                    <input type="text" name="pgnumb" class="form-control" required>    
                </div>
            </div>
            <div class=" row form-group">
                <div class="col-md-6">
                    <!-- <label for="name">Passport</label>
                    <input type="file" name="passport" class="form-control" required> -->
                   
                </div>
                <div class="col-md-6">
                    <!-- <label for="name">Parent/Guardian Phone number</label>
                    <input type="text" name="pgnumb" class="form-control">  -->  
                </div> 
            </div>
            <button type="submit" name="usersubmit" class="btn-success btn-block ">REGISTR</button>
            <p style="margin-bottom:50px;">Already have an account <a href="index.php">Login Here</a></p>
            </form>
            
        </div>
        <div class="col-md-2"></div>
     </div>
    
</body>
</html>
