<?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");
$currentPage= 'archivesList';

?>
<link rel="stylesheet" type="text/css" href="../JScalendar/jquery-ui-timepicker-addon.css" />
<link rel="stylesheet" media="all" href="../JScalendar/jquery-ui.css" />
<?php if($_SESSION["language"]==1){
    ?>
    <script type="text/javascript" src="../JScalendar/jquery-ui.min.Ar.js"></script>
    <script type="text/javascript" src="../JScalendar/jquery-ui-timepicker-addon-Ar.js"></script>
    <?php
}else{
    ?>
    <script type="text/javascript" src="../JScalendar/jquery-ui.min.js"></script>
    <script type="text/javascript" src="../JScalendar/jquery-ui-timepicker-addon.js"></script>
    <?php
}
?>
<script language="javascript" type="text/javascript">
function fetchDate(id)
{
	$("#delvId").val();
	$("#countdelvId").html();
	$("#sentId").val();
	$("#countSentId").html();
	$("#notDelvId").val();
	$("#countNotdelvId").html();
	$("#rejectId").val();
	$("#countRejectId").html();
	
	$.post("fetchNumberDetails.php",{ id:id,rand:Math.random() } ,function(res){
		data=res.split('#$#');
		$("#delvId").val(data[0]);
		$("#countdelvId").html(data[1]);
		$("#sentId").val(data[2]);
		$("#countSentId").html(data[3]);
		$("#notDelvId").val(data[4]);
		$("#countNotdelvId").html(data[5]);
		$("#rejectId").val(data[6]);
		$("#countRejectId").html(data[7]);
	});
	$('#detailViewModal').modal('show');
}
function deletePage(start)
{
	document.formList.start.value=start;
    document.formList.action ='archivesDelete.php';
    document.formList.submit();
}
function pageing(url,start)
{
	document.formList.start.value=start;
    document.formList.action =url;
    document.formList.submit()
}
function modifyPage(start)
{
	document.formList.start.value=start
    document.formList.action ='archivesEdit2.php';
    document.formList.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;
	}
}
</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
		if(empty($_POST["start"]))
			$_POST["start"] = 0;
		else
			$_POST["start"] = (int) $_POST["start"];
		$_POST["archivesResult"]=25;
		$url="archivesList.php";
		$idTemp="";
		
		$where=" where 1 ";
		if(!empty($_POST["name"]))
			$where .= " and `userSender` like '%".$_POST["name"]."%' ";
		if(!empty($_POST["msg"]))
			$where .= " and msg like '%".$_POST["msg"]."%'";
		if(!empty($_POST["number"]))
			$where.= " AND `numbers` like '%".$_POST["number"]."%'";
		if(!empty($_POST['searchFrom'])){
			$year = $month = $day = $hours = $minutes = "";
			$searchFromParts = explode(' - ', $_POST['searchFrom']);
			$searchFromDate = explode('/', $searchFromParts[0]);
			$month = $searchFromDate[0];
			$day = $searchFromDate[1];
			$year = $searchFromDate[2];
			$searchFromTime = explode(':', $searchFromParts[1]);
			$hours = $searchFromTime[0];
			$searchFromTimeParts = explode(' ', $searchFromTime[1]);
			if(strtolower($searchFromTimeParts[1]) == 'pm') {
				$hours+=12;
			}
			$minutes = $searchFromTimeParts[0];
			$searchFromTime = mktime($hours, $minutes, 00, $month, $day, $year);  
			$where.= " AND `date` >= '$searchFromTime'";
		}
		if(!empty($_POST['searchTo'])){
			$year = $month = $day = $hours = $minutes = "";
			$searchToParts = explode(' - ', $_POST['searchTo']);
			$searchToDate = explode('/', $searchToParts[0]);
			$month = $searchToDate[0];
			$day = $searchToDate[1];
			$year = $searchToDate[2];
			$searchToTime = explode(':', $searchToParts[1]);
			$hours = $searchToTime[0];
			$searchToTimeParts = explode(' ', $searchToTime[1]);
			if(strtolower($searchToTimeParts[1]) == 'pm') {
				$hours+=12;
			}
			$minutes = $searchToTimeParts[0];
			$searchToTime = mktime($hours, $minutes, 00, $month, $day, $year);
			$where.= " AND `date` <= '$searchToTime'";
		}
			
		$query  = "select distinct id,userSender,numbers,msg,date,countNumbers,numMsgs,points,`rejectedNumber`,`countNotAccepted`,userName from `history` $where AND delUser='0'";
		$querySelectAllData  = "select * from `history` $where AND delUser='0' ORDER BY `id` desc";
        $querySelectAllDataCount  = "select count(`id`) as countId from `history` $where AND delUser='0' ORDER BY `id` desc";
		$_SESSION['selectQuery'] =$querySelectAllData;
		$query .= " ORDER BY `id` desc limit ".$_POST["start"].",".$_POST["archivesResult"]."";
		$result = mysqli_query($conn, $query);
		$num = mysqli_num_rows($result);

        $resultSelectAllData = mysqli_query($conn, $querySelectAllData);
		$numSelectAllData = mysqli_num_rows($resultSelectAllData);
        $resultSelectAllData = mysqli_query($conn, $querySelectAllDataCount);
		$rowCountData= mysqli_fetch_assoc($resultSelectAllData);
		$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"]);
					?>
                    <div style="text-align: end; padding-left: 15px; padding-right: 15px; font-size: 15px; font-weight: bold; color: red;"><?php echo $archivesListLBL["details9"]?>(<?php echo $rowCountData['countId']?>)</div>
                        <form name="formList" action="" method="post">
                        	<?php echo ((!empty($_POST["name"]))? '<input type="hidden" class="form-control" name="name" value="'.$_POST["name"].'">' : '');?>
                            <?php echo ((!empty($_POST["msg"]))? '<input type="hidden" class="form-control" name="msg" value="'.$_POST["msg"].'">' : '');?>
                            <?php echo ((!empty($_POST["number"]))? '<input type="hidden" class="form-control" name="number" value="'.$_POST["number"].'">' : '');?>
                            <?php echo ((!empty($_POST["searchFrom"]))? '<input type="hidden" class="form-control" name="searchFrom" value="'.$_POST["searchFrom"].'">' : '');?>
                            <?php echo ((!empty($_POST["searchTo"]))? '<input type="hidden" class="form-control" name="searchTo" value="'.$_POST["searchTo"].'">' : '');?>
                            <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 $archivesListLBL["username"];?></th>
                                            <th scope="col"><?php echo $archivesListLBL["sender"];?></th>
                                           	<th scope="col"><?php echo $archivesListLBL["msg"];?></th>
                                            <th scope="col"><?php echo $archivesListLBL["numbersCount"];?></th>
                                            <th scope="col"><?php echo $archivesListLBL["msgLength"];?></th>
                                            <th scope="col"><?php echo $archivesListLBL["points"];?></th>
                                            <th scope="col"><?php echo $archivesListLBL["details"];?></th>
                                            <th scope="col"><?php echo $archivesListLBL["date"];?></th>
                                         </tr>
                                      </thead>
                                      <tbody>
                                      <?php
                                        if(!empty($numCounter)) {
                                            for($i=0;$i<$num;$i++){
												  $row= mysqli_fetch_assoc($result);
                        						  $idTemp .=$row["id"]." ";
												
                                        ?>
                                         <tr>
                                         <td ><input type=checkbox value=<?php echo $row['id']?> name='allcheck<?php echo $i?>' id='allcheck<?php echo $i?>'></td>
                                            <td><?php echo $row["userName"]?></td>
                                            <td><?php echo $row['userSender']?></td>
                                            <td><?php echo $row['msg']?></td>
                                            <td align="center"><?php echo $row["countNumbers"];?></td>
                                            <td align="center"><?php echo $row["numMsgs"];?></td>
                                            <td align="center"><?php echo $row["points"];?></td>
                                            <td align="center"><a href="javascript:fetchDate('<?php echo $row['id']?>');"><i class="fas fa-2x fa-file-alt" title="<?php echo $archivesListLBL["details"];?>"></i></a>&nbsp;</td>
                                            <td align="center"><?php echo date("A h:i Y.m.d",$row['date']);?></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">
                                  <div class="col-sm-12 col-md-7">
                                  <?php
									$query = "SELECT count(id) from history  $where AND delUser='0'";
									pagingView($query,$_POST["start"],$_POST["archivesResult"]);
									?> 
                                  </div>
                               </div>
								<?php if($numCounter!=0){?>
                               <div class="btn-collection-top">
									<?php if($where != " where 1 "){?>
                               		<button type="button" title="<?php echo $button["export"];?>" class="btn bg-primary btn-sm btn-flat text-white m-1" onclick="location.href='exportArchivesList.php'"><i class="fas fa-download"></i> <?php echo $button["export"];?></button>
                               		<?php }?>
									<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>
                               </div>
<?php }?>
                            </div>
                              	<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;?>">
                            </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 -->
    <div class="modal fade modal-fullscreen " style="z-index:10000;padding-<?php echo $align;?>:0px" id="detailViewModal" tabindex="-1" role="dialog" aria-labelledby="detailViewModalTitle" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered" role="document">
         <div class="modal-content text-right">
            <div class="modal-header modal-header-primary">
               <h5 class="modal-title" id="detailViewModalTitle"><?php echo $archivesListLBL["details"];?> : <span id="detailFullName" class=""></span></h5>
               <button type="button" class="close slidedown" data-dismiss="modal" aria-label="Close">
               <span aria-hidden="true">&times;</span>
               </button>
            </div>
            <div class="modal-body" style="text-align: <?php echo $align;?>;">
               <div class="">
                  <label for="f1" class="col-form-label myModalLabel"><?php echo $archivesListLBL["details1"];?> : </label>
                  <textarea name="" id="delvId" rows="4" cols="20" class="form-control" readonly></textarea>
               </div>
               <div class="">
                  <label for="f2" class="col-form-label myModalLabel"><?php echo $archivesListLBL["details2"];?> : </label>
                  <span id="countdelvId" class=""></span>
               </div>
               <div class="">
                  <label for="f1" class="col-form-label myModalLabel"><?php echo $archivesListLBL["details3"];?> : </label>
                  <textarea name="" id="sentId" rows="4" cols="20" class="form-control" readonly></textarea>
               </div>
               <div class="">
                  <label for="f2" class="col-form-label myModalLabel"><?php echo $archivesListLBL["details4"];?> : </label>
                  <span id="countSentId" class=""></span>
               </div>
               <div class="">
                  <label for="f1" class="col-form-label myModalLabel"><?php echo $archivesListLBL["details5"];?> : </label>
                  <textarea name="" id="notDelvId" rows="4" cols="20" class="form-control" readonly></textarea>
               </div>
               <div class="">
                  <label for="f2" class="col-form-label myModalLabel"><?php echo $archivesListLBL["details6"];?> : </label>
                  <span id="countNotdelvId" class=""></span>
               </div>
               <div class="">
                  <label for="f1" class="col-form-label myModalLabel"><?php echo $archivesListLBL["details7"];?> : </label>
                  <textarea name="" id="rejectId" rows="4" cols="20" class="form-control" readonly></textarea>
               </div>
               <div class="">
                  <label for="f2" class="col-form-label myModalLabel"><?php echo $archivesListLBL["details8"];?> : </label>
                  <span id="countRejectId" class=""></span>
               </div>
            </div>
         </div>
      </div>
    </div>
   
<?php
include("footer.php");
ob_end_flush();
?>
