<?php session_start();?>
<?php include'includes/header.php'; ?>
<?php include'includes/sidebar.php';?>
<?php include'includes/functions.php';?>
<?php include'includes/navbar.php'; ?>

<!-- Handle Add Slider -->
<style>
/*Core setup styles*/
.sideNav{
    height: 100%;
    /*width: 220px;*/
    position: fixed;
    z-index: 9;
}
.navbar{
    top: 0;
    position: fixed;
    z-index: 9999;
}
.the-links{
    padding-top: 10px;
}
/*.nav-links{
    height: 100%;
    overflow-y: auto;
}*/
.insider{
    margin-top: 10px;
    position: relative;
}
.card{
    height: auto;
}
.cat-pag{
    position: relative;
}
.cat-page-header{
    position: sticky;
    width: 100%;
    height: 50px;
    background:#F7F9FB; 
    top: 60px;
    right: 60px;
    z-index: 1;
}
@media(max-width: 992px){
.insider{
    margin-top: 10px;
    padding-left: 10px;
    padding-right: 10px;
    }        
}

  .negative{
    height: 500px;
  } 
  .card-header h5{
    font-size: 12px;
  }
}

.card{
    height: 500px;
}
.card-header h5{
  font-size: 13px;
  font-weight: bold;
}
.custom-fieldset-box{
  border: 1px solid #ccc;
  padding: 10px;
  border-radius: 5px;
  position: relative;
  margin-bottom: 20px;
  background: #F7F9FB;
}
.custom-field-title{
  position: absolute;
  top: -12px;
  left: 15px;
  background: #F7F9FB;
  padding: 0 10px;
  font-weight: bold;
  color: #1E5F67;
  font-size: 15px;
}

input{
  width: 100%;
  height: 50px;
  border:2px solid #ccc;
  outline: none;
  border-radius: 5px;
  padding-left: 10px;
  padding-right: 10px;
}
select{
  width: 100%;
  height: 50px;
  border:2px solid #ccc;
  outline: none;
  border-radius: 5px;
  padding-left: 10px;
  padding-right: 10px;
}
.avater{
    width: 60px;
    height: 60px;
    border:2px solid #ccc;
    border-radius: 50%;
}
</style>

<!-- Main page starts here -->
<div id="notification-containers" class="notification-containers"></div>
<div class="insider">
  <div class="container">
    <div class="cat-pag">
      <form action="" method="POST">
        <div class="cat-page-header d-flex justify-content-between align-items-center">
          <h5></h5>
          <div>
                          
            <button name="save-btn" 
              class="btn btn-sm 
              info text-white">
              <i class="fa fa-user-plus"></i> Add Staff
            </button>

          </div>
        </div>

        <div class="card mt-3">
          <div class="card-header" style="display: flex; align-items: center; justify-content: space-between;">
            <h5>ADD NEW STAFF</h5>
          </div>
          <div class="card-body" style="background: #F7F9FB;">
            <div class="row" style="margin-top: -10px;">

              <!-- Title -->
              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Title</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <select name="title" required="">
                        <option value="Mr.">Mr.</option>
                        <option value="Mrs.">Mrs.</option>
                        <option value="Miss">Miss</option>
                      </select>
                    </div>
                  </div>
                </div>
              </div>

              <!-- Othernames -->
              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Other Names</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="text" 
                      name="othernames" 
                      autocomplete="OFF" 
                      placeholder="Names"
                      required="" 
                      class="form-control">               
                    </div>
                  </div>
                </div>
              </div>

              <!-- Date of birth -->
              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Email Address</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="email" 
                      name="email"
                      autocomplete="OFF" 
                      required="" 
                      placeholder="Email Address" 
                      class="form-control">                  
                    </div>
                  </div>
                </div>
              </div>

              <!-- Gender -->
              <div class="col-lg-6 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Phone Number</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="number" 
                      name="mobile"
                      autocomplete="OFF" 
                      required="" 
                      placeholder="Mobile Number" 
                      class="form-control">                         
                    </div>
                  </div>
                </div>
              </div>

              <!-- Class -->
              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Select Role</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                     <select name="role" required="">
                       <option hidden="" >Select Role</option>
                       <option value="admin">Admin</option>
                       <option value="principal">Principal</option>
                       <option value="vice_principal">Vice Principal</option>
                       <option value="class_teacher">Class Teacher</option>
                       <option value="subject_teacher">Subject Teacher</option>
                     </select>                       
                    </div>
                  </div>
                </div>
              </div>

              <!-- New Password -->
              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">New Password</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="password" 
                      name="new_password"
                      placeholder="Set New Password" 
                      required="" 
                      class="form-control">                          
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-lg-4 mt-3">
                <div class="custom-fieldset-box">
                  <span class="custom-field-title">Confirm Password</span>
                  <div class="inner-cont">
                    <div class="input-wraps">
                      <input type="password"
                      required=""
                      placeholder="Enter password again" 
                      name="confirm" 
                      class="form-control">                          
                    </div>
                  </div>
                </div>
              </div>

              <div class="col-lg-12">
                <div class="box-overall">
                  <span>Select Class</span>
                  <div class="class-box">
                    <?php
                    $fetchClass = "SELECT * FROM classes";
                    $classStmt = $pdo->prepare($fetchClass);
                    $classStmt->execute();
                    while ($classRow = $classStmt->fetch(PDO::FETCH_ASSOC)) {
                        $allClass = $classRow['className'];
                    ?>
                    <label class="class-item">
                      <input type="radio" name="class" value="<?=$allClass?>">
                      <span><?=$allClass?></span>
                    </label>

                    <?php } ?>
                  </div>
                </div>
              </div>

            </div>
          </div>
        </div>
      </form>

      

    </div>
   
  </div>
</div>
<?php
if (isset($_POST['save-btn'])) {
  $title    = $_POST['title'];
  $email    = $_POST['email'];
  $name     = $_POST['othernames'];
  $mobile   = $_POST['mobile'];
  $roles    = $_POST['role'];
  $password = $_POST['new_password'];
  $confirm  = $_POST['confirm'];
  
  
  if ($password != $confirm) {
    $_SESSION['message'] = 'Password does not match';
    $_SESSION['type'] = 'error';
  }else{
    if (isset($_POST['class'])) {
        $class = $_POST['class'];
    }else{
        $class = '';
    }
    if ($roles == 'class_teacher' && $class == '') {
      $_SESSION['message'] = 'Please select class for class teacher';
      $_SESSION['type'] = 'error';
    }else{
      $valPass = password_hash($password, PASSWORD_DEFAULT);
      
      $insert = "INSERT INTO staff (title, fullname, email, phone_number, password, roles) 
                  VALUES(?, ?, ?, ?, ?, ?)";
      $insertpdo = $pdo->prepare($insert);
      if ($insertpdo->execute([$title, $name, $email, $mobile, $valPass, $roles])) {
          if ($roles == 'Admin') {
              $instro = "INSERT INTO admin (fullname, email, phone_number, password, roles)
                          VALUES(?, ?, ?, ?, ?)";
              $thestmt = $pdo->prepare($instro);
              $thestmt->execute([$name, $email, $mobile, $valPass, $roles]);
          }
          $lastId = $pdo->lastInsertId();
          if (!empty($class)) {
              $theclas = "INSERT INTO class_teacher (staffId, class) VALUES(?, ?)";
              $classStmt = $pdo->prepare($theclas);
              $classStmt->execute([$lastId, $class]);
          }

          $_SESSION['message'] = 'Staff Added Successfully.';
          $_SESSION['type'] = 'success';

          ?>
          <script type="text/javascript">
            setTimeout("location.href='staff.php';", 2000);
          </script>
          <?php 
      }else{
          $_SESSION['message'] = 'Something went wrong.';
          $_SESSION['type'] = 'error';
      }
    }
       
  }

}

?>


<?php include 'includes/footer.php'; ?>

<script>
document.addEventListener("DOMContentLoaded", () => {
  const roleSelect = document.querySelector("select[name='role']");
  const boxOverall = document.querySelector(".box-overall");

  roleSelect.addEventListener("change", () => {
    if (roleSelect.value === "class_teacher") {
      boxOverall.style.display = "block"; // ✅ show when Class Teacher
    } else {
      boxOverall.style.display = "none";  // ✅ hide for others
    }
  });
});
</script>



<style type="text/css">

.class-box {
  display: flex;
  gap: 15px;
  overflow-x: auto;
  padding: 10px 0;
  scrollbar-width: thin;
}

.class-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  white-space: nowrap;
  background-color: #f9f9f9;
  cursor: pointer;
  transition: all 0.2s ease;
}

.class-item:hover {
  background-color: #eef5ff;
  border-color: #5a9bff;
}

.class-item input[type="checkbox"] {
  accent-color: #007bff;
}

.class-item input[type="radio"] {
  accent-color: #007bff;
}

.box-overall {
  display: none; /* ✅ hidden initially */
  width: 100%;
  height: auto;
  border:1px solid #ccc;
  padding-left: 20px;
  padding-top: 10px;
  border-radius: 8px;
}


.box-overall span{
  font-weight: bold;
  color: #1E5F67;
  font-size: 15px;
}

.class-box{
  margin-top: 5px;
  width: 100%;
  height: 100px;
  display: flex;
  gap:25px;
  padding-bottom: 20px;
  overflow-x: auto;  
}
.sing-box{
  padding: 5px;
  border:1px solid #ccc;
  border-radius: 5px;
}
</style>
<?php include'includes/notification-alert-js.php';?>

