<?php include("homeheader.php");?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap -->
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="../vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- iCheck -->
    <link href="../vendors/iCheck/skins/flat/green.css" rel="stylesheet">
    <!-- Datatables -->
    <link href="../vendors/datatables.net-bs/css/dataTables.bootstrap.min.css" rel="stylesheet">
    <link href="../vendors/datatables.net-buttons-bs/css/buttons.bootstrap.min.css" rel="stylesheet">
    <link href="../vendors/datatables.net-fixedheader-bs/css/fixedHeader.bootstrap.min.css" rel="stylesheet">
    <link href="../vendors/datatables.net-responsive-bs/css/responsive.bootstrap.min.css" rel="stylesheet">
    <link href="../vendors/datatables.net-scroller-bs/css/scroller.bootstrap.min.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="../css/custom.css" rel="stylesheet">
  </head>
  <body>

  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
	  
	   <?php 
			$user = $dat['company'];
			$qry=mysql_query("select * from airtime_admin where username='$user' and account='Mobisky' ");
			
			if(mysql_num_rows($qry)> 0){
				?>
				 <div class="col-md-3 left_col">
          <div class="left_col scroll-view">
            <div class="navbar nav_title" style="border: 0;">
              <a href="index.html" class="site_title">
			    <div class="profile_pic" style="margin-top:-10px;">
					<img src="../images/mlogo.png" alt="" class="img-circle profile_img" />
			    </div>
			  <!--i class="fa fa-paw"></i--> <span>Instant Airtime </span></a>
            </div>
            <div class="clearfix"></div>
            <!-- menu profile quick info -->
            <div class="profile">
              <div class="profile_pic">
			    <?php 
			  if($img == ""){
				  ?>
				   <img src="../profileimages/noimage.gif" alt="..." class="img-circle profile_img">
				  <?php
			  }else{
				?>
				 <img src="<?php echo $photo ?>" alt="..." class="img-circle profile_img" width="40px;" height="60px;">
            <?php				
			  }
			  ?>
              </div>
              <div class="profile_info">
                <span>Mobisky Admin </span>
				<h2> <?php $fname = $dat['fname']; $lname = $dat['lname']; echo $fname." ".$lname ?> </h2>
             <!--   <h2>John Doe</h2> -->
              </div>
            </div>
            <!-- /menu profile quick info -->
            <br/>	
				  <!-- sidebar menu -->
            <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
              <div class="menu_section">
			  
                <h3>General</h3>
                <ul class="nav side-menu">
                  <li class="active"><a  href="adminpage.php" ><i class="fa fa-home"></i>  Home <span class="fa fa-chevron-right"></span></a> </li>
                  <li><a href="viewadmins.php">System Users <span class="fa fa-chevron-right"></span></a> </li>
                  <li><a href="requests.php">Topup Requests  <span class="fa fa-chevron-right"></span> </a>   </li>
				   <li><a href="sendinghistory.php"> Topup History  <span class="fa fa-chevron-right"></span></a>   </li>
                </ul>
              </div>
            </div>
			<!--end side menubar -->
			</div>
        </div>
		
		 <?php include("admintopmenubar.php"); ?>
		 
					 <!-- page content -->
        <div class="right_col" role="main">
		  <!-- /top navigation -->
		     <div class="row">
          <!-- top tiles -->
          <div class="row tile_count">
            <div class="col-md-2 col-sm-4 col-xs-6 tile_stats_count">
              <p>Airtime Portal </p>
            </div>
            
          </div>
		  </div>
          <!-- /top tiles -->
		 <div class="row">
		 	 <!-- contacts table -->
		       <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Airtime Topup Requests List    </h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                     <table id="datatable" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>Account For</th>
						  <th>Company</th>
						  <th>Amount Requested</th>
						  <th>Request Date</th>
						  <th>Amount Awarded</th>
						  <th>Date Awarded</th>
						   <th>Status</th>
                          <th>Delete</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
					  $us = mysql_query("select * from airtime_accounts ");
					   while($rws = mysql_fetch_assoc($us)){
							    $id=  $rws['id']; 
								$cid=  $rws['account_id'];
						         $amnt=  $rws['amount_requested'];
						         $sts=  $rws['status'];
						         $rdte=  $rws['date_requested'];
								 
					  
						   $s = mysql_query("select * from airtime_customer  ");
						   $rw = mysql_fetch_assoc($s);
							    $fname =  $rw['fname'];
							    $lname =  $rw['lname'];
								  
								    echo "<tr>";
									echo '<td>' . $fname. " ".$lname. '</td>';
									echo '<td>' .  $rw['company']. '</td>';
									echo '<td>' . $amnt . '</td>';
									echo '<td>' . $rdte . '</td>';
									echo '<td>' .$rws['amount_awarded'].'</td>';
									echo '<td>' .$rws['date_awarded'].'</td>';
									?>
								   <td> 
										<?php if( $sts == "Done"){
									   	echo '<div class="btn btn-success btn-xs">' . $sts. '</div>';
										}else{
									   	echo '<div class="btn btn-danger btn-xs">' . $sts. '</div>';
										}
									   ?>
								   </td>
									
									<td>
										<a rel="tooltip" title="Delete Request" id="<?php echo $id ?>" href="#deleteadminrequest<?php echo $id ?>" data-toggle="modal">
								       <i class='fa fa-trash'></i></a>
									    <?php include('deleteadminrequest.php'); ?>	
										</td>
									<?php		
						   }
						   
							  
                       echo "</table>";
					  
					  ?>  
                       
                      </tbody>
                    </table>
                  </div> 
                </div>
				
				
			<form method="post"	>
				<div class="x_panel">
                  <div class="x_title">
				      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Account For:</label>
                        <div class="col-md-3 col-sm-3 col-xs-3">
                          <select class="form-control"  name="user" required>
                            <option value="">Choose User</option>
							<?php
							
								$us = mysql_query("select * from airtime_accounts where status='Pending'  ");
								while($rws = mysql_fetch_assoc($us)){
									$cid=  $rws['account_id'];
									
									 $sql = "select * from airtime_customer where customer_id='$cid' order by fname desc";
								  $s=mysql_query($sql);
								  	while ($grp = mysql_fetch_array($s)){
													echo ' <option value="'.$grp['customer_id'].'">'.$grp['fname']." ".$grp['lname'].'</option>';
												}
								}
								 
							?>
                          </select>
                        </div>
                      </div>
				  <br/>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
				    <div class="col-md-3 col-sm-3 col-xs-3">
					  Enter Amount to Topup (Ksh.):
                          <input type="text" id="amount2" name="amount2" required="required" placeholder="Ksh" class="form-control col-md-3 col-xs-3">
                    </div>
						<div class="col-md-3 col-sm-3 col-xs-3">
							<button type="submit" style="margin-top:20px;" class="btn btn-primary " name="sendairtime2">Topup Selected Account</button>  
					  </div>
				  </div>
				</div>
		    </form>
		  
		  <?php 
		      if(isset($_POST['sendairtime2'])){
				
					    $amnt=$_POST['amount2'];
						$cid=$_POST['user'];
						$amount=(int)$amnt;
						
				 if(!empty($_POST['user']) && !empty($amnt)){
						
						  $us = mysql_query("select * from airtime_accounts where account_id='$cid' and amount_awarded='0' and status='Pending' ");
							$rdt=mysql_fetch_array($us);
							$rid=$rdt['id'];
							
							$qr=mysql_query("select * from airtime_customer where customer_id='$cid' ");
								$comp=mysql_fetch_array($qr);
								$admcomp=$comp['company'];
								$fnam=$comp['fname'];
								$lname=$comp['lname'];
								$name=$fnam." ".$lname;
					 
							$query=mysql_query("update airtime_accounts set amount_awarded='$amnt', status='Done', date_awarded=Now() where id='$rid' ");
							if($query){
								
								// update admin credit accounts
								$cur=mysql_query("select * from airtime_admin_credits where customer_id='$cid' ");
								// if first time user, create user, else addup user credit
								if(mysql_num_rows($cur) > 0){
									// sum up total credit for this admin
									$curdt=mysql_fetch_array($cur);
									$curval = $curdt['current_amount'];
								
									$total_amount = (int)$curval + $amount;
									 mysql_query("update airtime_admin_credits set current_amount='$total_amount' where customer_id='$cid'  ");
									 
								}else{
									// first time admin, create admin credit account
									 mysql_query("insert into airtime_admin_credits values('','$cid','$admcomp','$amount',Now())");
								}
								
								$qry2 = mysql_query("select * from airtime_logs where id='$session_lid' ");
								$sdata=mysql_fetch_array($qry2);				
								$actions = $sdata['user_actions'];
								mysql_query("update airtime_logs set user_actions='$actions Credited account for $name of $admcomp with Ksh. $amnt.' where id='$session_lid'  ");
								?>
									<script>
										alert("Account updated Successfully.");
										window.location="requests.php";
									</script>
								<?php
							}else{
							?>
								<script>
									alert("Error updating this request! Please try again.");
									window.location="requests.php";
								</script>
							<?php
							}
					 
				}else{
					?>
					<script>
					alert("Error! Select Account and Enter Amount to Topup.");
					</script>
					<?php
				}
				 
			 }
		  ?>
		  
				
				
              </div>
		 <!-- end contacts table -->
		 	 
		  </div>
		  
		  </div>
		 
		 
			<?php	
			}
			else{
				?>
				<div class="col-md-3 left_col">
          <div class="left_col scroll-view">
            <div class="navbar nav_title" style="border: 0;">
              <a href="index.html" class="site_title">
			     <div class="profile_pic" style="margin-top:-10px;">
			  <img src="../images/mlogo.png" alt="" class="img-circle profile_img" />
			    </div>
			  <!--i class="fa fa-paw"></i--> <span>Instant Airtime </span></a>
            </div>

            <div class="clearfix"></div>
            <!-- menu profile quick info -->
            <div class="profile">
              <div class="profile_pic">
			     <?php 
			  if($img == ""){
				  ?>
				   <img src="../profileimages/noimage.gif" alt="..." class="img-circle profile_img">
				  <?php
			  }else{
				?>
				 <img src="<?php echo $photo ?>" alt="..." class="img-circle profile_img" width="40px;" height="60px;">
            <?php				
			  }
			  ?>
               
              </div>
             <div class="profile_info">

                <span>Admin </span>
				<h2> <?php $fname = $dat['fname']; $lname = $dat['lname']; echo $fname." ".$lname ?> </h2>
             <!--   <h2>John Doe</h2> -->
              </div>
            </div>
            <!-- /menu profile quick info -->

            <br />

            <!-- sidebar menu -->
          <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
              <div class="menu_section">
			  
                <h3>General</h3>
                <ul class="nav side-menu">
                  <li ><a  href="adminpage.php" ><i class="fa fa-home"></i>  Home <span class="fa fa-chevron-right"></span></a> </li>
                   <li><a href="#">  Users <span class="fa fa-chevron-down"></span></a> 
				  <ul class="nav child_menu">
					    <li><a  href="adduser.php" ><i class="fa fa-bug"></i>Create User<span class="fa fa-chevron-right"></span></a> </li>
						<li><a  href="viewuser.php" ><i class="fa fa-edit"></i>View Users<span class="fa fa-chevron-down"></span></a> </li>
					   </ul>
				  </li>
                  <li  ><a href="contacts.php">  View Contacts <span class="fa fa-chevron-right"></span> </a> </li>
                  <li><a href="groups.php"> View Groups  <span class="fa fa-chevron-right"></span> </a>   </li>
                  <li class="active"><a href="#">Topup Requests  <span class="fa fa-chevron-right"></span> </a>   </li>
                   <li><a href="topupaccount.php">Topup My account <span class="fa fa-chevron-right"></span> </a>   </li>
				   <li><a href="#"> Reports <span class="fa fa-chevron-down"></span></a>  
				      <!--  <ul class="nav side-menu"> -->
					   <ul class="nav child_menu">
					    <li><a  href="buyinghistory.php" > Purchase History <span class="fa fa-chevron-right"></span> </a> </li>
						<li><a  href="sendinghistory.php" > Sending History <span class="fa fa-chevron-right"></span> </a></li>
					   </ul>
				   </li>
                </ul>
              </div>

            </div>
			 </div>
        </div>
		<?php  include("topmenubar.php"); ?>
		
				 <!-- page content -->
        <div class="right_col" role="main">
		  <!-- /top navigation -->
		     <div class="row">
          <!-- top tiles -->
          <div class="row tile_count">
            <div class="col-md-2 col-sm-4 col-xs-6 tile_stats_count">
              <p>Airtime Portal </p>
            </div>
            
          </div>
		  </div>
          <!-- /top tiles -->
		 <div class="row">
		 	 <!-- contacts table -->
		       <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Airtime Topup Requests List    </h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                     <table id="datatable" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>Account For</th>
						  <th>Message</th>
						  <th>Amount Requested</th>
						  <th>Request Date</th>
						  <th>Amount Awarded</th>
						  <th>Date Awarded</th>
						   <th>Status</th>
                          <th>Delete</th>
                        </tr>
                      </thead>
                      <tbody>
                      <?php
					  $frm  = $dat['company'];
					  $us = mysql_query("select * from airtime_topup_requests where company='$frm' ");
					   while($rws = mysql_fetch_assoc($us)){
							    $id=  $rws['request_id']; 
								$cid=  $rws['customer_id'];
						         $message=  $rws['message'];
						         $amnt=  $rws['amount_requested'];
						         $sts=  $rws['status'];
						         $rdte=  $rws['date_requested'];
								 
					  
						   $s = mysql_query("select * from airtime_customer where customer_id='$cid' ");
						   $rw = mysql_fetch_assoc($s);
							    $fname =  $rw['fname'];
							    $lname =  $rw['lname'];
								  
								    echo "<tr>";
									echo '<td>' . $fname. " ".$lname. '</td>';
									echo '<td>' . $message . '</td>';
									echo '<td>' . $amnt . '</td>';
									echo '<td>' . $rdte . '</td>';
									echo '<td>' .$rws['amount_awarded'].'</td>';
									echo '<td>' .$rws['date_awarded'].'</td>';
									?>
								   <td> 
										<?php if( $sts == "Done"){
									   	echo '<div class="btn btn-success btn-xs">' . $sts. '</div>';
										}else{
									   	echo '<div class="btn btn-danger btn-xs">' . $sts. '</div>';
										}
									   ?>
								   </td>
									
									<td>
										<a rel="tooltip" title="Delete Request" id="<?php echo $id ?>" href="#deleterequest<?php echo $id ?>" data-toggle="modal">
								       <i class='fa fa-trash'></i></a>
									    <?php include('deleterequest.php'); ?>	
										</td>
									<?php		
						   }
						   
							  
                       echo "</table>";
					  
					  ?>  
                       
                      </tbody>
                    </table>
                  </div> 
                </div>
				
				
			<form method="post"	>
				<div class="x_panel">
                  <div class="x_title">
				      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Account For:</label>
                        <div class="col-md-3 col-sm-3 col-xs-3">
                          <select class="form-control"  name="user" required>
                            <option value="">Choose User</option>
							<?php
							 $frm  = $dat['company'];
								$us = mysql_query("select * from airtime_topup_requests where company='$frm' and status='Pending'  ");
								while($rws = mysql_fetch_assoc($us)){
									$cid=  $rws['customer_id'];
									
									 $sql = "select * from airtime_customer where customer_id='$cid' order by fname desc";
								  $s=mysql_query($sql);
								  	while ($grp = mysql_fetch_array($s)){
													echo ' <option value="'.$grp['customer_id'].'">'.$grp['fname']." ".$grp['lname'].'</option>';
												}
								}
								 
							?>
                          </select>
                        </div>
                      </div>
				  <br/>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
				    <div class="col-md-3 col-sm-3 col-xs-3">
					  Enter Amount to Topup (Ksh.):
                          <input type="text" id="amount2" name="amount2" required="required" placeholder="Ksh" class="form-control col-md-3 col-xs-3">
                    </div>
						<div class="col-md-3 col-sm-3 col-xs-3">
							<button type="submit" style="margin-top:20px;" class="btn btn-primary " name="sendairtime2">Topup Selected Account</button>  
					  </div>
				  </div>
				</div>
		    </form>
		  
		  <?php 
		      if(isset($_POST['sendairtime2'])){
				  //  check whether admin account has some credit
				  if($bal=0 ){
					  ?>
						<script>
							alert("Erro! You have no credit left in your Acount. Please Topup your Account.");
							window.location="requests.php";
						</script>
					<?php
				  }else{
					  
					    $amnt=$_POST['amount2'];
						$cid=$_POST['user'];
						$amount=(int)$amnt;
						$bal=$adcr['current_amount'];
						
				 if(!empty($_POST['user']) && !empty($amnt)){
					 
					 // check whether admin account have enough credit
					 if($amount > $bal) {
						
						 ?>
						<script>
							alert("Error! You have no enough credit in your Acount to send <?php echo "Ksh. ".$amnt; ?>. Your Account balance is <?php echo "Ksh. ".$bal; ?>.");
							window.location="requests.php";
						</script>
					<?php
					 }else{
						
						  $us = mysql_query("select * from airtime_topup_requests where customer_id='$cid' and amount_awarded='0' and status='Pending' ");
							$rdt=mysql_fetch_array($us);
							$rid=$rdt['request_id'];
					 
							$query=mysql_query("update airtime_topup_requests set amount_awarded='$amnt', status='Done', date_awarded=Now() where request_id='$rid' ");
							if($query){
								$remamnt = ((int)$bal) - $amount;
								//send credit to this account user
								// get current amount
								$cur=mysql_query("select * from airtime_user_credits where customer_id='$cid' ");
								// if first time user, create user, else addup user credit
								if(mysql_num_rows($cur) > 0){
									// sum up total credit for this user
									$curdt=mysql_fetch_array($cur);
									$curval = $curdt['current_amount'];
								
									$total_amount = (int)$curval + $amount;
									 mysql_query("update airtime_user_credits set current_amount='$total_amount ' where customer_id='$cid'  ");
									 
								}else{
									// first time user, create user credit account
									 mysql_query("insert into airtime_user_credits values('','$cid','$amnt',Now())");
								}
								
								mysql_query("update airtime_admin_credits set current_amount='$remamnt', last_updated=Now() where customer_id='$session_id'");
								
								$qr=mysql_query("select * from airtime_customer where customer_id='$cid' ");
								$usrdt=mysql_fetch_array($qr);
								$fname = $usrdt['fname'];
								$lname = $usrdt['lname'];
								$name = $fname." ".$lname;
								
								$qry2 = mysql_query("select * from airtime_logs where id='$session_lid' ");
								$sdata=mysql_fetch_array($qry2);				
								$actions = $sdata['user_actions'];
								mysql_query("update airtime_logs set user_actions='$actions Credited account for $name with Ksh. $amnt.' where id='$session_lid'  ");
								?>
									<script>
										alert("Account updated Successfully.");
										window.location="requests.php";
									</script>
								<?php
							}else{
							?>
								<script>
									alert("Error updating this request! Please try again.");
									window.location="requests.php";
								</script>
							<?php
							}
						 
						}
					 
				}else{
					?>
					<script>
					alert("Error! Select Account and Enter Amount to Topup.");
					</script>
					<?php
				}
					  
				  }
				 
			 }
		  ?>
		  
				
				
              </div>
		 <!-- end contacts table -->
		 	 
		  </div>
		  
		  </div>
		
		
				
			<?php	
			}
			
	?>
	  
        
		 
	 
		</div>
		  
	</div>  
		  
		          <!-- /page content. End of main -->

     <?php include("footerlinks.php"); ?>
	 
	   <!-- jQuery -->
    <script src="../vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="../vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="../vendors/fastclick/lib/fastclick.js"></script>
    <!-- NProgress -->
    <script src="../vendors/nprogress/nprogress.js"></script>
    <!-- Datatables -->
    <script src="../vendors/datatables.net/js/jquery.dataTables.min.js"></script>
    <script src="../vendors/datatables.net-bs/js/dataTables.bootstrap.min.js"></script>
    <script src="../vendors/datatables.net-buttons/js/dataTables.buttons.min.js"></script>
    <script src="../vendors/datatables.net-buttons-bs/js/buttons.bootstrap.min.js"></script>
    <script src="../vendors/datatables.net-buttons/js/buttons.flash.min.js"></script>
    <script src="../vendors/datatables.net-buttons/js/buttons.html5.min.js"></script>
    <script src="../vendors/datatables.net-buttons/js/buttons.print.min.js"></script>
    <script src="../vendors/datatables.net-fixedheader/js/dataTables.fixedHeader.min.js"></script>
    <script src="../vendors/datatables.net-keytable/js/dataTables.keyTable.min.js"></script>
    <script src="../vendors/datatables.net-responsive/js/dataTables.responsive.min.js"></script>
    <script src="../vendors/datatables.net-responsive-bs/js/responsive.bootstrap.js"></script>
    <script src="../vendors/datatables.net-scroller/js/datatables.scroller.min.js"></script>
    <script src="../vendors/jszip/dist/jszip.min.js"></script>
    <script src="../vendors/pdfmake/build/pdfmake.min.js"></script>
    <script src="../vendors/pdfmake/build/vfs_fonts.js"></script>

    <!-- Custom Theme Scripts -->
    <script src="../js/custom.js"></script>

    <!-- Datatables -->
    <script>
      $(document).ready(function() {
        var handleDataTableButtons = function() {
          if ($("#datatable-buttons").length) {
            $("#datatable-buttons").DataTable({
              dom: "Bfrtip",
              buttons: [
                {
                  extend: "copy",
                  className: "btn-sm"
                },
                {
                  extend: "csv",
                  className: "btn-sm"
                },
                {
                  extend: "excel",
                  className: "btn-sm"
                },
                {
                  extend: "pdfHtml5",
                  className: "btn-sm"
                },
                {
                  extend: "print",
                  className: "btn-sm"
                },
              ],
              responsive: true
            });
          }
        };

        TableManageButtons = function() {
          "use strict";
          return {
            init: function() {
              handleDataTableButtons();
            }
          };
        }();

        $('#datatable').dataTable();
        $('#datatable-keytable').DataTable({
          keys: true
        });

        $('#datatable-responsive').DataTable();

        $('#datatable-scroller').DataTable({
          ajax: "js/datatables/json/scroller-demo.json",
          deferRender: true,
          scrollY: 380,
          scrollCollapse: true,
          scroller: true
        });

        var table = $('#datatable-fixed-header').DataTable({
          fixedHeader: true
        });

        TableManageButtons.init();
      });
    </script>
    <!-- /Datatables -->
  </body>
</html>