<?php

	// Description: EXTRACT MOMENT ID, AND IF VALID, DISPLAY ITS INFO

	require_once 'processes/config.php';
	require_once 'processes/dbh.inc.php';
	require_once 'processes/checkLogin.php';
	
	// IF VIEW VARIABLE NOT GIVEN, EXIT
	if(!isset($_GET['view'])) {
		exit();
	}
	
	// FETCH MOMENT
	$momentID = mysqli_real_escape_string($connection, trim($_GET['view']));
	$query1 = "SELECT * FROM subquestionnair WHERE id = '$momentID';";
	$result1 = mysqli_query($connection, $query1) or die("Error: ".mysqli_error($connection));
	$row1 = mysqli_fetch_array($result1);

	// IF MOMENT NOT IN DATABASE, EXIT
	if($result1->num_rows !== 1) {
		echo "Requested experience does not exist.";
		exit();
	}
	
	// IF MOMENT DOESN'T BELONG TO USER (WHO IS NOT AN ADMIN), EXIT
	if($_SESSION['dbUserData']['admin'] !== '1') {
	
		$queryCheckUser = "SELECT id FROM subquestionnair WHERE id = '$momentID' AND id IN (SELECT id_subquestionnair FROM experience_subquestionnair WHERE id_experience IN (SELECT id_experience FROM researcher_experience WHERE id_researcher = '" . $_SESSION['dbUserData']['id_researcher'] . "'));";
		if(mysqli_query($connection, $queryCheckUser)->num_rows === 0) {
			echo "You are not authorized to view this moment.";
			exit();
		}
		
	}
	
	// EXTRACT & DISPLAY THE NUMBER OF QUESTIONS IN THE MOMENT
	$queryNumQuestions = "SELECT * FROM question WHERE id IN (SELECT id_question FROM subquestionnair_question WHERE id_subquestionnair = '$momentID');";
	$resultNumQuestions = mysqli_query($connection, $queryNumQuestions);
	$numQuestions = $resultNumQuestions->num_rows;

	include_once 'header.php';
		
?>
	
	<!-- START OF viewMoment.php -->
	<body>
		
			<header id="main-header">
    	    	<a id="logo" href=".">
					TANIA
            		<img src="./img/pen_800x800.png" alt="tania logo pen" width="25" height="25">
        		</a>
        		<div id="account">
        		    <a class="sign-out" href="./processes/logout.php">Sign Out</a>
        		</div>
    		</header>
		
		<div class="container" style="margin-top: 8rem; min-height: calc(100vh - 8rem - 10vh);">
		
			<div class="row">
				<div class="col-sm-8">
					<h2>Moment</h2>
					<p><font size='4'><b> Title: </b> <?php echo $row1['title']; ?></font></p>
					<p><font size='4'><b> Description: </b> <?php echo $row1['description']; ?></font></p>
					<p><font size='4'><b> Due Date: </b><?php echo date("M j, Y (g:i A)", strtotime($row1['date_to_administer'])); ?></font></p>
					<p><font size='4'><b> Amount of Questions: </b> <?php echo $numQuestions; ?></font></p>
				</div>
				<div class="col-sm-4">
					<div class="hidden-xs">
						<br><br><br>
					</div>
					<div id="view-experience-buttons" style="max-width: 18rem;">
  						<div data-toggle="tooltip" data-placement="top" title="Add Existing Question" id="experience-pen" onclick="nextModal('#AddQuestion')">
  							<span class="fas fa-plus"></span>
	  					</div>
						<div data-toggle="tooltip" data-placement="top" title="Edit Moment" id="experience-pen" onclick="nextModal('#Edit')">
							<span class="fas fa-pen"></span>
						</div>
					</div>	
				</div>
			</div><!--row-->
			
			<br>

			<div class="row">
				<div class="col-sm-12">
					<?php if($numQuestions > 0): ?>
					<table class="table table-hover table-striped" id="tableEvaluations">
						<thead>
							<tr>
								<th><h4>#</h4></th>
								<th><h4>Premise</h4></th>
							</tr>
						</thead>
						<tbody>
						<?php
						
							$query2 = "SELECT * FROM question WHERE id IN (SELECT id_question FROM subquestionnair_question WHERE id_subquestionnair = '$momentID');";
							$result2 = mysqli_query($connection, $query2);
							$x = 1;
							
							while($row2 = mysqli_fetch_array($result2)):
							
								$queryMinText = "SELECT value FROM question_type WHERE label = 'min_texto' AND id_question = '" . $row2['id'] . "';";
								$resultMinText = mysqli_query($connection, $queryMinText);
								$rowMinText = mysqli_fetch_array($resultMinText);
								
								$queryMaxVal = "SELECT value FROM question_type WHERE label = 'max_val' AND id_question = '" . $row2['id']. "';";
								$resultMaxVal = mysqli_query($connection, $queryMaxVal);
								$rowMaxVal = mysqli_fetch_array($resultMaxVal);

								$queryMaxText = "SELECT value FROM question_type WHERE label = 'max_texto' AND id_question = '" . $row2['id']. "';";
								$resultMaxText = mysqli_query($connection, $queryMaxText);
								$rowMaxText = mysqli_fetch_array($resultMaxText);
								
						?>
							<tr style="cursor: pointer;" data-toggle="modal" data-target="#EditQuestion" data-question-number="<?php echo $x; ?>" data-question-id="<?php echo $row2['id']; ?>" data-premise="<?php echo $row2['premise']; ?>" data-type="<?php echo $row2['id_type']; ?>" data-min_text="<?php echo $rowMinText['value']; ?>" data-max_val="<?php echo $rowMaxVal['value']; ?>" data-max_text="<?php echo $rowMaxText['value']; ?>" data-categoryid="<?php echo $row2['id_category']; ?>" data-subcategoryid="<?php echo $row2['id_subcategory']; ?>">
								<th><?php echo $x; ?></th>
								<td><p><?php echo $row2['premise']; ?></p></td>
								<!-- confirmDeleteQuestionSubquestionnaire.php?del=<?php echo $row2['id']; ?>&quest=<?php echo $momentID; ?> -->
							</tr>
						<?php $x++; endwhile; ?>
						</tbody>
					</table>
					<?php else: ?>
 					<h3 class="text-center"><small>It seems you haven't added any questions to this moment...</small></h3>
					<?php endif; ?>
				</div><!-- col -->
			</div><!-- row -->

			<br>
			
			 		<div class="row">
  						<div class="col-sm-2 col-sm-offset-5">
  							<div data-toggle="tooltip" data-placement="top" title="Add Existing Question" id="experience-pen" onclick="nextModal('#AddQuestion')" style="padding: 0.5em; margin: 0 auto; width: 60px;">
  								<span class="fas fa-plus"></span>
  							</div>
  						</div>
  					</div>
  					
  			<br><br>

		</div><!-- container -->
		


		<!-- POPUP FOR EDIT MOMENT -->
		<form id="updateMoment" class="form-horizontal">
			<div class="modal fade" id="Edit" tabindex="-1" role="dialog" aria-labelledby="EditLabel" aria-hidden="true">
				<div class="modal-dialog" role="document">
 					<div class="modal-content">
						
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close">
			  	 	  			<span aria-hidden="true">&times;</span>
			  	 	  		</button>
			  	 	  		<h3 class="modal-title" id="EditLabel">Edit Moment</h3>
   						</div>
 						
 						<div class="modal-body">


							<!-- ID -->
							<input type="hidden" name="momentID" value="<?php echo $momentID; ?>">
	
							<!-- TITLE -->
							<br>
							<div class="form-group">
								<label class="col-sm-offset-1 control-label col-sm-2" for="newTitle">Title:</label>
								<div class="col-sm-7">
									<input type="text" class='form-control text-center' id="newTitle" name="newTitle" value="<?php echo $row1['title']; ?>">
								</div>
							</div>
				
							<!-- DESCRIPTION -->
							<div class="form-group">
								<label class="col-sm-offset-1 control-label col-sm-2" for="newDescription">Description:</label>
								<div class="col-sm-7">
									<textarea class="form-control text-center" id="newDescription" name="newDescription" rows="2"><?php echo $row1['description']; ?></textarea>
								</div>
							</div>


							<br>


		<!-- ERROR ALERT FOR USER -->			
	<div id="error-updateMoment" class="row" style="display: none;">
		<div class="col-sm-12">
			<div class="alert alert-danger mb-1" role="alert">
				<h4 class="error-lead">Error!</h4>
				<p class="error-description"></p>
			</div>
		</div>
	</div>




      					</div><!--modal-body-->
      					
      					<!-- SUBMIT OR CANCEL -->
     					<div class="modal-footer">
       						<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
       						<button type="button" class="btn btn-danger" data-dismiss="modal" onclick="nextModal('#DeleteMoment')">Delete</button>
        					<button type="submit" class="btn btn-primary" name="updateMoment">Save Changes</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>



		<!-- POPUP FOR DELETE MOMENT -->
		<form id="deleteMoment">
			<div class='modal fade' id='DeleteMoment' tabindex='-1' role='dialog' aria-labelledby='DeleteMomentLabel' aria-hidden='true'>
				<div class='modal-dialog modal-dialog-centered' role='document'>
 					<div class='modal-content'>

						<div class='modal-header'>
			  	 	  		<h3 class='modal-title' id='DeleteMomentLabel'>Delete Question<button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button></h3>
   						</div>

 						<div class='modal-body'>



							<!-- QUESTIONNAIRE ID -->
 							<input type='hidden' name='momentID' value="<?php echo $momentID; ?>">
 							
							<span class='text-center'>
								<h3>Are you sure you want to delete this Moment?</h3>
<!-- 								<h3 id="deletePremise" style='color:#ff8f00;'></h3> -->
							</span>
							
							<br>
							<p class='alert alert-warning text-justify'>This action will delete the moment. If you have any problems, contact technical support at: <a class='alert-link' href='#'>support@tania.uprrp.edu</a>.</p>
							<br>
							<!--You should be aware of this and opt to edit manually any affected moments and experiences in the following pages: <a class='alert-link' href='#'>experience</a>.-->



		<!-- ERROR ALERT FOR USER -->			
	<div id="error-deleteMoment" class="row" style="display: none;">
		<div class="col-sm-12">
			<div class="alert alert-danger mb-1" role="alert">
				<h4 class="error-lead">Error!</h4>
				<p class="error-description"></p>
			</div>
		</div>
	</div>



      					</div><!--modal-body-->
      					
      					<!-- SUBMIT OR CANCEL -->
     					<div class='modal-footer'>
       						<button type='button' class='btn btn-default' data-dismiss='modal'>Cancel</button>
        					<button type='submit' class='btn btn-danger' name='deleteMoment'>Delete Anyway</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>








		<!-- POPUP FOR ADDING QUESTION TO MOMENT -->
		<form id="addExistingQuestionToMoment">
			<div class="modal fade" id="AddQuestion" tabindex="-1" role="dialog" aria-labelledby="AddQuestionLabel" aria-hidden="true">
				<div class="modal-dialog" role="document">
 					<div class="modal-content">
						
						<div class="modal-header">
							<button type="button" class="close" data-dismiss="modal" aria-label="Close">
			  	 	  			<span aria-hidden="true">&times;</span>
			  	 	  		</button>
			  	 	  		<h3 class="modal-title" id="AddQuestionLabel">Add Question</h3>
   						</div>
 						
 						<div class="modal-body">


							<!-- MOMENT ID -->
							<input type="hidden" name="momentID" value="<?php echo $momentID; ?>">

							<div class="row">
								<div class="col-sm-12 col-sm-offset-1">
									<h4>Choose Existing Question</h4>
								</div>
							</div>


							<!-- QUESTION ID -->
							<div class="form-horizontal">
								<div class="form-group">
									<div class='col-sm-offset-2 col-sm-9'>
										<select class="form-control" id="questionID" name="questionID" style="width:100%; text-align-last:center;" required>
										<!-- <option disabled selected value>Select Question</option> -->
										<?php 
											$queryQuestions = "SELECT * FROM `question` WHERE `id` IN (SELECT `id_question` FROM `questionnair_question` WHERE `id_questionnair` = (SELECT id_questionnair FROM subquestionnair WHERE id = '$momentID'));";
											$resultQuestions = mysqli_query($connection, $queryQuestions);
											while($rowQuestions = mysqli_fetch_assoc($resultQuestions)):
										?>
											<option value="<?php echo $rowQuestions['id']; ?>"><?php echo $rowQuestions['premise']; ?></option>
										<?php endwhile; ?>
										</select>
									</div>
								</div>
							</div>




		<!-- ERROR ALERT FOR USER -->			
	<div id="error-addExistingQuestionToMoment" class="row" style="display: none;">
		<div class="col-sm-12">
			<div class="alert alert-danger mb-1" role="alert">
				<h4 class="error-lead">Error!</h4>
				<p class="error-description"></p>
			</div>
		</div>
	</div>



      					</div><!--modal-body-->
      					
      					<!-- SUBMIT OR CANCEL -->
     					<div class="modal-footer">
       						<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        					<button type="submit" class="btn btn-primary" name="addExistingQuestionToMoment">Add</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>





<!-- POPUP FOR EDIT QUESTION FROM MOMENT -->
<form id="updateQuestion">

			<div class='modal fade' id='EditQuestion' tabindex='-1' role='dialog' aria-labelledby='EditQuestionLabel' aria-hidden='true'>
				<div class='modal-dialog modal-lg' role='document'>
 					<div class='modal-content'>

						<div class='modal-header'>
			  	 	  		<h3 class='modal-title' id='EditQuestionLabel'>Edit Question<button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button></h3>
   						</div>

 						<div class='modal-body'>



							<!-- QUESTIONNAIRE ID -->
<!--  							<input type='hidden' name='questionnaireID'> -->
 							
							<!-- QUESTION ID -->
 							<input type='hidden' name='questionID'>

							<br>
						
							<div class="row">
								<div class="col-sm-12 col-sm-offset-1">
									<h4>Question <span class="question-number"></span></h4>
								</div>
							</div>
							
							<div class="form-horizontal">
							
								<!-- PREMISE -->
								<div class="form-group">
									<label class='col-sm-offset-1 control-label col-sm-2'>Premise:</label>
									<div class='col-sm-8'>
										<input id="update_q_premise" type="text" class="form-control text-center" name="update_q_premise" placeholder="Premise" maxlength="600" required>
									</div>
								</div>
						
								<!-- TYPE -->
								<div class="form-group">
									<label class='col-sm-offset-1 control-label col-sm-2'>Question Type:</label>
									<div class='col-sm-8'>
										<select class="form-control text-center" id="update_q_type" name="update_q_type" style="width:100%; text-align-last:center;" onchange="newThing(event)" required>
<!-- 											<option disabled selected value="">Type</option> -->
											<option value="1">Scaled</option>
											<option value="2">Open Answer</option>
										</select>
									</div>
								</div>
							
							</div>
							
							<!-- MIN/MAX VALUES/TEXT (OPTIONAL) -->
							<div class="range">
								<div class="form-inline col col-sm-offset-2">
									<div class="form-group">
										<label class='control-label col-sm-2'>Min. Value:</label>
										<div class="col-sm-8">
											<input type="number" class="form-control text-center" id="update_min_val" name="update_min_val" value="1" readonly>
										</div>
									</div>
									<div class="form-group">
										<label class='control-label col-sm-2'>Min. Text:</label>
										<div class="col-sm-8">
											<input type="text" class="form-control text-center" id="update_min_text" name="update_min_text" maxlength="40" placeholder="Very Dissatisfied">
										</div>
									</div>
								</div>
							
								<div class="form-inline col col-sm-offset-2">
									<div class="form-group">
										<label class='control-label col-sm-2'>Max. Value:</label>
										<div class="col-sm-1">
											<input type="number" class="form-control text-center" id="update_max_val" name="update_max_val" value="5" min="2">
										</div>
									</div>
									<div class="form-group">
										<label class='control-label col-sm-2'>Max. Text:</label>
										<div class="col-sm-1">
											<input type="text" class="form-control text-center" id="update_max_text" name="update_max_text" maxlength="40" placeholder="Very Satisfied">
										</div>
									</div>
								</div>
								
								<br>
								
							</div>
							
							<div class="form-horizontal">
							
								<!-- CORRESPONDING CATEGORY -->
								<div class="form-group">
									<label class='col-sm-offset-1 control-label col-sm-2'>Category:</label>
									<div class='col-sm-8'>
										<select class="form-control text-center" id="update_q_category" name="update_q_category" style="width: 100%; text-align-last:center;" required>
<!-- 											<option disabled selected value="">Category</option> -->
											<?php
												$queryCategories = "SELECT * FROM category WHERE id IN (SELECT id_category FROM question WHERE id IN (SELECT id_question FROM subquestionnair_question WHERE id_subquestionnair = '$momentID'));";
												$resultCategories = mysqli_query($connection, $queryCategories);
												while($rowCategories = mysqli_fetch_array($resultCategories)):
											?>
												<option value="<?php echo $rowCategories['id']; ?>"><?php echo $rowCategories['categoria']; ?></option>
											<?php endwhile; ?>
										</select>
									</div>
								</div>
								
								<!-- CORRESPONDING SUBCATEGORY -->
								<div class="form-group">
									<label class='col-sm-offset-1 control-label col-sm-2'>Subcategory:</label>
									<div class='col-sm-8'>
										<select class="form-control text-center" id="update_q_subcategory" name="update_q_subcategory" style="width: 100%; text-align-last:center;" required>
<!-- 											<option disabled selected value="">Subcategory</option> -->
											<?php
												$querySubcategories = "SELECT * FROM subcategory WHERE id IN (SELECT id_subcategory FROM question WHERE id IN (SELECT id_question FROM subquestionnair_question WHERE id_subquestionnair = '$momentID'));";
												$resultSubcategories = mysqli_query($connection, $querySubcategories);
												while($rowSubcategories = mysqli_fetch_array($resultSubcategories)):
											?>
												<option value="<?php echo $rowSubcategories['id']; ?>"><?php echo $rowSubcategories['subcategoria']; ?></option>
											<?php endwhile; ?>
										</select>
									</div>
								</div>
							
							</div>
							
							
							<br>
							
	<!-- ERROR ALERT FOR USER -->			
	<div id="error-updateQuestion" class="row" style="display: none;">
		<div class="col-sm-12">
			<div class="alert alert-danger mb-1" role="alert">
				<h4 class="error-lead">Error!</h4>
				<p class="error-description"></p>
			</div>
		</div>
	</div>
	
	

      					</div><!--modal-body-->
      					
      					<!-- SUBMIT OR CANCEL -->
     					<div class='modal-footer'>
       						<button type='button' class='btn btn-default' data-dismiss='modal'>Cancel</button>
       						<button type='button' class='btn btn-danger' data-dismiss='modal' onclick="nextModal('#Delete')">Remove</button>
        					<button type='submit' class='btn btn-primary' name='updateQuestion'>Save Changes</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
</form>


		
		
		
		<!-- POPUP FOR REMOVE QUESTION FROM MOMENT -->
		<form id="removeQuestionFromMoment">
			<div class='modal fade' id='Delete' tabindex='-1' role='dialog' aria-labelledby='DeleteLabel' aria-hidden='true'>
				<div class='modal-dialog modal-dialog-centered' role='document'>
 					<div class='modal-content'>

						<div class='modal-header'>
			  	 	  		<h3 class='modal-title' id='DeleteLabel'>Remove Question<button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button></h3>
   						</div>

 						<div class='modal-body'>



							<!-- MOMENT ID -->
 							<input type='hidden' name='momentID' value="<?php echo $momentID; ?>">
 							
							<!-- QUESTION ID -->
 							<input type='hidden' name='questionID'>
 							
							<span class='text-center'>
								<h3>Are you sure you want to remove Question <span class="question-number"></span>?</h3>
<!-- 								<h3 id="deletePremise" style='color:#ff8f00;'></h3> -->
							</span>
							
							<br>
							<p class='alert alert-warning text-justify'>This action will remove the question from this moment. If you have any problems, contact technical support at: <a class='alert-link' href='#'>support@tania.uprrp.edu</a>.</p>
							<br>




		<!-- ERROR ALERT FOR USER -->			
	<div id="error-removeQuestionFromMoment" class="row" style="display: none;">
		<div class="col-sm-12">
			<div class="alert alert-danger mb-1" role="alert">
				<h4 class="error-lead">Error!</h4>
				<p class="error-description"></p>
			</div>
		</div>
	</div>



      					</div><!--modal-body-->
      					
      					<!-- SUBMIT OR CANCEL -->
     					<div class='modal-footer'>
       						<button type='button' class='btn btn-default' data-dismiss='modal'>Cancel</button>
        					<button type='submit' class='btn btn-danger' name='removeQuestionFromMoment'>Remove Anyway</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>





<script src="js/viewMoment.js"></script>
<script src="js/handleSubmit.js"></script>
<script src="js/editQuestion.js"></script>

<script>

["updateMoment", "removeQuestionFromMoment", "addExistingQuestionToMoment", "updateQuestion", "deleteMoment"].forEach(function(formName) {
			
	var form = document.getElementById(formName);
			
	if(form) {
		form.addEventListener('submit', function(e) {
			handleSubmit(e, formName);
		});
	}
			
});

</script>


		<!-- END OF viewMoment.php -->

<?php include_once 'footer.php'; ?>