<?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['editrequest'])) {
				// GET AD ID FROM FORM
				$id = $_POST['requestvalue'];
				$slect = mysql_query("Select * FROM Web_Withdraws WHERE id='$id'");
				$resultas = mysql_fetch_array($slect);
				?>
	<div class="row">
      <div class="col-md-8 col-md-offset-2">
        <div class="well">
			<div class="row">
				<center>
				<form name="csrf_form" id="campaignform" action="" method="POST">
					<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
							<center>
							<input class="form-control" value="<?php echo $resultas['message']?>" placeholder="Message" name="message" type="text">
							</center>
							<center>
							<input id="paid" class="toggle toggle-left" name="paystatus" value="1" type="radio" <?php if($resultas['status'] == '1'){echo 'checked';}?>><label style="min-width:30.2%;width:auto" class="btn btn-default" for="paid" class="btn">Paid</label>
							<input id="cancel" class="toggle toggle-right" name="paystatus" value="0" type="radio" <?php if($resultas['status'] == '0'){echo 'checked';}?>><label style="min-width:30.2%;width:auto" class="btn btn-default" for="cancel" class="btn">Canceled</label>
							<input id="unprocessed" class="toggle toggle-right" name="paystatus" value="-2" type="radio" <?php if($resultas['status'] == '-2'){echo 'checked';}?>><label style="min-width:30.2%;width:auto" class="btn btn-default" for="unprocessed" class="btn">Unprocessed</label>
							</center>
							<span class="input-group-btn">
								<input value="Edit" class="btn btn-default" style="width: 150px;" name="editprocess" type="submit">
							</span>					
					</div>
					<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
					<input type="hidden" name="requestvalue" value="<?php echo $id; ?>">
				</form>
				</br>
				</center>
			</div>
        </div>
      </div>
    </div>
				<?php

			}// WHEN FORM NOT SUBMITTED
///////////////////////////////////////////////////////////////////
			if(isset($_POST['editprocess'])) {
				// GET AD ID FROM FORM
				$id = $_POST['requestvalue'];
				$messagea = $_POST['message'];
				$paystatus = $_POST['paystatus'];
				$statusquery = "SELECT * FROM Web_Withdraws WHERE id='$id' LIMIT 1";
				$statusresult = mysql_query($statusquery) or die('Error, query failed');
				$statusarray = mysql_fetch_array($statusresult);
				$currentstatus = $statusarray['status'];
				if($paystatus == '1') {
					$edit = mysql_query("UPDATE Web_Withdraws SET status='$paystatus',message='$messagea' WHERE id='$id'");
				}
				if($paystatus == '0') {
					if($currentstatus != $paystatus && $currentstatus == '-2') {
					$query = "SELECT * FROM Web_Withdraws WHERE id='$id' LIMIT 1";
					$result = mysql_query($query) or die('Error, query failed');

					if(mysql_num_rows($result)<1){
   				}else{
     				while($row = mysql_fetch_array($result))
						{
							$usernameCHECK = $row['client_username'];
							$acctypeCHECK = $row['client_acctype'];
							$paymailCHECK = $row['client_paymail'];
							$amountCHECK = $row['amount'];
							$gatewayCHECK = $row['gateway'];
						}
					}
					$q1 = "SELECT `fee_withdraw` FROM `Web_Gateways` WHERE `id` = '$gatewayCHECK'";
					$r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
					$d1 = mysql_result($r1, 0);
					$usercashback = $d1 + $amountCHECK;
					if($acctypeCHECK == 'publisher') {
						$edit = mysql_query("UPDATE Accounts_Classic_Publishers SET balance=balance + '$usercashback' WHERE username='$usernameCHECK'");
					}
					if($acctypeCHECK == 'advertiser') {
						$edit = mysql_query("UPDATE Accounts_Classic_Advertisers SET balance=balance + '$usercashback' WHERE username='$usernameCHECK'");
					}
					}
					$edit = mysql_query("UPDATE Web_Withdraws SET status='$paystatus',message='$messagea' WHERE id='$id'");
				}
				if($paystatus == '-2') {
					if($currentstatus != $paystatus && $currentstatus == '1' && $currentstatus != '0') {
					$query = "SELECT * FROM Web_Withdraws WHERE id='$id' LIMIT 1";
					$result = mysql_query($query) or die('Error, query failed');

					if(mysql_num_rows($result)<1){
   				}else{
     				while($row = mysql_fetch_array($result))
						{
							$usernameCHECK = $row['client_username'];
							$acctypeCHECK = $row['client_acctype'];
							$paymailCHECK = $row['client_paymail'];
							$amountCHECK = $row['amount'];
							$gatewayCHECK = $row['gateway'];
						}
					}
					$q1 = "SELECT `fee_withdraw` FROM `Web_Gateways` WHERE `id` = '$gatewayCHECK'";
					$r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
					$d1 = mysql_result($r1, 0);
					$usercashback = $d1 + $amountCHECK;
					if($acctypeCHECK == 'publisher') {
						$edit = mysql_query("UPDATE Accounts_Classic_Publishers SET balance=balance + '$usercashback' WHERE username='$usernameCHECK'");
					}
					if($acctypeCHECK == 'advertiser') {
						$edit = mysql_query("UPDATE Accounts_Classic_Advertisers SET balance=balance + '$usercashback' WHERE username='$usernameCHECK'");
					}
				}
					$edit = mysql_query("UPDATE Web_Withdraws SET status='$paystatus',message='$messagea' WHERE id='$id'");
				}
				?>
					<div style="margin-top: -80px;" class="alert alert-success" role="alert">
						<strong>Well done!</strong> Withdraw edited
					</div>
				<?php

			}// WHEN FORM NOT SUBMITTED
///////////////////////////////////////////////////////////////////
			if(isset($_POST['deleterequest'])) {
				// GET AD ID FROM FORM
				$id = $_POST['requestvalue'];
				$query = "SELECT * FROM Web_Withdraws WHERE id='$id' LIMIT 1";
				$result = mysql_query($query) or die('Error, query failed');

				if(mysql_num_rows($result)<1){
   			}else{
     			while($row = mysql_fetch_array($result))
					{
						$usernameCHECK = $row['client_username'];
						$acctypeCHECK = $row['client_acctype'];
						$paymailCHECK = $row['client_paymail'];
						$amountCHECK = $row['amount'];
						$gatewayCHECK = $row['gateway'];
					}
				}
				$q1 = "SELECT `fee_withdraw` FROM `Web_Gateways` WHERE `id` = '$gatewayCHECK'";
				$r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
				$d1 = mysql_result($r1, 0);
				$usercashback = $d1 + $amountCHECK;
				if($acctypeCHECK == 'publisher') {
					$edit = mysql_query("UPDATE Accounts_Classic_Publishers SET balance=balance + '$usercashback' WHERE username='$usernameCHECK'");
				}
				if($acctypeCHECK == 'advertiser') {
					$edit = mysql_query("UPDATE Accounts_Classic_Advertisers SET balance=balance + '$usercashback' WHERE username='$usernameCHECK'");
				}
				$delete = mysql_query("DELETE FROM Web_Withdraws WHERE id='$id'");
				?>
					<div style="margin-top: -80px;" class="alert alert-success" role="alert">
						<strong>Well done!</strong> Withdraw deleted
					</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>List of paid withdrawals</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;'>Client Username</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Pay Mail</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Gateway</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Manager</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Amount</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Message</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a061; ?></th>
											</tr>
										</thead>
		<?php
			$query = "SELECT * FROM Web_Withdraws WHERE status='1' ORDER BY id DESC";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
   echo "
						<tr>
							<td>0 paid withdrawals</td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
						</tr>
   ";
   }else{
	$numberoflink = 0;
     while($row = mysql_fetch_array($result))
				{
					$numberoflink = $numberoflink + 1;
					if (empty($row['message'])) {
									$row['message'] = "---";
					};
					$gatewayid = $row['gateway'];
					$querygateway = "SELECT * FROM Web_Gateways WHERE id='$gatewayid'";
					$resultgateway = mysql_query($querygateway) or die('Error, query failed');
					$resultgateway = mysql_fetch_array($resultgateway);
					$gatewayname = $resultgateway['name'];
					?>
										<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['client_username']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['client_paymail']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $gatewayname; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['manager_username']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $acu['ISO']; ?> <?php echo $row['amount']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['message']; ?></h5>
											</td>
					<?php
					echo "				<form name='actions' action='' method='POST'>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<input type='hidden' name='requestvalue' value='{$row['id']}'>
												<input style ='float:right' value='$a090' class='btn btn-default' name='editrequest' type='submit'>
												<input style ='float:right' value='$a111' class='btn btn-default' name='deleterequest' type='submit'>
											</td>
											</form>
										</tr>";

				}

       }
	?>
									</table>
        </div>
        
    <div class="text-center">
      <h1><small>List of unpaid withdraws</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;'>Client Username</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Pay Mail</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Gateway</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Manager</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Amount</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Message</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a061; ?></th>
											</tr>
										</thead>
		<?php
			$query = "SELECT * FROM Web_Withdraws WHERE status='-2' ORDER BY id DESC";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
   echo "
						<tr>
							<td>0 unpaid withdrawals</td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
						</tr>
   ";
   }else{
	$numberoflink = 0;
     while($row = mysql_fetch_array($result))
				{
					$numberoflink = $numberoflink + 1;
					if (empty($row['message'])) {
									$row['message'] = "---";
					};
					
					$gatewayid = $row['gateway'];
					$querygateway = "SELECT * FROM Web_Gateways WHERE id='$gatewayid'";
					$resultgateway = mysql_query($querygateway) or die('Error, query failed');
					$resultgateway = mysql_fetch_array($resultgateway);
					$gatewayname = $resultgateway['name'];
					?>
										<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['client_username']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['client_paymail']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $gatewayname; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['manager_username']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $acu['ISO']; ?> <?php echo $row['amount']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['message']; ?></h5>
											</td>
					<?php
					echo "				<form name='actions' action='' method='POST'>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<input type='hidden' name='requestvalue' value='{$row['id']}'>
												<input style ='float:right' value='$a090' class='btn btn-default' name='editrequest' type='submit'>
												<input style ='float:right' value='$a111' class='btn btn-default' name='deleterequest' type='submit'>
											</td>
											</form>
										</tr>";

				}

       }
	?>
									</table>
        </div>
        
   <div class="text-center">
      <h1><small>List of canceled withdraws</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;'>Client Username</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Pay Mail</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Gateway</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Manager</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Amount</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Message</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'><?php echo $a061; ?></th>
											</tr>
										</thead>
		<?php
			$query = "SELECT * FROM Web_Withdraws WHERE status='0' ORDER BY id DESC";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
   echo "
						<tr>
							<td>0 canceled withdrawals</td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
						</tr>
   ";
   }else{
	$numberoflink = 0;
     while($row = mysql_fetch_array($result))
				{
					$numberoflink = $numberoflink + 1;
					if (empty($row['message'])) {
									$row['message'] = "---";
					};
					
					$gatewayid = $row['gateway'];
					$querygateway = "SELECT * FROM Web_Gateways WHERE id='$gatewayid'";
					$resultgateway = mysql_query($querygateway) or die('Error, query failed');
					$resultgateway = mysql_fetch_array($resultgateway);
					$gatewayname = $resultgateway['name'];
					?>
										<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['client_username']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['client_paymail']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $gatewayname; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['manager_username']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $acu['ISO']; ?> <?php echo $row['amount']; ?></h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5><?php echo $row['message']; ?></h5>
											</td>
					<?php
					echo "				<form name='actions' action='' method='POST'>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<input type='hidden' name='requestvalue' value='{$row['id']}'>
												<input style ='float:right' value='$a090' class='btn btn-default' name='editrequest' type='submit'>
												<input style ='float:right' value='$a111' class='btn btn-default' name='deleterequest' type='submit'>
											</td>
											</form>
										</tr>";

				}

       }
	?>
									</table>
        </div>
</div>

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