<?php
/**
 * Processing Forget password form data:
 * check data then generate new password and send it through SMPT Email
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 * @todo           Nothing
 */
ob_start();
session_start();
if(empty($_POST['id'])){
	exit;
}
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");

$id=$_POST['id'];
$queryCatsAndNums = "SELECT 
".$_SESSION['user_category'].".`id` AS `catId`, 
".$_SESSION['user_category'].".`name` AS `catName`,
sub_category.`id` AS `subCatId`,
sub_category.`name` AS `subCatName`,
COUNT(".$_SESSION['user_number'].".`id`) AS counter
 ";
$queryCatsAndNums.= "FROM ".$_SESSION['user_number'].", ".$_SESSION['user_category'].", sub_category ";
$queryCatsAndNums.= "WHERE ".$_SESSION['user_category'].".`userId`=$_SESSION[userId] AND sub_category.`cat_id`=".$_SESSION['user_category'].".`id` AND ".$_SESSION['user_number'].".`categoryId`=".$_SESSION['user_category'].".`id` AND ".$_SESSION['user_number'].".`sub_cat`=sub_category.`id` ";
$queryCatsAndNums.= "GROUP BY ".$_SESSION['user_number'].".`sub_cat` ";
$queryCatsAndNums.= "ORDER BY `catId` ASC";
$resultCatsAndNums = mysqli_query($conn, $queryCatsAndNums);
$numCatsAndNums = mysqli_num_rows($resultCatsAndNums);
if(!empty($numCatsAndNums)) {
	$catgroiesDataArr = array();
	$catgroiesDataArrDuplicat = array();
	while($rowCatsAndNums = mysqli_fetch_assoc($resultCatsAndNums))
		$catgroiesDataArr[$rowCatsAndNums["catId"]][] = array($rowCatsAndNums["catName"], $rowCatsAndNums["subCatId"], $rowCatsAndNums["subCatName"], $rowCatsAndNums["counter"]);
	$catgroiesDataArrDuplicat = $catgroiesDataArr;					
	$resU=mysqli_query($conn, "SELECT `admin_groups` FROM `user_login` WHERE `id`=".$id);
	$admin_groups=array();
		if($resU) {
			$rowU=mysqli_fetch_assoc($resU);
			if(!empty($rowU["admin_groups"])){
				$admin_groups=array_filter(explode(",",$rowU["admin_groups"]));
			}
		}
	?>
		<?php
		$catCounter = 0;
		$subCatTDs = '';					
		
		if(!empty($catgroiesDataArr)) {
		foreach($catgroiesDataArr AS $catgoryId => $subCatgoriesArray) {
			$subCatCounter = 0;											
			if($catCounter == 0)
			?>
            	<div class="form-group form-check">
                <input <?php echo (in_array($catgoryId,$admin_groups))?"checked":""?> class="form-check-input" type="checkbox" name="categoryId[]" id="mainCategory<?php echo $catgoryId;?>" value="<?php echo $catgoryId;?>">
                <label class="form-check-label myModalLabel" for="mainCategory<?php echo $catgoryId;?>">
                   <?php echo $subCatgoriesArray[0][0];?> 
                </label>
                <div id="reportResultNumber<?php echo $catgoryId;?>" style="margin: 0 30px 0 0;display: none"></div>
               </div>
			<?php
			++$catCounter;
		}
		
	}
	?>
   	 		<div class="form-group">
                <label class="form-check-label myModalLabel" style="color:red;font-size:12px">
                    <?php echo $usersLBL["shareNote"];?>
                </label>
               </div>
		<script>
			$(document).ready(function(){
				 if($("input[name='categoryId[]']").is(":checked") && $("input[name='categoryId[]']:checked").length == 1){
					 $("#unCheckAll").prop('checked',false);
				 }			
				 if($("input[name='categoryId[]']").length > $("input[name='categoryId[]']:checked").length){
					 $("#checkAll").prop('checked',false);
				 }
				if($("input[name='categoryId[]']").length == $("input[name='categoryId[]']:checked").length){
					 $("#checkAll").prop('checked',true);
				 }
			});
			$("input[name='categoryId[]']").click(function(){			
				 if($("input[name='categoryId[]']").is(":checked") && $("input[name='categoryId[]']:checked").length == 1){
					 $("#unCheckAll").prop('checked',false);
				 }			
				 if($("input[name='categoryId[]']").length > $("input[name='categoryId[]']:checked").length){
					 $("#checkAll").prop('checked',false);
				 }
				if($("input[name='categoryId[]']").length == $("input[name='categoryId[]']:checked").length){
					 $("#checkAll").prop('checked',true);
				 }
		 	});
			</script>
            
<?php
}
ob_end_flush();
?>
