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

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>STAFF REGISTRATION</title>
    <link rel="stylesheet" href="bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <form action="staff_reg.php" method="POST" enctype="multipart/form-data">
        <h1 style="padding:70px;" align="center"><b>STAFF REGISTRATION</b></h1>
        <div class="row form-group">
            <div class="col-md-6">
                <input type="text" name="fname" class="form-control" placeholder="Full name" required>
            </div>
            <div class="col-md-6">
                <select name="dept" id="dept" class="form-control" required >
                    <option>Select Department</option>
                    <option>Computer Science</option>
                    <option>Science lab technology</option>
                    <option>Business Administration</option>
                    <option>Maritime transport and business studies</option>
                    <option>Computer Engineering Technology</option>
                    <option>Marine engineering technolog</option>
                    <option>Electrical/Electronics Engineering</option>
                 
                </select>
                
            </div>
        </div>
        <div class="row form-group">
            <div class="col-md-6">

            <select name="cate" id="cate" class="form-control" required >
                    <option>Select Category</option>
                    <option>Non- teaching (Junior)</option>
                    <option>Non- teaching (Senior)</option>
                    <option>Academic</option>
                    
                </select>
                
            </div>
            <div class="col-md-6">
            <select name="design" id="design" class="form-control" required >
                    <option>Select Designation</option>
                    <option>Lecturer I</option>
                    <option>Lecturer II</option>
                    <option>Lecturer III</option>
                    <option>Assistant Lecturer </option>
                    <option>Senior Lecturer </option>
                    <option>Assistant Lecturer </option>
                    <option>Chief Lecturer </option>
                    <option>Principal  Lecturer </option>
                    <option>Technologist I </option>
                    <option>Technologist II </option>
                    <option>secretary </option>
                </select>
                
            </div>
        </div>
        <div class="row form-group">
            <div class="col-md-6">
                <input type="text" name="academic" class="form-control" placeholder="Academic qualification" required>
            </div>
            <div class="col-md-6">
                <input type="text" name="publi" class="form-control" placeholder="Publication" required>
            </div>
        </div>
        <div class="row form-group">
            <div class="col-md-6">
                <label for="good">Passport</label>
                <input type="file" name="good" class="form-control" required>
            </div>
            <div class="col-md-6">
                <label for="cv">CV</label>
                <input type="file" name="cv" class="form-control" required>
            </div>
        </div>
        <button type="submit" name="staffsubmit" class="btn btn-success btn-block">Submit</button>
    </form>
</div>

<?php
if (isset($_POST['staffsubmit'])) {
    $fname = mysqli_real_escape_string($db, $_POST['fname']);
    $dept = mysqli_real_escape_string($db, $_POST['dept']);
    $cate = mysqli_real_escape_string($db, $_POST['cate']);
    $design = mysqli_real_escape_string($db, $_POST['design']);
    $academic = mysqli_real_escape_string($db, $_POST['academic']);
    $publi = mysqli_real_escape_string($db, $_POST['publi']);
    
    // File upload handling
    $good = $_FILES['good']['name'];
    $cv = $_FILES['cv']['name'];
    $good_tmp = $_FILES['good']['tmp_name'];
    $cv_tmp = $_FILES['cv']['tmp_name'];
    $good_type = strtolower(pathinfo($good, PATHINFO_EXTENSION));
    $cv_type = strtolower(pathinfo($cv, PATHINFO_EXTENSION));
    $good_size = $_FILES['good']['size'];
    $cv_size = $_FILES['cv']['size'];
    
    // Directory where files will be saved
    $target_dir = "uploads/";
    
    // Target file paths
    $target_good = $target_dir . basename($good);
    $target_cv = $target_dir . basename($cv);
    
    // Allowed file types
    $allowed_types = array('jpeg', 'png', 'jpg');
    $min_size = 1000000; // Minimum file size (1MB)
    
    $errors = array();
    
    // Validate passport file
    if (!in_array($good_type, $allowed_types)) {
        $errors[] = "Invalid passport file type. It must be a JPG, JPEG, or PNG image.";
    } elseif ($good_size > $min_size) {
        $errors[] = "Passport file size too small. It must be at least 1MB.";
    } else {
        // Move uploaded passport file to the target directory
        if (!move_uploaded_file($good_tmp, $target_good)) {
            $errors[] = "Error uploading passport file.";
        }
    }

    // Validate CV file
    if (!in_array($cv_type, $allowed_types)) {
        $errors[] = "Invalid CV file type. It must be a JPG, JPEG, or PNG image.";
    } elseif ($cv_size > $min_size) {
        $errors[] = "CV file size too small. It must be at least 1MB.";
    } else {
        // Move uploaded CV file to the target directory
        if (!move_uploaded_file($cv_tmp, $target_cv)) {
            $errors[] = "Error uploading CV file.";
        }
    }
    
    if (empty($errors)) {
        // Insert data into database including file paths
        $query = "INSERT INTO staff_reg (fname, dept, cate, design, academic, publi, good, cv) 
                  VALUES ('$fname', '$dept', '$cate', '$design', '$academic', '$publi', '$good', '$cv')";
        
        if (mysqli_query($db, $query)) {
            echo "Successfully registered";
        } else {
            echo "Registration not successful: " . mysqli_error($db);
        }
    } else {
        foreach ($errors as $error) {
            echo $error . "<br>";
        }
    }
}
?>
</body>
</html>
