<?php
/**
 * Contain All site report Charts
 *
 * PHP version 7.3
 *
 * @package        SMS
 * @version        Release 1.0.0
 * @create_by      Mohammad aljaouni
 * @create_date    10-08-2020
 * @edited_by      Mohammad aljaouni
 * @last_edit_date 10-08-2020
 * @todo           Nothing
 */
ob_start();
session_start();
$chain = "AWdrh78y892Djk*^523*%$#ghgHGHia&%$*)AsfdsLJnLaad";
include("validateLogin.php");
include("includeInformation.php");
include("header.php");

$query = "SELECT `id`, `category`, `image` FROM `".$category2."` WHERE 1 ORDER BY `id` DESC";
$result = mysqli_query($conn, $query);
$num =  mysqli_num_rows($result);

?>
<script type="text/javascript">
function checkCatAdd()
{
	if (document.formAdd.product.value=='')
	{
		alert("<?php echo sprintf($insert,$category2LBL["catName"]);?>");
		return false;
	}
	if (document.formAdd.departName.value==0)
	{
		alert("<?php echo sprintf($insert,$category2LBL["departName"]);?>");
		return false;
	}
	return true;
}
function fetchDate(id)
{
	$("#catName").val('');
	$("#catId").val(id);
	openbox("", 1,"box23");
	$("#waitIcon").show();	
	$.post("fetchCategory2Name.php",{ id:id,rand:Math.random() } ,function(res){
		data=res.split('#$#');
		$("#catName").val(data[0]);	
		$("#waitIcon").hide();
	});
}

function checkDelete(url)
{
	var result = confirm("<?php echo $confirmAlert;?>");
	if (result==true) {
		self.location.href = url;
	}
}
</script>

<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));
	}
}


</script>
<!-- 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 -->

        <div class="page-content-wrapper">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-xl-12">
                        <div class="card">
                        <?php
						if(!empty($_GET["res"]))
							echo result($_GET["res"]);
						?>
                            <div class="card-body mt-2">
                             
							 <div class="btn-collection-top" align="<?php echo $align2;?>">
                             	<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 class="row container-grid projects-wrapper">
                          
                             <?php
								if(!empty($num)) {
										while($row = mysqli_fetch_row($result)) {
							  ?>
                                    <div class="col-xl-3 col-md-4 col-sm-6">
                                    <?php
										$queryCountMsgs = "SELECT COUNT(`id`) AS `msgCount` FROM `temp_msg` WHERE `idCategory`='".$row[0]."'";
										$resultCountMsgs = mysqli_query($conn, $queryCountMsgs);
										$rowCountMsgs = mysqli_fetch_assoc($resultCountMsgs);						
										?> 
                                        <div class="gallery-box mt-4" align="center">
                                            <a class="gallery-popup" href="tempMsg.php?catId=<?php echo $row[0]?>" title="">
                                                <img class="gallery-demo-img img-fluid mx-auto" src="<?php echo str_replace('../', '', $row[2]);?>" alt="1" />
                                                <h5 class="overlay-title"><?php echo $row[1];?> <b>[<?php echo $rowCountMsgs['msgCount'];?>]</b></h5>
                                                 </a>
                                                 </div>
                                        </div>
                                    </div>
                                <?php
									}
								} else {
								?>
                                 <div class="col-md-12 " align="center">
                                    <?php echo $category2LBL["noData"];?>
                                    </div>
                                <?php
                                }
								?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- end row -->

            </div>
            <!-- end container-fluid -->
        </div>
        <!-- end page-content-wrapper -->
    </div>
        <!-- End Page-content -->
    </div>
    <!-- end main content -->
<!-- End Dashboard page -->
  
 <!--  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="category2Edit.php" target="_parent">
   <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="catName" class="col-form-label myModalLabel"> <?php echo $category2LBL["catName"];?>:</label>
                  <input type="text" class="form-control" id="catName2" name="catName">
               </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>
</form>
 <!--  edit Modal -->
  <script>
 $(document).on("click", '.editModal', function(e) {

	var id = $(this).attr('recordid');
	$("#catName2").val('');
	$("#departNameId").val('');
	$("#catId").val(id);
	$.post("fetchCategory2Name.php",{ id:id,rand:Math.random() } ,function(res){
		data=res.split('#$#');
		$("#catName2").val(data[0]);	
		$("#departNameId").val(data[1]);
	});
	$('#editModal').modal('show');
	
})
 </script>
<?php
include("footer.php");
ob_end_flush();
?>
