<?php

    ini_set('display_errors', 1); 
    ini_set('display_startup_errors', 1); 
    error_reporting(E_ALL);

    $output['result'] = "";
    $output['success'] = "";
    $output['debug'] = "";
    $output['session'] = "";

    function debug($string)
    {
        $GLOBALS['output']['debug'] .= "[Debug - " . $string . "] ";
    }

    function error($string)
    {
        $GLOBALS['output']['debug'] .= "[Error - " . $string . "] ";
    }

    function sendOutput()
    {
        echo json_encode($GLOBALS['output']);
    }

    //First we need to connect to the database
    $conn = new mysqli("localhost", "ps2020", "ps2020pass");

    if ($conn->connect_errno) {
		echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
	}
	else {
		debug("Successful Connection");
		// debug($conn->host_info);
    }
    
    if($conn->select_db('ps2020')) { debug("Using ps2020"); }
	else { error($conn->error); }

    $code = $_GET["q"];



    $sql = "SELECT * FROM school";
    $result = $conn->query($sql);

    if($result->num_rows > 0){
        while($row = $result->fetch_assoc()) {
            //This is to check if code is for a teacher
            if($row["specific_teacherCode"] == $code){
				
				$school_name = $row["school_name"];
				echo "t-".$school_name;
                break;
            }
            
        }
	}
	
	$sql = "SELECT * FROM school_class";
    $result = $conn->query($sql);

    if($result->num_rows > 0){
        while($row = $result->fetch_assoc()) {
            //THis is to check if it is a student code
            if($row["specific_classCode"] == $code){
                //So we have found an existing account
				//Username is already taken!
				$class_name = $row["class_name"];
                echo "s-".$class_name;
                break;
            }
            
        }
	}

	//Username is available
	//But no class or school has been assigned
    echo "n";
	$conn->close();
	exit();
?>
