<?php 
function connect(){
$dbc = mysqli_connect("localhost", "bowensta_bowen", "80622BOWen") or die('No server');
	mysqli_select_db($dbc, "bowensta_sshub") or die('No db');
	return $dbc;

}


session_start();

require 'init.php';

$username = $_SESSION['staff_reg'];
$dbc = connect();
$query1 = "SELECT * FROM nonacadstaffs WHERE staffsId='$username' AND password IS NULL";

$run1 = mysqli_query($dbc, $query1);
$num = mysqli_num_rows($run1);
if($num <= 0){


	header('Location: ../../index.php');
}

function getdeviceip(){
	// Get real visitor IP behind CloudFlare network
    if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
              $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
              $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
    }
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}

function calogout($username){
	$dbc = connect();
	
		$timestamp = strtotime("now");
	$did = getdeviceip();
		mysqli_query($dbc, "INSERT into logs(action,actor,message,lastseen, deviceid) VALUES('Logout','$username', 'User logged out', '$timestamp','$did' )");
		
	}

function currentSession(){
	$year=date('Y');
	$month=date('n');
	switch($month){
		case 9: return(($year).'/'.($year+1)); //current year and next year
		case 10: return(($year).'/'.($year+1)); //current year and next year
		case 11: return(($year).'/'.($year+1)); //current year and next year
		case 12: return(($year).'/'.($year+1)); //current year and next year
		case 1: return(($year-1).'/'.($year)); //previous year and current year
		case 2: return(($year-1).'/'.($year)); //previous year and current year
		case 3: return(($year-1).'/'.($year)); //previous year and current year
		case 4: return(($year-1).'/'.($year)); //previous year and current year
		case 5: return(($year-1).'/'.($year)); //previous year and current year
		case 6: return(($year-1).'/'.($year)); //previous year and current year
		case 7: return(($year-1).'/'.($year)); //previous year and current year
		case 8: return(($year-1).'/'.($year)); //previous year and current year
 	}
	
}

?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Staff | Registration Page</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.7 -->
  <link rel="stylesheet" href="../../bower_components/bootstrap/dist/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../../bower_components/font-awesome/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="../../bower_components/Ionicons/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../../dist/css/AdminLTE.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="../../plugins/iCheck/square/blue.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->

  <!-- Google Font -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
</head>
<body class="hold-transition register-page">
<div class="register-box" style="margin-top: -30px;">
  <div class="register-logo">
       <span class="logo-lg"><img src="../../images/logo.png" width="100%"></span>
  </div>

<?php 
if(isset($_POST['register'])){
	$username;
	$title = $_POST['title'];
	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	$middlename = $_POST['midname'];
	$sex = $_POST['sex'];
	$dob = $_POST['dob'];
	$email = $_POST['email'];
	//$level = $_POST['stafflevel'];
	//$dept = $_POST['depts'];
	$fac = $_POST['fac'];
	$phone = $_POST['phone'];
	
	$password = $_POST['password'];
	
	
	
	$pre = "http://".$_SERVER['SERVER_NAME'];
				 $newfilename = strtotime("now");
	
	    $file_name = $_FILES['passport']['name'];
      $file_size =$_FILES['passport']['size'];
     
      $file_type=$_FILES['passport']['type'];
      
      

$tmp = explode('.', $file_name);
$ext = end($tmp);
      $dir = "../../profilepictures/$newfilename.$ext";
     
      
      
     $hashed = password_hash("$password", PASSWORD_ARGON2I);
       
		 move_uploaded_file($_FILES["passport"]["tmp_name"], $dir);
        
      $ndr = $pre."/profilepictures/$newfilename.$ext";
	
	 $query = "UPDATE nonacadstaffs SET title='$title',firstname='$firstname',lastname='$lastname',middlename='$middlename',sex='$sex',day='$dob',month='$dob',email='$email',faculty='$fac',picture='$ndr',password='$password', phonenumber='$phone', hashedPass = '$hashed' WHERE staffsId='$username'";
	$dbc = connect();
	mysqli_query($dbc, $query);
	echo mysqli_error($dbc);
	
	
	
	
		echo "<script type='text/javascript'>alert('Regitration Successful')</script>";
	
		calogout($username);
	
	
	
	
	
	

	echo "<script>
	window.location.replace('../../index.php');
	</script>";
	
	
	



//header('Location:../../index.php');
}

?>
  <div class="register-box-body">
    <p class="login-box-msg">Staff Registration Page</p>

    <form action="registerstaff.php" method="post" enctype="multipart/form-data">
      <div class="form-group has-feedback">
          <label for="title">Title</label>
	  <select class="form-control" name="title" id="title" >
		<option>Mr</option>
		<option>Miss</option>
		<option>Mrs</option>
		<option>Dr</option>
		<option>Prof</option>
		
	  </select>
       
       
      </div>
	  <div class="form-group has-feedback">
      <label for="firstname">Firstname</label>  <input type="text" class="form-control" placeholder="Firstname" id="firstname" name="firstname" required>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
	  <div class="form-group has-feedback">
       <label for="othernames">Othernames</label> <input type="text" class="form-control" placeholder="Other Names" id="othernames" name="midname" required>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
	  <div class="form-group has-feedback">
       <label for="lastname">Lastname</label> <input type="text" class="form-control" placeholder="Lastname" id="lastname" name="lastname" required>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>
	  
	   
	  <div class="form-group has-feedback">
       <label>Sex</label>
	   <br>
        <input type="radio"  name="sex" value="Male"> Male
		<span style='float: right;'><input type="radio"  name="sex" value="Female"> Female</span>
      
      </div>
	  <div class="form-group has-feedback">
      <label for="dob">Date of Birth</label> <input id="dob" type="date" class="form-control" placeholder="DOB" name="dob" required>
        <span class="glyphicon glyphicon-calendar form-control-feedback"></span>
      </div>
	  
      <div class="form-group has-feedback">
       <label for="mail">Bowen University Email Address</label><input type="email" class="form-control" id="mail" onchange="checkemail()" name="email" required placeholder="Bowen Univerity Email">
        
        <div id="error_mail"><font color="red">Unrecognized Email Address</font></div>
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
      </div>
      <div class="form-group has-feedback">
          <label for="phone">Phonenumber</label>
        <input type="number" class="form-control" id="phone" name="phone" placeholder="234***********" required>
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
      </div>
	
	  <div class="form-group ">
	 <label for="fac">Directorate</label> <select class="form-control" name="fac" id="fac"  >
		<?php 
		$query = "SELECT unitname FROM units WHERE unitcode='$unit'";
		$run = mysqli_query($dbc, $query);
		$unitname = mysqli_fetch_row($run)[0];
		
			echo "<option value='$unit' selected>$unitname</option>";
		?>
		
		
	  
      </select>
         
      </div>
	  
	  <div class="form-group">
	
       <label for="passport">Profile Picture</label> <input type="file" id="passport" name="passport" placeholder="Profile Image" required>
        
         <div id="error_image"><font color="red">File is not a picture</font></div>
      </div>
	  
	  
	 
	  
      <div class="form-group has-feedback">
        <input type="password" class="form-control" minlength="6" name="password" placeholder="Password" required>
       
      </div>
      <div class="form-group has-feedback">
        <input type="password" class="form-control" minlength="6" placeholder="Retype password" required>
      
      </div>
       
	  
      <div class="row">
       
        <div class="col-xs-4">
          <button type="submit" id="register" name="register" class="btn btn-primary btn-block btn-flat">Register</button>
        </div>
     
      </div>
    </form>

    

   
  </div>
  <!-- /.form-box -->
</div>
<!-- /.register-box -->

<!-- jQuery 3 -->
<script src="../../bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="../../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="../../plugins/iCheck/icheck.min.js"></script>
<script>


 
  
  $("#error_mail").hide(); 
  $("#error_image").hide(); 
 
  function checkimage(){
      
  }
  function checkemail(){
      var txt = $('#mail').val();
       var n = txt.indexOf("@");
        var t = txt.length;
        var res = txt.substr(n, t);
        
        if(res === "@bowen.edu.ng"){
             $("#error_mail").hide(); 
             $("#register").attr("disabled", false);
        }else{
          
            $("#error_mail").show(); 
             $("#register").attr("disabled", true);
        }
      
      
  }
  
  
  $('#passport').on('change', function() {
  Object.values(this.files).forEach(function(file) {
   // console.log(`Type: ${file.type}`);
    var vidtype = ['image/gif', 'image/jpeg', 'image/png'];
    var len = vidtype.length;
    for(var i = 0; i < len; i++){
        
        if(file.type == vidtype[i]){
           // alert("Am here");
             $("#error_image").hide();
		  $("#register").attr("disabled", false);
		  return false;
        }else{
             $("#error_image").show();
		  $("#register").attr("disabled", true);
        }
    }
  })
})
</script>

</body>
</html>
