<?php
session_start();
include '../manage/connection.php';
if(isset($_POST['email']) && isset($_POST['password']))
{
    $logid=htmlentities($_POST['email']);
    $password=htmlentities($_POST['password']);
    if(!empty($logid) && !empty($password))
    {
        if(isset($_GET['type']))
        {
            if($_GET['type']=='admin')
            {
                $getAdmin=$connect->query("SELECT * FROM admin WHERE email='$logid' OR phone='$logid'");
                if($getAdmin->num_rows!=0)
                {
                    $admin=$getAdmin->fetch_assoc();
                    if($admin['password']==md5($password))
                    {
                        $_SESSION['admin']=array("id"=>$admin['id'], "email"=>$logid, "password"=>md5($password));
                        
                        echo 'success';
                        if(isset($_SESSION['teacher']))
                        {
                            unset($_SESSION['teacher']);
                        }
                    }
                    else
                    { 
                        echo ' Password Does not match ';
                    }

                }
                else
                {
                    echo 'Admin Not Found';
                }
            }
            else
            {
                // teacher Login
                $getteacher=$connect->query("SELECT * FROM teacher WHERE phone='$logid'");
                if($getteacher->num_rows!=0)
                {
                    $teacher=$getteacher->fetch_assoc();
                    if($teacher['password']==md5($password))
                    {
                        if($teacher['status']=='active')
                        {
                            $_SESSION['teacher']=array("id"=>$teacher['id'], "phone"=>$logid, "password"=>md5($password));
                            echo 'success';
                            if(isset($_SESSION['admin']))
                            {
                                unset($_SESSION['admin']);
                            }
                        }
                        else
                        {
                            echo 'Your account is not active, contact admin.';
                        }
                    }
                    else
                    {
                        echo 'Password Does not match';
                    }

                }
                else
                {
                    echo 'teacher Not Found';
                }
            }
        } 
    }
    else
    {
        echo ' Fields Cannot be empty';
    }
}
?>