<?php
/**
 * AltaPay module for WooCommerce
 *
 * Copyright © 2020 AltaPay. All rights reserved.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
?>

<form method="post" action="options.php">
	@php
		settings_fields('altapay-settings-group');
		do_settings_sections('altapay-settings-group');
	@endphp
	<table class="form-table">
		<tr>
			<th scope="row"><?php esc_html_e( 'Gateway URL', 'altapay' ); ?></th>
			<td><input class="input-text regular-input" type="text" placeholder="{{__('Enter gateway url','altapay')}}" name="altapay_gateway_url"
					   value="{{$gatewayURL}}" required />
			   <i><p style="font-size: 10px;">{{__('e.g. https://testgateway.altapaysecure.com', 'altapay')}}</p></i>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'API username', 'altapay' ); ?></th>
			<td><input class="input-text regular-input" type="text" placeholder="{{__('Enter API username','altapay')}}" name="altapay_username"
					   value="{{$username}}" required />
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'API password', 'altapay' ); ?></th>
			<td><input class="input-text regular-input" type="password" placeholder="{{__('Enter API password','altapay')}}" name="altapay_password"
					   value="{{$password}}" required />
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Payment page', 'altapay' ); ?></th>
			<td>
				@php // Validate if payment page exists by looping through the pages
					$pages = get_pages();
					foreach ($pages as $page) {
						if ($page->post_name === 'altapay-payment-form') {
							$exists = true;
							$pageTitle = $page->post_title;
							$pageID = $page->ID;
							update_option('altapay_payment_page', $pageID);
						}
					}
				@endphp
				@if (!$exists)
					<input type="button" id="create_altapay_payment_page" style="color:white; background-color:#006064;" name="create_page"
						   value="Create Page" class="button button-primary btn-lg"/>
					<i><p style="font-size: 10px;" id="payment-page-msg">{{__('Payment page does not exist, create a new one', 'altapay')}}</p></i>
					<span id="payment-page-msg"></span>
					<input type="hidden" name="altapay_payment_page"  id="altapay_payment_page" value="">
				@else
					<input type="hidden" name="altapay_payment_page"
					 value="{{$paymentPage}}">{{$pageID}}: {{$pageTitle}}
				@endif

			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Checkout form style', 'altapay' ); ?></th>
			<td>
				<select name="altapay_cc_form_styling">
					<option @if($cc_form_styling == 'legacy') selected @endif value="legacy">Legacy</option>
					<option @if($cc_form_styling == 'checkout') selected @endif value="checkout">Checkout</option>
					<option @if($cc_form_styling == 'checkout_v2') selected @endif value="checkout_v2">Checkout v2</option>
					<option @if($cc_form_styling == 'custom') selected @endif value="custom">Custom</option>
				</select>
			</td>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Payment page layout', 'altapay' ); ?></th>
			<td>
				<select name="altapay_payment_page_layout">
					<option @if($payment_page_layout == 'default_layout') selected @endif value="default_layout">Default</option>
					<option @if($payment_page_layout == 'checkout_independent') selected @endif value="checkout_independent">Checkout Independent</option>
				</select>
				<i><p style="font-size: 10px;">The default option follows the theme styling, <br /> while the custom option
						displays the payment page independently of the theme styling.</p></i>
			</td>
		</tr>

		<tr>
			<th scope="row" colspan="2">
				<h2 style="background: #006064; color:white; line-height: 30px; padding-left: 1%;"><?php esc_html_e( 'Fraud detection service', 'altapay' ); ?></h2>
			</th>
		</tr>

		<tr>
			<th scope="row"><?php esc_html_e( 'Fraud detection', 'altapay' ); ?></th>
			<td>
				<select name="altapay_fraud_detection">
					<option value="0">Disabled</option>
					<option @if($altapay_fraud_detection) selected @endif value="1">Enabled</option>
				</select>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Release/Refund on Fraud detection', 'altapay' ); ?></th>
			<td>
				<select name="altapay_fraud_detection_action">
					<option value="0">No</option>
					<option @if($altapay_fraud_detection_action) selected @endif value="1">Yes</option>
				</select>
			</td>
		</tr>

		@if ($terminals)
			<tr>
				<th scope="row" colspan="2">
					<h2 style="background: #006064; color:white; line-height: 30px; padding-left: 1%;"><?php esc_html_e( 'Terminals', 'altapay' ); ?></h2>
				</th>
			</tr>
			@foreach ($terminals as $terminal)
				<tr>
					<th scope="row">{{$terminal->name}}</th>
					<td><input type="checkbox" name="altapay_terminals_enabled[]"
							   value="{{$terminal->key}}"
							   @if (in_array($terminal->key, $enabledTerminals)) checked="checked"/> @endif
					</td>
				</tr>
			@endforeach
			<tr>
				<td>
					<a href="admin.php?page=wc-settings&amp;tab=checkout">
						<?php
						esc_html_e(
							'Go to WooCommerce payment methods',
							'altapay'
						);
						?>
					</a>
				</td>
			</tr>
		@endif

	</table>
	<input type="submit" class="button" style="color:white; background-color:#006064;" value="Save changes"/>
</form>
