<?php
namespace Elementor_Pay_Addons\Form\Fields;

use Elementor_Pay_Addons\Form\Field_Base;
use Elementor\Controls_Manager;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

class Time extends Field_Base {

	public function get_type() {
		return 'time';
	}

	public function get_name() {
		return esc_html__( 'Time', 'pay-addons-for-elementor' );
	}

	public function get_script_depends(): array {
		return [ ];
	}

	public function get_style_depends(): array {
		return [ ];
	}

	public function render( $item, $item_index, $form ) {
		$form->add_render_attribute( 'input' . $item_index, 'class', 'elementor-field-textual elementor-time-field' );
		$form->add_render_attribute('input' . $item_index, 'class', 'elementor-use-native');
		?>
		<input <?php $form->print_render_attribute_string( 'input' . $item_index ); ?>>
		<?php
	}

	public function validation( $field, $record, $ajax_handler ) {
		if ( empty( $field['value'] ) ) {
			return;
		}

		if ( preg_match( '/^(([0-1][0-9])|(2[0-3])):[0-5][0-9]$/', $field['value'] ) !== 1 ) {
			$ajax_handler->add_error( $field['id'], esc_html__( 'The field should be in HH:MM format.', 'pay-addons-for-elementor' ) );
		}
	}
}
