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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width= , initial-scale=1.0">
    <title>Document</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">
                            <input type="text" name="dept" class="form-control" placeholder="Department" required>
                       </div>
                   </div>
                   <div class="row form-group">
                       <div class="col-md-6">
                            <input type="text" name="cate" class="form-control" placeholder="Category" required>
                       </div>
                       <div class="col-md-6">
                            <input type="text" name="design" class="form-control" placeholder="Designation" required>
                       </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="">Passport</label>
                             <input type="file" name="good" class="form-control" placeholder="goodport" required>
                       </div>
                       <div class="col-md-6">
                        <label for="">CV</label>
                             <input type="file" name="cv" class="form-control" placeholder="CV" required>
                       </div>
                   </div>                                                             
                   
                    <button type="submit" name="staffsubmit" class="btn btn-success btn-block">Submit</button>
                    <div class="col-md-6">
                        
                        </div>
</div>
    </form>
    <?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'];
        
        // Directory where files will be saved
        $target_dir = "uploads/";
        
        // Target file paths
        $target_good = $target_dir . basename($good);
        $target_cv = $target_dir . basename($cv);
        
        // Move uploaded files to the target directory
        move_uploaded_file($_FILES['good']['tmp_name'], $target_good);
        move_uploaded_file(     $_FILES['cv']['tmp_name'], $target_cv);

        // Insert data into database including file paths
        $query = "INSERT INTO staff_reg (fname, dept, cate, design, academic, publi, passport, cv) 
                  VALUES ('$fname', '$dept', '$cate', '$design', '$academic', '$publi', '$target_good', '$target_cv')";
                  
        if (mysqli_query($db, $query)) {
            echo "Successfully registered";  
        } else {
            echo "Registration not successful";  
        }
    }
    ?>
</body>
</html>
