<?php

	require_once 'processes/config.php';
	require_once 'processes/dbh.inc.php';
	require_once 'processes/checkLogin.php';
	include_once 'header.php';
	
	// IF USER HAS QUESTIONS, DISPLAY THEM
	// ELSE DON'T DISPLAY THE EMPTY TABLE
	// CAMBIAR $query A "SELECT * FROM `question` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
	$query1 = "SELECT * FROM `question` WHERE `id_category` IN(SELECT `id` FROM `category`) AND `id_subcategory` IN(SELECT `id` FROM `subcategory`);";
	$result1 = mysqli_query($connection, $query1);
	$x = 1;
	
?>  
		<!-- START OF bankOfQuestions.php -->
		<div class='container'>
		
			<div class='row'>
				<div class='col-sm-10'>
					<h2>Bank Of Questions</h2>
					<p>View, add, and delete any question you like so that, later on, you can create a questionnaire.</p>
				</div>
				<div class='col-sm-2'>
					<br><br>
					<button id='adding' type='button' class='btn btn-default' data-toggle='modal' data-target='#New'>Add Question</button>
				</div>
			</div><!--row-->
		
			<div class='row'><div class='col'><br><hr><br></div></div>
			
			<?php if($result1): ?>
			<div class='row'>
				<div class='col'>
					<table class='table table-hover table-striped' id='tableEvaluations'>
						<thead>
							<tr>
								<th><h4>#</h4></th>
								<th><h4>Premise</h4></th>
								<th><h4>Category</h4></th>
								<th><h4>Subcategory</h4></th>
							</tr>
						</thead>
						<tbody>
						<?php
							while($row = mysqli_fetch_array($result1)):
								$subquery1 = "SELECT `category`.`categoria` FROM `category` WHERE `category`.`id`=(SELECT `question`.`id_category` FROM `question` WHERE `question`.`id`=".$row['id'].");";
								$subresult1 = mysqli_query($connection, $subquery1);
								$subrow1 = mysqli_fetch_array($subresult1);
								$subquery2 = "SELECT `subcategory`.`subcategoria` FROM `subcategory` WHERE `subcategory`.`id`=(SELECT `question`.`id_subcategory` FROM `question` WHERE `question`.`id`=".$row['id'].");";
								$subresult2 = mysqli_query($connection, $subquery2);
								$subrow2 = mysqli_fetch_array($subresult2);
						?>
							<tr>
								<th><?php echo $row['id']; ?></th>
								<td class='text-justify' style='width:35em;'><p><?php echo $row['premise']; ?></p></td>
								<td style='width:18em;'><p><?php echo $subrow1['categoria']; ?></p></td>
								<td style='width:18em;'><p><?php echo $subrow2['subcategoria']; ?></p></td>
								<td style='text-align:center;width:10em;'>
									<div class='btn-group'>
										<button type='button' class='btn btn-default btn-sm' data-toggle='modal' data-target='#Edit_<?php echo $row['id']; ?>'>Edit</button>
										<button type='button' class='btn btn-danger btn-sm' data-toggle='modal' data-target='#Delete_<?php echo $row['id']; ?>'>Delete</button>
									</div>
								</td>
							</tr>
						<?php $x++; endwhile; ?>
						</tbody>
					</table>
				</div><!--col-->
			</div><!--row-->
			<?php endif; ?>
			
		</div><!--container-->
		
		<!-- POPUP FOR NEW QUESTION -->
		<form class='form-horizontal' action='processes/insertQuestion.php' method='POST'>
			<div class='modal fade' id='New' tabindex='-1' role='dialog' aria-labelledby='NewLabel' 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='NewLabel'>New 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'>

							
							
							<br>
							<!-- PREMISE -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='question_premise'>Premise:</label>
								<div class='col-sm-7'>
									<textarea type='text' class='form-control' id='question_premise' name='question_premise' rows=4 style='resize:vertical'></textarea>
								</div>
							</div>

							<!-- CATEGORY -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='question_category'>Category:</label>
								<div class='col-sm-7'>
									<select name='question_category' id='question_category' style='width:100%;'>
										<option disabled selected>Select the Category</option>
										<?php
											// CAMBIAR $query A "SELECT `id`, `categoria` FROM `category` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
											$query2 = "SELECT `id`, `categoria` FROM `category`;";
											$result2 = mysqli_query($connection, $query2);
											foreach($result2 as $categorias):
										?>
										<option value="<?php echo $categorias['id']; ?>"><?php echo $categorias['categoria']; ?></option>
										<?php endforeach; ?>
									</select>
 								</div>
							</div>

							<!-- SUBCATEGORY -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='question_subcategory'>Subcategory:</label>
								<div class='col-sm-7'>
									<select name='question_subcategory' id='question_subcategory' style='width:100%;'>
										<option disabled selected>Select the Subcategory</option>
										<?php
											// CAMBIAR $query A "SELECT `id`, `subcategoria` FROM `subcategory` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
											$query3 = "SELECT `id`, `subcategoria` FROM `subcategory`;";
											$result3 = mysqli_query($connection, $query3);
											foreach($result3 as $subcategorias):
										?>
										<option value="<?php echo $subcategorias['id']; ?>"><?php echo $subcategorias['subcategoria']; ?></option>
										<?php endforeach; ?>
									</select>
 								</div>
							</div>

							<!-- TYPE -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='question_type'>Type:</label>
								<div class='col-sm-7'>
									<select name='question_type' id='question_type' style='width:100%;'>
										<option disabled selected>Select the Type of Question</option>
										<?php
											// CAMBIAR $query A "SELECT `id`, `description` FROM `type_of_question` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
											$query4 = "SELECT `id`, `description` FROM `type_of_question`;";
											$result4 = mysqli_query($connection, $query4);
											foreach($result4 as $types):
										?>
										<option value="<?php echo $types['id']; ?>"><?php echo $types['description']; ?></option>
										<?php endforeach; ?>
									</select>
								</div>
							</div>

							<!-- REFERENCE -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='question_reference'>Reference:</label>
								<div class='col-sm-7'>
									<select name='question_reference' id='question_reference' style='width:100%;'>
										<option disabled selected>Select the Reference</option>
										<?php
											// CAMBIAR $query A "SELECT `id`, `referencia` FROM `reference` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
											$query5 = "SELECT `id`, `referencia` FROM `reference`;";
											$result5 = mysqli_query($connection, $query5);
											foreach($result5 as $references):
										?>
										<option value="<?php echo $references['id']; ?>"><?php echo $references['referencia']; ?></option>
										<?php endforeach; ?>
									</select>
								</div>
							</div>
							<br>



      					</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='insertQuestion' disabled>Save</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>
		
		<?php $result1 = mysqli_query($connection, $query1); ?>
		<?php while($row = mysqli_fetch_array($result1)): ?>
		<!-- POPUP FOR EDIT QUESTION -->
		<form class='form-horizontal' action='processes/updateQuestion.php' method='POST'>
			<div class='modal fade' id='Edit_<?php echo $row['id']; ?>' tabindex='-1' role='dialog' aria-labelledby='EditLabel_<?php echo $row['id']; ?>' 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='EditLabel_<?php echo $row['id']; ?>'>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'>

							
							
							<br>
							<!-- PREMISE -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='newPremise'>Premise:</label>
								<div class='col-sm-7'>
									<textarea type='text' class='form-control' id='newPremise' name='newPremise' rows=4 style='resize:vertical'><?php echo $row['premise']; ?></textarea>
								</div>
							</div>

							<!-- CATEGORY -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='newCategory'>Category:</label>
								<div class='col-sm-7'>
									<select name='newCategory' id='newCategory' style='width:100%;'>
										<option disabled selected>Select the Category</option>
										<?php
											// CAMBIAR $query A "SELECT `id`, `categoria` FROM `category` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
											$query2 = "SELECT `id`, `categoria` FROM `category`;";
											$result2 = mysqli_query($connection, $query2);
											foreach($result2 as $categorias):
										?>
										<option value="<?php echo $categorias['id']; ?>"><?php echo $categorias['categoria']; ?></option>
										<?php endforeach; ?>
									</select>
 								</div>
							</div>

							<!-- SUBCATEGORY -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='newSubcategory'>Subcategory:</label>
								<div class='col-sm-7'>
									<select name='newSubcategory' id='newSubcategory' style='width:100%;'>
										<option disabled selected>Select the Subcategory</option>
										<?php
											// CAMBIAR $query A "SELECT `id`, `subcategoria` FROM `subcategory` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
											$query3 = "SELECT `id`, `subcategoria` FROM `subcategory`;";
											$result3 = mysqli_query($connection, $query3);
											foreach($result3 as $subcategorias):
										?>
										<option value="<?php echo $subcategorias['id']; ?>"><?php echo $subcategorias['subcategoria']; ?></option>
										<?php endforeach; ?>
									</select>
 								</div>
							</div>

							<!-- TYPE -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='newType'>Type:</label>
								<div class='col-sm-7'>
									<select name='newType' id='newType' style='width:100%;'>
										<option disabled selected>Select the Type of Question</option>
										<?php
											// CAMBIAR $query A "SELECT `id`, `description` FROM `type_of_question` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
											$query4 = "SELECT `id`, `description` FROM `type_of_question`;";
											$result4 = mysqli_query($connection, $query4);
											foreach($result4 as $types):
										?>
										<option value="<?php echo $types['id']; ?>"><?php echo $types['description']; ?></option>
										<?php endforeach; ?>
									</select>
								</div>
							</div>

							<!-- REFERENCE -->
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-2' for='newReference'>Reference:</label>
								<div class='col-sm-7'>
									<select name='newReference' id='newReference' style='width:100%;'>
										<option disabled selected>Select the Reference</option>
										<?php
											// CAMBIAR $query A "SELECT `id`, `referencia` FROM `reference` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
											$query5 = "SELECT `id`, `referencia` FROM `reference`;";
											$result5 = mysqli_query($connection, $query5);
											foreach($result5 as $references):
										?>
										<option value="<?php echo $references['id']; ?>"><?php echo $references['referencia']; ?></option>
										<?php endforeach; ?>
									</select>
								</div>
							</div>
							<br>



      					</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='updateQuestion' disabled>Save</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>
		
		<!-- POPUP FOR DELETE QUESTION -->
		<form class='form-horizontal' action='processes/deleteQuestion.php' method='POST'>
			<div class='modal fade' id='Delete_<?php echo $row['id']; ?>' tabindex='-1' role='dialog' aria-labelledby='DeleteLabel_<?php echo $row['id']; ?>' 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_<?php echo $row['id']; ?>'>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'>



 							<!-- ID -->
 							<input type='hidden' name='id' value='<?php echo $row['id']; ?>'>
 							
							<span class='text-center'>
								<h3>Are you sure you want to delete this question?</h3>
								<h3 style='color:#ff8f00;'><?php echo $row['premise']; ?></h3>
							</span>
							
							<br>
							<p class='alert alert-warning text-justify'>This action will delete the question, removing it from existing <b>questionnaires and subquestionnaires</b>, while also deleting <b>all results</b> that were made from this question. You should be aware of this and opt to edit manually any affected questionnaires and subquestionnaires in the following pages: <a class='alert-link' href='bankOfQuestions.php'>questions</a> and <a class='alert-link' href='questionnaires.php'>questionnaires</a>.</p>
							<br>
							
							
							
      					</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='deleteQuestion' disabled>Delete Anyway</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>
		<?php endwhile; ?>		
		<!-- END OF bankOfQuestions.php -->

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