<?php
include('../includes/functions.php');
session_start();
make_connection();
if(is_loggedin());
else
	redirect_to('index.php');
$user=get_user();
if(!$user)
	redirect_to('index.php');
$user_id=$user['regno'];
$user_name=$user['name'];
$fbid="";
if(isset($user['fbid']) and !empty($user['fbid']))
	$fbid=$user['fbid'];

if(isset($_POST['reviewsubmit'])){
	$message=$_POST['message'];
	$success=insert_review($fbid,$user_id,$user_name,$message);
	if($success)
		echo "<script>alert('Successfully added review');</script>";
	else
		echo "<script>alert('Some problem occurred adding your review');</script>";
}
if(isset($_POST['deletemyreview_submit'])){
	$success_delete=delete_review($review_id);
	if($success_delete)
		echo "<script>alert('Successfully deleted review');</script>";
	else
		echo "<script>alert('Some problem occurred deleting your review');</script>";
}
$all_reviews=get_all_reviews();
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<?php include('../includes/head.php');?>
	<title>Utopia- Reviews</title>
	<link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css">
	<link rel="stylesheet" type="text/css" href="materialize/css/materialize.css">
	<link rel="stylesheet" type="text/css" href="css/home.css">
	<style type="text/css">
		@media (min-width: 769px){
			.passenger {
				padding-top: 2.5rem;
			}
		}
		.passenger {
			position: relative;
			padding-top: 1rem;
		}
		.form-control {
			display: block;
			width: 100%;
			height: 104px;
			padding: 6px 12px;
			font-size: 14px;
			line-height: 1.42857143;
			color: #555;
			background-color: #fff;
			background-image: none;
			border: 1px solid #ccc;
			border-radius: 4px;
			-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
			box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
			-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
			-o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
			transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
		}
		textarea {
			overflow: auto;
			margin-bottom: 1rem;
		}
		.fa-trash{
			color: red;
		}
		.deletemyreview{
			position: absolute;
			top: 10px;
			right: 10px;
			cursor: pointer;

		}
		#deletemyreview_submit{
			text-transform: lowercase;
		}
		.review-pic-pp{
			max-width: 90%;
			border-radius: 50%;
		}
	</style>
</head>
<body>
	<div class="body row">
		<?php
		global $navactive;
		$navactive="reviews";
		include('../includes/nav.php');
		?>

		<div class="main">
			<div class="container">
				<header>
					<h3 class="center">Reviews</h3>
				</header>
				<div class="containerr">
					<div class="form-wrap">
						<form action="#" method="POST">
							<div class="input-field col s12">
								<textarea id="message" name="message" class="form-control" required placeholder="Things you want to say.."></textarea>
							</div>
							<br><br>
							<div class="col s12 right-align">
								<button type="submit" class="btn waves-effect waves-light" name="reviewsubmit">Add a review
									<i class="material-icons right">send</i>
								</button>
							</div>
						</form>
					</div>
					<br>
					<h5 class="left-align whatotherpeople">What other people say..</h5>
					<form action="#" method="POST" onsubmit="return confirm('Are you sure, you want to delete your review?')">
						<div class="reviews-wrap">
							<?php
							$noreviews=0;
							while($row=mysqli_fetch_assoc($all_reviews))
							{
								$noreviews++;
								$uid_rw=$row['regno'];
								$id_rw=$row['id'];
								$fbid_rw=$row['fbid'];
								if($uid_rw==$user_id)
									$deletemyreview_hide='show';
								else
									$deletemyreview_hide='hide';
								$name_rw=$row['name'];
								$message_rw=$row['message'];
								$picture_rw="https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg";
								if(isset($row['fbid']) and !empty($row['fbid']))
									$picture_rw="https://graph.facebook.com/".$row['fbid']."/picture?type=large";
								?>
								<section class="passenger card-panel hoverable" id="<?php echo $id_rw;?>">
									<div class="row">
										<div id="deletemyreview" class="deletemyreview <?php echo $deletemyreview_hide;?>" title="Delete My Review">
											<input value="Delete" class="btn" type="submit" name="deletemyreview_submit" id="deletemyreview_submit">
										</div>
										<div class="imgdiv col s3 center-align valign-wrapper">
											<img src="<?php echo $picture_rw;?>" class="valign review-pic-pp">
										</div>
										<div class="detailsdiv col s9">
											<h6 class="fontsize14rem"><b><?php echo $name_rw;?></b></h6>
											<p><?php echo $message_rw;?></p>
										</div>
									</div>
								</section>
								<?php
							}
							?>
						</div>
					</form>
				</div>
			</div>
		</div>

	</div>
	<script src="js/jquery.js"></script>
	<script src="materialize/js/materialize.js"></script>
	<script>
		$(".button-collapse").sideNav();
		$('.button-collapse').sideNav({
      menuWidth: 300, // Default is 240
      edge: 'left', // Choose the horizontal origin
      closeOnClick: false // Closes side-nav on <a> clicks, useful for Angular/Meteor
  }
  );
</script>
<script>
	$(document).ready(function(){
    // the "href" attribute of .modal-trigger must specify the modal ID that wants to be triggered
    $('.modal-trigger').leanModal();
});
</script>
</body>
</html>