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

$fallback_messages = SftcyWordformBase::wordform_default_validation_messages();

$show_validation_summary = isset( $validation_messages['validation-summary-show'] ) ? sanitize_text_field( $validation_messages['validation-summary-show'] ) : sanitize_text_field( $fallback_messages['validation-summary-show'] );
$validation_summary_msg  = isset( $validation_messages['validation-summary-msg'] ) ? sanitize_text_field( $validation_messages['validation-summary-msg'] ) : sanitize_text_field( $fallback_messages['validation-summary-msg'] );

$validation_sections = array(
	array(
		'title'       => __( 'Validation Summary Message', 'wordform-pro' ),
		'description' => __( 'Show a top-level message when the form has one or more field errors.', 'wordform-pro' ),
		'custom_html' => true,
	),
	array(
		'title'       => __( 'Input Field Validation Messages', 'wordform-pro' ),
		'description' => __( 'These messages are shown when required fields are incomplete or invalid.', 'wordform-pro' ),
		'rows'        => array(
			array(
				'key'   => 'text-msg',
				'label' => __( 'Name / Text', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-regular fa-user',
			),
			array(
				'key'   => 'firstname-msg',
				'label' => __( 'First Name', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-regular fa-address-card',
			),
			array(
				'key'   => 'lastname-msg',
				'label' => __( 'Last Name', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-regular fa-address-card',
			),
			array(
				'key'   => 'number-msg',
				'label' => __( 'Number', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-solid fa-arrow-up-1-9',
			),
			array(
				'key'   => 'textarea-msg',
				'label' => __( 'Description / Textarea', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-solid fa-text-width',
			),
			array(
				'key'   => 'richeditor-msg',
				'label' => __( 'Rich Text Editor', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-pen-to-square',
			),
			array(
				'key'   => 'range-msg',
				'label' => __( 'Range Slider', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-sliders',
			),
			array(
				'key'   => 'url-msg',
				'label' => __( 'URL', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-link',
			),
			array(
				'key'   => 'tel-msg',
				'label' => __( 'Tel', 'wordform-pro' ),
				'icon'  => 'fa fa-phone',
			),
			array(
				'key'   => 'date-msg',
				'label' => __( 'Date', 'wordform-pro' ),
				'icon'  => 'fa fa-calendar',
			),
			array(
				'key'   => 'time-msg',
				'label' => __( 'Time', 'wordform-pro' ),
				'icon'  => 'fa-regular fa-clock',
			),
			array(
				'key'   => 'datetime-msg',
				'label' => __( 'Date Time', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-clock',
			),
			array(
				'key'   => 'address-msg',
				'label' => __( 'Address', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-location-dot',
			),
			array(
				'key'   => 'maskinput-msg',
				'label' => __( 'Mask Input', 'wordform-pro' ),
				'icon'  => 'fa-regular fa-keyboard',
			),
			array(
				'key'   => 'rating-msg',
				'label' => __( 'Rating', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-star',
			),
			array(
				'key'   => 'likert-msg',
				'label' => __( 'Likert / Matrix', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-table-list',
			),
			array(
				'key'   => 'signature-msg',
				'label' => __( 'Signature', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-signature',
			),
			array(
				'key'   => 'select-msg',
				'label' => __( 'Dropdown Options', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-solid fa-square-caret-down',
			),
			array(
				'key'   => 'checkbox-msg',
				'label' => __( 'Checkboxes', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-solid fa-square-check',
			),
			array(
				'key'   => 'gdpr-msg',
				'label' => __( 'GDPR Consent', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-shield-heart',
			),
			array(
				'key'   => 'radio-msg',
				'label' => __( 'Multiple Choice', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-regular fa-circle-dot',
			),
			array(
				'key'   => 'email-msg',
				'label' => __( 'Email', 'wordform-pro' ),
				'icon'  => 'fa-sharp fa-solid fa-at',
			),
		),
	),
	array(
		'title'       => __( 'Payment Field Validation Messages', 'wordform-pro' ),
		'description' => __( 'These messages are used by the newer payment-aware fields when they are required.', 'wordform-pro' ),
		'rows'        => array(
			array(
				'key'   => 'payment-custom-amount-msg',
				'label' => __( 'Payment Custom Amount', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-sack-dollar',
			),
			array(
				'key'   => 'payment-quantity-msg',
				'label' => __( 'Payment Quantity', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-calculator',
			),
			array(
				'key'   => 'payment-method-msg',
				'label' => __( 'Payment Method', 'wordform-pro' ),
				'icon'  => 'fa-regular fa-credit-card',
			),
		),
	),
	array(
		'title'       => __( 'Captcha And Security Messages', 'wordform-pro' ),
		'description' => __( 'These messages appear for captcha and verification failures.', 'wordform-pro' ),
		'rows'        => array(
			array(
				'key'   => 'captcha-empty-msg',
				'label' => __( 'Numeric Captcha [Empty]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-shield',
			),
			array(
				'key'   => 'captcha-wrong-msg',
				'label' => __( 'Numeric Captcha [Wrong]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-shield',
			),
			array(
				'key'   => 'captcha-expire-msg',
				'label' => __( 'Numeric Captcha [Expire]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-shield',
			),
			array(
				'key'   => 'gcaptcha-error-msg',
				'label' => __( 'Google reCaptcha', 'wordform-pro' ),
				'icon'  => 'fa-brands fa-google',
			),
			array(
				'key'   => 'hcaptcha-error-msg',
				'label' => __( 'hCaptcha', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-shield-halved',
			),
		),
	),
	array(
		'title'       => __( 'Upload Validation Messages', 'wordform-pro' ),
		'description' => __( 'These messages are used for file selection, type, and size issues.', 'wordform-pro' ),
		'rows'        => array(
			array(
				'key'   => 'upload-required-msg',
				'label' => __( 'Upload [Required]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-upload',
			),
			array(
				'key'   => 'upload-allow-msg',
				'label' => __( 'Upload [File Type Allowed]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-upload',
			),
			array(
				'key'   => 'upload-size-msg',
				'label' => __( 'Upload [Individual File Size Exceed]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-upload',
			),
			array(
				'key'   => 'upload-allsize-msg',
				'label' => __( 'Upload [All Files Size Exceed]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-upload',
			),
			array(
				'key'   => 'upload-maxinput-msg',
				'label' => __( 'Upload [Max Number Of Files Exceed]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-upload',
			),
			array(
				'key'   => 'upload-filealready-added-msg',
				'label' => __( 'Upload [File Already Selected Once]', 'wordform-pro' ),
				'icon'  => 'fa-solid fa-upload',
			),
		),
	),
);

foreach ( $validation_sections as $section ) :
	?>
	<tr class="sc-wordform-validation-section-row">
		<td colspan="2">
			<div class="sc-wordform-validation-section-head">
				<h4><?php echo esc_html( $section['title'] ); ?></h4>
				<?php if ( ! empty( $section['description'] ) ) : ?>
					<p><?php echo esc_html( $section['description'] ); ?></p>
				<?php endif; ?>
			</div>
		</td>
	</tr>
	<?php if ( ! empty( $section['custom_html'] ) ) : ?>
		<tr class="sc-wordform-validation-setting-row">
			<td class="sc-wordform-settings-validation-tab-label-td">
				<h4>
					<i class="fa-solid fa-circle-exclamation"></i>
					<?php esc_html_e( 'Show Top Summary Message', 'wordform-pro' ); ?>
				</h4>
			</td>
			<td>
				<label class="sc-wordform-validation-inline-toggle">
					<input type="hidden" name="validation-summary-show" value="no" />
					<input type="checkbox" name="validation-summary-show" value="yes" <?php checked( 'yes', $show_validation_summary ); ?> />
					<span><?php esc_html_e( 'Show the top summary when validation fails.', 'wordform-pro' ); ?></span>
				</label>
			</td>
		</tr>
		<tr class="sc-wordform-validation-setting-row">
			<td class="sc-wordform-settings-validation-tab-label-td">
				<h4>
					<i class="fa-regular fa-message"></i>
					<?php esc_html_e( 'Summary Message Text', 'wordform-pro' ); ?>
				</h4>
			</td>
			<td>
				<input
					type="text"
					name="validation-summary-msg"
					class="sc-wordform-validation-summary-msg"
					value="<?php echo esc_attr( $validation_summary_msg ); ?>"
				/>
			</td>
		</tr>
		<?php
		continue;
	endif;
	?>
	<?php foreach ( $section['rows'] as $row ) : ?>
		<tr class="sc-wordform-validation-setting-row">
			<td class="sc-wordform-settings-validation-tab-label-td">
				<h4>
					<i class="<?php echo esc_attr( $row['icon'] ); ?>"></i>
					<?php echo esc_html( $row['label'] ); ?>
				</h4>
			</td>
			<td>
				<input
					type="text"
					name="<?php echo esc_attr( $row['key'] ); ?>"
					class="<?php echo esc_attr( 'sc-wordform-validation-' . str_replace( '-', '-', $row['key'] ) ); ?>"
					value="<?php echo isset( $validation_messages[ $row['key'] ] ) ? esc_attr( $validation_messages[ $row['key'] ] ) : esc_attr( $fallback_messages[ $row['key'] ] ); ?>"
				/>
			</td>
		</tr>
	<?php endforeach; ?>
<?php endforeach; ?>
