<?php
/**
 * Processing Forget password form data:
 * check data then generate new password and send it through SMPT Email
 *
 * 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");

$archiveData = '<html>';
$archiveData.= '<head>';
$archiveData.= '<META http-equiv="Content-Language" content="ar-jo">';
//$archiveData.= '<META http-equiv="Content-Type" content="text/html; charset=windows-1256">';
$archiveData.= '<META http-equiv="Content-Type" content="text/html; charset=utf-8">';
$archiveData.= '<link type="text/css" rel="stylesheet">';
$archiveData.= '</head>';
$archiveData.= '<body>';
$archiveData.= '<table border="1" dir="$dirPages2" bordercolor="#ABCDEF" width="50%" cellspacing="0" cellpadding="0">';
$archiveData.= "<tr>
  <td align='center' width='100' class='gridHeaer'>".$reportSearchLBL["username"]."</td>
  <td align='center' width='100' class='gridHeaer'>".$reportSearchLBL["msg"]."</td>
  <td align='center' width='120' class='gridHeaer'>".$reportSearchLBL["sender"]."</td>
  <td align='center' width='150' class='gridHeaer'>".$reportSearchLBL["msgLength"]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$reportSearchLBL["numbers"]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$reportSearchLBL["date"]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[0]." </td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[1]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[2]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[3]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[4]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[5]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[6]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[7]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[8]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[9]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[10]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[11]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[12]."</td>
  <td align='center' width='150' nowrap='nowrap' class='gridHeaer'>".$exportArray[13]."</td>
</tr>";


//username
//msg
//sender
//msgNumber
//numbers
//dateAndTime
//point
//count_number
//numer sent
//Count of sent
//numer delivered
//Count delivered
//numer not delivered
//Count not delivered
//numer not sent
//Count not sent
//numer deleted
//Count deleted
//numer failed
//Count failed




$result = mysqli_query($conn, "SELECT id,section,user,`sender`,`message`,`msg_lenth`,`count_number`,`send_time`,`points`,`insert_time`,`numbers`,`numbers_status`,`real_insert_time` FROM `msg_search` WHERE `searched_by`=".$_SESSION["userId"]." ORDER BY `insert_time` DESC");
while($row= mysqli_fetch_assoc($result))
{
	$allNumbers = $row['numbers'];
	$explodeNumber = explode(',',$allNumbers);
	$explodeNumber = array_filter($explodeNumber);
	$numbersStatus = unserialize($row['numbers_status']);
	$dateVal = date("A h:i Y.m.d",0+$row['real_insert_time']);
	if(count($explodeNumber)>2000)
	{
		$arrayNumbers2000 = array_chunk($explodeNumber, 2000);
		foreach($arrayNumbers2000 AS $stringNumber)
		{
			$send = "";
			$sendCount = 0;
			$delivered = "";
			$deliveredCount = 0;
			$notDelivered = "";
			$notDeliveredCount = 0;
			$notSend = "";
			$notSendCount = 0;
			$deleted = "";
			$deletedCount = 0;
			$fail = "";
			$failCount = 0;
			foreach($stringNumber as $val){
				if(is_array($numbersStatus[$val])){
					if($numbersStatus[$val][0] == 0){
						$send.= $val.",";
						$sendCount++;	
					}
					elseif($numbersStatus[$val][0] == 1){
						$fail.= $val.",";
						$failCount++;	
					}
					elseif($numbersStatus[$val][0] == 2){
						$delivered.= $val.",";
						$deliveredCount++;	
					}
					elseif($numbersStatus[$val][0] == 5){
						$notDelivered.= $val.",";
						$notDeliveredCount++;	
					}
					elseif($numbersStatus[$val][0] == 6){
						$deleted.= $val.",";
						$deletedCount++;	
					}
					else{
						$notSend.= $val.",";
						$notSendCount++;	
					}
				}else{
					if($numbersStatus[$val] == 0){
						$send.= $val.",";
						$sendCount++;	
					}
					elseif($numbersStatus[$val] == 1){
						$fail.= $val.",";
						$failCount++;	
					}
					elseif($numbersStatus[$val] == 2){
						$delivered.= $val.",";
						$deliveredCount++;	
					}
					elseif($numbersStatus[$val] == 5){
						$notDelivered.= $val.",";
						$notDeliveredCount++;	
					}
					elseif($numbersStatus[$val] == 6){
						$deleted.= $val.",";
						$deletedCount++;	
					}
					else{
						$notSend.= $val.",";
						$notSendCount++;	
					}
				}
			}
			 $implodeNumber = implode(',',$stringNumber);
			 $archiveData.= "<tr>
			 <td align='right' valign='top'>".$row['user']."</td>
			 <td align='right' valign='top'>".$row['message']."</td>
			 <td align='center' >".$row['sender']."</td>
			 <td align='center' >".$row['msg_lenth']."</td>
			 <td align='center'>".$implodeNumber."</td>
			 <td align='center'>".$dateVal."</td>
			 <td align='center'>".$row['points']."</td>
			 <td align='center'>".$row['count_number']."</td>
			 <td align='center'>".$send."</td>
			 <td align='center'>".$sendCount."</td>
			 <td align='center'>".$delivered."</td>
			 <td align='center'>".$deliveredCount."</td>
			 <td align='center'>".$notDelivered."</td>
			 <td align='center'>".$notDeliveredCount."</td>
			 <td align='center'>".$notSend."</td>
			 <td align='center'>".$notSendCount."</td>
			 <td align='center'>".$deleted."</td>
			 <td align='center'>".$deletedCount."</td>
			 <td align='center'>".$fail."</td>
			 <td align='center'>".$failCount."</td>
			</tr>";
		}
	}
	else
	{
		$send = "";
		$sendCount = 0;
		$delivered = "";
		$deliveredCount = 0;
		$notDelivered = "";
		$notDeliveredCount = 0;
		$notSend = "";
		$notSendCount = 0;
		$deleted = "";
		$deletedCount = 0;
		$fail = "";
		$failCount = 0;
		foreach($explodeNumber as $val){
				if(is_array($numbersStatus[$val])){
					if($numbersStatus[$val][0] == 0){
						$send.= $val.",";
						$sendCount++;	
					}
					elseif($numbersStatus[$val][0] == 1){
						$fail.= $val.",";
						$failCount++;	
					}
					elseif($numbersStatus[$val][0] == 2){
						$delivered.= $val.",";
						$deliveredCount++;	
					}
					elseif($numbersStatus[$val][0] == 5){
						$notDelivered.= $val.",";
						$notDeliveredCount++;	
					}
					elseif($numbersStatus[$val][0] == 6){
						$deleted.= $val.",";
						$deletedCount++;	
					}
					else{
						$notSend.= $val.",";
						$notSendCount++;	
					}
				}else{
					if($numbersStatus[$val] == 0){
						$send.= $val.",";
						$sendCount++;	
					}
					elseif($numbersStatus[$val] == 1){
						$fail.= $val.",";
						$failCount++;	
					}
					elseif($numbersStatus[$val] == 2){
						$delivered.= $val.",";
						$deliveredCount++;	
					}
					elseif($numbersStatus[$val] == 5){
						$notDelivered.= $val.",";
						$notDeliveredCount++;	
					}
					elseif($numbersStatus[$val] == 6){
						$deleted.= $val.",";
						$deletedCount++;	
					}
					else{
						$notSend.= $val.",";
						$notSendCount++;	
					}
				}
			}
			
		$dateVal = date("A h:i Y.m.d",0+$row['real_insert_time']);
		$archiveData.= "<tr>
		  <td align='right' valign='top'>".$row['user']."</td>
		 <td align='right' valign='top'>".$row['message']."</td>
		 <td align='center' >".$row['sender']."</td>
		 <td align='center' >".$row['msg_lenth']."</td>
		 <td align='center' nowrap='nowrap' width='150'>".$allNumbers."</td>
		 <td align='center'nowrap='nowrap' width='150'>".$dateVal."</td>
		  <td align='center'>".$row['points']."</td>
		 <td align='center'>".$row['count_number']."</td>
		 <td align='center'>".$send."</td>
		 <td align='center'>".$sendCount."</td>
		 <td align='center'>".$delivered."</td>
		 <td align='center'>".$deliveredCount."</td>
		 <td align='center'>".$notDelivered."</td>
		 <td align='center'>".$notDeliveredCount."</td>
		 <td align='center'>".$notSend."</td>
		 <td align='center'>".$notSendCount."</td>
		 <td align='center'>".$deleted."</td>
		 <td align='center'>".$deletedCount."</td>
		 <td align='center'>".$fail."</td>
		 <td align='center'>".$failCount."</td>
		</tr>";
	}
}
$archiveData.= '</table>';
$archiveData.= '</body>';
$archiveData.= '</html>';
echo $archiveData;
header("Content-length: ".strlen($archiveData));
header("Content-type: Text");
header("Content-Disposition: attachment; filename=archivesList.xls");

mysqli_close($conn);
ob_end_flush();  
?>
