<?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();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");
if(empty($_POST['id']))
{
	header("location: index.php");
	exit;
}
$catId = (int) $_POST['id'];
if(empty($catId))
{
	header("location: index.php");
	exit;
}
$querySubcatsAndNums = "SELECT `sub_category`.`id`, `sub_category`.`name`,`".$_SESSION['user_number']."`.`sub_cat`, COUNT(`".$_SESSION['user_number']."`.`id`) AS `numCount` ";
$querySubcatsAndNums.= "FROM `sub_category`, `".$_SESSION['user_number']."` ";
$querySubcatsAndNums.= "WHERE `sub_category`.`cat_id`=$catId AND `".$_SESSION['user_number']."`.`sub_cat` = `sub_category`.`id` AND `sub_category`.`user_id`=$_SESSION[userId] ";
$querySubcatsAndNums.= "GROUP BY `".$_SESSION['user_number']."`.`sub_cat` ";
$querySubcatsAndNums.= "ORDER BY `sub_category`.`id` ASC";

$resultSubcatsAndNums = mysqli_query($conn, $querySubcatsAndNums);
$numSubcatsAndNums = mysqli_num_rows($resultSubcatsAndNums);
$subCatTDs = '';
if(!empty($numSubcatsAndNums))
{
	$subCatTDs = '<form name="allNumbers" id="allNumbersId" action="allNumberAdd.php" method="post">';
	$subCatTDs.= '<div align="center" style="max-height:250px; padding:0px; margin:0px">';
	$subCatTDs.= '<table align="center" border="0" width="100%" cellpadding="3" cellspacing="0" style="border-collapse:collapse; margin:0px">';
	while($rowSubcatsAndNums = mysqli_fetch_assoc($resultSubcatsAndNums))
	{
		$subCatTDs.= '	<tr>';
		$subCatTDs.= '		<td><input type="checkbox" name="subCatsIds[]" value="'.$rowSubcatsAndNums['id'].'" id="subCat'.$rowSubcatsAndNums['id'].'"><a href="#" id="subCatLink'.$rowSubcatsAndNums['id'].'" rdata="'.$rowSubcatsAndNums['id'].'">'.$rowSubcatsAndNums['name'].'<b style="direction:ltr"> ('.$rowSubcatsAndNums['numCount'].')</b></a></td>';
		$subCatTDs.= '	</tr>';
		
		$queryNumsCats = "SELECT `categories`.`id`, `categories`.`name`, COUNT(`".$_SESSION['user_number']."`.`id`) AS `numCount` ";
		$queryNumsCats.= "FROM `categories`, `".$_SESSION['user_number']."` ";
		$queryNumsCats.= "WHERE `categories`.`view`='0' AND `".$_SESSION['user_number']."`.`cateUser` = `categories`.`id` AND `".$_SESSION['user_number']."`.`sub_cat`=$rowSubcatsAndNums[id] AND `".$_SESSION['user_number']."`.`sub_cat`=$rowSubcatsAndNums[id] AND `".$_SESSION['user_number']."`.`userId`=$_SESSION[userId] AND `".$_SESSION['user_number']."`.`categoryId`=$catId ";
		$queryNumsCats.= "GROUP BY `categories`.`id` ";
		$queryNumsCats.= "ORDER BY  `categories`.`id` ASC";
		$resultNumsCats = mysqli_query($conn, $queryNumsCats);
		$numNumsCats = mysqli_num_rows($resultNumsCats);
		if(!empty($numNumsCats))
		{
			$subCatTDs.= '	<tr id="numsCatsRow'.$rowSubcatsAndNums['id'].'" style="display:none" allNumCats="'.$numNumsCats.'">';
			$subCatTDs.= '		<td>';
			$subCatTDs.= '		<table align="center" border="0" cellpadding="3" cellspacing="0" style="border-collapse:collapse; margin:0px; margin-<?php echo $align?>:25px">';
			while($rowNumsCats = mysqli_fetch_assoc($resultNumsCats))
			{
				$subCatTDs.= '	<tr>';
				$subCatTDs.= '		<td style="white-space:nowarp"><input type="checkbox" name="numsCatsIds[]" value="'.$rowSubcatsAndNums['id'].'-'.$rowNumsCats['id'].'" id="numsCat'.$rowSubcatsAndNums['id'].'-'.$rowNumsCats['id'].'" fatherId="subCat'.$rowSubcatsAndNums['id'].'"><a href="#" id="numsCatLink'.$rowSubcatsAndNums['id'].'-'.$rowNumsCats['id'].'">'.$rowNumsCats['name'].'<b style="direction:ltr"> ('.$rowNumsCats['numCount'].')</b></a></td>';
				$subCatTDs.= '	</tr>';
			$subCatTDs.= '		</table>';
			$subCatTDs.= '		</td>';			
			$subCatTDs.= '	<tr>';
		}
			$queryNumbers = "SELECT `id`, `name`, `number` ";
	$queryNumbers.= "FROM `".$_SESSION['user_number']."` ";
	$queryNumbers.= "WHERE `sub_cat`=$rowSubcatsAndNums[id] AND `userId`=$_SESSION[userId] AND `categoryId`=$catId ";
	$queryNumbers.= "ORDER BY BINARY `name` ASC";
	$resultNumbers = mysqli_query($conn, $queryNumbers);
	if($resultNumbers)
	$numNumbers = mysqli_num_rows($resultNumbers);
	if(!empty($numNumbers))
	{
		$subCatTDs.= '	<tr id="numbersRow'.$rowSubcatsAndNums['id'].'" style="display:none" allNumbers="'.$numNumbers.'">';
		$subCatTDs.= '		<td><div align="center" style="max-height: 200;overflow: auto;text-align: center;">';
		$subCatTDs.= '		<table align="center" width="300px" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; margin:0px; margin-'.$align2.':25px; float:'.$align2.';background-color:#FFF">';
		$subCatTDs.= '		<tr>';
		$subCatTDs.= '			<td style="white-space:nowarp; text-align:center"><b>'.$sendMsgLBL["numbers"].'</b></td>';
			$subCatTDs.= '			<td style="white-space:nowarp; text-align:center"><b>'.$sendMsgLBL["names"].'</b></td>';
		$subCatTDs.= '		</tr>';							
		while($rowNumbers = mysqli_fetch_assoc($resultNumbers))
		{
			$subCatTDs.= '	<tr>';
			$subCatTDs.= '		<td style="white-space:nowarp; width:150px"><input type="checkbox" name="numbersIds[]" value="'.$rowNumbers['id'].'" id="numId'.$rowNumbers['id'].'" fatherId="subCat'.$rowSubcatsAndNums['id'].'">&nbsp;'.$rowNumbers['number'].'</td>';
			$subCatTDs.= '		<td style="white-space:nowarp; width:150px"><span style="font-weight:bold">'.$rowNumbers['name'].'</span></td>';
			$subCatTDs.= '	</tr>';						
		}
		$subCatTDs.= '		</table>';
		$subCatTDs.= '		</div></td>';
		$subCatTDs.= '	</tr>';							
	}
	}
	$subCatTDs.= '<tr>';
	$subCatTDs.= '<td colspan="3" align="center" height="35px" valign="bottom"><input type="button" class="btn bg-primary btn-flat text-white m-1" value="'.$button["add"].'" id="nmberAddId"></td>';
	$subCatTDs.= '</tr>';

	
	$subCatTDs.= '</table>';
	$subCatTDs.= '</div>';
	$subCatTDs.= '</form>';
	$subCatTDs.= '<script type="text/javascript">';
//	$subCatTDs.= '$("a[id^=\'subCatLink\']").click( function () {
//		var numsCatsRowId = $(this).attr(\'id\');
//		numsCatsRowId = numsCatsRowId.replace("subCatLink", "numsCatsRow");
//		$("#"+numsCatsRowId).toggle();
//	});';
	
//	$subCatTDs.= '$("a[id^=\'numsCatLink\']").click( function () {
//		var numsRanksRowId = $(this).attr(\'id\');
//		numsRanksRowId = numsRanksRowId.replace("numsCatLink", "numsRanksRow");
//		$("#"+numsRanksRowId).toggle();
//	});';
	if(empty($_POST["share"])){
		$subCatTDs.= '$("a[id^=\'subCatLink\']").click( function () {
			var numbersRowId = $(this).attr(\'rdata\');
			$("#numbersRow"+numbersRowId).toggle();
		});';
	}
	$subCatTDs.= '$("input[id^=\'subCat\']").click( function () {
		var checkStatus = $(this).prop("checked");
		var trId = $(this).attr(\'id\');
		trId = trId.replace("subCat", "numsCatsRow");
			$("#"+trId+":input[type=\'checkbox\']").each(function (){
				$(this).prop(\'checked\', checkStatus);	
			});
	});';
	
	$subCatTDs.= '$("input[id^=\'numsCat\']").click( function () {
		var checkStatus = $(this).prop("checked");
		var trId = $(this).attr(\'id\');
		trId = trId.replace("numsCat", "numsRanksRow");
			$("#"+trId+":input[type=\'checkbox\']").each(function (){
				$(this).prop(\'checked\', checkStatus);
			});
		if(checkStatus == false)
		{
			myFirstFatherId = $(this).attr("fatherId");
			$("#"+myFirstFatherId).prop(\'checked\', false);
		}
		else
		{
			mySecondFatherTable = $(this).closest(\'table\');
			myThirdFatherId = $(this).attr("fatherId");
			if($("input[fatherId=\'"+myThirdFatherId+"\']:checked").length == $(mySecondFatherTable).closest(\'tr\').attr(\'allNumCats\'))
				$("#"+myThirdFatherId).prop(\'checked\', true);				
		}
	});';
	
	$subCatTDs.= '$("input[id^=\'subCat\']").click( function () {
		var checkStatus = $(this).prop("checked");
		var trId = $(this).attr(\'id\');
		$("input[fatherid='."'".'"+trId+"'."'".']").each(function (){
			$(this).prop("checked", checkStatus);
		});
		
	});';

	$subCatTDs.= '$("input[id^=\'numId\']").click( function () {
		var checkStatus = $(this).prop("checked");
		if(checkStatus == false)
		{
			myFirstFatherId = $(this).attr("fatherId");
			$("#"+myFirstFatherId).prop(\'checked\', false);
			
			mySecondFatherId = $("#"+myFirstFatherId).attr("fatherId");
			$("#"+mySecondFatherId).prop(\'checked\', false);
			
			myThirdFatherId = $("#"+mySecondFatherId).attr("fatherId");
			$("#"+myThirdFatherId).prop(\'checked\', false);			
		}
		else
		{
			var myTable = $(this).closest(\'table\');
			if($(\'td input[type="checkbox"]:checked\',myTable).length == $(myTable).closest(\'tr\').attr(\'allNumbers\'))
			{
				myFirstFatherId = $(this).attr("fatherId");
				$("#"+myFirstFatherId).prop(\'checked\', true);
				
				
				myFirstFatherTable = $("#"+myFirstFatherId).closest(\'table\');
				mySecondFatherId = $("#"+myFirstFatherId).attr("fatherId");
				if($("input[fatherId=\'"+mySecondFatherId+"\']:checked").length == $(myFirstFatherTable).closest(\'tr\').attr(\'allNumRanks\'))
					$("#"+mySecondFatherId).prop(\'checked\', true);
					
				mySecondFatherTable = $("#"+mySecondFatherId).closest(\'table\');
				myThirdFatherId = $("#"+mySecondFatherId).attr("fatherId");
				if($("input[fatherId=\'"+myThirdFatherId+"\']:checked").length == $(mySecondFatherTable).closest(\'tr\').attr(\'allNumCats\'))
					$("#"+myThirdFatherId).prop(\'checked\', true);				
					
			}
		}
	});';	
//	$subCatTDs.= '$("#nmberAddId").click(function(){
//				    var valuesNum = new Array();
//					if($("input:checkbox[name=\'numbersIds[]\']").length) {	
//						$.each($("input:checkbox[name=\'numbersIds[]\']:checked"), function(){
//							valuesNum.push($(this).val());
//						});	
//					}
//					var numSelected = valuesNum;
//					var oldNums = $("#numbers").val();
//					$.post("allNumberAdminAdd.php",{numSelectedAll: ""+numSelected,oldNumsAdd: oldNums,rand:Math.random() } ,function(resNumm){
//							$("#numbers").val(resNumm);
//							closebox("box22");
//						});
//				});';
	$subCatTDs.= '$("#nmberAddId").click(function(){
				    var valuesNum = new Array();
					if($("input:checkbox[name=\'subCatsIds[]\']").length) {	
						$.each($("input:checkbox[name=\'subCatsIds[]\']:checked"), function(){
							valuesNum.push($(this).val());
						});	
					}
					var numSelected = valuesNum;
				    var valuesNum2 = new Array();
					if($("input:checkbox[name=\'numbersIds[]\']").length) {	
						$.each($("input:checkbox[name=\'numbersIds[]\']:checked"), function(){
							valuesNum2.push($(this).val());
						});	
					}
					var numSelected2 = valuesNum2;
					var oldNums = $("#numbers").val();
					$.post("allNumberAdminAdd.php",{numSelectedAll: ""+numSelected,numSelectedAll2: ""+numSelected2,oldNumsAdd: oldNums,rand:Math.random() } ,function(resNumm){
							$("#numbers").val(resNumm);
							$("#catgoriesNumbersModal").modal("hide");
						});
				});';

	$subCatTDs.= 'if($("#mainCategory"+'.$catId.').prop("checked")) {
				  	$("#allNumbersId input[type=\'checkbox\']").each( function() {
						$(this).prop("checked", true)
					})
				  }';
	$subCatTDs.= '</script>';
	
}
else
	$subCatTDs = '<center><b>'.$readyMsgLBL["noData"].'</b></center>';
echo $subCatTDs;
exit;
ob_end_flush();
?>
