<?php
/**
 * Input component view.
 *
 * @package Awesomemotive\Rewardswp\Views\Admin\Components
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Admin\Components;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use Awesomemotive\Rewardswp\Interfaces\ViewInterface;
use function Awesomemotive\Rewardswp\Helpers\tag_attr;

class InputComponentView extends AbstractView implements ViewInterface {
	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		$args = wp_parse_args(
			$args,
			[
				'section'      => '',
				'name'         => '',
				'id'           => '',
				'x-bind:id'    => '',
				'value'        => '',
				'x-bind:value' => '',
				'x-on:input'   => '',
				'x-on:focus'   => '',
				'x-model'      => '',
				'placeholder'  => '',
				'type'         => 'text',
				'required'     => false,
			]
		);

		if ( empty( $args['section'] ) || empty( $args['name'] ) ) {
			esc_html_e( 'Failed to render the input component, section and name arguments are required.', 'rewardswp' );
			return;
		}

		$input_name = sprintf( 'rewardswp_settings[%s][%s]', $args['section'], $args['name'] );

		$value_input_name = empty( $args['x-bind:value'] )
			? 'value'
			: 'x-bind:value';
		$value_input_val  = empty( $args['x-bind:value'] )
			? $args['value']
			: $args['x-bind:value'];

		$id_input_name = empty( $args['x-bind:id'] )
			? 'id'
			: 'x-bind:id';
		$id_input_val  = empty( $args['x-bind:id'] )
			? $args['id']
			: $args['id'];

		?>
		<div
			class="flex items-center rounded-md bg-white px-3 outline-1 -outline-offset-1 outline-gray-300 focus-within:outline-2 focus-within:-outline-offset-2 focus-within:outline-blue-600"
		>
			<input
				<?php tag_attr( 'x-model', $args['x-model'] ); ?>
				<?php tag_attr( $value_input_name, $value_input_val ); ?>
				<?php tag_attr( 'x-on:input', $args['x-on:input'] ); ?>
				<?php tag_attr( 'x-on:focus', $args['x-on:focus'] ); ?>
				<?php tag_attr( $id_input_name, $id_input_val ); ?>
				type="<?php echo esc_attr( $args['type'] ); ?>"
				name="<?php echo esc_attr( $input_name ); ?>"
				placeholder="<?php echo esc_attr( $args['placeholder'] ); ?>"
				<?php if ( $args['required'] ) : ?>
					required
				<?php endif; ?>
				class="block min-w-0 grow py-2 pr-3 pl-1 text-base text-gray-900 placeholder:text-gray-400 focus:outline-none sm:text-sm/6"
			>
		</div>
		<?php
	}
}
