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

	$newSchool = $_GET["newSchool"];
	$newTeacher = $_GET["newTeacher"];
	$newClass = $_GET["newClass"];
	$newCode = $_GET["newCode"];
	// $newOrder = $_GET["newOrder"] +1;

	session_start();
	$accType = $_SESSION['accountType'];
	$new_tid = $newTeacher;

		// echo 'school: '.$newSchool.' | teacher: '.$newTeacher.' | class: '.$newClass.' | code: '.$newCode;

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

		if ($accType == 2){	// Teacher account
			$new_tid = $_SESSION['userID'];
		}

		// generate 'id'
		$id_set = false;
		$code_exist = false;

		$result = $conn->query("SELECT * FROM school_class");
			if($result) {
				$id = 1;
				while ( $row = $result->fetch_assoc() ){
					
					if ($id == $row['class_id'] && $id_set == false){  //if 'id' already exist, increment
						$id++;
					}
					else if ($id < $row['class_id']){  //no more increment
						$id_set = true;
					}

					if ($newCode == $row['specific_classCode']){
						if ($newCode != ''){
							$code_exist = true;
						}
					}
				}
			}


		if ($code_exist == false){
			if ($conn->query("INSERT INTO school_class VALUES ('$id', '$newClass', '$newSchool', '$new_tid', '$newCode')") === TRUE) {
				echo 1;
			}
		}
	
   $conn->close();
?>