<?php
// INCLUDE HEADER IN PAGE
include('other_header.php');
// GET CURRENT DATE AND TIME
$date = date('Y-m-d H:i:s');
$accounttypenow = $_SESSION['acctype'];
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($_POST['success'] == 'paymentqueted') {
	?>
	<div style="margin-top: -80px;" class="alert alert-success" role="alert">
		<strong>Well done!</strong> Payment inserted into queue.
	</div>
	<?php
	}
}

if(isset($_POST['error'])) {
	if($_POST['error'] == 'minimum') {
	?>
	<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
		<strong>Sorry!</strong> You must have atlease same balance value as minimum withdraw amount <?php echo $sa['mincashout']; ?>
	</div>
	<?php
	}
	if($_POST['error'] == 'paymailnotset') {
	?>
	<div style="margin-top: -80px;" class="alert alert-danger" role="alert">
		<strong>Sorry!</strong> Please set your payment email at Account settings page.
	</div>
	<?php
	}
}
													if($accounttypenow == 0){
														$currentbalance = $seeP['balance'];
													}
													if($accounttypenow == 1) {
														$currentbalance = $seeA['balance'];
													}
													// PAYMENT PROCCESS
													if($currentbalance >= $sa['mincashout']) {
													$pmethod = mysql_real_escape_string($_POST['gateway']);													
													$query = "SELECT * FROM Web_Gateways WHERE id = '$pmethod'";
													$result = mysql_query($query) or die('Error, query failed');
			
													if(mysql_num_rows($result)>0)
   												{
     													while($row = mysql_fetch_array($result))
														{
															$phpname = $row['phpname'];
															$fee_withdraw = $row['fee_withdraw'];
															if($accounttypenow == 0){
																$payeem = $seeP[$phpname.'email'];
																if (empty($payeem)){
																	$payeem = '0';
																}
															}
															if($accounttypenow == 1) {
																$payeem = $seeA[$phpname.'email'];
																if (empty($payeem)){
																	$payeem = '0';
																}
															}
														}
													}
													
													$rqc = mysql_real_escape_string($_POST['rqc']);
													if($rqc) {
														if ($payeem != '0'){
														// 2TH FACTOR VALIDATION >> IF USER HAVE MORE THAN BALANCE ON ACCOUNT
														if($accounttypenow == 0){//Publisher
																if($currentbalance >= $sa['mincashout']) {
															$willbepayed = $currentbalance - $fee_withdraw;
															// INSERT PAYOUT REQUEST TO DB
															$query = "SELECT * FROM Accounts_Premium_Payments ORDER BY rand() LIMIT 6";
															$result = mysql_query($query) or die('Error, query failed');
			
															if(mysql_num_rows($result)<1)
															{
   															echo '0 PAYMENT MANAGERS IN DATABASE';
   														}
   														else
   														{
     															while($row = mysql_fetch_array($result))
																{
																	$assignedsupporter = $row['username'];
																}
															}
															if($accounttypenow == 0){//Publisher
																$accounttypenowname = 'publisher';
															}
															if($accounttypenow == 1){//Advertiser
																$accounttypenowname = 'advertiser';
															}
															$addpayment = mysql_query("INSERT INTO Web_Withdraws VALUES('','$session_username','$accounttypenowname','$payeem','$assignedsupporter','$willbepayed','$pmethod','$date','-2','','')");
															$qstats = mysql_query("UPDATE Web_Stats SET tcash=tcash + '$currentbalance' WHERE id='1'");
															$qstatss = mysql_query("UPDATE Web_Stats SET membcash7=membcash7 + '$currentbalance' WHERE id='1'");
															if($accounttypenow == 0){//Publisher
																$delamount = mysql_query("UPDATE Accounts_Classic_Publishers SET balance='0.00' WHERE username='$session_username'");
															}
															if($accounttypenow == 1){//Advertiser
																$delamount = mysql_query("UPDATE Accounts_Classic_Advertisers SET balance='0.00' WHERE username='$session_username'");
															}
															?>
																<form action='p.Withdraw' method='post' name='frm'>
																	<input type='hidden' name='success' value='paymentqueted'>
																</form>
																<script language="JavaScript">
																	document.frm.submit();
																</script>
															<?php
														}else{// ERROR COULDN'T INSERT TO DB - USER DOTN'T HAVE MORE THAN MINUMUM CASHOUT ON ACCOUNT
															?>
																<form action='p.Withdraw' method='post' name='frm'>
																	<input type='hidden' name='error' value='minimum'>
																</form>
																<script language="JavaScript">
																	document.frm.submit();
																</script>
															<?php
														}
														}
														if($accounttypenow == 1){//Advertiser
														if($currentbalance >= $sa['mincashout']) {
															$willbepayed = $currentbalance - $fee_withdraw;
															// INSERT PAYOUT REQUEST TO DB
															$query = "SELECT * FROM Accounts_Premium_Payments ORDER BY rand() LIMIT 6";
															$result = mysql_query($query) or die('Error, query failed');
			
															if(mysql_num_rows($result)<1)
															{
   															echo '0 PAYMENT MANAGERS IN DATABASE';
   														}
   														else
   														{
     															while($row = mysql_fetch_array($result))
																{
																	$assignedsupporter = $row['username'];
																}
															}
															if($accounttypenow == 0){//Publisher
																$accounttypenowname = 'publisher';
															}
															if($accounttypenow == 1){//Advertiser
																$accounttypenowname = 'advertiser';
															}
															$addpayment = mysql_query("INSERT INTO Web_Withdraws VALUES('','$session_username','$accounttypenowname','$payeem','$assignedsupporter','$willbepayed','$pmethod','$date','-2','','')");
															$qstats = mysql_query("UPDATE Web_Stats SET tcash=tcash + '$currentbalance' WHERE id='1'");
															$qstatss = mysql_query("UPDATE Web_Stats SET membcash7=membcash7 + '$currentbalance' WHERE id='1'");
															if($accounttypenow == 0){//Publisher
																$delamount = mysql_query("UPDATE Accounts_Classic_Publishers SET balance='0.00' WHERE username='$session_username'");
															}
															if($accounttypenow == 1){//Advertiser
																$delamount = mysql_query("UPDATE Accounts_Classic_Advertisers SET balance='0.00' WHERE username='$session_username'");
															}
															?>
																<form action='p.Withdraw' method='post' name='frm'>
																	<input type='hidden' name='success' value='paymentqueted'>
																</form>
																<script language="JavaScript">
																	document.frm.submit();
																</script>
															<?php
														}else{// ERROR COULDN'T INSERT TO DB - USER DOTN'T HAVE MORE THAN MINUMUM CASHOUT ON ACCOUNT
															?>
																<form action='p.Withdraw' method='post' name='frm'>
																	<input type='hidden' name='error' value='minimum'>
																</form>
																<script language="JavaScript">
																	document.frm.submit();
																</script>
															<?php
														}
														}
														
														
														}else{
															?>
																<form action='p.Withdraw' method='post' name='frm'>
																	<input type='hidden' name='error' value='paymailnotset'>
																</form>
																<script language="JavaScript">
																	document.frm.submit();
																</script>
															<?php
														}
													}
												}
?>
  <div class="container">
    <div class="text-center">
      <h1><?php echo $title; ?>
      	<small>/
      			<?php
						if($accounttypenow == 0){//Publisher
					?>
							Active is: Publisher Account
					<?php
						}
						if($accounttypenow == 1){//Advertiser
					?>
							Active is: Advertiser Account
					<?php
						}
					?>
			</small>
		</h1>
 	 </div>
		</br>
	<div class="text-center">
      <h1><small>Publisher's account</small></h1>
 	</div>
    <?php      
        if ( isset( $_POST[ 'depositsub' ] ) )
{
    try
    {
        // Run CSRF check, on POST data, in exception mode, for 10 minutes, in one-time mode.
        NoCSRF::check( 'csrf_token', $_POST, true, 60*10, false );
        // form parsing, DB inserts, etc.
        // ...
        $result = 'CSRF check passed. Form parsed.';
    }
    catch ( Exception $e )
    {
        // CSRF attack detected
        echo"<script type='text/javascript'>javascript: window.history.go(-1);</script>";
    }
}
else
{
    $result = 'No post data yet.';
}
// Generate CSRF token to use in form hidden field
$token = NoCSRF::generate( 'csrf_token' );
?>
					<?php
						if($accounttypenow == 0){//Publisher
							if($sa['mincashout'] <= $seeP['balance']) {
							?>
											<div class="col-md-6 col-md-offset-3">
												<div class="panel panel-default">
													<div class="panel-heading">Withdraw</div>
													<div class="panel-body">
														<form action="" method="POST">
															<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
																<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
																	<center>
																		<?php 
																		$query = "SELECT * FROM Web_Gateways WHERE active_withdraw = '1'";
																		$result = mysql_query($query) or die('Error, query failed');
			
																		if(mysql_num_rows($result)>0)
   																	{
     																		while($row = mysql_fetch_array($result))
																			{
																			?>
																				<input id="<?php echo $row['phpname']; ?>" class="toggle toggle-left" name="gateway" value="<?php echo $row['id']; ?>" type="radio" checked><label style="width:auto" class="btn btn-default" for="<?php echo $row['phpname']; ?>" class="btn"><?php echo $row['name']; ?> - Fee: <?php echo $row['fee_withdraw']; ?></label>
																			<?php
																			}
																		}
																		?>
																	</center>				
																</div>
															<input class="btn btn-primary" type="submit" name="rqc" value='Withdraw'>
														</form>
													</div>
												</div>
											</div>
							<?php
							}else {
							?>
											<div class="col-md-6 col-md-offset-3">
												<div class="panel panel-default">
													<div class="panel-heading">Withdraw</div>
													<div class="panel-body">
														<div class="form-group has-error">
															<h1><b><span style="font-size: 20px;" class="help-block"><?php echo $a258; ?> <?php echo $sa['mincashout'];?> <?php echo $acu['ISO']; ?></span></b></h1>
														</div>
													</div>
												</div>
											</div>
							<?php
							}						
						}
						if($accounttypenow == 1){//Advertiser
							if($sa['mincashout'] <= $seeA['balance']) {
							?>
											<div class="col-md-6 col-md-offset-3">
												<div class="panel panel-default">
													<div class="panel-heading">Withdraw</div>
													<div class="panel-body">
														<form action="" method="POST">
															<input type="hidden" name="csrf_token" value="<?php echo $token; ?>">
																<div data-original-title="Enter your long URL" data-toggle="tooltip" data-placement="top" title="555" class="input-group">
																	<center>
																		<?php 
																		$query = "SELECT * FROM Web_Gateways WHERE active_withdraw = '1'";
																		$result = mysql_query($query) or die('Error, query failed');
			
																		if(mysql_num_rows($result)>0)
   																	{
     																		while($row = mysql_fetch_array($result))
																			{
																			?>
																				<input id="<?php echo $row['phpname']; ?>" class="toggle toggle-left" name="gateway" value="<?php echo $row['id']; ?>" type="radio" checked><label style="width:auto" class="btn btn-default" for="<?php echo $row['phpname']; ?>" class="btn"><?php echo $row['name']; ?> - Fee: <?php echo $row['fee_withdraw']; ?></label>
																			<?php
																			}
																		}
																		?>
																	</center>				
																</div>
															<input class="btn btn-primary" type="submit" name="rqc" value='Withdraw'>
														</form>
													</div>
												</div>
											</div>
							<?php
							}else {
							?>
											<div class="col-md-6 col-md-offset-3">
												<div class="panel panel-default">
													<div class="panel-heading">Withdraw</div>
													<div class="panel-body">
														<div class="form-group has-error">
															<h1><b><span style="font-size: 20px;" class="help-block"><?php echo $a258; ?> <?php echo $sa['mincashout'];?> <?php echo $acu['ISO']; ?></span></b></h1>
														</div>
													</div>
												</div>
											</div>
							<?php
							}
						}
					?>
	<div class="col-sm-12">
		<div class="text-center">
      <h1><small>Your withdrawals</small></h1>
 		</div>
          <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;'>ID</th>
												<th style='max-width:140px;color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Payment Method</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Payment Email</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Payment 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;'>Date of request</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Status</th>
												<th style='color: #DF691A; background-color: #2B3E50;border-collapse: collapse;border: 1px solid #DF691A;'>Message</th>
											</tr>
										</thead>
											<?php
			$query = " SELECT * FROM Web_Withdraws WHERE client_username='$session_username'";
			$result = mysql_query($query) or die('Error, query failed');

			// print the random numbers
			
			if(mysql_num_rows($result)<1){
   echo "
						<tr>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>You don't have any withdrawals</td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'></td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'></td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'></td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'></td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'></td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'></td>
							<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'></td>
						</tr>
   ";
   }else{
	$numberoflink = 0;
     while($row = mysql_fetch_array($result))
				{
					$numberoflink = $numberoflink + 1;
					$payment = $row['gateway'];
					$q1 = "SELECT `name` FROM `Web_Gateways` WHERE `id` = '$payment'";
					$r1 = mysql_query($q1) or die($q1."<br/><br/>".mysql_error());
					$paymentmethod = mysql_result($r1, 0);
					$statusnumber = $row['status'];
					if($statusnumber == -2) {
						$status = 'Waiting approval';
					}elseif($statusnumber == 0) {
						$status = 'Rejected';
					}elseif($statusnumber == 1) {
						$status = 'Paid';
					}
					echo "
										<tr>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>$numberoflink</h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>$paymentmethod</h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>{$row['client_paymail']}</h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>{$row['manager_username']}</h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>{$row['amount']}</h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>{$row['date']}</h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>$status</h5>
											</td>
											<td style='color: #FFF; background-color: #DF691A;border-collapse: collapse;border: 1px solid #2B3E50;'>
												<h5>{$row['message']}</h5>
											</td>
										</tr>";

				}

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

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