<?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($thirdLevel){
	exit;
}

?>
<script language="javascript" type="text/javascript">
function pageing(url,start)
{
	document.formUsersList.start.value=start;
	document.formUsersList.action =url;
	document.formUsersList.submit();
}
</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(empty($_POST["start"])) {
			$_POST["start"] = 0;
			if(empty($_GET["start"]))
				$_POST["start"] = 0;
			else
				$_POST["start"] = (int) $_GET["start"];	
		} else
			$_POST["start"] = (int) $_POST["start"];
			
		if(empty($_POST["numberResult"]))
			$_POST["numberResult"] = 25;
		else
			$_POST["numberResult"] = (int) $_POST["numberResult"];

		$msgResult = 10;
		$url="chargeArchives.php";
		$idTemp="";
		$where=" where 1";
		if(!empty($_POST["userName"]))
		{
			$allUser = array();	
			$resultUserInfo = mysqli_query($conn, "SELECT `id` FROM `user_login` WHERE `userName`='".$_POST["userName"]."' ");
			$rowUserInfo=mysqli_fetch_assoc($resultUserInfo);
			$where .= " and `userId`='".$rowUserInfo['id']."'";	
		}	
		if(!empty($_POST["dateBegin"]))
		{		
			$mBegin = (int)substr($_POST["dateBegin"],0,2);
			$dBegin = (int)substr($_POST["dateBegin"],3,2);
			$yBegin = (int)substr($_POST["dateBegin"],6,4);
			$hBegin = (int)substr($_POST["dateBegin"],11,2);
			$miBegin = (int)substr($_POST["dateBegin"],14,2);
			date_default_timezone_set('Asia/Riyadh');
			$dateBeginTime = mktime($hBegin, $miBegin, 0, $mBegin, $dBegin, $yBegin);
		}	
		if(!empty($_POST["dateEnd"]))
		{		
			$mEnd = (int)substr($_POST["dateEnd"],0,2);
			$dEnd = (int)substr($_POST["dateEnd"],3,2);
			$yEnd = (int)substr($_POST["dateEnd"],6,4);
			$hEnd = (int)substr($_POST["dateEnd"],11,2);
			$miEnd = (int)substr($_POST["dateEnd"],14,2);
			date_default_timezone_set('Asia/Riyadh');
			$dateEndTime = mktime($hEnd, $miEnd, 0, $mEnd, $dEnd, $yEnd);
		}	
		if(!empty($dateBeginTime) && !empty($dateEndTime))
			$where .= " and `date`>='".$dateBeginTime."' and `date`<='".$dateEndTime."'";
		$query  = "select distinct id,userId,`date`,charge,total_charge,chargeFrom,account,totalChargeBeforeCharge,accountBeforeCharge from `user_charge` $where";
		$query .= " ORDER BY `id` desc limit ".$_POST["start"].",".$_POST["numberResult"]."";
		$result = mysqli_query($conn, $query);
		$num = mysqli_num_rows($result);
		$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["res"]))
						echo result($_GET["res"]);
					?>
						<form  name="formUsersList" method="post" style="margin:0px">
                            <div class="card-body">
                               
                               <div class="table-responsive">
                                   <table class="table table-bordered">
                                      <thead>
                                         <tr>
                                            <th scope="col"><?php echo $chargeArchiveLBL["userName"];?></th>
                                            <th scope="col"><?php echo $chargeArchiveLBL["chargeDate"];?></th>
                                            <th scope="col"><?php echo $chargeArchiveLBL["chargeAmount"];?></th>
                                            <th scope="col"><?php echo $chargeArchiveLBL["totalBalanceAfterCharge"];?></th>
                                            <th scope="col"><?php echo $chargeArchiveLBL["currentBalanceAfterCharge"];?></th>
                                            <th scope="col"><?php echo $chargeArchiveLBL["totalBalanceBeforeCharge"];?></th>
                                            <th scope="col"><?php echo $chargeArchiveLBL["currentBalanceBeforeCharge"];?></th>
                                         </tr>
                                      </thead>
                                      <tbody>
                                      <?php
                                        if(!empty($numCounter)) {
                                            while($row= mysqli_fetch_assoc($result)) {
                                        ?>
                                         <tr>
                                            <td>
                                            <?php 
												$resultUserSelect = mysqli_query($conn, "SELECT `userName` FROM `user_login` WHERE `id`='".$row["userId"]."' ");
												$rowUserSelect=mysqli_fetch_assoc($resultUserSelect);
											 echo $rowUserSelect["userName"]
											 ?>
                                            </td>
                                            <td align="center"><?php echo date("A h:i Y.m.d",$row['date']);?></td>
                                            <td align="center"><?php echo $row['charge']?></td>
                                            <td align="center"><?php echo $row['total_charge']?></td>
                                            <td align="center"><?php echo $row['account']?></td>
                                            <td align="center"><?php echo $row["totalChargeBeforeCharge"];?></td>
                                            <td align="center"><?php echo $row["accountBeforeCharge"];?></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 > $msgResult) {?>
                                  <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 $msgResult;?> <?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 user_charge $where";
									pagingView($queryPaging,$_POST["start"],$_POST["numberResult"]);
									?> 
                                  </div>
                               </div>
                               <div class="btn-collection-top">
                               		<!--<button type="button" title="<?php echo $chargeRequestLBL["searchChargeArchives"];?>" data-toggle="modal" data-target="#searchModal" class="btn bg-primary btn-sm btn-flat text-white m-1"><i class="fas fa-search"></i> <?php echo $chargeRequestLBL["searchChargeArchives"];?></button>-->
                               </div>
                            </div>
                              <input type="hidden" name="counter" value='<?php echo $numCounter?>'>
       						 <input type="hidden" name="start" value='<?php echo $_POST["start"]?>'>
                            </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 -->
	<!--  search Modal -->

<script language="javascript" type="text/javascript">
function checkSearch()
{
	if(document.formSearch.searchValue.value == "")
	{
		alert("<?php echo $usersLBL["enterSearchVal"];?>");
		document.formSearch.searchValue.focus();		
		return false;
	}
	return true;
}
</script>

<?php
include("footer.php");
ob_end_flush();
?>
