<?php
/**
 * Claim reward form view.
 *
 * @package Awesomemotive\Rewardswp\Views\Panel\Friend
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Panel\Friend;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use Awesomemotive\Rewardswp\Services\AntiSpamService;


/**
 * Claim Reward Form View.
 *
 * @since 1.0.0
 */
class ClaimRewardFormView extends AbstractView implements ViewInterface {
	/**
	 * Anti-spam service instance.
	 *
	 * @since 1.2.0
	 *
	 * @var AntiSpamService
	 */
	private AntiSpamService $anti_spam_service;

	/**
	 * Constructor.
	 *
	 * @since 1.2.0
	 *
	 * @param AntiSpamService $anti_spam_service Anti-spam service instance.
	 */
	public function __construct( AntiSpamService $anti_spam_service ) {
		$this->anti_spam_service = $anti_spam_service;
	}
	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args {
	 *     @type string $form_nonce    The nonce for security validation. Default ''.
	 *     @type string $referral_code The referral code used by the advocate. Default ''.
	 * }
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		$args = wp_parse_args(
			$args,
			[
				'form_nonce'    => '',
				'referral_code' => '',
				'email'         => '',
				'error_message' => '',
			]
		);
		?>

		<p class="mb-4 text-gray-600"><?php esc_html_e( 'Add your email below to claim your gift. You will get a coupon code to use at checkout.', 'rewardswp' ); ?></p>
		<form id="rewardswp-friend-form" class="space-y-4">
			<input type="hidden" name="nonce" value="<?php echo esc_attr( $args['form_nonce'] ); ?>">
			<input type="hidden" name="referral_code" value="<?php echo esc_attr( $args['referral_code'] ); ?>">

			<?php $this->anti_spam_service->render_field(); ?>

			<div>
				<label for="friend-email" class="block text-sm font-medium leading-6 text-gray-900 mb-1"><?php esc_html_e( 'Email', 'rewardswp' ); ?></label>
				<input type="email" value="<?php echo esc_attr( $args['email'] ); ?>" id="friend-email" name="email" required class="block w-full rounded-md px-3 py-2 text-base sm:text-sm/6 focus:-outline-offset-2 outline-1 -outline-offset-1 text-gray-900 outline-gray-300 focus:outline-2 placeholder:text-gray-400 focus:outline-blue-500">
			</div>

			<?php if ( ! empty( $args['error_message'] ) ) : ?>

			<div class="text-sm bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert">
				<p><?php echo esc_html( $args['error_message'] ); ?></p>
			</div>

			<?php endif; ?>

			<button type="submit" class="w-full px-4 py-3 mb-6 text-white rounded-md bg-brand hover:bg-brand-hover focus:outline-none focus:ring-2 focus:ring-brand-hover focus:ring-opacity-50 font-bold cursor-pointer">
				<?php esc_html_e( 'Claim your reward', 'rewardswp' ); ?>
			</button>
		</form>

		<?php
	}
}
