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

	session_start();

	$edited_username = $_REQUEST['edited_username'];
	$edited_email = $_REQUEST['edited_email'];
	$edited_password = $_REQUEST['edited_password'];
	$edited_class = $_REQUEST['edited_class'];

	$username = $_REQUEST['username'];
	$email = $_REQUEST['email'];
	$password = hash('sha256', mysqli_real_escape_string($conn,$_REQUEST["password"]), false);
	$codeVal_arr = $_REQUEST['codeVal'];
	$codeVal = explode(',', $codeVal_arr);
	// echo $codeVal[0];



	// get 'ID' & 'Username'
		$init_id = '';
		$init_username = '';
		
		$result = $conn->query("SELECT * FROM user_account WHERE username ='".$_SESSION['user']."'");
		
		if($result) {
			while ( $row = $result->fetch_assoc() ){
				$init_id = $row['user_id'];
				$init_username = $row['username'];
			}
		}

	// get 'Class ID'
		$school_id = '';
		$class_id = '';
		
		if ( !empty($codeVal[0]) ){
			$result = $conn->query("SELECT * FROM school_class WHERE class_name ='".$codeVal[4]."'");
			
			if($result) {
				while ( $row = $result->fetch_assoc() ){
					$school_id = $row['school_id'];
					$class_id = $row['class_id'];
				}
			}
		}

		
	// Update to database
		$updated = false;

		// Username
		if ( $edited_username == 'true' ){

			if ( $result = $conn->query("UPDATE user_account SET username='".$username."' WHERE user_id='".$init_id."'") === true){
				$updated = true;  // update success
			}
		}

		// Email
		if ( $edited_email == 'true' ){

			// random Code generator (for 'verify_code')
			function getRandomCode() {

		        $n=10;
		        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		        $randomString = '';
		      
		        for ($i = 0; $i < $n; $i++) {
		            $index = rand(0, strlen($characters) - 1);
		            $randomString .= $characters[$index];
		        }
		      
		        return $randomString;
		    }
		    $verify_code = getRandomCode();

			include_once(dir_rootPHP.'php/request/sendEmail_verifyCode.php');

				if($mail->send()){
			        
			        if ( $result = $conn->query("UPDATE user_account SET email='".$email."', verifyCode='".$verify_code."', active = 0 WHERE user_id='".$init_id."'") === true){
						$updated = true;  // update success
					}
			    }
		}

		// Password
		if ( $edited_password == 'true' ){
			if ( $result = $conn->query("UPDATE user_account SET password='".$password."' WHERE user_id='".$init_id."'") ){
				$updated = true;  // update success
			}
		}

		// Class
		if ( $edited_class == 'true' ){
			
			if ( empty($codeVal[0]) ){
				$role_toParticipant = 'accountType = 0,';
				$school_id = $class_id = 0;
			}
			else
				$role_toParticipant = 'accountType = '.$codeVal[0].',';
			// $role_toParticipant = "accountType = '".$codeVal[0]."',";

			if ( $result = $conn->query("UPDATE user_account SET ".$role_toParticipant." school_id='".$school_id."', class_id='".$class_id."' WHERE user_id='".$init_id."'") === true){
				$updated = true;  // update success
			}
		}

	if ($updated){
		echo 1;
	}



	// switch ($act){
	// 	case 'update_sName':
	// 		$sid=$_GET['sid'];
	// 		$sName=$_GET['sName'];
	// 		update_sName($sid,$sName);
	// 		break;
	// 	case 'update_sUrl':
	// 		$sid=$_GET['sid'];
	// 		$sUrl=$_GET['sUrl'];
	// 		update_sUrl($sid,$sUrl);
	// 		break;
	// }


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

	// 		$result = $conn->query("UPDATE survey SET survey_name='".$sName."' WHERE survey_id='".$sid."'");

	// 	$conn->close();
	// }

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

	// 		$result = $conn->query("UPDATE survey SET survey_url='".$sUrl."' WHERE survey_id='".$sid."'");

	// 	$conn->close();
	// }
?>