<?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");


?>
<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 deletePage(start)
{
	var doDelete = false;
	for (var k=0; k<document.formList.counter.value; 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 ='queueDelete.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 fetchDate2(id)
{
	$.post("fetchNumberDataEdit.php",{ id:id,rand:Math.random() } ,function(res){
		$("#f11").val(res);
		$("#idNum").val(id);
		
	});
	$('#editumbersModal').modal('show');
}
function fetchMessageNumbers(msgKey)
{	
	$("#resultMessageNumbers").text("");
	$.post("getMessageNumbers.php",{rand:Math.random(),msgKey:msgKey} ,function(res){
		$("#resultMessageNumbers").text(res);
		$('#messageNumbersModal').modal('show');
	});
}
function newMessage(msgKey,status)
{
	var result = confirm("<?php echo $confirmAlert;?>");
	if (result == true) {	
		$.post("approveMessage.php",{rand:Math.random(),msgKey:msgKey,status:status} ,function(res){
			if(res == 1)
				alert('<?php echo $queueListLBL["operationDone"] ;?>');
			if(res == 2)
				alert('<?php echo $queueListLBL["operationNotDone"] ;?>');
			location.reload();																															
		});
	}
}
</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["queueResult"]=10;
		$url="queueList.php";
		$idTemp="";
		$querySendMsgTemp = "select * from msg_send_temp where `api`!='1' AND `counter`>='20' AND sent!='1'";
		$querySendMsgTemp .= " ORDER BY `numbers` desc limit ".$_POST["start"].",".$_POST["queueResult"]."";  
		$resultSendMsgTemp = mysqli_query($conn, $querySendMsgTemp);
		$numSendMsgTemp = mysqli_num_rows($resultSendMsgTemp);
		$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['sendErrorMsg'])) {
						if($_GET['sendErrorMsg'] == 2)
							echo result(221);
					}
					if(!empty($_GET["res"]))
						echo result($_GET["res"]);
					?>
                        <form name="formList" action="tempMsg.php" method="post">
                            <div class="card-body">
                               <div class="table-responsive">
                                   <table class="table table-bordered">
                                      <thead>
                                         <tr>
                                         	<th scope="col"><?php echo $archivesListLBL["username"];?></th>
                                         	<th scope="col"><?php echo $queueListLBL["sender"];?></th>
                                           	<th scope="col"><?php echo $queueListLBL["msg"];?></th>
                                            <th scope="col"><?php echo $queueListLBL["number"];?></th>
                                            <th scope="col"><?php echo $queueListLBL["date"];?></th>
                                            <th scope="col"><?php echo $queueListLBL["counter"];?></th>
                                            <th scope="col"><?php echo $queueListLBL["sendCase"];?></th>
                                            <th scope="col" align="center" style="text-align:center"><?php echo $dataGtidLBL["operations"];?></th>
                                         </tr>
                                      </thead>
                                      <tbody>
                                      <?php
                                        if(!empty($numSendMsgTemp)) {
                                            for($i=0;$i<$numSendMsgTemp;$i++){
												$rowSendMsgTemp = mysqli_fetch_array($resultSendMsgTemp);
												$query2  = "select userName from `user_login` WHERE  id='".$rowSendMsgTemp["userId"]."'";
												$result2 = mysqli_query($conn, $query2);
												$numresult2 = mysqli_num_rows($result2);
												$userName = "---";
												if(!empty($numSendMsgTemp)){
													$rowResult2= mysqli_fetch_assoc($result2);
													$userName = $rowResult2['userName'];
												}
												
												$resultSendMsgTempCheck = mysqli_query($conn, "select * from msg_send_temp where numbers='".$rowSendMsgTemp["numbers"]."' and msg='".$rowSendMsgTemp["msg"]."' and userSender='".$rowSendMsgTemp["userSender"]."'");
												$numSendMsgTempCheck = mysqli_num_rows($resultSendMsgTempCheck);
											if($numSendMsgTempCheck>1){		
                                        ?>
                                        
                                         <tr>
                                         	<td style="color:red"><?php echo $userName?></td>
                                            <td style="color:red"><?php echo $rowSendMsgTemp["userSender"]?></td>
                                            <td style="color:red"><?php echo stripcslashes($rowSendMsgTemp["msg"])?></td>
                                            <td style="color:red" align="center"><textArea rows="3" cols="20" name=numbers<?php echo $row[0]?> style="background-color:#fff;border:0;overflow:auto;color:red;border-top-style:solid;border-top-width:1;border-bottom-style:solid;border-bottom-width:1;"><?php echo $rowSendMsgTemp["numbers"]?></textArea></td>
                                            <td  align="center" style="color:red"><?php if(empty($rowSendMsgTemp["insert_date"])){echo " ";} else  {echo date('d-m-Y h:i:s A',$rowSendMsgTemp["insert_date"]);}?></td>
                                            <td style="color:red" align="center"><?php echo $rowSendMsgTemp["counter"]?></td>
                                            <td style="color:red" align="center"><?php if(empty($rowSendMsgTemp[$msgTypeMobily])){echo " ";} else  {echo iconv('UTF-8','windows-1256',$rowSendMsgTemp[$msgTypeMobily]);};?></td>
                                            <td  align="center"><a href=queueDelete.php?id=<?php echo $rowSendMsgTemp["id"]?>&userId=<?php echo $rowSendMsgTemp["userId"]?>><i class="fas fa-2x fa-trash" title="<?php echo $button["delete"];?>"></i></a></td>
                                         </tr>
                                         <?php
											}else{
										?>
                                        <tr>
                                        	<td ><?php echo $userName?></td>
                                            <td ><?php echo $rowSendMsgTemp["userSender"]?></td>
                                            <td ><?php echo stripcslashes($rowSendMsgTemp["msg"])?></td>
                                            <td style="color:red" align="center"><textArea rows="3" cols="20" name=numbers<?php echo $row[0]?> style="background-color:#fff;?>;border:0;overflow:auto;color:red;border-top-style:solid;border-top-width:1;border-bottom-style:solid;border-bottom-width:1;"><?php echo $rowSendMsgTemp["numbers"]?></textArea></td>
                                            <td  align="center" style="color:red"><?php if(empty($rowSendMsgTemp["insert_date"])){echo " ";} else  {echo date('d-m-Y h:i:s A',$rowSendMsgTemp["insert_date"]);}?></td>
                                            <td  align="center"><?php echo $rowSendMsgTemp["counter"]?></td>
                                            <td  align="center"><?php if(empty($rowSendMsgTemp[$msgTypeMobily])){echo " ";} else  {echo iconv('UTF-8','windows-1256',$rowSendMsgTemp[$msgTypeMobily]);};?></td>
                                            <td  align="center"><a href=queueDelete.php?id=<?php echo $rowSendMsgTemp["id"]?>&userId=<?php echo $rowSendMsgTemp["userId"]?>><i class="fas fa-2x fa-trash" title="<?php echo $button["delete"];?>"></i></a></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">
                               	<?php if($numCounter > $_POST["queueResult"]) {?>
                                  <div class="col-sm-12 col-md-5">
                                     <div class="dataTables_info" id="example_info" role="status" aria-live="polite"><?php echo $dataGtidLBL["show"];?> <?php echo $_POST["start"]+1;?> <?php echo $dataGtidLBL["to"];?> <?php echo $_POST["queueResult"];?> <?php echo $dataGtidLBL["from"];?> <?php echo $numCounter;?> <?php echo $dataGtidLBL["entered"];?><span class="select-info"><span class="select-item"> </span></span></div>
                                  </div>
                                  <?php }?>
                                  <div class="col-sm-12 col-md-7">
                                  <?php
									$queryPaging = "SELECT count(id) from msg_send_temp where `api`!='1'  AND `counter`>='20' AND sent!='1'";
									pagingView($queryPaging,$_POST["start"],$_POST["queueResult"]);
									?> 
                                  </div>
                               </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;?>">
                                <?php
								if(!empty($_POST["searchMembers"]))
									echo "<input name='searchMembers' value='".$_POST["searchMembers"]."' type='hidden'>";		
								if(!empty($_POST["name"]))
									echo "<input name='name' value='".$_POST["name"]."' type='hidden'>";
								if(!empty($_POST["number"]))
									echo "<input name='number' value='".$_POST["number"]."' type='hidden'>";
								if(!empty($_POST["msg"]))
									echo "<input name='msg' value='".$_POST["msg"]."' type='hidden'>";	
								if(!empty($_POST["searchFrom"]))
									echo "<input name='searchFrom' value='".$_POST["searchFrom"]."' type='hidden'>";	
								if(!empty($_POST["searchTo"]))
									echo "<input name='searchTo' value='".$_POST["searchTo"]."' type='hidden'>";		
							?>
                            </form>
                        </div>
                    </div> <!-- end col -->
                </div>
                <!-- end row -->
                </div>
                <!-- end container-fluid -->
            </div>
            <!-- end page-content-wrapper -->
        </div>
        <!-- End Page-content -->
    </div>
    <script type="text/javascript">
		function changeConnections(rowId,connection) {	
			$("#sendConnections").children('option[value='+connection+']').attr('selected','selected');
			$("#changeAll").attr('checked',false);
			$("#rowId").val(rowId);
			$("#companyCode").val(connection);
			$('#changeConnectionsModal').modal('show');
		}
		
		function saveNewConnection()
		{
			var newConnection = $("#sendConnections").children(':selected').attr('value');
			if(newConnection == '-1')
			{
				alert('<?php $queueListLBL["changestatusNo"];?>');
				$("#sendConnections").focus();
				return false;
			}
			var rowId = $("#rowId").val();
			var companyCode = $("#companyCode").val();
			var changeAll = 0;
			if ( $("#changeAll").is(':checked') )
				var changeAll = 1;
			$.post("changeConnection.php",{rand:Math.random(),rowId:rowId,newConnection:newConnection,changeAll:changeAll,companyCode:companyCode} ,function(res){
				if(res == 2)
				{
					$("#changeFalse").hide();
					$("#changeTrue").show();
				}
				else
				{
					$("#changeTrue").hide();
					$("#changeFalse").show();
				}
				setTimeout(function(){ location.reload(); },2000);
			});
			
		}
		</script>
    <!-- end main content -->
     <!--  editNumber Modal -->
       <form  name="formEdit" method="POST" action="numberEdit.php" target="_parent" onsubmit="return sub_onclickEdite();">

    <div class="modal fade modal-fullscreen " style="z-index:10000;padding-<?php echo $align;?>:0px" id="editumbersModal" 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 $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="searchValue" class="col-form-label myModalLabel"> <?php echo $queueListLBL["mobile"];?>:</label>
                  <input type="text" class="form-control" id="f11" name="mobile" >
               </div>
             
               
            </div>
            <div class="modal-footer">
               <input type="submit" name="button1" value='<?php echo $button["save"];?>' class="btn bg-primary btn-flat text-white m-1">
            </div>
         </div>
      </div>
    </div>
     <input type="hidden" name="idNum" id="idNum" />
    </form>
    <!--  editNumber Modal -->
    
    <!--  reSendMsgModal Modal -->
    <div class="modal fade modal-fullscreen " style="z-index:10000;padding-<?php echo $align;?>:0px" id="changeConnectionsModal" 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 $queueListLBL["sendTime"];?></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="searchValue" class="col-form-label myModalLabel"> <?php echo $queueListLBL["gate"];?>:</label>
                  <select id="sendConnections" class="form-control" style="padding:0px">
                        <option value="-1"><?php echo $queueListLBL["notSelected"];?></option>
                        <option value="1">STC</option>
                        <option value="2">Zain</option>
                        <option value="3">Mobily</option>
                    </select>
               </div>
               <div class="form-group form-check" style="padding-top:10px">
                <input class="form-check-input" type="checkbox" name="changeAll" id="changeAll">
                <label class="form-check-label myModalLabel" for="changeAll">
                    <?php echo $queueListLBL["changeAll"];?>
                </label>
               </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="button" name="save" value='<?php echo $button["save"];?>' onclick="javascript:saveNewConnection()" class="btn bg-primary btn-flat text-white m-1">
               <input type="hidden" name="rowId" id="rowId"  />
        		<input type="hidden" name="companyCode" id="companyCode"  />
            </div>
         </div>
      </div>
    </div>
    <!--  reSendMsgModal Modal -->
<?php
include("footer.php");
ob_end_flush();
?>
