<?php

require __dir__ . '/init.php';

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	
	if( isset($_POST['action']) ) {
		
		switch( $_POST['action'] ):
		
			case 'activate':
			case 'deactivate':
					$status = array( 'status' => (int)($_POST['action'] == 'activate') );
					$SQL = sQuery::update( 'payment_methods', $status, "id = {$_POST['id']}" );
					$table_result = $link->query( $SQL );
					$table_msg = $table_result ? "Payment method successfully {$_POST['action']}d" : "Payment method not {$_POST['action']}d";
				break;
				
			case 'delete':
					$SQL = "DELETE FROM payment_methods WHERE id = {$_POST['id']}";
					$table_result = $link->query( $SQL );
					$table_msg = $table_result ? "Payment method deleted" : "Payment method not deleted";
				break;
		
		endswitch;
		
	} else {
		
		$image = help::process_image( $_FILES['net_icon'], 2048 );
		if( !empty($image) && $image['error'] != 4 ) {
			$name = md5($image['name']) . ".{$image['extension']}";
			$target = PANEL_DIR . '/images/payicon/' . $name;
			$saved = move_uploaded_file($image['tmp_name'], $target);
			if( $saved ) $_POST['net_icon'] = $name;
		};
		
		$_POST['net_abbr'] = strtoupper($_POST['net_abbr']);
		
		$SQL = sQuery::insert( 'payment_methods', $_POST );
		$result = $link->query( $SQL );
		$msg = $result ? "Payment Method successfully added" : "Payment Method not added";
		
	};
}

template::get_header(); 

?>  

	<?php template::title( 'Payment Methods' ); ?>
	
	<div class='row'>
		<div class='col-xl-4'>
			<div class='card'>
				<div class='card-body'>
					
					<?php template::alert( $msg, $result ?? null ); ?>
					
					<form method='post' enctype='multipart/form-data'>
						<div class='form-group'>
							<label>Network Abbreviation</label>
							<input type='text' class='form-control' name='net_abbr' placeholder='e.g BTC' required>
						</div>
						<div class='form-group'>
							<label>Network Name</label>
							<input type='text' class='form-control' name='net_name' placeholder='e.g Bitcoin' required>
						</div>
						<div class='form-group'>
							<label>Wallet Address</label>
							<input type='text' class='form-control' name='net_addr' placeholder='e.g 1WQBxHeDdoM0...' required>
						</div>
						<div class='form-group'>
							<label>Wallet Icon</label>
							<input type='file' class='form-control' name='net_icon'>
						</div>
						<hr>
						<div class=''>
							<button type='submit' class='btn btn-primary'>
								Add New
							</button>
						</div>
					</form>
					
				</div>
			</div>
		</div>
		<div class='col-xl-8'>
			<div class='card'>
				<div class='card-body'>
					
					<?php 
						$SQL = sQuery::select( 'payment_methods', "1 ORDER BY id DESC" );
						$result = $link->query( $SQL );
						if( !$result->num_rows ):
					?>
						
						<div class='text-center'>
						
							<img src='<?php echo help::url( USERS_DIR . '/img/payment-method.jpg' ); ?>' width='250px' class='img-fluid'>
							
							<h4>No Payment Method Added</h4>
						
						</div>
						
					<?php else: ?>
					
					<?php template::alert( $table_msg ?? null, $table_result ?? null ); ?>
					
					<div class='table-responsive'>
						<table class='table' data-table='simple'>
							<thead>
								<tr>
									<th>Abbr</th>
									<th>Name</th>
									<th>Address</th>
									<th>Icon</th>
									<th>Status</th>
									<th></th>
								</tr>
							</thead>
							<tbody>
							<?php 
								while( $method = $result->fetch_assoc() ): 
							?>
								<tr>
									<td><?php echo $method['net_abbr']; ?></td>
									<td><?php echo $method['net_name']; ?></td>
									<td><?php echo $method['net_addr']; ?></td>
									<td>
										<img src='<?php echo help::url( PANEL_DIR . '/images/payicon/' . $method['net_icon'] ); ?>' height='36px'>
									</td>
									<td><?php echo help::stdCheck($method['status'], null, null); ?></td>
									<td>
										<form method='post' onsubmit='return confirm("Click OK to confirm")'>
											<input type='hidden' name='id' value='<?php echo $method['id']; ?>'>
											<div class='btn-group'>
												<?php if( !empty($method['status']) ): ?>
													<button class='btn btn-warning' title='deactivate' name='action' value='deactivate'>
														<i class='fas fa-ban'></i>
													</button>
												<?php else: ?>
													<button class='btn btn-success' title='activate' name='action' value='activate'>
														<i class='fas fa-check'></i>
													</button>
												<?php endif; ?>
												<button class='btn btn-danger' title='delete' name='action' value='delete'>
													<i class='fas fa-trash'></i>
												</button>
											</div>
										</form>
									</td>
								</tr>
							<?php endwhile; ?>
							</tbody>
						</table>
					</div>
						
					<?php endif; ?>
					
				</div>
			</div>
		</div>
	</div>
	
<?php template::get_footer(); ?>