<?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");

if(empty($_POST["start"])) {
	$_POST["start"] = 0;
	if(empty($_GET["start"]))
		$_POST["start"] = 0;
	else
		$_POST["start"] = (int) $_GET["start"];	
} else
	$_POST["start"] = (int) $_POST["start"];
	
if(empty($_POST["numberResult"]))
	$_POST["numberResult"] = 25;
else
	$_POST["numberResult"] = (int) $_POST["numberResult"];
$pageBack = "users.php"; 
if($_SESSION['userId']==1)
	$pageBack = "users.php";
	
?>
<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 = "";
		if(empty($_POST["categoryIdTemp"]))
			$_POST["subCatefilter"]="";
		$query = "SELECT * FROM ".$userNumber." WHERE userId='".$_SESSION["userId"]."'";
		if(!empty($_POST["categoryIdTemp"]))
			$query.= " AND  categoryId='".$_POST["categoryIdTemp"]."'";
		$query.= " LIMIT ".$_POST["start"].",".$_POST["numberResult"]."";     
		$result = mysqli_query($conn, $query);
		$num = mysqli_num_rows($result);
		$numCounter = $num;
		
        ?>  
        <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="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>
									<button type="button" title="<?php echo $button["search"];?>" data-toggle="modal" data-target="#searchModal" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-search"></i> <?php echo $button["search"];?></button>
                                    <button type="button" title="<?php echo $button["export"];?>" data-toggle="modal" data-target="#exportModal" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-download"></i> <?php echo $button["export"];?></button>
                                    <button type="button" title="<?php echo $button["add"];?>" data-toggle="modal" data-target="#addDirectNumbers" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-plus"></i> <?php echo $button["add"];?></button>
                                    <button type="button" title="<?php echo $numberLBL["importFile"];?>" data-toggle="modal" data-target="#importFile" class="btn bg-primary btn-sm btn-flat text-white m-1 importFile"><i class="fas fa-file-upload"></i> <?php echo $numberLBL["importFile"];?></button>
                               </div>
                               <div class="row mb-2 mt-2">
                                  <div class="col-sm-12 col-md-12" style="display:inline-flex">
                                     <label><?php echo $numberLBL["fillterCat"];?> </label>&nbsp;
                                        <select name="categoryIdTemp" onchange="return filter();" aria-controls="example1" class="custom-select custom-select-sm form-control form-control-sm" style="width: 10%;">
                                           <option value=""><?php echo $numberLBL["allCat"];?></option>
                                           		<?php
												$queryCategory  = "select id,name from ".$userCategory." where userId='".$_SESSION["userId"]."' ";
												$resultCategory = mysqli_query($conn, $queryCategory);
												$numCategory = mysqli_num_rows($resultCategory);
												for($c=0;$c<$numCategory;$c++) {
													$rowCategory= mysqli_fetch_row($resultCategory);
													if(@$_POST["categoryIdTemp"]==$rowCategory[0]) {
														?>
											<option selected value='<?php echo $rowCategory[0]?>'><?php echo $rowCategory[1]?></option>
													<?php } else {?>
											<option  value='<?php echo $rowCategory[0]?>'><?php echo $rowCategory[1]?></option>
														<?php
													}
												}
											?>
                                        </select>
                                     &nbsp;&nbsp;
                                      <?php if(!empty($_POST['categoryIdTemp']) && isset($_POST['categoryIdTemp'])) {?>
                                     <label><?php echo $numberLBL["subCategory"];?> </label>&nbsp;
                                     <?php
									$querysubCatefilter  = "select id,name from `sub_category` where user_id='".$_SESSION["userId"]."' and `cat_id`='$_POST[categoryIdTemp]'";
									$resultsubCatefilter = mysqli_query($conn, $querysubCatefilter);
									$numsubCatefilter= mysqli_num_rows($resultsubCatefilter);
									?>
                                        <select id="subCatefilter" name="subCatefilter" onchange="return filter2();" aria-controls="example1" class="custom-select custom-select-sm form-control form-control-sm" style="width: 10%;">
                                           <option value=""><?php echo $numberLBL["all"];?></option>
                                           <?php for($c=0;$c<$numsubCatefilter;$c++) {
												$rowsubCatefilter= mysqli_fetch_row($resultsubCatefilter);
												if(@$_POST["subCatefilter"]==$rowsubCatefilter[0]) { ?>
										<option selected value='<?php echo $rowsubCatefilter[0]?>'><?php echo $rowsubCatefilter[1]?></option>
												<?php } else { ?>
										<option  value='<?php echo $rowsubCatefilter[0]?>'><?php echo $rowsubCatefilter[1]?></option>
													<?php
												}
											}
										?>
                                        </select>
                                     &nbsp;&nbsp;
                                    <?php }?>
                                     <label><?php echo $numberLBL["linesCount"];?> </label>&nbsp;
                                        <select name="numberResult" onchange="return filter2()" aria-controls="example3" class="custom-select custom-select-sm form-control form-control-sm" style="width: 5%;">
                                           <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="25") echo"selected";?> value="25">25</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="50") echo"selected";?>  value="50">50</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="75") echo"selected";?>  value="75">75</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="100") echo"selected";?> value="100">100</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="200") echo"selected";?> value="200">200</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="300") echo"selected";?> value="300">300</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="400") echo"selected";?> value="400">400</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="500") echo"selected";?> value="500">500</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="1000") echo"selected";?> value="1000">1000</option>
                                            <option <?php if(!empty($_POST["numberResult"]) && $_POST["numberResult"]=="2000")echo"selected";?> value="2000">2000</option>
                                        </select>
                                  </div>
                               </div>
                               <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["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($num)) {
                                            for($i=0;$i<$num;$i++) {
												$row= mysqli_fetch_row($result);
												$idTemp .=$row[0]." ";
                                        ?>
                                         <tr>
                                         	<td><input type="checkbox" value=<?php echo $row[0]?> name='allcheck<?php echo $i?>' id='allcheck<?php echo $i?>'></td>
                                            <td>
                                            <?php
                               					$queryCategory  = "select id,name from ".$userCategory." where userId='".$_SESSION["userId"]."' ";
												$resultCategory = mysqli_query($conn, $queryCategory);
												$numCategory = mysqli_num_rows($resultCategory);
												for($c=0;$c<$numCategory;$c++) {
													$rowCategory= mysqli_fetch_row($resultCategory);					
													if($row[4]==$rowCategory[0]) {
														echo $rowCategory[1];
													}
												}
												?>
											</td>
                                           <td><?php echo (!empty($row[1]))? $row[1] : '---'; ?></td>
                                           <td><?php echo $row[2]?></td>
                                           <td><?php echo (!empty($row[5]))? $row[5] : '---'; ?></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 $usersLBL["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
                                	 $select  =" select count(id) from ".$userNumber." where userId='".$_SESSION["userId"]."'";
											if(isset($_POST["categoryIdTemp"]))
											if($_POST["categoryIdTemp"])
											$select .= " and categoryId='".$_POST["categoryIdTemp"]."'";
											pagingView($select,$_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>
									<button type="button" title="<?php echo $button["search"];?>" data-toggle="modal" data-target="#searchModal" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-search"></i> <?php echo $button["search"];?></button>
                                    <button type="button" title="<?php echo $button["export"];?>" data-toggle="modal" data-target="#exportModal" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-download"></i> <?php echo $button["export"];?></button>
                                    <button type="button" title="<?php echo $button["add"];?>" data-toggle="modal" data-target="#addDirectNumbers" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-plus"></i> <?php echo $button["add"];?></button>
                                    <button type="button" title="<?php echo $numberLBL["importFile"];?>" data-toggle="modal" data-target="#importFile" class="btn bg-primary btn-sm btn-flat text-white m-1 importFile"><i class="fas fa-file-upload"></i> <?php echo $numberLBL["importFile"];?></button>


                               </div>
                            </div>
                              <input type="hidden" name="counter" value='<?php echo $numCounter?>'>
       						 <input type="hidden" name="start" value='<?php echo $_POST["start"]?>'>
                             <input type="hidden" name="idTemp" value="<?php echo $idTemp?>">
                            </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 -->

     <!--  addDirectNumbers Modal -->
    <?php include("numberModalAdd.php");?>
    <!--  addDirectNumbers Modal -->
    
     <!--  importFileFilter Modal -->
    <?php include("numberModalImportFile.php");?>
    <!--  importFileFilter Modal -->
    
    <!--  editModl Modal -->
    <?php include("numberModalEdit.php");?>
    <!--  editModl Modal -->
    
    <!--  search Modal -->
	<?php include("numberModalSearch.php");?>
    <!--  search Modal -->
    
    <!--  search Modal -->
	<?php include("numberModalExport.php");?>
    <!--  search 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);
		});
	});
</script> 
    
<?php
include("footer.php");
ob_end_flush();
?>
