<?php
/**
 * GFB Email Template View
 *
 * @package GFB.
 */

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

global $apbgfEmail;
?>
<div class="main-reports--div">
	<div class="reports--content">
		<div class="top--heading">
			<h2><?php echo esc_html__('Email Notification', 'appointment-and-booking-for-gravity-forms'); ?></h2>
		</div>
	</div>
	<div class="apbgf_wrap">        

		<div class="apbgf_maintab-content">
			<h4 class="apbgf_section-title"><?php echo esc_html__('Codes used for email notification', 'appointment-and-booking-for-gravity-forms'); ?></h4>
			
			<table class="apbgf_maintable_codes">
				<tbody>
					<tr>
						<td style="width:33%;">
							<table class="apbgf_maintable_codes">
							<?php 
							$codes = $apbgfEmail->apbgf_codes(); 
							$index = 0;                                
							foreach ($codes as $code => $desc) {
								
								if ($index > 0 && $index % 5 == 0) {
									echo '</table></td>';
									echo '<td style="width:33%;"><table class="apbgf_maintable_codes">';
								}
								
								echo '<tr>';
									echo '<td><strong>' . esc_attr( $code ) . '</strong></td>';
									echo '<td>' . esc_html( $desc ) . '</td>';
								echo '</tr>';
								$index++;
							}
							?>
							</table>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<br />
		<div class="apbgf_maintab-content">
			<form name="app_notification_form" id="app_notification_form" method="post">        		

				<div id="accordion">

					<?php
					$pro_template_ids = apply_filters( 'apbgf_pro_email_templates_ids', array( 2, 4, 5, 6, 7, 8, 9 ));
					foreach ( APBGF_Email::$email_templates as $template ) {
						$class = '';
						$email_template = '';
						$pro_label = '';
						$template_radios = '';
						$template_subject = '';
						$template_radios_class = '';
						$template_subject_class = '';
						if (in_array( $template['id'], $pro_template_ids)) {

							$class='pro-email-template';
							$email_template = 'pro';
							$pro_label = 'PRO';
							$template_radios = 'disabled';
							$template_subject = 'disabled';
							$template_radios_class = 'pro-email-template-radio';
							$template_subject_class = 'pro-email-template-subject';

						}
						?>
					
						<?php if ( $email_template === 'pro' ) : ?>
						<div class="apbgf-pro-trigger" data-apbgf-pro-modal="apbgf-pro-modal">
						<?php endif; ?>

						<h4 class='apbgf_section-title <?php echo esc_attr($class); ?>' ><?php echo esc_html($template['title']); ?> <?php echo esc_html($template['title']); ?> <span class=" <?php echo !empty($pro_label) ?  esc_attr('pro-email-template-label') :''; ?>"> <?php echo esc_attr($pro_label); ?> </span></h4>
						<div>
							<div class="form-section">
								<div class="form-group-elements">
									<label><?php echo esc_html__('Enable Template', 'appointment-and-booking-for-gravity-forms'); ?></label>
									<?php if ($email_template == 'pro') : ?>
										<input type="radio" class='<?php echo esc_attr($template_radios_class . '-enabled'); ?>' value="enabled"  <?php echo esc_attr($template_radios); ?> /> <span class="pro-email-template-radio-label">Enable</span> &nbsp;&nbsp;
										<input type="radio" class='<?php echo esc_attr($template_radios_class . '-disabled'); ?>' value="disabled" <?php checked('disabled', 'disabled', true); ?>  <?php echo esc_attr($template_radios); ?> />  <span class="pro-email-template-radio-label">Disable</span>
									<?php else : ?>
										<input type="radio" name='switch_<?php echo esc_attr( $template['id'] ); ?>' id="switch_enabled" value="enabled" <?php checked('enabled', $template['switch'], true); ?>  /> <?php esc_html_e('Enable', 'appointment-and-booking-for-gravity-forms'); ?>&nbsp;&nbsp;
										<input type="radio" name='switch_<?php echo esc_attr( $template['id'] ); ?>' id="switch_disabled" value="disabled" <?php checked('disabled', $template['switch'], true); ?>  />  <?php esc_html_e('Disable', 'appointment-and-booking-for-gravity-forms'); ?>
									<?php endif; ?>
								</div>

								<?php 
								if ( isset($template['day']) ) {
									?>
									<div class="form-group-elements">
										<label class=""><?php esc_html_e('Set Reminder', 'appointment-and-booking-for-gravity-forms'); ?></label>
										<?php if ($email_template == 'pro') : ?>
										<input type="number" min="1" step="1"  class="form-control" value='<?php echo esc_attr( $template['day'] ); ?>' disabled/>
										<?php else : ?>
											<input type="number" min="1" step="1" name='day_<?php echo esc_attr( $template['id']); ?>' id='date_<?php echo esc_attr( $template['id'] ); ?>' class="form-control" value='<?php echo esc_attr( $template['day']); ?>' />
										<?php endif; ?>
										<small><?php esc_html_e('Enter the number of days before the appointment that you would like the reminder email to be sent. Default is 1.', 'appointment-and-booking-for-gravity-forms'); ?></small>
									</div>
									<?php
								}
								?>

								<div class="form-group-elements">
									<label class=""><?php esc_html_e('Subject', 'appointment-and-booking-for-gravity-forms'); ?></label>
									<input type="text" name='subject_<?php echo esc_attr( $template['id'] ); ?>' id='subject_<?php echo esc_attr( $template['id'] ); ?>' class="form-control" value='<?php echo esc_attr( $template['subject'] ); ?>' />
								</div>
								
								<div class="form-group-elements">
									<label class=""><?php esc_html_e('Message', 'appointment-and-booking-for-gravity-forms'); ?></label>
									<?php    
									if ( $email_template == 'pro' ) {
										$content = $template['message'];
										$editor_id = 'pro-template-message-' . esc_attr( $template['id'] );
										$settings = array(
											'textarea_name' => '',
											'media_buttons' => false,
											'editor_height' => 384,
											'editor_class'  => 'pro-email-template-message',
										);
											
										wp_editor( $content, $editor_id, $settings );
									} else {
										$content = $template['message'];
										$editor_id = 'message_' . esc_attr( $template['id'] );
										$name = 'message_' . esc_attr( $template['id'] );
										
										$settings = array(
											'textarea_name' => $name,
											'media_buttons' => false,
											'editor_height' => 384,
										);
											  
										wp_editor( $content, $editor_id, $settings );   
									}                     
									?>
								</div>		                
							</div>        				
						</div>

						<?php if ( $email_template === 'pro' ) : ?>
						</div>
						<?php endif; ?>
						<?php
					}
					?>
				</div>

				<div class="form-group-elements">
					<div class="form-element email-notify-submit">
						<?php submit_button('Save Notifications'); ?>
					</div>
				</div>

			</form>        	
		</div>
	   
	</div>
</div>
