<?php

// defaults.
$vars = array(
	'error_message' => '',
	
	'name_error'    => '',
	'multi_id'      => '',
	'fields'        => array(),
	'form_fields'   => array(),
	'email_fields'  => array(),
);
/** @var array $template_vars */
foreach ( $template_vars as $key => $val ) {

	
	$vars[ $key ] = $val;
}


$signingLogic = esig_forminator_get('signing_logic',$vars);
$displayType = esig_forminator_get('display_type',$vars);
$selectSad = esig_forminator_get('select_sad_doc',$vars);


?>

<div class="forminator-integration-popup__header">

	<h3 id="forminator-integration-popup__title" class="sui-box-title sui-lg" style="overflow: initial; white-space: normal; text-overflow: initial;"><?php echo esc_html( __( 'E-Signature Settings', 'esign' ) ); ?></h3>

	<p id="forminator-integration-popup__description" class="sui-description"><?php esc_html_e( 'Set up the E-Signature settings below to link this form with an E-Signature document.', 'esign' ); ?></p>

	<?php if ( ! empty( $vars['error_message'] ) ) : ?>
		<div
			role="alert"
			class="sui-notice sui-notice-red sui-active"
			style="display: block; text-align: left;"
			aria-live="assertive"
		>

			<div class="sui-notice-content">

				<div class="sui-notice-message">

					<span class="sui-notice-icon sui-icon-info" aria-hidden="true"></span>

					<p><?php echo esc_html( $vars['error_message'] ); ?></p>

				</div>

			</div>

		</div>
	<?php endif; ?>

</div>

<form>

<div tabindex="0" role="group" class="sui-form-field" style="margin-bottom: 0;">

		

<table class="sui-table" style="margin-top: 5px; margin-bottom: 0;">

	<thead>

	

	</thead>

	<tbody>

	<?php
	if ( ! empty( $vars['fields'] ) ) :

		foreach ( $vars['fields'] as $key => $field_title ) : ?>

			<tr>

				<td>
					<?php echo esc_html( $field_title ); ?>
					<?php if ( 'signer_name' === $key ) : ?>
						<span class="integrations-required-field sui-error">*</span>
					<?php endif; ?>
					<?php if ( 'email' === $key ) : ?>
						<span class="integrations-required-field sui-error">*</span>
					<?php endif; ?>
				</td>

				<td>
					<?php
					$forminator_fields = $vars['form_fields'];

					if ( 'email' === $key ) {
						$forminator_fields = $vars['email_fields'];
					}
					
					$current_error    = '';
					$current_selected = '';
					if ( isset( $vars[ $key . '_error' ] ) && ! empty( $vars[ $key . '_error' ] ) ) {
						$current_error = $vars[ $key . '_error' ];
					}
					if ( isset( $vars['fields_map'][ $key ] ) && ! empty( $vars['fields_map'][ $key ] ) ) {
						$current_selected = $vars['fields_map'][ $key ];
					}

					

					
					

					?>
					<div class="sui-form-field <?php echo esc_attr( ! empty( $current_error ) ? 'sui-form-field-error' : '' ); ?>"<?php echo esc_attr(( ! empty( $current_error ) )) ? ' style="padding-top: 5px;"' : ''; ?>>
						<?php if ( 'signer_name' === $key ) : ?>
							<!-- Multi-select for signer name fields -->
							<select name="fields_map[<?php echo esc_attr( $key ); ?>][]" class="sui-select sui-select-sm" multiple="multiple" style="min-height: 100px;">
								<?php
								if ( ! empty( $forminator_fields ) ) :
									// Handle both single value (backward compatibility) and array
									$selected_values = array();
									if ( is_array( $current_selected ) ) {
										$selected_values = $current_selected;
									} elseif ( ! empty( $current_selected ) ) {
										$selected_values = array( $current_selected );
									}
									
									foreach ( $forminator_fields as $forminator_field ) : ?>
										<option value="<?php echo esc_attr( $forminator_field['element_id'] ); ?>"
											<?php selected( in_array( $forminator_field['element_id'], $selected_values ), true ); ?>>
											<?php echo esc_html( $forminator_field['field_label'] . ' | ' . $forminator_field['element_id'] ); ?>
										</option>
									<?php endforeach;
								endif;
								?>
							</select>
						<?php else : ?>
							<!-- Single select for other fields like email -->
							<select name="fields_map[<?php echo esc_attr( $key ); ?>]" class="sui-select sui-select-sm">
								<option value=""><?php esc_html_e( 'None', 'esign' ); ?></option>
								<?php
								if ( ! empty( $forminator_fields ) ) :
									foreach ( $forminator_fields as $forminator_field ) : ?>
										<option value="<?php echo esc_attr( $forminator_field['element_id'] ); ?>"
											<?php selected( $current_selected, $forminator_field['element_id'] ); ?>>
											<?php echo esc_html( $forminator_field['field_label'] . ' | ' . $forminator_field['element_id'] ); ?>
										</option>
									<?php endforeach;
								endif;
								?>
							</select>
						<?php endif; ?>
						<?php if ( ! empty( $current_error ) ) : ?>
							<span class="sui-error-message"
								  style="margin-top: 5px; margin-bottom: 5px;"><?php echo esc_html( $current_error ); ?></span>
						<?php endif; ?>
					</div>
				</td>
			</tr>
			
		<?php endforeach;
	endif;
	 ?>
	</tbody>

</table>

<br>
</div>

	<div class="sui-form-field <?php echo esc_attr( ! empty( $vars['signing_logic_error'] ) ? 'sui-form-field-error' : '' ); ?>">
		<label class="sui-label"><?php esc_html_e( 'Signing Logic', 'esign' ); ?> <span class="sui-error">*</span></label>
		
		<select class="sui-select" name="signing_logic" id="signing_logic">
		<option value=""><?php _e('Select Signing Logic','esign')?></option>
		<option <?php if($signingLogic == 'redirect'){echo 'selected';} ?> value="redirect"><?php _e('Redirect user to the E-Signature document after submission','esign')?></option>
		<option <?php if($signingLogic == 'email'){echo 'selected';} ?> value="email"><?php _e('Send user an email requesting their signature after submission','esign')?></option>
		</select>

	</div>

	

	<div class="sui-form-field <?php echo esc_attr( ! empty( $vars['display_type_error'] ) ? 'sui-form-field-error' : '' ); ?>">
		<label class="sui-label"><?php esc_html_e( 'Display Type', 'esign' ); ?></label>
		
		<select class="sui-select" name="display_type" id="display_type">
			
		<option <?php if($displayType == 'underline'){echo 'selected';} ?> value="underline"><?php _e('Underline the data that was submitted in the form','esign')?></option>
		<option <?php if($displayType == 'not_underline'){echo 'selected';} ?> value="not_underline"><?php _e('Do not underline the data that was submitted in the form.','esign')?></option>
		
	</select>
	</div>

	<div class="sui-form-field <?php echo esc_attr( ! empty( $vars['select_sad_doc_error'] ) ? 'sui-form-field-error' : '' ); ?>">
		<label class="sui-label"><?php esc_html_e( 'Select Document', 'esign' ); ?><span class="sui-error">*</span></label>
		
		<select class="sui-select" name="select_sad_doc" id="select_sad_doc">
			
		<?php
                                if (class_exists('esig_sad_document')) {

                                    $sad = new esig_sad_document();
                                    $sad_pages = $sad->esig_get_sad_pages();
                                    echo '<option value=""> ' . __('Select an agreement page', 'esig') . ' </option>';
                                    foreach ($sad_pages as $page) {
                                        $selected = ($page->page_id == $selectSad) ? "selected" : null;
                                        if (get_the_title($page->page_id)) {
                                            echo '<option value="' . $page->page_id . '" ' . $selected . '> ' . get_the_title($page->page_id) . ' </option>';
                                        }
                                    }
                                }
                                ?>
		</select>
	</div>

	<?php
	
	

	

	$enableReminder = esig_forminator_sanitize_init(esig_forminator_get('enabled_signing_reminder',$vars['reminder_email_settings']));
	$first_reminder_email = esig_forminator_sanitize_init(esig_forminator_get('first_reminder_email',$vars['reminder_email_settings']));
	$second_reminder_email = esig_forminator_sanitize_init(esig_forminator_get('second_reminder_email',$vars['reminder_email_settings']));
	$expire_reminder = esig_forminator_sanitize_init(esig_forminator_get('expire_reminder',$vars['reminder_email_settings']));
	
	
	
	?>

	<div class="sui-form-field">

	<input type="checkbox" id="enabled_reminder" name="reminder_email_settings[enabled_signing_reminder]" value="1" <?php if($enableReminder == '1'){ echo "checked"; } ?> > <?php _e('  Enable Signing Reminder Email', 'esig'); ?> <br>
<br>
	<div id="signing_reminder_div" <?php if($enableReminder != '1'){ ?> style="display:none;font-size: 14px;" <?php }else{ ?> style="font-size: 14px;" <?php } ?>>

	<?php if($enableReminder == '1'){ ?>
		<input type="hidden" name="esign_enabled_reminder" id="esign_enabled_reminder" value="enabled">
	<?php } ?>
	

	<?php _e('Send the first reminder to the signer ', 'esig'); ?> <input type="number" value="<?php echo $first_reminder_email;  ?>" name="reminder_email_settings[first_reminder_email]"   style="width:70px;height:30px;"> <?php _e(' days after the initial signing request. ', 'esig'); ?><br><br>
	<?php _e('Send the second reminder to the signer ', 'esig'); ?> <input type="number" value="<?php echo $second_reminder_email; ?>" name="reminder_email_settings[second_reminder_email]"  style="width:70px;height:30px;"> <?php _e(' days after the initial signing request.', 'esig'); ?><br><br>
    <?php _e('Send the last reminder to the signer ', 'esig'); ?><input type="number" value="<?php echo $expire_reminder; ?>" name="reminder_email_settings[expire_reminder]"  style="width:70px;height:30px;">  <?php _e(' days after the initial signing request.', 'esig'); ?>



	</div>

	</div>



	<input type="hidden" name="multi_id" value="<?php echo esc_attr( $vars['multi_id'] ); ?>">
</form>

<script type="text/javascript">


(function ($) {
		$(document).ready(function (e) {
    $('#enabled_reminder').change(function () {
        if (!this.checked){
       
           $('#signing_reminder_div').fadeOut('slow');
		   $("#esign_enabled_reminder").remove();
		} 
        else{ 
            $('#signing_reminder_div').fadeIn('slow');
			$("<input>").attr({
                name: "esign_enabled_reminder",
                id: "esign_enabled_reminder",
                type: "hidden",
                value: "enabled"
            }).insertAfter( "#enabled_reminder" );
		}
    });
});
	})(jQuery);
</script>
