<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once '../auth/auth.php';
require_once '../models/db_connection.php';
require_once '../models/student_model.php';
require_once '../models/user_model.php';
$student =  new Student($conn);
$enrollId1 = $student->generateEnrollmentId();
if (isset($_POST["addStudent"])) {
  $fullname = $_POST['fullname'];
  $work = $_POST['work'];
  $education = $_POST['education'];
  $identNumber = $_POST['identNumber'];
  $gender = $_POST['gender'];
  $sensitiveCase = $_POST['sensitiveCase'];
  $eyeProblems = $_POST['eyeProblem'];
  $earProblems = $_POST['earProblem'];
  $address = $_POST['address'];
  $street = $_POST['street'];
   $age = $_POST['age'];
  $phoneNumber = $_POST['phoneNumber'];
    $user =  new User($conn);
  $userData = $user->getUserById($_SESSION['user']);
  $username = $userData[0]['username'];
// $enrollId = $student->generateEnrollmentId();
    $enrollId = $_POST['certNumber'];
     if($student->getStudentIdByEnrollmentId($enrollId) != null){
         header("Location: add_student.php?message=Student with enrollment id already exists");
        exit(); 
     }
    
 // Check if file was uploaded without errors
 if(isset($_FILES["image"]) && $_FILES["image"]["error"] == 0) {
  $allowed_types = array("image/jpeg", "image/png", "image/gif");
  $file_type = $_FILES["image"]["type"];
  if(in_array($file_type, $allowed_types)) {
      // Specify the directory where the file will be stored
      $upload_dir = "../uploads/";
       // Generate a random filename
        $random_string = uniqid();
        $file_name = $random_string . "_" . $_FILES["image"]["name"];
      // Move the uploaded file to the specified directory
      if(move_uploaded_file($_FILES["image"]["tmp_name"], $upload_dir . $file_name)) {
      } else {
          die('fileproblems'.$_FILES["image"]["error"]);
      }
  } else {
      die('notallowedfile');
  }
} else {
     die('no file selected');
}
$success = $student->createStudent($enrollId,$fullname,$identNumber,$street,$address,$work,$education,$gender,$phoneNumber,$eyeProblems,$earProblems,$sensitiveCase,$file_name,$username,$age);
if ($success) {
  echo "<script>
          alert('Student added successfully');
          window.location.href = 'view_students.php';
      </script>";
} else {
    die('student not added successfuly'.$success);
}
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
 <title>Student | Add Student </title>
 <?php include("../templates/header.php"); ?>
</head>
<body>
    <?php
      if(isset($_GET["message"])){
        $message = $_GET["message"];
        echo "<script>alert('".$message."')</script>";
      }
  ?>
  <div class="container-scroller">
    <!-- partial:partials/_navbar.html -->
    <?php include("../templates/nav.php"); ?>
    <!-- partial -->
    <div class="container-fluid page-body-wrapper">
      <!-- partial:partials/_sidebar.html -->
      <?php include("../templates/sidenav.php"); ?>
      <!-- partial -->
      <div class="main-panel">
        <div class="content-wrapper">
        <div class="col-12 grid-margin">
              <div class="card">
                <div class="card-body">
                  <h4 class="card-title">Add Student Form</h4>
                  <form class="form-sample" action="add_student.php"  method="POST" enctype="multipart/form-data">
                    <p class="card-description">
                      Personal info
                    </p>
                    <div class="row">
                      <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Fullname</label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" name="fullname" required>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Identification Number</label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" name="identNumber">
                          </div>
                        </div>
                      </div>
                           <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Certificate Number</label>
                          <div class="col-sm-9">
                            <input type="number" class="form-control" name="certNumber" required>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Gender</label>
                          <div class="col-sm-9">
                            <select class="form-control" name="gender">
                              <option>Male</option>
                              <option>Female</option>
                            </select>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Work</label>
                          <div class="col-sm-9">
                            <input class="form-control" placeholder="" name="work" required>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row">
                      
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Eye Problem</label>
                          <div class="col-sm-4">
                            <div class="form-check">
                              <label class="form-check-label">
                                <input type="radio" class="form-check-input" name="eyeProblem" id="membershipRadios1" value="No" checked="">
                                No
                              <i class="input-helper"></i></label>
                            </div>
                          </div>
                          <div class="col-sm-5">
                            <div class="form-check">
                              <label class="form-check-label">
                                <input type="radio" class="form-check-input" name="eyeProblem" id="membershipRadios2" value="Yes">
                                Yes
                              <i class="input-helper"></i></label>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Ear Problem</label>
                          <div class="col-sm-4">
                            <div class="form-check">
                              <label class="form-check-label">
                                <input type="radio" class="form-check-input" name="earProblem" id="membershipRadios1" value="No" checked="">
                                No
                              <i class="input-helper"></i></label>
                            </div>
                          </div>
                          <div class="col-sm-5">
                            <div class="form-check">
                              <label class="form-check-label">
                                <input type="radio" class="form-check-input" name="earProblem" id="membershipRadios2" value="Yes">
                                Yes
                              <i class="input-helper"></i></label>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Sensitive case</label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" name="sensitiveCase" required>
                          </div>
                        </div>
                      </div>
                    </div>
                    <hr>
                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Address </label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" name="address" required>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Street</label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" name="street" required>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Phone Number</label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" name="phoneNumber" required>
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Education</label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" name="education" required>
                          </div>
                        </div>
                      </div>

                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Passport</label>
                          <div class="col-sm-9">
                            <input type="file" class="form-control" name="image" accept="image/*"  required>
                          </div>
                        </div>
                      </div>
                       <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Age</label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" name="age" required>
                          </div>
                        </div>

                    </div>
                    <div class="row">
                      <div class="col-md-6">
                        <div class="form-group row">
                         
                          <div class="col-sm-9">
                          <button type="submit" name="addStudent" class="btn btn-dark btn-lg btn-block">Add Student</button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </form>
                </div>
              </div>
            </div>
        </div>
        <!-- content-wrapper ends -->
        <!-- partial:partials/_footer.html -->
        <?php include("../templates/footer.php"); ?>
        
        <!-- partial -->
      </div>
      <!-- main-panel ends -->
    </div>
    <!-- page-body-wrapper ends -->
  </div>
    <?php include("../templates/inc_scripts.php"); ?>
</body>

</html>

