<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

?>

<?php if (isset($_POST['apbgf_setting_message_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash( $_POST['apbgf_setting_message_nonce'] ) ), 'apbgf_setting_message_action')) : ?>
	<div id="after_update_dialog" class="updated below-h2">
		<p>
			<strong><?php echo esc_html__('GFB settings updated successfully.', 'appointment-and-booking-for-gravity-forms'); ?></strong>
		</p>
	</div>
<?php endif; ?>

<h3><span><i class="fa fa-cogs"></i> <?php echo esc_html__('Gravity Booking Settings', 'appointment-and-booking-for-gravity-forms'); ?></span></h3>

<p><?php echo esc_html__('Connect form fields to register customer when form is submitted. This is mandatory to set this setting, otherwise appointment will not create.', 'appointment-and-booking-for-gravity-forms'); ?></p>

<?php
if ( is_array($all_fields) && count($all_fields) > 0 ) {    
	?>
	<form method="post" class="modal--form">
		<input type="hidden" value="<?php echo esc_attr( $form_id ); ?>" name="apbgf_form_id"/>

		<div class="form-col-full form-col-max-25">
			<label for="apbgf-fname"><?php echo esc_html__('First Name*', 'appointment-and-booking-for-gravity-forms'); ?></label>
			<select name="fname" id="apbgf-fname">
				<option value=""><?php echo esc_html__( '--Select field--', 'appointment-and-booking-for-gravity-forms' ); ?></option>
				<?php
				foreach ( $all_fields as $field ) {
					echo '<option value="' . esc_attr( $field['value'] ) . '" ' . selected($settings['registration_field']['fname'], $field['value'], false) . '>' . esc_attr( $field['label'] ) . '</option>';
				}
				?>
			</select>
		</div>

		<div class="form-col-full form-col-max-25">
			<label for="apbgf-lname"><?php echo esc_html__('Last Name', 'appointment-and-booking-for-gravity-forms'); ?></label>
			<select name="lname" id="apbgf-lname">
				<option value=""><?php echo esc_html__( '--Select field--', 'appointment-and-booking-for-gravity-forms' ); ?></option>
				<?php
				foreach ( $all_fields as $field ) {
					echo '<option value="' . esc_attr( $field['value'] ) . '" ' . selected($settings['registration_field']['lname'], $field['value'], false) . '>' . esc_attr( $field['label'] ) . '</option>';
				}
				?>
			</select>
		</div>

		<div class="form-col-full form-col-max-25">
			<label for="apbgf-email"><?php echo esc_html__('Email*', 'appointment-and-booking-for-gravity-forms'); ?></label>
			<select name="email" id="apbgf-email">
				<option value=""><?php echo esc_html__( '--Select field--', 'appointment-and-booking-for-gravity-forms' ); ?></option>
				<?php
				foreach ( $all_fields as $field ) {
					echo '<option value="' . esc_attr( $field['value'] ) . '" ' . selected($settings['registration_field']['email'], $field['value'], false) . '>' . esc_attr( $field['label'] ) . '</option>';
				}
				?>
			</select>
		</div>

		<div class="form-col-full form-col-max-25">
			<label for="apbgf-phone"><?php echo esc_html__('Phone', 'appointment-and-booking-for-gravity-forms'); ?></label>
			<select name="phone" id="apbgf-phone">
				<option value=""><?php echo esc_html__( '--Select field--', 'appointment-and-booking-for-gravity-forms' ); ?></option>
				<?php
				foreach ( $all_fields as $field ) {
					echo '<option value="' . esc_attr( $field['value'] ) . '" ' . selected($settings['registration_field']['phone'], $field['value'], false) . '>' . esc_attr( $field['label'] ) . '</option>';
				}
				?>
			</select>
		</div>

		<input type="submit" name="apbgf_setting" class="form-col-max-25 save--modal" value="Update Setting"/>
		<?php
			$nonce = wp_create_nonce('apbgf_setting_message_action');
			echo '<input type="hidden" name="apbgf_setting_message_nonce" value="' . esc_attr($nonce) . '"/>';
		?>
	</form>
	<?php
} else {
	esc_html_e('Create form first then you will be able to see settings.', 'appointment-and-booking-for-gravity-forms');
}
