<?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 pageing(url,start)
{
	document.formList.start.value=start;
	document.formList.action =url;
	document.formList.submit()
}    
function deletePage(start)
         {
         document.formList.start.value=start;
         document.formList.action ='archivesDelete.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
		if(@!$_POST["start"])
			$_POST["start"]=0;
	
			$_POST["start"]=(integer)$_POST["start"];
	
		$_POST["archivesResult"]=25;
		$url="archivesList.php";
		$idTemp="";
		
		$where=" where idUser='".$_SESSION["userId"]."'  ";
		if(@$_POST["name"])
			$where .= " and `userSender` like '%".$_POST["name"]."%' ";
		if(@$_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` from `history` $where AND delUser='0'";
		$query .= " ORDER BY `id` desc limit ".$_POST["start"].",".$_POST["archivesResult"]."";
		$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">
                        	<?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"><?php echo $archivesListLBL["sender"];?></th>
                                            <th scope="col"><?php echo $archivesListLBL["numbers"];?></th>
                                           	<th scope="col"><?php echo $archivesListLBL["msg"];?></th>
                                            <th scope="col"><?php echo $archivesListLBL["countAcceptedNum"];?></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_row($result);
												 $idTemp .=$row[0]." ";
                                        ?>
                                         <tr>
                                            <td><?php echo $row[1]?></td>
                                            <td><textArea rows="3" cols="20" name=msg<?php echo $row[0]?> style="background-color:#fff;border:0;overflow:auto;width:100%"  readonly="readonly"><?php echo $row[2]?></textArea></td>
                                            <td><textArea rows="3" cols="20" name=msg<?php echo $row[0]?> style="background-color:#fff;border:0;overflow:auto;width:100%"  readonly="readonly"><?php echo $row[3]?></textArea></td>
                                            <td align="center"><?php echo $row[5];?></td>
                                            <td align="center"><?php echo $row[6];?></td>
                                            <td align="center"><?php echo $row[7];?></td>
                                            <td align="center"><a href="javascript:fetchDate('<?php echo $row[0]?>');"><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[4]);?></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">
                               	<?php if($numCounter > $_POST["archivesResult"]) {?>
                                 
                                  
                                  <div class="col-sm-12 col-md-12">
                                  <?php
                               		 $query = "SELECT count(id) from `history` $where AND delUser='0'";
									pagingView($query,$_POST["start"],$_POST["archivesResult"]);
									?> 
                                  </div>
                                  <?php }?>
                               </div>
                            </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 -->
	<!--  messageNumbersModal Modal -->
    <div class="modal fade modal-fullscreen " style="z-index:10000;padding-<?php echo $align;?>:0px" id="messageNumbersModal" tabindex="-1" role="dialog" aria-labelledby="searchModalTitle" 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="searchModalTitle"><?php echo $archivesListLBL["numbers"];?></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="form-group" style="margin-bottom: 0px;">
                  <textarea id="resultMessageNumbers" class="form-control" readonly></textarea>
               </div>
               
            </div>
         </div>
      </div>
    </div>
    <!--  messageNumbersModal Modal -->
  <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();
?>
