<?php
/**
 * Data Grid Page Example
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    18-08-2020
 * @edited_by
 * @last_edit_date
 * @todo           Nothing
 */
ob_start();
session_start();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
$dataTable=true;
include("validateLogin.php");
include("includeInformation.php");
include("header.php");

$idCategory = (integer) $_POST['idCategory'];
$subCateSer = (integer) $_POST['subCateSer'];
if(empty($idCategory) && empty($subCateSer))
{
	header("location: number.php");
	exit;
}
$resultGroup = mysqli_query($conn, "SELECT `name` FROM `$userCategory` WHERE `userId`='".$_SESSION["userId"]."' AND `id`='$idCategory'");
$rowGroup = mysqli_fetch_assoc($resultGroup);
$categoryName = $rowGroup['name'];
if(empty($_POST["start"]))
	$_POST["start"] = 0;
$_POST["start"] = (integer)$_POST["start"];
if(empty($_POST["numberResult"]))
	$_POST["numberResult"]=25;
$number = array();	
$selNumber = "";
$serSubCateSerch="";
if(!empty($subCateSer))
	$serSubCateSerch="AND `sub_cat`='$subCateSer'";
$resultNumber = mysqli_query($conn, "SELECT `number` FROM `$userNumber` WHERE `userId`='".$_SESSION['userId']."' AND `categoryId`='$idCategory' $serSubCateSerch GROUP BY `number` HAVING COUNT(id) > 1  ORDER BY `number` DESC LIMIT $_POST[start], $_POST[numberResult]");
if(mysqli_num_rows($resultNumber))
{
	while($rowNumber = mysqli_fetch_assoc($resultNumber))
		$number[] = $rowNumber['number'];
	$selNumber = implode(',', $number);
}

?>
<script>
function deletePage2(start)
{
	var doDelete = false;
	for (var k=0; k<document.formList.counter.value; k++)
	{
		var allcheckName = 'allcheck' + k;
		if ($('#'+allcheckName).is(":checked"))
			doDelete = true;
	}
	if(doDelete){		
		var result = confirm("<?php echo $confirmAlert;?>");
		if (result == true) {
			document.formList.start.value=0;
			document.formList.action ='numberDelete.php';
			document.formList.submit();
		}
	}
}
function allcheck_2()
{
	for (var k=0; k<document.formList.counter.value; k++)
	{
		if (document.formList.checkall.checked === true)
		{
			document.getElementById('allcheck' + k).checked = true;
		}
		else
		{
			document.getElementById('allcheck' + k).checked = false;
		}
	}
}
function pageing(url,start)
{
	document.formList.start.value=start;
	document.formList.action =url;
	document.formList.submit();
}
function filter()
{
	if($('#subCatefilter').length)
		$('#subCatefilter').val("");
	document.formList.start.value="";
	document.formList.submit();
}
function filter2()
{
	document.formList.start.value="";
	document.formList.submit();
}
function fetchDate(id)
{	
	$("#name").val('');
	$("#number").val('');
	$("#city").val('');
	$("#categoryId").val('');
	$("#cateUserSer").val('');
	$("#rankSer").val('');
	$("#CatId").val('');
	$("#numberId22").val(id);
	$.post("fetchNumberData.php",{ id:id,rand:Math.random() } ,function(res){
		data=res.split('#$#');
		$("#name").val(data[1]);
		$("#number").val(data[2]);
		$("#city").val(data[3]);
		$("#categoryId").val(data[0]);
		$("#CatId").val(data[5]);
		$("#cateUserSer").val(data[4]);
		$("#categoryId").change();
		$("#cateUserSer").change();
	});
	$('#editModal').modal('show');
}
</script>
<!-- start main-content -->
<div class="main-content">
    <!-- Start Page-content -->
    <div class="page-content">
        <!-- Page-Title -->
        <div class="page-title-box">
            <div class="container-fluid">
                <div class="row align-items-center">
                    <?php echo breadcrumbs(basename($_SERVER['PHP_SELF']));?>
                </div>
            </div>
        </div>
        <!-- end page title end breadcrumb -->
        <?php
		
		$url = "number.php"; 
		$idTemp = "";
        ?>  
        <div class="page-content-wrapper">
            <div class="container-fluid">
                <div class="row justify-content-center align-items-center table-hover">
                    <div class="col-12">
                        <div class="card">
                        <?php
					if(!empty($_GET["res"]))
						echo result($_GET["res"]);
					?>
                        <form name="formList" action="number.php" method="post" style="margin:0px">
                            <div class="card-body">
                               <div class="table-responsive">
                                   <table class="table table-bordered">
                                      <thead>
                                         <tr>
                                         	<th scope="col"><input type="checkbox" name="checkall" onclick="allcheck_2();"></th>
                                            <th scope="col"><?php echo $numberLBL["category"];?></th>
                                            <th scope="col"><?php echo $numberLBL["subCategory"];?></th>
                                            <th scope="col"><?php echo $numberLBL["name"];?></th>
                                            <th scope="col"><?php echo $numberLBL["mobile"];?></th>
                                            <th scope="col"><?php echo $numberLBL["city"];?></th>
                                            <th scope="col"><?php echo $button["edit"];?></th>
                                         </tr>
                                      </thead>
                                      <tbody>
                                      <?php
                                        if(!empty($selNumber)) {
											$resultNumberRep = mysqli_query($conn, "SELECT `id`,`name`,`number`,`city`,`sub_cat`,`cateUser` FROM `$userNumber` WHERE `userId`='".$_SESSION['userId']."' AND `categoryId`='$idCategory' $serSubCateSerch AND `number` IN ($selNumber) ORDER BY `number` DESC");
											$i=0;
                                           while($row = mysqli_fetch_assoc($resultNumberRep)){
												$idTemp .=$row[0]." ";
                                        ?>
                                         <tr>
                                         	<td><input type="checkbox" value="<?php echo $row['id'];?>" name="allcheck<?php echo $row['id'];?>" id="allcheck<?php echo $i++;?>"></td>
                                            <td><?php echo $categoryName;?></td>
                                            <td>
											<?php
												$querySelectSuCat = "SELECT `name` FROM `sub_category` WHERE `id`='$row[sub_cat]'";
												$resultSelectSuCat = mysqli_query($conn, $querySelectSuCat);
												$rowSelectSuCat = mysqli_fetch_assoc($resultSelectSuCat);
											 echo $rowSelectSuCat['name'] ;
											 ?>
										   </td>
                                           <td><?php echo $row['name']?></td>
                                           <td><?php echo $row['number']?></td>
                                           <td><?php echo $row['city']?></td>
                                           <td><a href="javascript:fetchDate('<?php echo $row[0]?>')"><i class="fas fa-2x fa-edit" title="<?php echo $button["edit"];?>"></i></a></td>
                                         </tr>
                                         <?php
                                                }
                                            } else {
                                        ?>
                                        <tr>
                                            <td colspan="12" style="text-align:center;font-weight:bold"><?php echo $tempMsgLBL["noData"];?></td>
                                         </tr>
                                        <?php
                                            }
                                        ?>
                                      </tbody>
                                   </table>
                               </div>
                               
                               <div class="row">
                               	<?php if($numCounter > $_POST["numberResult"]) {?>
                                  <div class="col-sm-12 col-md-5">
                                     <div class="dataTables_info" id="example_info" role="status" aria-live="polite"><?php echo $dataGtidLBL["show"];?> <?php echo $_POST["start"]+1;?> <?php echo $dataGtidLBL["to"];?> <?php echo $_POST["numberResult"];?> <?php echo $dataGtidLBL["from"];?> <?php echo $numCounter;?> <?php echo $dataGtidLBL["entered"];?><span class="select-info"><span class="select-item"> </span></span></div>
                                  </div>
                                  <?php }?>
                                  <div class="col-sm-12 col-md-7" align="left">
                                  <?php
                                	 $queryPaging = "SELECT `number` FROM `$userNumber` WHERE `userId`='".$_SESSION['userId']."' AND `categoryId`='$idCategory' $serSubCateSerch GROUP BY `number` HAVING COUNT(id) > 1  ";
									pagingView($queryPaging,$_POST["start"],$_POST["numberResult"]);
									?> 
                                  </div>
                               </div>
                               <div class="btn-collection-top">
                               		<button type="button" title="<?php echo $button["delete"];?>" class="btn bg-primary btn-sm btn-flat text-white m-1" onclick='return deletePage2(<?php echo $_POST["start"]?>)'><i class="fas fa-trash"></i> <?php echo $button["delete"];?></button>
                               </div>
                            </div>
                            	<input type="hidden" name="counter" value="<?php echo $num;?>">
           						<input type="hidden" name="idTemp" value="<?php echo $idTemp?>">   
                              	<input type="hidden" name="idCategory" value="<?php echo $idCategory?>">
							 	<input type="hidden" name="start" value="<?php echo $_POST["start"]?>">
                            </form>
                        </div>
                    </div> <!-- end col -->
                </div>
                <!-- end row -->
                </div>
                <!-- end container-fluid -->
            </div>
            <!-- end page-content-wrapper -->
        </div>
        <!-- End Page-content -->
    </div>
    <!-- end main content -->
    
    <!--  editModl Modal -->
    <?php include("numberModalEdit.php");?>
    <!--  editModl Modal -->

    
<script type="text/javascript">
	$(".importFile").click(function(){
		$('#formNumber')[0].reset();
	});
	$("#categoryId").change(function (){
		$.post("fetchSubCategory3.php",{id:$("#categoryId").val(),numId:$("#numberId").val(),rand:Math.random() } ,function(res){
		$("#subCateEditId").html(res);
		});
	});

	$(document).ready(function(){
		$("#categoryIdSer").change(function (){
			$.post("fetchSubCategory.php",{ id:$("#categoryIdSer").val(),rand:Math.random() } ,function(res){
			$("#subCateSer").html(res);
			});
		});
	});
	
	$.post("fetchSubCategory.php",{ id:$("#categoryIdSer").val(),rand:Math.random() } ,function(res){
	$("#subCateSer").html(res);
	});
</script> 
    
<?php
include("footer.php");
ob_end_flush();
?>
