<?php
include '../../classes/teachers.php';
$teacher=new Teachers("../../manage/connection.php");
$address="";
$image="default.jpg";
$admin=0;
if(isset($_POST['fullname']) && isset($_POST['teacher_id']) && isset($_POST['phone']) && isset($_POST['gender']))
{
    if(isset($_POST['address']))
    {
        $address=htmlentities($_POST['address']);
    }
    $teacher_id=htmlentities($_POST['teacher_id']);
    $fullname=htmlentities($_POST['fullname']);
    $phone=htmlentities($_POST['phone']);
    $gender=htmlentities($_POST['gender']);
    
    
    if($teacher->findTeacher($phone))
    {
        $tInfo=$teacher->getTeacher($teacher_id);
        if(isset($_POST['password']) && !$_POST['password']=="")
        {
            $password=md5($_POST['password']);
        }
        else
        {
            $password=$tInfo['password'];
        }
        // Check for image
        if(isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name']))
        {
            $temp_name=$_FILES['image']['tmp_name'];
            $image=$phone.'_'.$_FILES['image']['name'];
            move_uploaded_file($temp_name,'../../images/'.$image);
        }
        else
        {
            $image=$tInfo['image'];
        }


        if($teacher->updateTeacher($fullname,$teacher_id,$phone,$address,$gender,$password, $image))
        {
            echo 'Teacher Updated';
        }
        else
        {
            echo 'Could Not Update';
        }
    }
    else
    {
        echo "Teacher Not Exist";
    }
}
else
{
    if(!isset($_POST['fullname']))
    {
        echo '<div>Name Not Set</div>';
    }
    if(!isset($_POST['phone']))
    {
        echo '<div>Phone Not Set</div>';
    }
    if(!isset($_POST['gender']))
    {
        echo '<div>Gender Not Set</div>';
    }
    if(!isset($_POST['password']))
    {
        echo '<div>Passwor Not Set</div>';
    }
}
?>