<?php 
	
	require_once 'processes/config.php';
	require_once 'processes/dbh.inc.php';
	require_once 'processes/checkLogin.php';
	include_once 'header.php';
	
	// IF USER HAS CATEGORIES, DISPLAY THEM
	// ELSE DON'T DISPLAY THE EMPTY TABLE
	// CAMBIAR $query A "SELECT * FROM `category` WHERE `id_researcher`='".$dbUserData['id_researcher']."';"
	$query1 = "SELECT * FROM `category`;";
	$result1 = mysqli_query($connection, $query1);
	$x = 1;
	
?>
		
		<!-- START OF categories.php -->
		<div class='container'>
		
			<div class='row'>
				<div class='col-sm-10'>
					<h2>Categories</h2>
					<p>View, add, and delete any category you like so that, later on, you can categorize a question. You can also view, add, or delete any subcategory.</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 Category</button>
				</div>
			</div><!--row-->
			
			<div class='row'><div class='col'><br><hr><br></div></div>
			
			<?php if($result1): ?>
			<div class='row'>
				<div class='col'>
					<!-- DISPLAY AVAILABLE CATEGORIES -->
					<table class='table table-hover table-striped' id='tableEvaluations' align='center'>
						<thead>
							<tr>
								<th><h4>#</h4></th>
								<th><h4>Category</h4></th>
								<th><h4>Related Subcategories</h4></th>
							</tr>
						</thead>
						<tbody>
						<?php while($row = mysqli_fetch_array($result1)): ?>
							<tr>
								<th><?php echo $row['id']; ?></th>
								<td><?php echo $row['categoria']; ?></td>
								<td>
									<ul>
									<?php 
										$query2 = "SELECT * FROM `subcategory` WHERE `id_categoria`=".$row['id'].";";
										$result2 = mysqli_query($connection, $query2);
										while($subrow = mysqli_fetch_array($result2)):
									?>
										<li><?php echo $subrow['subcategoria']; ?></li>
									<?php endwhile; ?>
									</ul>
								</td>
								<td style='text-align:center;'>
									<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 CATEGORY -->
		<form class='form-horizontal' action='processes/insertCategory.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 modal-lg' role='document'>
 					<div class='modal-content'>
 						<div class='modal-body'>



							<!-- CLOSE BUTTON -->
							<button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>

							<!-- NEW TITLE -->
							<br>
							<div class='form-group'>
								<label class='col-sm-2 control-label'>Title:</label>
								<div class='col-sm-7'>
   									<input class='form-control' type='text' name='categoria'>
 								</div>
 								<button type='submit' class='btn btn-primary' name='insertCategory' disabled>Save</button>
       						</div>



      					</div><!--modal-body-->
    				</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 CATEGORY -->
		<form class='form-horizontal' action='processes/updateCategory.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 Category<button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button></h3>
   						</div>
 						
 						<div class='modal-body'>

							
							
							<span class='text-center'>
								<h3 style='color:#ff8f00;'><?php echo $row['categoria']; ?></h3>
								<br>
								<h4>Subcategories:</h4>
								<h6>(check the ones you wish to delete)</h6>
							</span>
							<br>
							<div class='row'>
								<div class='col'>
									<ul>
									<?php 
										$query2 = "SELECT * FROM `subcategory` WHERE `id_categoria`=".$row['id'].";";
										$result2 = mysqli_query($connection, $query2);
										while($subrow = mysqli_fetch_array($result2)):
									?>
										<div class='row'>
											<li class='col-sm-offset-3 col-sm-5'><?php echo $subrow['subcategoria']; ?></li>
											<input class='col-sm-1' type='checkbox' name='deleteSubcategory_<?php $subrow['id']; ?>' value='<?php $subrow['id']; ?>'>
											<!--<li style='list-style:none;' class='col-sm-3'><a class='btn btn-danger btn-xs' href='#'>Remove</a></li>-->
										</div>
										<br>
									<?php endwhile; ?>
									</ul>
								</div>
							</div>
							<br>
							<div class='form-group'>
								<label class='col-sm-offset-1 control-label col-sm-3' for='newSubcategory'>Add Subcategory:</label>
								<div class='col-sm-5' id='addQuestionnaires'>
									<!-- OJO TENGO QUE NOMBRAR EL ID DEL SELECT DIFERENTE-->
									<select id='questionnaire' name='newSubcategory' style='width:100%;'>
										<option disabled selected>Select the Subcategory</option>
										<?php
											// CAMBIAR $query A "SELECT * FROM `subcategory` WHERE `id_categoria`=".$row['id']." AND `id_researcher`='".$dbUserData['id_researcher']."';"
											$query3 = "SELECT * FROM `subcategory`;";
											$result3 = mysqli_query($connection, $query3);
											foreach($result3 as $subcategories):
										?>
										<option value="<?php echo $subcategories['id']; ?>"><?php echo $subcategories['subcategoria']; ?></option>
										<?php endforeach; ?>
									</select>
								</div>
								<!-- ADD MORE SUBCATEGORIES -->
								<div class='col-sm-2'>
									<button type='button' class='btn btn-xs btn-warning' onclick='AddQuestionnaire()' style='color: white;'><b>Add More</b></button>
								</div>
							</div>
							


							
							
							
							<br>
							<p class='alert alert-warning text-justify'>Deleting any selected subcategory will change every <b>question</b> associated with it to have an <b>unknown subcategory</b>. You should be aware of this and opt to edit manually any affected question in the following page: <a class='alert-link' href='bankOfQuestions.php'>questions</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-primary' name='updateCategory' disabled>Save</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>
		
		<!-- POPUP FOR DELETE CATEGORY -->
		<form class='form-horizontal' action='processes/deleteCategory.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 Category<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 category?</h3>
								<h3 style='color:#ff8f00;'><?php echo $row['categoria']; ?></h3>
							</span>
							
							<br>
							<p class='alert alert-warning text-justify'>This action will delete the category and change every <b>question</b> associated with it to have an <b>unknown category (and subcategory)</b>. It will also delete any associated subcategory stored in the database. You should be aware of this and opt to edit manually any affected question in the following page: <a class='alert-link' href='bankOfQuestions.php'>questions</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='deleteCategory' disabled>Delete Anyway</button>
      					</div>
      					
    				</div><!--modal-content-->
  				</div><!--modal-dialog-->
			</div><!--modal-->
		</form>
		<?php endwhile; ?>
		<!-- END OF categories.php -->

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