<?php
        include("homeheader.php");
    ?>

  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-3 left_col">
          <div class="left_col scroll-view">

            <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>Welcome,</span>
				<h2> <?php $fname = $dat['fname']; $lname = $dat['lname']; echo $fname." ".$lname ?> </h2>
             <!--   <h2>John Doe</h2> -->
              </div>
            </div>

            <br />

            <!-- sidebar menu -->
            <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
              <div class="menu_section">
			  <br/>
                <h3>General</h3>
                <ul class="nav side-menu">
                  <li ><a  href="homepage.php" ><i class="fa fa-home"></i>  Home <span class="fa fa-chevron-right"></span></a> </li>
                  <li ><a href="#"><i class="fa fa-building"></i> My Contacts <span class="fa fa-chevron-down"></span></a> 
				  <ul class="nav child_menu">
					    <li ><a  href="addcontact.php" ><i class="fa fa-user-plus"></i>Add New Contact<span class="fa fa-chevron-right"></span></a> </li>
						<li><a  href="addnewcontact.php" ><i class="fa fa-eye"></i>View Contacts<span class="fa fa-chevron-right"></span></a> 
						</li>
					   </ul>
				  </li>
                  <li ><a href="#"><i class="fa fa-users"></i> Groups <span class="fa fa-chevron-down"></span></a> 
				        <ul class="nav child_menu">
					   <li  ><a  href="creategroup.php" ><i class="fa fa-paw"></i>Create Group<span class="fa fa-chevron-right"></span></a> </li>
						<li><a  href="viewgroups.php" ><i class="fa fa-eye"></i>View Groups<span class="fa fa-chevron-right"></span></a> </li>
					   </ul>
				  </li>
                  <li><a><i class="fa fa-table"></i> Topup <span class="fa fa-chevron-down"></span></a>  
				   <ul class="nav child_menu">
                   		<li><a  href="sendtogroup.php" ><i class="fa fa-cloud-download"></i>Top-up Group Contacts<span class="fa fa-chevron-right"></span></a> </li>
						<li><a href="selectcontacttosend.php" ><i class="fa fa-building"></i>Select Contact(s)<span class="fa fa-chevron-right"></span></a> </li>
						<li><a href="sendtome.php" ><i class="fa fa-mobile"></i> Top-up My Line <span class="fa fa-chevron-right"></span></a> </li>
						<li class="active"><a href="#" ><i class="fa fa-recycle"></i> Auto-Send Airtime <span class="fa fa-chevron-right"></span></a> </li>
					   </ul>
					  				  
				  </li>
                 
				  <li><a><i class="fa fa-gg"></i> Billing <span class="fa fa-chevron-down"></span></a> 
				   <ul class="nav child_menu">
                     <li><a  href="purchasetome.php" ><i class="fa fa-server"></i> Top-up My Account<span class="fa fa-chevron-right"></span></a> </li>
					   </ul>
				  </li>
				    <li><a><i class="fa fa-bar-chart-o"></i> History <span class="fa fa-chevron-down"></span></a>  
				      <!--  <ul class="nav side-menu"> -->
					   <ul class="nav child_menu">
					    <li><a  href="creditassignment.php" ><i class="fa fa-bars"></i>Credit Assignment<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>   </p>
            </div>
            
          </div>
		  </div>
          <!-- /top tiles -->
		  
		
		    <br/>
			<div class="x_title">
                    <h2>Auto-Send Airtime to Your Contacts  </h2>
                    <div class="clearfix"></div>
                  </div>
				   
				   <b style="margin-left:20px;font-size:14px;color:green">Send Airtime at Specified Dates Automatically from the System.</b> 
				   <br/> <br/>
					  
				<?php
					if(isset($_POST['auto-send'])) {
						$sendall = $_POST['selectAll'];
						$conts = $_POST['thingp'];
						$grps = $_POST['grup'];
						$sendamnt = $_POST['amount'];
						$senddt = $_POST['senddate'];
							
							$bal = $usrcr['amount'];
							$int = (int)$sendamnt;
							
						if( !empty($sendall) && empty($conts) && empty($grps) ){
							
							$s = mysql_query("select * from airtime_contacts where customer_id = '$session_id' ");
							$nums = mysql_num_rows($s);
							
							$total = $int * $nums;
							
							if( $int < 10 ){
									// error message
								echo ' 
									<b style="color:red;font-size:20px;"> Error! Minimum Amount that you can Auto-Send is Ksh. 10. Please try again. </b> <hr/>
									';
									
							}else{
									
								if($total > $bal){
								// credit not enough, give warning and set auto-send job
									echo '
										<b style="color:red;"> Note! Your Account Balance is insufficient to Send Ksh. '.$total.' Airtime to '.$nums.' Recipient(s). Your account balance is Ksh. '.$bal.'. Please make sure your Account has ENOUGH Balance before Date  '.$senddt.' to Auto-Send Airtime. Thank you. </b> 
										 ';
										
										// set auto-send job
										$s = mysql_query("insert into autosend_airtime values ('', '$session_id', '$senddt','$sendamnt', 'All', '', 'Pending', Now() ) ");
										if($s){
											$qry = mysql_query("select * from airtime_logs where id='$session_lid' ");
											$sdata=mysql_fetch_array($qry);
											$actions=$sdata['user_actions'];
											mysql_query("update airtime_logs set user_actions='$actions  Set an Auto-Send Airtime job to All Contacts, Ksh. $sendamnt each, on Date $senddt.' where id = '$session_lid'  ");
										}
										echo '<br/> <b style="color:#f49e42;font-size:18px;"> Success. Auto-Send Airtime Activated on Date '.$senddt.' at 12:00 AM. Thank you. </b><hr/>';
										
								}else{
									// set auto-send send credit task
										$s = mysql_query("insert into autosend_airtime values ('','$session_id','$senddt','$sendamnt', 'All', '', 'Pending', Now() ) ");
										if($s){
											$qry = mysql_query("select * from airtime_logs where id='$session_lid' ");
											$sdata=mysql_fetch_array($qry);
											$actions=$sdata['user_actions'];
											mysql_query("update airtime_logs set user_actions='$actions  Set an Auto-Send Airtime job to All Contacts, Ksh. $sendamnt each, on Date $senddt.' where id = '$session_lid'  ");
										}
										
										echo ' <b style="color:#f49e42;font-size:18px;"> Success. Auto-Send Airtime Activated on Date '.$senddt.' at 12:00 AM. Thank you.</b><hr/>';
								}
							}
						}
						elseif(!empty($conts) && empty($grps) && empty($sendall) ){
							
							$nums = count($_POST['thingp']);
							$total = $int * $nums;
							
							if( $int < 10 ){
									// error message
								echo ' 
									<b style="color:red;font-size:20px;"> Error! Minimum Amount that you can Auto-Send is Ksh. 10. Please try again. </b> <hr/>
									';
									
							}else{
									
								if($total > $bal){
								// credit not enough, give warning and set auto-send job
									echo '
										<b style="color:red;"> Note! Your Account Balance is insufficient to Send Ksh. '.$total.' Airtime to '.$nums.' Recipient(s). Your account balance is Ksh. '.$bal.'. Please make sure your Account has ENOUGH Balance before Date  '.$senddt.' to Auto-Send Airtime. Thank you. </b> 
										 ';
										 
										 foreach($conts as $things){
											$no = $things;
											
											 	// set auto-send job
											$s1 = mysql_query("insert into autosend_airtime values ('', '$session_id', '$senddt','$sendamnt', '$no', '', 'Pending', Now() ) ");
											if($s1){
												$qry = mysql_query("select * from airtime_logs where id = '$session_lid' ");
												$sdata = mysql_fetch_array($qry);
												$actions = $sdata['user_actions'];
												mysql_query("update airtime_logs set user_actions='$actions  Set an Auto-Send Airtime job to phone number -  $no, Ksh. $sendamnt, on Date $senddt.' where id = '$session_lid'  ");
											}
											
										 }
										
										echo '<br/> <b style="color:#f49e42;font-size:18px;"> Success. Auto-Send Airtime Activated on Date '.$senddt.' at 12:00 AM. Thank you. </b><hr/>';
										 
								}else{
									
										foreach($conts as $things){
											$no = $things;
											
											 	// set auto-send job
											$s1 = mysql_query("insert into autosend_airtime values ('', '$session_id', '$senddt','$sendamnt', '$no', '', 'Pending', Now() ) ");
											if($s1){
												$qry = mysql_query("select * from airtime_logs where id = '$session_lid' ");
												$sdata = mysql_fetch_array($qry);
												$actions = $sdata['user_actions'];
												mysql_query("update airtime_logs set user_actions='$actions  Set an Auto-Send Airtime job to phone number -  $no, Ksh. $sendamnt, on Date $senddt.' where id = '$session_lid'  ");
											}
											
										 }
										
										echo '<br/> <b style="color:#f49e42;font-size:18px;"> Success. Auto-Send Airtime Activated on Date '.$senddt.' at 12:00 AM. Thank you. </b><hr/>';
								}
							}
							
							
						}
						elseif( !empty($grps) && empty($conts) && empty($sendall) ){
							
							if( $int < 10 ){
									// error message
								echo ' 
									<b style="color:red;font-size:20px;"> Error! Minimum Amount that you can Auto-Send is Ksh. 10. Please try again. </b> <hr/>
									';
									
							}else{
								// find total amount to auto-send
								foreach($grps as $ids){
									$gid = $ids;
					                
								$qry = mysql_query("Select SUM(mysum) from (select count(contact) as mysum from airtime_contacts where group_id = '$gid' ) AS bb ");
									$result = mysql_query("select count(contact) AS value_sum from airtime_contacts where group_id = '$gid' and customer_id = '$session_id' ");
									$row = mysql_fetch_array($result); 
					                $nums = $row['value_sum'].',';
					                $cars = array($nums);
					               //  $cars = array(2,4,6,8);
                                     //   echo array_sum($cars);
					              
									
									$sx = mysql_query("select * from airtime_groups where group_id = '$gid' ");
									$getname = mysql_fetch_array($sx);
									$gname = $getname['name'];
												
													 // set auto-send job
										$s3 = mysql_query("insert into autosend_airtime values ('', '$session_id', '$senddt','$sendamnt', 'NA', '$gid', 'Pending', Now() ) ");
											if($s3){
												$qry = mysql_query("select * from airtime_logs where id = '$session_lid' ");
												$sdata = mysql_fetch_array($qry);
												$actions = $sdata['user_actions'];
												mysql_query("update airtime_logs set user_actions = '$actions  Set an Auto-Send Airtime job to Members of Group  -  $gname, Ksh. $sendamnt each, on Date $senddt.' where id = '$session_lid'  ");
											}
								}
								echo '
										<b style="color:red;"> Note! Please make sure your Account has ENOUGH Balance before Date  '.$senddt.' to Auto-Send Airtime. Thank you. </b> 
												';
												
								echo '<br/> <b style="color:#f49e42;font-size:18px;"> Success. Auto-Send Airtime Activated on Date '.$senddt.' at 12:00 AM. Thank you. </b><hr/>';
							}
							
						}elseif( empty($sendall) && empty($conts) && empty($grps) ){
							echo ' <b style="margin-left:20px;color:red;font-size:20px;"> Error! No Recipient(s) Selected. Please Select Recipient(s) to Auto-Send Airtime. </b> <hr/> ';
						}
						else{
							echo ' 
									<b style="margin-left:20px;color:red;font-size:20px;"> Error! Make just ONE selection of the Recipient(s) and try again. </b> <hr/>
									';
						}
						
					}
		
				?>
				   
				 <form name="contactsx" data-parsley-validate class="form-horizontal form-label-left" method="post" >
						<div class="form-group">
						    <b style="margin-left:150px;font-size:14px;" > Select Recipient:  </b> <b style="margin-left:20px;font-size:14px;color:red;">Only one selection criteria allowed at a time</b><br/>
							
						
							<label class="control-label col-md-3 col-sm-3 col-xs-3" for="first-name">Send to All My Contacts: <span class="required"> </span></label>
								<div class="col-md-4 col-sm-4 col-xs-5">
										<input type="checkbox" name="selectAll" value="All" style="margin-top:10px;"> Send to All Contacts<br>
								</div>
						</div>
						<br/>
						
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-3" for="first-name">Select from Contacts List: <span class="required"> </span></label>
								<div class="col-md-7 col-sm-7 col-xs-7">
									<select class="select2_multiple form-control" multiple="multiple" name="thingp[]" >
										<option >--Select Contact--</option>
										<?php
											$sql = "select * from airtime_contacts where customer_id = '$session_id' order by fname ASC";
											$s=mysql_query($sql);
												while ($grp = mysql_fetch_array($s)){
													echo ' <option value="'.$grp['contact'].'">'.$grp['contact']." - ".$grp['fname'].'</option>';
												}
										?>
									</select>
								</div>
						</div>
						
						<br/> 
						<div class="form-group">
								<label class="control-label col-md-3 col-sm-3 col-xs-3" for="last-name"> Select from Group(s)</label>
									<div class="col-md-7 col-sm-7 col-xs-7">
											<select class="select2_single form-control" multiple="multiple" name="grup[]" >
												<option>--Select Group--</option>
													<?php 
														$grps = mysql_query("select * from airtime_groups where customer_id = '$session_id' order by group_id");
															$numrows = mysql_num_rows($grps);
																if($numrows == 0){
														         echo ' <div class="col-md-offset-3 col-md-9">
																	<button id="btn-signup" type="button" class="btn btn-error"><i class="icon-hand-right"></i>Invalid! You Have Not Created Any Group Yet! </button>
                                         
																	</div>';
																}
																else{
																	while($record = mysql_fetch_array($grps)){
																		
																			echo ' <option value="'.$record['group_id'].'">'.$record['name'].'</option>';
																	}
																}
														
													?>    
												</select>
									</div>
						</div>
						<br/> 
					  
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-3" for="first-name">Set Sending Date: <span class="required"> </span></label>
								<div class="col-md-6 col-sm-6 col-xs-5">
										<input type="date" name="senddate" required="required" />&nbsp;&nbsp;&nbsp;
										<b>Time:</b> &nbsp;&nbsp;&nbsp;<input type="time" name="senddate" required="required" />&nbsp;&nbsp;&nbsp;
										GMT Time
								</div>
								
						</div>
						<br/>
						
						<div class="form-group">
							<label class="control-label col-md-3 col-sm-3 col-xs-3" for="first-name">Amount to Send: <span class="required"> </span> </label>
								<div class="col-md-3 col-sm-3 col-xs-3">
									<input type="text" id="amount" name="amount" required="required" placeholder="Ksh" class="form-control col-md-3 col-xs-3">
								</div>
						</div>
					 
					   <br/> <br/>
					   <div class="form-group">
							<div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
								<input type="reset"  class="btn btn-primary" name="cancel" value="Cancel" />
								<input type="submit"  class="btn btn-success" name="auto-send" id="auto_test_send" value="Auto-Send Airtime"/>
							</div>
                      </div>
			  </form>
			    <br/>
			  <br/>
			  
	         <!-- test if the values are retrieved from the button from the form -->
	         <script>
	             $('#auto_test_send').click(function(
	                 alert("Hello test");
	                 ));
	         </script>
		 
		  
		</div>
		</div>
	</div>		
		          <!-- /page content. End of main -->

     <?php include("footerlinks.php"); ?>
 <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>
    <!-- Chart.js -->
    <script src="vendors/Chart.js/dist/Chart.min.js"></script>
    <!-- gauge.js -->
    <script src="vendors/bernii/gauge.js/dist/gauge.min.js"></script>
    <!-- bootstrap-progressbar -->
    <script src="vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
    <!-- iCheck -->
    <script src="vendors/iCheck/icheck.min.js"></script>
    <!-- Skycons -->
    <script src="vendors/skycons/skycons.js"></script>
    <!-- Flot -->
    <script src="vendors/Flot/jquery.flot.js"></script>
    <script src="vendors/Flot/jquery.flot.pie.js"></script>
    <script src="vendors/Flot/jquery.flot.time.js"></script>
    <script src="vendors/Flot/jquery.flot.stack.js"></script>
    <script src="vendors/Flot/jquery.flot.resize.js"></script>
    <!-- Flot plugins -->
    <script src="js/flot/jquery.flot.orderBars.js"></script>
    <script src="js/flot/date.js"></script>
    <script src="js/flot/jquery.flot.spline.js"></script>
    <script src="js/flot/curvedLines.js"></script>
    <!-- jVectorMap -->
    <script src="js/maps/jquery-jvectormap-2.0.3.min.js"></script>
    <!-- bootstrap-daterangepicker -->
    <script src="js/moment/moment.min.js"></script>
    <script src="js/datepicker/daterangepicker.js"></script>
	
	   <script src="vendors/select2/dist/js/select2.full.min.js"></script>
	   
	    <!-- Select2 -->
    <script>
      $(document).ready(function() {
        $(".select2_single").select2({
          placeholder: "Select Group(s)",
          allowClear: true
        });
        $(".select2_group").select2({});
        $(".select2_multiple").select2({
       placeholder: "Chose Contacts to topup",
          allowClear: true
        });
      });
    </script>
    <!-- /Select2 -->
	   
	</body>	  