<?php
    include($_SERVER['DOCUMENT_ROOT'] ."/_ps2020/config/connect.php");
    include_once($_SERVER['DOCUMENT_ROOT'].'/_ps2020/_global/dir_root.php');

    session_start();

    // $userAccount = $conn->query("SELECT accountType, school_id FROM user_account WHERE username = '".$_SESSION['user']. "'");
    // $row = $userAccount->fetch_assoc();
    // $userAccountType = $row['accountType'];
    // $school_id = $row['school_id'];

	$newName = $_GET["newName"];

    $sid = 0;
    $userFound = false;

    $id_set = false;
    $username = $newName;
    $password = "";
    $email = "";
    $accountType = 0; // 0 = Participant (Base AccountType)
    $school_id = 0; // 0 = Unallocated (particpants don't belong to a school)
    $class_id = 0; // 0 = Unallocated (particpants don't belong to a class)
    $accountType = 2; // assigned to be particpant by default
    $verify_code = 0;
    $completed_survey = 0;
	//$school_id = $_GET["school_id"];


    $result = $conn->query("SELECT school_id FROM user_account WHERE user_id = ".$_SESSION['userID']);
        if($result) {
            while ( $row = $result->fetch_assoc() ){
                $sid = $row['school_id'];
            }
        }


    $userSearch = $conn->query("SELECT * FROM user_account WHERE username = '".$newName. "'");
        if($userSearch) {
            if($userSearch->num_rows != 0)
            {
                // $row = $result->fetch_assoc();
                $userFound = true;
            }
        }
		
        
        
	if($userFound) {
		echo "false";
		
    }
    else
    {
        // generate 'id'
            $result = $conn->query("SELECT * FROM user_account");
                if($result) {
                    $id = 1;
                    while ( $row = $result->fetch_assoc() ){
                        
                        if ($id <= $row['user_id']){  //if 'id' already exist, increment
                            $id = $row['user_id'] +1;
                        }
                    }
                }

        if ($conn->query("INSERT INTO user_account VALUES ('$id', '$username', '$password', '$email', $accountType, '$sid', '$class_id', NOW(), NOW(), '$verify_code', '$completed_survey', 0)") === TRUE) 
        {
            echo "true";
        }
        else {
            error($conn->error);
            echo "here";
        }
        // if ($conn->query("INSERT INTO teacher VALUES ('$id', '$newName', '$newUrl')") === TRUE) {
        //     echo 'done';
            
        //echo "true";

        
    }

    $conn->close();
?>