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

if(!empty($_POST['username'])){
    $designation  = $_POST['designation'];
    $phone_no = $_POST['phone'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $dob = $_POST['dob'];
    $gender = $_POST['gender'];
    $religion = $_POST['religion'];
    $settings_id = $_POST['settings_id'];
    $username = $_POST['username'];
    $old_teacher_name = $_POST['old_teacher_name'];
    $name = $_POST['name'];


    //check if image was selected

    //perform validations for already existing details
    $sql = "SELECT * FROM teachers WHERE phone =:phoneno AND phone != ''  AND id !=:settings_id";
    $conn->query($sql);
    $conn->bind(":phoneno", $phone_no);
    $conn->bind(":settings_id", $settings_id);
    if($conn->rowCount() > 0){
        echo "<script>
              toastr['error']('Phone number already exists.');
             </script>";
        return false;
    }
      //check if email already exists
       $sql = "SELECT * FROM teachers WHERE email =:email AND email != '' AND id !=:settings_id";
    $conn->query($sql);
    $conn->bind(":email", $email);
    $conn->bind(":settings_id", $settings_id);
    if($conn->rowCount() > 0){
        echo "<script>
              toastr['error']('The email already exists.');
             </script>";
        return false;
    }else {
        //process  updating of data into database
       $sql = "UPDATE teachers SET designation =:designation, phone =:phoneno, email =:email, address =:address, dob =:dob, gender =:gender, religion =:religion,
               name =:teacher_name  WHERE username =:username";
       $conn->query($sql);
       $conn->bind(":username", $username);
       $conn->bind(":designation", $designation);
       $conn->bind(":phoneno", $phone_no);
       $conn->bind(":email", $email);
       $conn->bind(":address", $address);
       $conn->bind(":dob", $dob);
       $conn->bind(":gender", $gender);
       $conn->bind(":religion", $religion);
       $conn->bind(":teacher_name", $name);

        $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 "<script>
              toastr['success']('Your Settings have been updated successfully.');
             </script><meta http-equiv='refresh' content='3; settings'>";

        } else {
            echo "<script>
              toastr['error']('An error occurred while updating data');
             </script><meta http-equiv='refresh' content='3; settings'>";
        }
    }

}else{

    echo "<script>
              toastr['error'](' Fields marked (*) are required..');
             </script>";
}



