<?php
session_start();
include 'includes/database.php';
include 'includes/functions.php';
$conn= new Functions();

if(!empty($_POST['name']) && !empty($_POST['designation']) && !empty($_POST['email'])
    && !empty($_POST['joining_date'])){
    $name = $_POST['name'];
    $designation = $_POST['designation'];
    $email = $_POST['email'];
    $joining_date = $_POST['joining_date'];
    $gender = $_POST['gender'];
    $religion = $_POST['religion'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $username = $_POST['username'];
    $status = $_POST['status'];
    $old_teacher_name = $_POST['old_teacher_name'];

        //process  updating  of data into database

        $sql = "UPDATE teachers SET name=:name, designation=:designation, gender=:gender, religion= :religion, email=:email,  phone= :phone,  address=:address,  joining_date =:joining_date, status =:status, username=:username WHERE username =:username";
        $conn->query($sql);
        $conn->bind(":name", $name);
        $conn->bind(":designation", $designation);
        $conn->bind(":gender", $gender);
        $conn->bind(":religion", $religion);
        $conn->bind(":email", $email);
        $conn->bind(":phone", $phone);
        $conn->bind(":address", $address);
        $conn->bind(":joining_date", $joining_date);
        $conn->bind(":username", $username);
        $conn->bind(":status", $status);

        $send = $conn->execute();

    //update teacher information in:
    /*
     * 1. classes table
     * 2. sections table
     */


    $sql = "SELECT teacher_name FROM classes WHERE teacher_name =:old_teacher_name";
    $conn->query($sql);
    $conn->bind(":old_teacher_name", $old_teacher_name);
    if($conn->rowCount() > 0){
        //update old student name with new student name
        $sql = "UPDATE classes SET teacher_name =:new_teacher_name WHERE teacher_name =:old_teacher_name";
        $conn->query($sql);
        $conn->bind(":new_teacher_name", $name);
        $conn->bind(":old_teacher_name", $old_teacher_name);
        $conn->execute();
    }

    $sql = "SELECT teacher_name FROM sections WHERE teacher_name =:old_teacher_name";
    $conn->query($sql);
    $conn->bind(":old_teacher_name", $old_teacher_name);
    if($conn->rowCount() > 0){
        //update old student name with new student name
        $sql = "UPDATE sections SET teacher_name =:new_teacher_name WHERE teacher_name =:old_teacher_name";
        $conn->query($sql);
        $conn->bind(":new_teacher_name", $name);
        $conn->bind(":old_teacher_name", $old_teacher_name);
        $conn->execute();
    }

        if ($send) {
            echo "<p class='alert alert-success alert-dismissible fade show' role='alert'>
                    <i class='fas fa-check-circle'></i> Teacher $name was updated successfully.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='3; teacher'>
           </p>";
        } else {
            echo "<p class='alert alert-danger alert-dismissible fade show' role='alert'>
                    <i class='fas fa-ban'></i> An error occurred while adding data.
                  <button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-label=\"Close\">
                             <span aria-hidden=\"true\">×</span>
                  </button>
                  <meta http-equiv='refresh' content='4; teacher'>
           </p>";
        }
   


}else{
    echo "<p class='alert alert-danger alert-dismissible fade show text-center' role='alert'>
              <i class='fas fa-ban'></i>
               Fields marked (*) are required.
                  <button type='button' class='close' data-dismiss='alert' aria-label='Close'>
                             <span aria-hidden='true'>×</span>
                  </button>
           </p>";
}



