<?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($_REQUEST['catId'])) {
	header("location: readyMsg.php");
	exit;
}
$catId = (int) $_REQUEST['catId'];
if(empty($catId)) {
	header("location: readyMsg.php");
	exit;
}
if(!isset($_REQUEST["showMsg"]))
	$showMsg = 1;
else
	$showMsg = (int) $_REQUEST["showMsg"];
?>

<script language="javascript" type="text/javascript">
function deleteTempMsg(url)
{
	var result = confirm("<?php echo $confirmAlert;?>");
	if (result == true) {
		document.formList.start.value=0;
		document.formList.action = url;
		document.formList.submit();
	}	
}
function pageing(url,start)
{
	document.formList.start.value=start
	document.formList.action =url;
	document.formList.submit()
}

function postMsg(msgId)
{
	document.formList.readyMsgId.value = msgId;
	document.formList.action = 'sendMsg.php';
	document.formList.submit();
}
</script>
<?php
if($_SESSION["power"]=="-") {
	?>
<script type="text/javascript">
function verify ()
{
	if (document.rMsgAdd.idCategory.value == "")
	{
		alert ("<?php echo $category2LBL["pleaseSelectCat"];?>");
		document.rMsgAdd.idCategory.focus();
		return false;
	}
	if (document.rMsgAdd.msg.value == "")
	{
		alert ("<?php echo $category2LBL["enterMsg"];?>");
		document.rMsgAdd.msg.focus();
		return false;
	}
	strText = document.rMsgAdd.msg.value;
	if(strText == "")
	{
		//يجب عليك إدخال نص الرسالة
		alert('<?php echo $category2LBL["enterMsg2"];?>');
		document.rMsgAdd.msg.focus();
		return false;
	}
	msgTemp = "";
	for(i=0; i<document.rMsgAdd.msg.value.length; i++)
	{
		if(document.rMsgAdd.msg.value.charCodeAt(i)=="1632")
			msgTemp += "0";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1633")
			msgTemp += "1";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1634")
			msgTemp += "2";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1635")
			msgTemp += "3";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1636")
			msgTemp += "4";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1637")
			msgTemp += "5";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1638")
			msgTemp += "6";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1639")
			msgTemp += "7";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1640")
			msgTemp += "8";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1641")
			msgTemp += "9";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1785")
			msgTemp += "9";
		else if(document.rMsgAdd.msg.value.charCodeAt(i)=="1642")
			msgTemp += "%";			
		else
			msgTemp +=document.rMsgAdd.msg.value.charAt(i);
	}
	document.rMsgAdd.msg.value = msgTemp;
	for(i=0; i<document.rMsgAdd.msg.value.length; i++)
	{
		if(document.rMsgAdd.msg.value.charAt(i) =='ـ')
		{
			if(document.rMsgAdd.confirmMsg.value == '0')
			{
				//الحرف التالي (شفت و ت) قد لا يصل بشكل صحيح على جميع أنواع الجوالات
				alert('<?php echo $category2LBL["enterSenderMsg"];?>');
				document.rMsgAdd.confirmMsg.value = '1';
				document.rMsgAdd.msg.focus();
				return false;
			}
		}
	}
	
	return true;
}
function unicode(strText)
{
	str = "دجحخهعغفقثصضطكمنتالبيسشظزوةىلارؤءئإلإألأآلآ،؛؟ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيـًٌٍَُِّْ";
	for (i=0; i<strText.length; i++)
		if (str.indexOf(strText.charAt(i)) != -1)
			return 1;
	return 0;
}

function charPlus()
{
	strLen = 0;
	strText = $('#msgId').val();
	for(i=0; i<strText.length; i++)
	{
		if(strText.charCodeAt(i)!=13)
			strLen++;
	}
	$('#numCharsId').val(strLen);
	numChar = strLen;
	if (unicode($('#msgId').val()))
	{
		$('#unicode1Id').val(1);
		if(numChar <= 70)
			$('#numMsgsId').val(1);
		else
			$('#numMsgsId').val(Math.ceil(numChar/67));
	}
	else
	{
		$('#unicode1Id').val(0);
		if(numChar <= 160)
			$('#numMsgsId').val(1);
		else
			$('#numMsgsId').val(Math.ceil(numChar/153));
	}
}
function editTempMsg(msgId)
{
	$.post("getTempMessageInfo.php",{rand:Math.random(),msgId:msgId} ,function(data){
		var msgCat = $("#idCategoryEditId").val(data[1]);
		var msgText = $("#msgIdEdit").val(data[0]);
		var msgStatus = $("#ckStatus input[value="+data[2]+"]").attr('checked','checked');
		$("#temMsgIdInput").val(msgId);
		$("#msgIdEdit").keydown();
	},'json');
	
	$('#editModal').modal('show');		
}


function verifyEdit()
{
	if (document.tMsgEdit.idCategory.value == "")
	{
		alert ("<?php echo $category2LBL["pleaseSelectCat"];?>");
		document.tMsgEdit.idCategory.focus();
		return false;
	}
	if (document.tMsgEdit.msg.value == "")
	{
		alert ("<?php echo $category2LBL["enterMsg"];?>");
		document.tMsgEdit.msg.focus();
		return false;
	}
	strText = document.tMsgEdit.msg.value;
	if(strText == "")
	{
		//يجب عليك إدخال نص الرسالة
		alert('<?php echo $category2LBL["enterMsg2"];?>');
		document.tMsgEdit.msg.focus();
		return false;
	}
	msgTemp = "";
	for(i=0; i<document.tMsgEdit.msg.value.length; i++)
	{
		if(document.tMsgEdit.msg.value.charCodeAt(i)=="1632")
			msgTemp += "0";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1633")
			msgTemp += "1";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1634")
			msgTemp += "2";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1635")
			msgTemp += "3";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1636")
			msgTemp += "4";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1637")
			msgTemp += "5";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1638")
			msgTemp += "6";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1639")
			msgTemp += "7";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1640")
			msgTemp += "8";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1641")
			msgTemp += "9";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1785")
			msgTemp += "9";
		else if(document.tMsgEdit.msg.value.charCodeAt(i)=="1642")
			msgTemp += "%";			
		else
			msgTemp +=document.tMsgEdit.msg.value.charAt(i);
	}
	document.tMsgEdit.msg.value = msgTemp;
	for(i=0; i<document.tMsgEdit.msg.value.length; i++)
	{
		if(document.tMsgEdit.msg.value.charAt(i) =='ـ')
		{
			if(document.tMsgEdit.confirmMsg.value == '0')
			{
				alert('<?php echo $category2LBL["enterSenderMsg"];?>');
				document.tMsgEdit.confirmMsg.value = '1';
				document.tMsgEdit.msg.focus();
				return false;
			}
		}
	}
	
	return true;
}


function unicode(strText)
{
	str = "دجحخهعغفقثصضطكمنتالبيسشظزوةىلارؤءئإلإألأآلآ،؛؟ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيـًٌٍَُِّْ";
	for (i=0; i<strText.length; i++)
		if (str.indexOf(strText.charAt(i)) != -1)
			return 1;
	return 0;
}

function charPlus2()
{
	strLen = 0;
	strText = $('#msgIdEdit').val();
	for(i=0; i<strText.length; i++)
	{
		if(strText.charCodeAt(i)!=13)
			strLen++;
	}
	$('#numCharsEditId').val(strLen);
	numChar = strLen;
	if (unicode($('#msgIdEdit').val()))
	{
		$('#unicode1EditId').val(1);
		if(numChar <= 70)
			$('#numMsgsEditId').val(1);
		else
			$('#numMsgsEditId').val(Math.ceil(numChar/67));
	}
	else
	{
		$('#unicode1EditId').val(0);
		if(numChar <= 160)
			$('#numMsgsEditId').val(1);
		else
			$('#numMsgsEditId').val(Math.ceil(numChar/153));
	}
}
</script>
<?php
}
?>
<!-- Start Dashboard page -->
<!-- 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
		$resultAll = mysqli_query($conn, "SELECT id,name,msg,date,idCategory,viewMsg FROM `".$tempMsg."`");
		$numAll = mysqli_num_rows($resultAll);
		
        $start = 0;
        if(!empty($_POST["start"]))
        	$start = (integer) $_POST["start"];
        $numberResult = 10;
        $url = "tempMsg.php";
        $idTemp = "";
		
        $where = " `viewMsg`='".$showMsg."'";
        if(!empty($catId))
        	$where .=" AND idCategory='".$catId."' ";
			
        $query = "SELECT id,name,msg,date,idCategory,viewMsg FROM `".$tempMsg."` ";
        if(!empty($where))
        	$query .= " WHERE ".$where;
        $query .= " ORDER BY `id` DESC LIMIT ".$start.",".$numberResult."";
        $result = mysqli_query($conn, $query);
        $num = mysqli_num_rows($result);
        ?>  
        <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">
                        <form name="formList" action="tempMsg.php?catId=<?php echo $_REQUEST['catId'];?>" method="post">
                            <div class="card-body">
                               
                               <table class="table table-bordered">
                                  <thead>
                                     <tr>
                                        <th scope="col"><?php echo $tempMsgLBL["textMsg"];?></th>
                                        <th scope="col"><?php echo $tempMsgLBL["date"];?></th>
                                        <th scope="col"><?php echo $tempMsgLBL["source"];?></th>
                                        <th scope="col"><?php echo $tempMsgLBL["category"];?></th>
                                     </tr>
                                  </thead>
                                  <tbody>
                                  <?php
									if(!empty($num)) {
											$queryCatNames = "SELECT `id`, `category` FROM `category`";
											$resultCatNames = mysqli_query($conn, $queryCatNames);
											$categoriesNames = array();
											while($rowCatNames = mysqli_fetch_assoc($resultCatNames))
												$categoriesNames[$rowCatNames['id']] = $rowCatNames['category'];
											while($row = mysqli_fetch_array($result)) {
									?>
                                     <tr>
                                        <td><?php echo substr($row[2], 0, 25)?>...<img src="images/messageIcon.png" align="<?php echo $align2;?>" width="18" height="22" hspace="5" vspace="0" border="0" style="cursor:pointer" title="<?php echo $tempMsgLBL["textMsg"];?>: <?php echo $row[2];?>" onclick="postMsg(<?php echo $row[0];?>);" /></td>
                                        <td><?php echo $row[3];?></td>
                                        <td><?php echo $row[1];?></td>
                                        <td><?php echo $categoriesNames[$row[4]];?></td>
                                     </tr>
                                     <?php
											}
										} else {
									?>
                                    <tr>
                                        <td colspan="4" style="text-align:center;font-weight:bold"><?php echo $tempMsgLBL["noData"];?></td>
                                        
                                     </tr>
                                    <?php
										}
									?>
                                  </tbody>
                               </table>
                               
                               
                               <div class="row">
                                  <div class="col-sm-12 col-md-5">
                                     <div class="dataTables_info" id="example_info" role="status" aria-live="polite">إظهار <?php echo $start+1;?> إلى <?php echo $numberResult;?> من أصل <?php echo $numAll;?> مُدخل<span class="select-info"><span class="select-item"> </span></span></div>
                                  </div>
                                  <div class="col-sm-12 col-md-7">
                                  <?php
									$select  =" SELECT count(id) FROM `".$tempMsg."`";	
									if(!empty($where))
										$select .= " WHERE ".$where;
												
									pagingView($select,$start,$numberResult);
									?> 
                
                                     
                                  </div>
                               </div>
                               <div class="btn-collection-top"><button type="button" title="<?php echo $button["addMsg"];?>" data-toggle="modal" data-target="#addCatModal" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-plus"></i> <?php echo $button["addMsg"];?></button></div>
                            </div>
                            	<input type="hidden" value="<?php echo $catId;?>" name="catId">
								<input type="hidden" value="0" name="readyMsgId">
                              <input type="hidden" name="counter" value='<?php echo $num?>'>
       						 <input type="hidden" name="start" value='<?php echo $start?>'>
       						 <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 -->
<!--Add Modal with Validation-->
 <!--  add Msg Modal -->
<form method="POST" action="addMsg2.php" name="rMsgAdd" onSubmit="return verify();">
   <div class="modal fade modal-fullscreen " style="z-index:10000;padding-<?php echo $align;?>:0px" id="addCatModal" tabindex="-1" role="dialog" aria-labelledby="addCatModalTitle" 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="addCatModalTitle"><?php echo $category2LBL["addMsg"];?></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">
                  <label for="product" class="col-form-label myModalLabel"> <?php echo $category2LBL["msgCats"];?>:</label>
                  <select name="idCategory" id="idCategory" class="form-control" style="padding:0px">
                     <option value="" selected><?php echo $category2LBL["selectCat"];?></option>
                     <?php
					$query = "SELECT * FROM `category`";
					$result = mysqli_query($conn, $query);
					while($row = mysqli_fetch_array($result)) {
						?>
					<option value="<?php echo $row["id"]?>" <?php if(isset($catId) && $catId == $row["id"]):?> selected="selected"<?php endif;?>><?php echo $row["category"]?></option>
						<?php
					}?>
                  </select>
               </div>
               <div class="form-group">
                  <label for="departName" class="col-form-label myModalLabel"> <?php echo $category2LBL["msg"];?>:</label>
                  <textarea name="msg" id="msgId"  onkeydown="charPlus();" onkeyup="charPlus();" class="form-control" ></textarea>
               </div>
               <div class="form-group" style="display:inline-flex">
                  <label for="numCharsId" class="col-form-label myModalLabel"> <?php echo $category2LBL["charCount"];?>:&nbsp;</label>
                  <input class="form-control" readOnly  value="0" name="numChars" id="numCharsId" style="width:60px">&nbsp;
                  <label for="numMsgsId" class="col-form-label myModalLabel"> <?php echo $category2LBL["msgCount"];?>:&nbsp; </label>
                  <input class="form-control" readOnly  value="0" name="numMsgs" id="numMsgsId" style="width:60px"> &nbsp;
               </div>
            </div>
            <div class="modal-footer">
               <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["add"];?>' id="submit1" name="submit1" class="btn bg-primary btn-flat text-white m-1">
            </div>
         </div>
      </div>
   </div>
</form>
<!--  add Msg Modal -->

 <!--  edit Modal -->
<form method="POST" action="tempMsgEdit.php" name="tMsgEdit" onSubmit="return verifyEdit();" dir="<?php echo $dir?>">
   <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 $category2LBL["editCat"];?></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">
                  <label for="product" class="col-form-label myModalLabel"> <?php echo $category2LBL["msgCats"];?>:</label>
                  <select  name="idCategoryEdit" id="idCategoryEditId" class="form-control" style="padding:0px">
                     <option value="" selected><?php echo $category2LBL["selectCat"];?></option>
                    <?php
					$query = "SELECT * FROM `category`";
					$result = mysqli_query($conn, $query);
					while($row = mysqli_fetch_array($result)) {
						?>
					<option  value="<?php echo $row["id"]?>"><?php echo $row["category"]?></option>
						<?php
					}?>
                  </select>
               </div>
               <div class="form-group">
                  <label for="departName" class="col-form-label myModalLabel"> <?php echo $category2LBL["msg"];?>:</label>
                  <textarea name="msg" id="msgIdEdit" onkeydown="javascript:charPlus2()" onkeyup="javascript:charPlus2()" class="form-control" ></textarea>
               </div>
               <div class="form-group" style="display:inline-flex">
                  <label for="numCharsId" class="col-form-label myModalLabel"> <?php echo $category2LBL["charCount"];?>:&nbsp;</label>
                  <input class="form-control" readOnly  value="0" ame="numChars" id="numCharsEditId" style="width:60px">&nbsp;
                  <label for="numMsgsId" class="col-form-label myModalLabel"> <?php echo $category2LBL["msgCount"];?>:&nbsp; </label>
                  <input class="form-control" readOnly  value="0" name="numMsgs" id="numMsgsEditId" style="width:60px"> &nbsp;
               </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 name="temMsgId" id="temMsgIdInput" type="hidden" value="0" />
  	<input type="hidden" value="0" name="unicode1" id="unicode1EditId">
</form>
 <!--  edit Modal -->
<?php
include("footer.php");
ob_end_flush();
?>
