<?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");
$pageBack = "users.php"; 


?>
<script>
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 fetchDate2(id)
{
	
	$("#catIdCopy").val(id);
	$('#copyModal').modal('show');
}
function fetchDate(id)
{
	$("#catName").val('');
	$("#catId").val('');	
	$.post("fetchCategoryName.php",{ id:id,rand:Math.random() } ,function(res){
		$("#catName").val(res);
		$("#catId").val(id);	
	});
	$('#editModal').modal('show');
}
function deletePage(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 ='categoryDelete.php';
			document.formList.submit();
		}
	}
}

</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
		
		$userCategory = $userCategory;
		if(@!$_POST["start"])
		$_POST["start"]=0;
	
		$_POST["start"]=(integer)$_POST["start"];
		$_POST["categoryResult"]=25;
		$url="category.php";
		$idTemp="";
    	$query  = "select distinct id,name from ".$userCategory." where userId='".$_SESSION["userId"]."' ORDER BY BINARY `id` ASC";
		$query .= " limit ".$_POST["start"].",".$_POST["categoryResult"]."";
		$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="tempMsg.php" method="post">
                            <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 deletePage(<?php echo $_POST["start"]?>);'><i class="fas fa-trash"></i> <?php echo $button["delete"];?></button>
                                    <button type="button" title="<?php echo $button["add"];?>" data-toggle="modal" data-target="#addModal" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-plus"></i> <?php echo $button["add"];?></button>
                               </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 $categoryLBL["category"];?></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 echo $row[1]?></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 $msgBoxListLBL["noData"];?></td>
                                         </tr>
                                        <?php
                                            }
                                        ?>
                                      </tbody>
                                   </table>
                               </div>
                               
                               <div class="row">
                                  <div class="col-sm-12 col-md-7">
                                  <?php
                        			$queryPaging = "SELECT count(id) from ".$userCategory." where userId='".$_SESSION["userId"]."'";
									pagingView($queryPaging,$_POST["start"],$_POST["categoryResult"]);
									?> 
                                  </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 deletePage(<?php echo $_POST["start"]?>);'><i class="fas fa-trash"></i> <?php echo $button["delete"];?></button>
                                    <button type="button" title="<?php echo $button["add"];?>" data-toggle="modal" data-target="#addModal" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-plus"></i> <?php echo $button["add"];?></button>
                               </div>
                            </div>
                              <input type="hidden" name="counter" value='<?php echo $numCounter?>'>
       						 <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 -->

    <!--  addModl Modal -->
    <?php include("categoryModalAdd.php");?>
    <!--  addModl Modal -->
    
    <!--  editModal Modal -->
    <?php include("categoryModalEdit.php");?>
    <!--  editModal Modal -->
    
    
<?php
include("footer.php");
ob_end_flush();
?>
