<?php
// INCLUDE HEADER IN PAGE
include('other_header.php');
// GET CURRENT DATE AND TIME
$date = date('Y-m-d H:i:s');
if(isset($_POST['success'])) {
	if($_POST['success'] == 'loggedin') {
	?>
	<div style="margin-top: -80px;" class="alert alert-success" role="alert">
		<strong>Well done!</strong> Successfully logged in.
	</div>
	<?php
	}
}

///////////////////////////////////////////////////////////////////
			if(isset($_POST['markaspaid'])) {
				// GET AD ID FROM FORM
				$gateway987 = $_POST['gateway'];
				$amount = $_POST['amount'];
				$manager_username987 = $_POST['username'];
				$q1 = "SELECT `id` FROM `Web_Gateways` WHERE `phpname` = '$gateway987'";
				$r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
				$idkol = mysql_result($r1, 0);
				///-------
				$queryes = "SELECT * FROM Web_Withdraws WHERE manager_username='$manager_username987' AND gateway='$idkol'";
				$changetopaid = mysql_query("UPDATE Accounts_Premium_Payments SET got=got + '$amount' WHERE username='$manager_username987'");
				$resultes = mysql_query($queryes) or die('Error, query failed');
				while($rowes = mysql_fetch_array($resultes))
					{	
						$rowidlol = $rowes['id'];
						$changetopaid = mysql_query("UPDATE Web_Withdraws SET adminstatus='1' WHERE id='$rowidlol'");
					}
				///---
				?>
					<div style="margin-top: -80px;" class="alert alert-success" role="alert">
						<strong>Well done!</strong> Marked as paid
					</div>
				<?php

			}// WHEN FORM NOT SUBMITTED
///////////////////////////////////////////////////////////////////
			if(isset($_POST['approverequest'])) {
				// GET AD ID FROM FORM
				$id = $_POST['requestvalue'];
				$changetoplay = mysql_query("UPDATE Ads_All SET approved='1' WHERE id='$id'");
				?>
					<div style="margin-top: -80px;" class="alert alert-success" role="alert">
						<strong>Well done!</strong> Campaign approved
					</div>
				<?php

			}// WHEN FORM NOT SUBMITTED
///////////////////////////////////////////////////////////////////
			if(isset($_POST['disapproverequest'])) {
				// GET AD ID FROM FORM
				$id = $_POST['requestvalue'];
				$changetoplay = mysql_query("UPDATE Ads_All SET approved='-1' WHERE id='$id'");
				?>
					<div style="margin-top: -80px;" class="alert alert-success" role="alert">
						<strong>Well done!</strong> Campaign disapproved
					</div>
				<?php

			}// WHEN FORM NOT SUBMITTED
///////////////////////////////////////////////////////////////////
			if(isset($_POST['addcredits'])) {
				// GET AD ID FROM FORM
				$id = $_POST['requestvalue'];
				// GET VALUE CREDITS FROM FORM
				$credits = $_POST['credits'];
				// DEFINE WHICH AD WANT USER EDIT BY ID AND USERNAME
				$cc = mysql_query("SELECT * FROM Ads_All WHERE id='$id'");
				$ccf = mysql_fetch_array($cc);
				// GET ALL ALLOWED COUNTRIES
				$adcountryf = $ccf['countries'];
				// GET AD TYPE FROM DB
				$adtypef = $ccf['adtype'];
				// DEFINE PRICE FOR COUNTRIES 
				$getprices = mysql_query("SELECT * FROM Web_Countries WHERE country='$adcountryf'");
				// GET ALL PRICES FOR ACTUAL AD
				$getprices2 = mysql_fetch_array($getprices);
				// IF AD TYPE IS INTERSTITATIAL
				if($adtypef == 'interstitial') 
				{
					// GET PRICES FOR INTERSTITATIAL
					$percredit = $getprices2['ppvadv'];
				}
				// IF AD TYPE IS video
				if($adtypef == 'video') 
				{
					// GET PRICES FOR INTERSTITATIAL
					$percredit = $getprices2['cpvvideo'];
				}
				// IF AD TYPE IS BANNER
				if($adtypef == 'banner') 
				{
					// GET PRICES FOR BANNER
					$percredit = $getprices2['ppvbadv'];
				}
				// CALCULATE FINAL PRICE
				$finalprice = $percredit * $credits;
				// WHEN FINAL PRICE VALUE IS LESS THAN USER BALANCE >> UPDATE AD
				$changetoplay = mysql_query("UPDATE Ads_All SET credits=credits + '$credits' WHERE id='$id'");
				?>
					<div style="margin-top: -80px;" class="alert alert-success" role="alert">
						<strong>Well done!</strong> Campaign successfully edited.
					</div>
				<?php

			}// WHEN FORM NOT SUBMITTED
///////////////////////////////////////////////////////////////////
?>
  <div class="container">
    <div class="text-center">
      <h1><?php echo $title; ?>
      	<small>/Active is: Admin Account
			</small>
		</h1>
 	 </div>
 	 </br>
	
   <div class="text-center">
      <h1><small>Send money to your payment managers</small></h1>
 	</div>
		<div class="col-sm-12">
          <table class="table table-bordered" style="width:100%;max-width:100%"  data-effect="fade">
										<thead>
											<tr>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>#</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Payment Manager</th>
		<?php
			$query = "SELECT * FROM Web_Gateways WHERE active_withdraw='1' ORDER BY id ASC";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
				?>
						<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>0 available gateways</th>
   			<?php
   		}else{
   			$numberofgateway = 0;
     			while($row = mysql_fetch_array($result))
				{
					$numberofgateway = $numberofgateway + 1;
					$gateway[$numberofgateway] = $row['phpname'];
					if(isset($gateways)) {
						$gateways = $gateways.','.$row['id'];
					}
					if(!isset($gateways)) {
						$gateways = $row['id'];
					}
					?>
						<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $row['name']; ?></th>
					<?php
				}
       	}
		?>
											</tr>
										</thead>
	<?php
			$query = "SELECT * FROM Accounts_Premium_Payments";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
	if(mysql_num_rows($result)<1){
   echo "
						<tr>
							<td>No advertisers</td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
						</tr>
   ";
   }else{
	$numberoflink = 0;
     while($row = mysql_fetch_array($result))
				{
					$numberoflink = $numberoflink + 1;
					$manager_username = $row['username'];
					$queryuser = "SELECT * FROM Accounts_Premium_Payments WHERE username='$manager_username'";
					$resultuser = mysql_fetch_array(mysql_query($queryuser)) or die('Error, query failed');
					$urlst = explode(",", $gateways);
					$icount = 0;
					while($icount < sizeof($urlst))
					{
						$cuntl = trim($urlst[$icount]);
						$icount++;
						$querywhile = "SELECT * FROM Web_Withdraws WHERE manager_username='$manager_username' AND gateway='$cuntl' AND adminstatus='0'";
						$resultwhile = mysql_query($querywhile) or die('Error, query failed');
						while($rowwhile = mysql_fetch_array($resultwhile))
						{
							$fullname = $gateway[$icount];
							$nextamount[$fullname] = $nextamount[$fullname] + $rowwhile['amount'];
						}
					}
				?>
						<tr>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
									<h5><?php echo $numberoflink; ?></h5>
							</td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
									<h5><?php echo $row['username']; ?></h5>
							</td>
							<?php
							$querye = "SELECT * FROM Web_Gateways WHERE active_withdraw='1' ORDER BY id ASC";
							$resulte = mysql_query($querye) or die('Error, query failed');
							if(mysql_num_rows($resulte)<1){
								?>
									<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>0 available gateways</th>
   							<?php
   						}else{
   							$numberofgateway = 0;
     							while($rowe = mysql_fetch_array($resulte))
								{	
									$namenow = $rowe['phpname'];
									$testamount = $nextamount[$namenow];
									if($testamount == 0) {
										?>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<center>
												<input type='submit' name='markaspaid' class='btn btn-success' value='Paid'>
												</center>
											</td>
										<?php
									}else {
										$queryemail = "SELECT * FROM Web_Gateways WHERE phpname='$namenow'";
										$resultemail = mysql_fetch_array(mysql_query($queryemail)) or die('Error, query failed');
										$inputmail = $resultemail['data6'];
										?>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<center>
												<form method='POST'>
													<input type='hidden' name='username' value='<?php echo $manager_username; ?>'>
													<input type='hidden' name='gateway' value='<?php echo $namenow; ?>'>
													<input type='hidden' name='amount' value='<?php echo $nextamount[$namenow]; ?>'>
        											<input type='submit' name='markaspaid' class='btn btn-default' value='Mark as paid'>
        										</form>
        										<input style="cursor:copy; opacity: 1" class="form-control autosize" rows="1" value="<?php echo $resultuser[$inputmail]; ?>" onmouseover="this.select()" readonly="">
        										<?php echo $acu['ISO'];?> <input style="cursor:copy; opacity: 1" class="form-control autosize" rows="1" value="<?php echo $nextamount[$namenow]; ?>" onmouseover="this.select()" readonly="">
												</center>
											</td>
										<?php
										$nextamount[$namenow] = 0;
									}
								}
       					}
						?>
						</tr>
				<?php
				}
			}
	?>
									</table>
        </div>
</div>

<?php
include('other_footer.php');
?>
