<?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;
else
	$_POST["start"] = (int) $_POST["start"];
	
if(empty($_POST["numberResult"]))
	$_POST["numberResult"] = 25;
else
	$_POST["numberResult"] = (int) $_POST["numberResult"];
?>
<script>
function deletePage()
{
	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 ='numberSearchDelete.php';
			document.formList.submit();
		}
	}
}

function modifyPage()
{
	document.formSearch2.action ='numberEdit3.php';
    document.formSearch2.submit();
}
         
function allcheck_2()
{
       for (var k=0; k<document.formList.counter.value; k++)
       {
             var allcheckName = 'allcheck'+k;
             if(document.formList.checkall.checked)
             	document.getElementById(allcheckName).checked = true;
             else
             	document.getElementById(allcheckName).checked = false;
       }
}
function pageing(url,start)
{
	document.formList.start.value=start;
	document.formList.action =url;
	document.formList.submit()
} 
function fetchDate(id)
{	
	$("#name").val('');
	$("#number").val('');
	$("#city").val('');
	$("#categoryId").val('');
	$("#cateUserSer").val('');
	$("#rankSer").val('');
	$("#CatId").val('');
	$("#numberId").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="search.php";
		$idTemp="";
		$query  = "select id,name,number,categoryId,`city`,`sub_cat`,`cateUser` from ".$userNumber." where userId='".$_SESSION["userId"]."'";
		
		if(@$_POST["name"])
		$query .= " and name like '%".$_POST["name"]."%' ";
		if(@$_POST["number"])
		$query .= " and number like '%".$_POST["number"]."%'";
		if(@$_POST["categoryId"])
		$query .= " and categoryId = '".$_POST["categoryId"]."'";
		if(@$_POST["subCateSer"])
		$query .= " and sub_cat = '".$_POST["subCateSer"]."'";
		if(@$_POST["cateUserSer"])
		$query .= " and cateUser = '".$_POST["cateUserSer"]."'";
		$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"]);
					?>
                    <?php
					if(!empty($_POST["name"]) || !empty($_POST["number"]) || !empty($_POST["categoryId"]) || !empty($_POST["subCateSer"]))
					{
						?>
                        <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["back"];?>" class="btn bg-primary btn-sm btn-flat text-white m-1" onclick="self.location.href='number.php';"><i class="fas fa-reply-all"></i> <?php echo $button["back"];?></button>
                               </div>
                               <div class="row mb-2 mt-2">
                               </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["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($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"]."' AND `id`='$row[3]'";
												$resultCategory = mysqli_query($conn, $queryCategory);
												$rowCategory= mysqli_fetch_row($resultCategory);
												echo $rowCategory[1];
											?>
											</td>
                                            <td>
											<?php
												$querySelectSuCat = "SELECT `name` FROM `sub_category` WHERE `id`='$row[5]'";
												$resultSelectSuCat = mysqli_query($conn, $querySelectSuCat);
												$rowSelectSuCat = mysqli_fetch_assoc($resultSelectSuCat);
											 echo $rowSelectSuCat['name'] ?>
										   </td>
                                           <td><?php echo (!empty($row[1]))? $row[1] : '---'; ?></td>
                                           <td><?php echo $row[2]?></td>
                                           <td><?php echo (!empty($row[4]))? $row[4] : '---'; ?></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">
                                  <?php
                                	 $queryPaging = "SELECT count(id) from `".$userCategory."` where userId='".$_SESSION["userId"]."'";
									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>
                                    <button type="button" title="<?php echo $button["back"];?>" class="btn bg-primary btn-sm btn-flat text-white m-1" onclick="self.location.href='number.php';"><i class="fas fa-reply-all"></i> <?php echo $button["back"];?></button>
                               </div>
                            </div>
                                 <input type="hidden" name="search" value='1'>
                                <input type="hidden" name="start" value='<?php echo $_POST["start"]?>'>
                                <input type="hidden" name="counter" value='<?php echo $numCounter?>'>
                                <input type="hidden" name="idTemp" value='<?php echo $idTemp?>'>
                                <?php if(!empty($_POST["name"])):?>
                                <input type="hidden" name="name" value='<?php echo $_POST["name"]?>'>
                                <?php endif;?>
                                <?php if(!empty($_POST["number"])):?>
                                <input type="hidden" name="number" value='<?php echo $_POST["number"]?>'>
                                <?php endif;?>
                                <?php if(!empty($_POST["categoryId"])):?>
                                <input type="hidden" name="categoryId" value='<?php echo $_POST["categoryId"]?>'>
                                <?php endif;?>
                                <?php if(!empty($_POST["subCateSer"])):?>
                                <input type="hidden" name="subCateSer" value='<?php echo $_POST["subCateSer"]?>'>
                                <?php endif;?>
                            </form>
                            <?php
							}
							?>
                        </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">
    $("#categoryId").change(function (){
        $.post("fetchSubCategory3.php",{id:$("#categoryId").val(),numId:$("#numberId").val(),rand:Math.random() } ,function(res){
        $("#subCateEditId").html(res);
        });
    });
    </script>
    <script type="text/javascript">
		$(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();
?>
