<?php
if ( ! defined( 'ABSPATH' ) ) exit;
$bingocr_s = BingoCR_DB::get_settings();
?>
<div class="wrap bingocr-admin-wrap">
	<h1><?php esc_html_e( 'Payment', 'bingocar' ); ?></h1>

	<div id="bingocr-payment-saved" class="notice notice-success" style="display:none;">
		<p><?php esc_html_e( 'Payment settings saved.', 'bingocar' ); ?></p>
	</div>

	<form id="bingocr-payment-form" novalidate>

		<h2><?php esc_html_e( 'Accepted Payment Methods', 'bingocar' ); ?></h2>
		<p class="description" style="margin-bottom:10px">
			<?php esc_html_e( 'Choose which payment options customers can pick during booking. These are pay-on-site methods — no money is collected online.', 'bingocar' ); ?>
		</p>
		<table class="form-table">
			<tr>
				<th><?php esc_html_e( 'Methods', 'bingocar' ); ?></th>
				<td>
					<label style="display:block;margin-bottom:6px">
						<input type="checkbox" name="payment_cash" value="1" <?php checked( ! empty( $bingocr_s['payment_cash'] ) ); ?>>
						<?php esc_html_e( 'Cash on pickup', 'bingocar' ); ?>
					</label>
					<label style="display:block;margin-bottom:6px">
						<input type="checkbox" name="payment_card" value="1" <?php checked( ! empty( $bingocr_s['payment_card'] ) ); ?>>
						<?php esc_html_e( 'Card on pickup', 'bingocar' ); ?>
					</label>
					<label style="display:block">
						<input type="checkbox" name="payment_bank" value="1" <?php checked( ! empty( $bingocr_s['payment_bank'] ) ); ?>>
						<?php esc_html_e( 'Bank transfer', 'bingocar' ); ?>
					</label>
				</td>
			</tr>
			<tr>
				<th><label for="s-payment-instructions"><?php esc_html_e( 'Payment Instructions', 'bingocar' ); ?></label></th>
				<td>
					<textarea id="s-payment-instructions" name="payment_instructions" rows="4" class="large-text"><?php echo esc_textarea( $bingocr_s['payment_instructions'] ?? '' ); ?></textarea>
					<p class="description"><?php esc_html_e( 'Shown to the customer at checkout and in the confirmation email (e.g. your bank account details for transfers). Basic HTML allowed.', 'bingocar' ); ?></p>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Online Payment — PayPal', 'bingocar' ); ?></h2>
		<?php if ( BingoCR_License::can( 'online_payments' ) ) : ?>
		<p class="description" style="margin-bottom:10px">
			<?php esc_html_e( 'Let customers pay online with PayPal. Enter your PayPal app credentials below. Live payment capture is being finalized — your settings are saved and ready.', 'bingocar' ); ?>
		</p>
		<table class="form-table">
			<tr>
				<th><?php esc_html_e( 'Enable PayPal', 'bingocar' ); ?></th>
				<td>
					<label>
						<input type="checkbox" name="paypal_enabled" value="1" <?php checked( ! empty( $bingocr_s['paypal_enabled'] ) ); ?>>
						<?php esc_html_e( 'Accept online PayPal payments', 'bingocar' ); ?>
					</label>
				</td>
			</tr>
			<tr>
				<th><label for="s-paypal-mode"><?php esc_html_e( 'Mode', 'bingocar' ); ?></label></th>
				<td>
					<select id="s-paypal-mode" name="paypal_mode">
						<option value="sandbox" <?php selected( ( $bingocr_s['paypal_mode'] ?? 'sandbox' ), 'sandbox' ); ?>><?php esc_html_e( 'Sandbox (testing)', 'bingocar' ); ?></option>
						<option value="live" <?php selected( ( $bingocr_s['paypal_mode'] ?? 'sandbox' ), 'live' ); ?>><?php esc_html_e( 'Live', 'bingocar' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<th><label for="s-paypal-client-id"><?php esc_html_e( 'Client ID', 'bingocar' ); ?></label></th>
				<td><input type="text" id="s-paypal-client-id" name="paypal_client_id" value="<?php echo esc_attr( $bingocr_s['paypal_client_id'] ?? '' ); ?>" class="regular-text" autocomplete="off"></td>
			</tr>
			<tr>
				<th><label for="s-paypal-client-secret"><?php esc_html_e( 'Client Secret', 'bingocar' ); ?></label></th>
				<td>
					<?php $bingocr_secret_set = ! empty( $bingocr_s['paypal_client_secret'] ); ?>
					<input type="password" id="s-paypal-client-secret" name="paypal_client_secret" value="" class="regular-text" autocomplete="new-password" placeholder="<?php echo $bingocr_secret_set ? '••••••••••••' : ''; ?>">
					<p class="description">
						<?php esc_html_e( 'From your PayPal Developer app. Stored encrypted.', 'bingocar' ); ?>
						<?php if ( $bingocr_secret_set ) : ?>
							<strong><?php esc_html_e( 'A secret is saved — leave blank to keep it.', 'bingocar' ); ?></strong>
						<?php endif; ?>
					</p>
				</td>
			</tr>
			<tr>
				<th><label for="s-paypal-webhook-id"><?php esc_html_e( 'Webhook ID', 'bingocar' ); ?></label></th>
				<td>
					<input type="text" id="s-paypal-webhook-id" name="paypal_webhook_id" value="<?php echo esc_attr( $bingocr_s['paypal_webhook_id'] ?? '' ); ?>" class="regular-text" autocomplete="off">
					<p class="description">
						<?php esc_html_e( 'Optional but recommended. In your PayPal app, add a webhook for the URL below, subscribe to the event "Payment capture completed", then paste the generated Webhook ID here. This lets PayPal confirm payments even if the customer\'s browser drops the response, so bookings never get stuck unpaid.', 'bingocar' ); ?>
					</p>
					<p class="description">
						<strong><?php esc_html_e( 'Webhook URL:', 'bingocar' ); ?></strong>
						<code><?php echo esc_url( rest_url( 'bingocr/v1/payments/paypal/webhook' ) ); ?></code>
					</p>
				</td>
			</tr>
		</table>
		<?php else : ?>
		<?php
		$bingocr_upsell_title = __( 'Online Payments — Pro Feature', 'bingocar' );
		$bingocr_upsell_desc  = __( 'Accept online payments via PayPal directly in the booking widget. Available in BingoCR Pro.', 'bingocar' );
		include __DIR__ . '/partial-pro-upsell.php';
		?>
		<?php endif; ?>

		<p class="submit">
			<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Payment Settings', 'bingocar' ); ?></button>
			<span class="bingocr-form-message"></span>
		</p>

	</form>
</div>
