<?php
	session_start();
	error_reporting(0);

	$userID = $_SESSION['marry_userID'];
	$userPW = $_SESSION['marry_userPW'];
	$dbp = "userDB/";
	
	if(empty($userID) == false) {
		$filename = $dbp . $userID . ".txt";
		$file = fopen($filename,"r");
			$readed = fread($file,filesize($filename));
			fclose($file);
		$rearray = explode("\r\n",$readed);
		$hashf = $rearray[0];
		$requested = $rearray[4];
		$reqsent = $rearray[7];
		
		$pstat = password_verify($userPW, $hashf);
		
		if($pstat == true) {
			//uncomment below if afterlogin script empty
			//header("Location: wrong-pass.html");
			//put your main script after login here
				
			if($requested == "requested") {
				$reqfrom = $rearray[5];
			}
			//script end
		}
		//do comment below if afterlogin script empty
		else {
			header("Location: wrong-pass.html");
		}
	}
	else {
		header("Location: anti-exploit.html");
	}
?>

<!DOCTYPE HTML>
	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="Refresh" content="600">
		<title>Marry</title>
	</head>
	<body>
		<style>
			.rqbg {
				background: #e1e2ed;
				display: none;
				padding: 15px;
			}
			.notif {
				color: red;
				display: none;
			}
			.congrats {
				color: dark_green;
				display: none;
				background: #d4d7ff;
				padding: 5px;
			}
			.divorce {
				display: none;
			}
			.divorce-input {
				background: #ff4538;
				color: white;
			}
		</style>
		
		<div>
			<p>
				Halo <?php echo $userID; ?>!<br>
				Selamat datang di Marry anda bisa menikah disini dengan.
			</p>
			
			<p>Request Pernikahan:</p>
			<div id="congrats" class="congrats">
				<p>Selamat! anda telah menikah dengan <b><?php echo $rearray[3]; ?></b></p>
				
				<form method="POST" id="divorce" action="divorce.php" class="divorce">
					<input type="hidden" value="divorce" name="divorce">
					<input type="submit" value="BERCERAI CONFIRM" class="divorce-input">
				</form>
				
				<button onclick="divorce()" id="divorce_req">Saya ingin bercerai</button><br>
			</div>
			
				<form id="reqform" method="POST" action="marry.php">
					<label for="tID">Target: </label>
					<input type="text" id="tID" name="tID">
					
					<input type="submit" value="ajukan!">
				</form>
				<div id="sent_req" class="notif">
					<p><b>Your request succesfully sent to <?php echo $reqsent; ?></b></p>
					<form method="POST" id="cancel" action="cancelreq.php">
						<input type="hidden" value="cancel" name="cancel">
						<input type="submit" value="BATALKAN PERMINTAAN">
					</form>
				</div><br>
			
			<div class="rqbg" id="r_warn">
				<p>
					Anda memiliki permitaan pernikahan dari:<br>
					<?php echo $reqfrom; ?>
				</p>
				
				<form method="POST" id="request" action="accept.php">
					<input type="hidden" value="sah" name="sah">
					<input type="submit" value="terima permintaan">
				</form>
				<form method="POST" id="deny" action="unreq.php">
					<input type="hidden" value="rejected" name="rejected">
					<input type="submit" value="TOLAK permintaan">
				</form>
			</div>
			
			<br>
			<button onclick="window.location.href='logout.php'">Log Out</button><br>
			<button onclick="window.location.href='changePW.php'">Change password</button><br>
			<button onclick="window.location.href='chat.php'">Chat</button>
			
			<script>
				var warning = document.getElementById("r_warn");
				var request_coming = "<?php echo $requested; ?>";
					if(request_coming == "requested") {
						warning.style.display = "inline-block";
					}
				
				var notif = document.getElementById("sent_req");
				var sent_stat = "<?php echo $rearray[6]; ?>"
					if(sent_stat == "requesting") {
						notif.style.display = "block";
					}
					
				var reqform = document.getElementById("reqform");
				var congrats = document.getElementById("congrats");
				var married_stat = "<?php echo $rearray[2]; ?>"
					if(married_stat == "married") {
						reqform.style.display = "none";
						congrats.style.display = "block";
					}
					
				function divorce() {
					var divorce = document.getElementById("divorce");
					var divorce_req = document.getElementById("divorce_req");
					divorce.style.display = "inline";
					divorce_req.style.display = "none";
				}
			</script>
		</div>
	</body>
</html>