<?php
session_start();
require_once '../Config/Database.php';
require_once '../Model/Login.php';
$login = new Login();

if(isset($_POST['login'])){
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $key = $_POST['key'];
    $status = $login->auth($email,$phone,$key);
    if($status){
        $_SESSION['key'] = $key;
        $_SESSION['email'] = $email;
        $_SESSION['phone'] = $phone;
        $_SESSION['name'] = $status[0]['name'];
        $_SESSION['student_id'] = $status[0]['student_id'];
        echo json_encode(array("message"=>"success"));
    }else{
        echo json_encode(array("message"=>"fail"));
    }
}

if(isset($_GET['logout'])){
    // Unset all session variables
    session_unset();

    // Destroy the session
    session_destroy();

    $redirect_url = "../login/";

    // Perform the redirect
    header("Location: $redirect_url");

    exit;
}