<?php
	session_start();
	error_reporting(0);
	
	$userID = $_SESSION['marry_userID'];
	$userPW = $_SESSION['marry_userPW'];
	$dbp = "userDB/";
	$filename = $dbp . $userID . ".txt";
	$chat_data = "chatDB/" . $userID . ".txt";
	
	//read userfile
	$file = fopen($filename,"r");
		$readed = fread($file,filesize($filename));
		fclose($file);
	$rearray = explode("\r\n",$readed);
	
	if(file_exists($chat_data) == false) {
		$content = "\r\n";
			
		$opn = fopen($chat_data,"w");
		$write = $content;
		fputs($opn,$write);
		fclose($opn);
	}
	
	//file chatDB open
	$fileread = fopen($chat_data, "r") or die("Unable to open file!");

	// Output one line until end-of-file
	// called later
?>

<!DOCTYPE HTML>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="Refresh" content="600">
		<title>Chat</title>
	</head>
	<body>
		<style>
			.msg {
				width: 78%;
			}
			.receive {
				background: #c8e0f7;
				padding: 11px;
			}
		</style>
	
		<div>
			<form method="POST" action="sendchat.php">
				<label for="target">Target user: </label>
				<input type="text" id="target" name="target" value="<?php echo $rearray[3]; ?>"><br>
			
				<label for="message">Pesan dikirim ingin: </label>
				<input type="text" id="message" name="message" class="msg" autocomplete="off"><br>
				
				<input type="submit" value="Kirim"><br>
				
				<input type="button" value="Selesai dan keluar" onclick="window.location.href='page.php'">
			</form>
			
			<p>Pesan diterima dan terkirim iya:</p>
			<input type="button" value="Reload" onclick="window.location.reload();">
			
			<p class="receive">
				<?php
					while(!feof($fileread)) {
					  echo fgets($fileread) . "<br>";
					}
					fclose($fileread);
				?>
			</p>
		</div>
	</body>
</html>