<?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");
?>
<script type="text/javascript">
function deletePage(start)
{
	var doDelete = false;
	for (var k=0; k<$("#counterId").val(); 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 ='msgBoxDelete.php';
			document.formList.submit();
		}
	}
}
function pageing(url,start)
{
	document.formList.start.value=start
	document.formList.action =url;
	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;
	}
}
function fetchDate(id)
{
	$("#nameId").val('');
	$("#msgId").val('');
	$("#readyId").val('');	
	$.post("fetchreadyMsgName.php",{id:id,rand:Math.random() } ,function(res){
		data=res.split('#$#');
		$("#nameId").val(data[0]);
		$("#msgId").val(data[1]);
		$("#readyId").val(id);	
	});
	$('#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
		if(@!$_POST["start"])
			$_POST["start"]=0;
		else
			$_POST["start"]=(integer)$_POST["start"];
			
		$_POST["readyMsgResult"]=25;
		$result = mysqli_query($conn, "SELECT * FROM `msgbox` WHERE userId='".$_SESSION['userId']."' ORDER BY id DESC limit ".$_POST["start"].",".$_POST["readyMsgResult"]." ");
		$num=mysqli_num_rows($result);
		$showTr = "style='display:none'";
		$showBorder = "style='border-top:1px solid #eee'";
		$result2 = mysqli_query($conn, "SELECT * FROM `msgbox` WHERE userId='".$_SESSION['userId']."' ORDER BY id DESC ");
		$num2=mysqli_num_rows($result2);
		$numCounter = $num;
		
		$resultAll = mysqli_query($conn, "SELECT * FROM `msgbox` WHERE userId='".$_SESSION['userId']."' ");
		$numAll=mysqli_num_rows($resultAll);
        ?>  
        <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' 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>
                               </div>
                               <table class="table table-bordered">
                                  <thead>
                                     <tr>
                                     	<th scope="col"><input type="checkbox" name="checkall" onclick="allcheck_2();"></th>
                                     	<th scope="col"><?php echo $msgBoxListLBL["msgBoxTitle"];?></th>
                                        <th scope="col"><?php echo $msgBoxListLBL["textMsg"];?></th>
                                        <th scope="col"><?php echo $button["edit"];?></th>
                                     </tr>
                                  </thead>
                                  <tbody>
                                  <?php
								   $idTemp = "";
									if(!empty($num)) {
											 for($i=0;$i<$num;$i++)
            								{
												$row= mysqli_fetch_row($result);
            									 $idTemp .=$row[0]." ";
           		  								$valZeyad = str_replace("'", "\'", stripcslashes($row[2]));
									?>
                                     <tr>
                                     	<td><input type="checkbox" value="<?php echo $row[0]?>" name="readyMsgIds[]" id='allcheck<?php echo $i?>'></td>
                                        <td><?php echo $row[5]?></td>
                                        <td><?php echo nl2br($row[2])?>.</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="4" style="text-align:center;font-weight:bold"><?php echo $msgBoxListLBL["noData"];?></td>
                                        
                                     </tr>
                                    <?php
										}
									?>
                                  </tbody>
                               </table>
                               <div class="row">
                               <?php if($numAll > $_POST["readyMsgResult"]) {?>
                                  <div class="col-sm-12 col-md-12">
                                  <?php
									$select = "SELECT count(id) FROM `msgbox` WHERE userId='".$_SESSION['userId']."'";
									pagingView($select,$_POST["start"],$_POST["readyMsgResult"]);
									?> 
                                  </div>
                                   <?php }?>
                               </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>
                               </div>
                            </div>
                              <input type=hidden name="start" value='<?php echo $_POST["start"]?>'>
								<input type=hidden name="counter" id="counterId" 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 -->
    <!-- End Dashboard page -->
  

     <!--  edit Modal -->
  <form method="POST" action="msgBoxEdit2.php" target="_parent"  name="formRankEdit">
       <div class="modal fade modal-fullscreen " style="z-index:10000;padding-<?php echo $align;?>:0px" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalTitle" 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="aeditModalTitle"><?php echo $button["edit"];?></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">
                    <label for="nameId" class="col-form-label myModalLabel"> <?php echo $msgBoxListLBL["msgBoxTitle"];?>:</label>
                     <input type="text" class="form-control"name="name" id="nameId">
                    </div>
                   <div class="form-group">
                      <label for="msgId" class="col-form-label myModalLabel"> <?php echo $msgBoxListLBL["textMsg"];?>:</label>
                      <textarea name="msg" id="msgId" class="form-control" ></textarea>
                   </div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="catId" id="catId"  />
                   <button type="button" class="btn bg-danger btn-flat text-white m-1" data-dismiss="modal"><?php echo $button["close"];?></button>
                   <input type="submit" value='<?php echo $button["edit"];?>' id="submit2" name="submit1" class="btn bg-primary btn-flat text-white m-1">
                </div>
             </div>
          </div>
       </div>
    	<input type="hidden" name="readyId" id="readyId"  />
    </form>
     <!--  edit Modal -->
<?php
include("footer.php");
ob_end_flush();
?>
