<?php
	$act=$_GET['act'];

	switch ($act){
		case 'update_username':
			$sid=$_GET['sid'];
			$sName = $_GET['sName'];
			update_username($sid,$sName);
			break;
		case 'update_email':
			$sid=$_GET['sid'];
			$sEmail = $_GET['sEmail'];
			update_email($sid,$sEmail);
			break;
		case 'update_password':
			$sid=$_GET['sid'];
			$password = $_GET['password'];
			update_password($sid,$password);
			break;
		case 'update_select':
			$index=$_GET['index'];
			$rVal=$_GET['rVal'];
			$sVal=$_GET['sVal'];
			$cVal=$_GET['cVal'];
			update_select($index,$rVal,$sVal,$cVal);
			break;
		case 'update_status':
			$index=$_GET['index'];
			$statVal=$_GET['statVal'];
			update_status($index,$statVal);
			break;
	}


	function update_username($sid,$sName){
		include($_SERVER['DOCUMENT_ROOT'] ."/_ps2020/config/connect.php");

			if ( $conn->query("UPDATE user_account SET username='".$sName."' WHERE user_id='".$sid."'") === true){
				echo 1;
			}

		$conn->close();
	}

	function update_email($sid,$sEmail){
		include($_SERVER['DOCUMENT_ROOT'] ."/_ps2020/config/connect.php");

			if ( $conn->query("UPDATE user_account SET email='".$sEmail."' WHERE user_id='".$sid."'") === true){
				echo 1;
			}

		$conn->close();
	}

	function update_password($index,$password){
		include($_SERVER['DOCUMENT_ROOT'] ."/_ps2020/config/connect.php");

		$password_hashed = hash('sha256', mysqli_real_escape_string($conn,$password), false);

			if ( $conn->query("UPDATE user_account SET password='".$password_hashed."' WHERE user_id='".$index."'") === true){
				echo 1;
			}

		$conn->close();
	}

	function update_select($index,$rVal,$sVal,$cVal){
		
		if (isset($sVal)){
			$school = ", school_id='".$sVal."'";
		}
		else
			$school = ", school_id='0'";

		if (isset($cVal)){
			$class = ", class_id='".$cVal."'";
		}
		else
			$class = ", class_id='0'";

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

			if ( $conn->query("UPDATE user_account SET accountType='".$rVal."'".$school.$class." WHERE user_id='".$index."'") === true){
				echo 1;
			}

		$conn->close();
	}

	function update_status($index,$statVal){
		include($_SERVER['DOCUMENT_ROOT'] ."/_ps2020/config/connect.php");

			if ( $conn->query("UPDATE user_account SET active='".$statVal."' WHERE user_id='".$index."'") === true){
				echo 1;
			}

		$conn->close();
	}
?>