<?php

require __dir__ . '/init.php';

require _SDIR . '/for-digital-deposit.php';

template::get_header();

$pay_methods = $link->query( sQuery::select( "payment_methods", "status = 1" ) );

$wallet = ($link->query( sQuery::select( "payment_methods", "id = " . ($_GET['pm'] ?? 0) ) ))->fetch_assoc();

?>

	<?php template::title( "Digital Deposit" ); ?>
		
	<div class='row'>
	
		<div class='col-lg-5 mb-3'>
			<div class='card'>
				<div class='card-body'>
				
					<form method='get'>
					
						<div class='alert alert-info'>
							Please select a deposit method
						</div>
						
						<div class='form-group'>
							<select name='pm' class='form-control'>
							<?php
								while( $method = $pay_methods->fetch_assoc() ):
									$selected = $method['id'] == ($_GET['pm'] ?? 0) ? "selected" : null;
							?>
								<option value='<?php echo $method['id']; ?>' <?php echo $selected; ?>>
									<?php echo $method['net_name']; ?>
								</option>
							<?php endwhile; ?>
							</select>
						</div>
						
						<div class='form-group'>
							<button class='btn btn-primary btn-block'>
								Continue
							</button>
						</div>
						
					</form>
					
				</div>
			</div>
		</div>
	
		<div class="col-lg-7 col-sm-12 col-sx-12">

			<div class="card">
				<div class="card-body">
					
					<?php if( !$wallet ): ?>
						
						<div class='text-center'>
							<img src='img/payment-method.jpg' width='300px' class='img-fluid'>
							<h3>Waiting...</h3>
							<h5 class='text-muted'>Please select deposit method</h5>
						</div>
						
					<?php else: ?>
						
						<?php if( !empty($wallet['net_icon']) ): ?>
							<div class='text-center mb-3'>
								<img src='<?php echo help::url( ADMIN_DIR . "/images/payicon/{$wallet['net_icon']}" ); ?>' width="40px" class='img-fluid'>
							</div>
						<?php endif; ?>
						
						<h4 class='text-center text-capitalize'>
							<i class="fa fa-refresh"></i> <?php echo $wallet['net_name']; ?> Payment
						</h4>
							
						<div class='text-center'>
							<img src="<?php echo help::QRCodeURL( $wallet['net_addr'] ); ?>"/>
						</div>
						
						<hr></hr>

						<h5 class='text-center'>
							Make payment to the below <?php echo ucwords($wallet['net_name']); ?> wallet
						</h5>
						 
						<div class='mb-3'>
							<input type="text" class="form-control mb-1" value="<?php echo $wallet['net_addr']; ?>" id="myInput" readonly>
							<button data-copy='#myInput' class="btn btn-info">
								Copy <?php echo ucwords($wallet['net_abbr']); ?> Address
							</button>
						</div>
						
						<div class="box-header with-border">
						
							<?php template::alert( $msg );  ?>

							<form class="form-horizontal" method="POST" >
								
								<input type='hidden' name='network' value='<?php echo $wallet['net_abbr']; ?>'>
								
								<div class="form-group">
									<input type="number" step='0.01' id="usd" name="usd" placeholder="Amount in USD" class="form-control mb-1">
									<div class='input-group'>
										<input type="double" id="rate" name="rate" placeholder="0.00000000" readonly="true" class="form-control">
										<div class='input-group-append'>
											<span class='input-group-text'>
												<?php echo $wallet['net_abbr']; ?>
											</span>
										</div>
									</div>
								</div>

								<div class="form-group">
									<input type="text"  name="btctnx" placeholder="Paste the transferred <?php echo $wallet['net_abbr']; ?> transaction ID " class="form-control">
								</div>
								
								<button style="" type="submit" class="btn btn-warning" name="depo" >Deposit</button>

							</form>

						</div>
					
					<?php endif; ?>
					
				</div>
			</div>
		</div>

	</div>
	
<?php 
	events::addListener("@footer", function() use($wallet) { 
		if( !$wallet ) return;
		$rate = help::price_rate( $wallet['net_abbr'] );
		if( !$rate ) $rate = 1;
?>
	<script>
		$("#usd").on('input', function() {
			var btc2usd = <?php echo $rate; ?>;
			var output = Number(this.value) / btc2usd;
			$("#rate").val( output );
		});
	</script>
<?php }); ?>
	
<?php template::get_footer(); ?> 