<?php

if ( ! defined( 'ABSPATH' ) ) {
	 exit;
}

// phpcs:ignoreFile WordPress.Files.FileName.NotHyphenatedLowercase
$form_row          = is_object( $row ) ? $row : (object) array();
$current_form_id   = isset( $form_row->id ) ? absint( $form_row->id ) : 0;
$current_form_name = isset( $form_row->name ) ? $form_row->name : '';
$preview_page_id   = (int) get_option( 'wpefb_preview_page_id', 0 );
$preview_url       = $preview_page_id ? get_permalink( $preview_page_id ) : home_url( '/' );
if ( ! is_string( $preview_url ) || '' === $preview_url ) {
	$preview_url = home_url( '/' );
}
$preview_url = add_query_arg( 'id', $current_form_id, $preview_url );
?>
<div class="advrf-wrapper wrap wef-forms-modern wef-dashboard-modern wef-configure-modern">
    <div class="advrf-page-title">		 
        <h1 class="wpefb-page-heading">
            <?php echo esc_html( $current_form_name ); ?></h1>
        <ul class="advrf-nav">
            <li class="advrf-nav-item">
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_manage_forms' ) ); ?>">
                    <span class="dashicons dashicons-feedback"></span>
                    <?php esc_html_e( 'All Forms', 'vedrixa-forms-registration-builder' ); ?></a>
            </li>
            <li class="advrf-nav-item">
                <a href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . $current_form_id ) ); ?>">
                    <span class="dashicons dashicons-admin-generic"></span>
                    	<?php esc_html_e( 'Dashboard', 'vedrixa-forms-registration-builder' ); ?>					</a>
            </li>
            <li class="advrf-nav-item">
                <a target="_blank" href="<?php echo esc_url( $preview_url ); ?>">
                    <span class="dashicons dashicons-visibility"></span>
                     <?php esc_html_e( 'Preview', 'vedrixa-forms-registration-builder' ); ?>	</a>
            </li>    
        </ul>
        
         </div>
    <div class="clearfix">
        <div class="advrf-form-conf-wrapper">
            <form action="" method="post" id="advrf_configuration_form">
                <fieldset class="advrf-config-wrap">
                    <div>
                        <div class="group-title">
                            <?php esc_html_e( 'Email Templates', 'vedrixa-forms-registration-builder' ); ?>				</div>
                        <div class="group-wrap">
                            <div class="group-title">
                            <?php esc_html_e( 'Notification Templates for User', 'vedrixa-forms-registration-builder' ); ?>				</div>

                            <?php
							$merge_tags   = array();
							$form_options = ! empty( $form_row->form_options ) ? json_decode( $form_row->form_options, true ) : array();
							if ( ! empty( $form_options['sections'] ) && is_array( $form_options['sections'] ) ) {
								foreach ( $form_options['sections'] as $section ) {
									if ( empty( $section['fields'] ) || ! is_array( $section['fields'] ) ) {
															continue;
									}
									foreach ( $section['fields'] as $field ) {
										$field_key = '';
										if ( ! empty( $field['name'] ) ) {
											$field_key = sanitize_key( $field['name'] );
										} elseif ( ! empty( $field['type'] ) ) {
											$field_key = sanitize_key( $field['type'] );
										}

										if ( '' === $field_key ) {
											continue;
										}

										$label                                  = ! empty( $field['label'] ) ? $field['label'] : $field_key;
										$merge_tags[ '{{' . $field_key . '}}' ] = $label;
									}
								}
							}
                            $GLOBALS['wpefb_email_template_merge_tags'] = $merge_tags;
							if ( ! function_exists( 'wpefb_email_template_media_buttons' ) ) {
								function wpefb_email_template_media_buttons( $editor_id ) {

                                    $allowed = array( 'new_user_email_content', 'user_email_activation_content', 'new_submission_email_content' );
									if ( ! in_array( $editor_id, $allowed, true ) ) {
															return;
									}

                                    $merge_tags = isset( $GLOBALS['wpefb_email_template_merge_tags'] ) ? $GLOBALS['wpefb_email_template_merge_tags'] : array();
									if ( empty( $merge_tags ) ) {
													echo '<span class="description" style="margin-left:12px;">' . esc_html__( 'No fields found for this form.', 'vedrixa-forms-registration-builder' ) . '</span>';
													return;
									}
									?>
                                    <span class="wef-merge-tags wef-merge-tags-inline" data-editor="<?php echo esc_attr( $editor_id ); ?>">
                                        <select class="wef-merge-tag-select">
                                            <option value=""><?php esc_html_e( 'Insert Field', 'vedrixa-forms-registration-builder' ); ?></option>
                                            <?php
                                            foreach ( $merge_tags as $tag => $label ) :
												?>
                                                <option value="<?php echo esc_attr( $tag ); ?>"><?php echo esc_html( $label . ' ' . $tag ); ?></option>
                                            <?php endforeach; ?>
                                            <option value="{{SUBMISSION_DATA}}"><?php esc_html_e( 'All Submission Data', 'vedrixa-forms-registration-builder' ); ?> {{SUBMISSION_DATA}}</option>
                                        </select>
                                        <button type="button" class="button wef-merge-tag-button" data-editor="<?php echo esc_attr( $editor_id ); ?>"><?php esc_html_e( 'Insert', 'vedrixa-forms-registration-builder' ); ?></button>
                                    </span>
										<?php
								}
                                add_action( 'media_buttons', 'wpefb_email_template_media_buttons', 15 );
                            }
                            ?>
                            
                                
                                <div class="advrf-row">
                                <div class="advrf-control-label">
                                    <label><?php esc_html_e( 'New User Email Subject', 'vedrixa-forms-registration-builder' ); ?></label>
                                </div>
                                <div class="advrf-control">
                                    <input type="text" class="advrf-input-field" name="meta[new_user_email_subject]" value="<?php echo esc_attr( isset( $meta['new_user_email_subject'] ) ? $meta['new_user_email_subject'] : '' ); ?>">
                                    <p class="description"><?php esc_html_e( 'Subject of the email to be sent to the newly created user.', 'vedrixa-forms-registration-builder' ); ?></p>
                                </div>
                                 
                            </div>
                            
                            <div class="advrf-row">
						<div class="advrf-control-label">
							<label><?php esc_html_e( 'New User Email Body(Mail Merge and HTML Supported)', 'vedrixa-forms-registration-builder' ); ?></label>
						</div>
						<div class="advrf-control">
							<?php
							$editor_id                               = 'new_user_email_content';
							$settings                                = array(
								'wpautop'           => false,
								'media_buttons'     => true,
								'textarea_name'     => 'meta[new_user_email_content]',
								'textarea_rows'     => 20,
								'tabindex'          => '',
								'tabfocus_elements' => ':prev,:next',
								'editor_css'        => '',
								'editor_class'      => 'advrf-editor',
								'teeny'             => false,
								'dfw'               => false,
								'tinymce'           => true, // <-----
								'quicktags'         => true,
							);
                                                        $before_form = isset( $meta['new_user_email_content'] )?$meta['new_user_email_content']:'';
							wp_editor( $before_form, $editor_id, $settings );
							?>
							<p class="description">
								<?php esc_html_e( 'Content of the email to be sent to the newly created user. You can use rich text and values the user submitted in the form for a more personalized message. Use the code {{PASS_RESET_LINK}} to add password reset link within this email itself.', 'vedrixa-forms-registration-builder' ); ?><br/>
			
							</p>
						</div>  
					</div>
                            
                            <div class="advrf-row">
                                <div class="advrf-control-label">
                                    <label><?php esc_html_e( 'User Activation Email Subject', 'vedrixa-forms-registration-builder' ); ?></label>
                                </div>
                                <div class="advrf-control">
                                    <input type="text" class="advrf-input-field" name="meta[user_email_activation_subject]" value="<?php echo esc_attr( isset( $meta['user_email_activation_subject'] ) ? $meta['user_email_activation_subject'] : '' ); ?>">
                                    <p class="description"><?php esc_html_e( 'Subject of the email to be sent to the activated user.', 'vedrixa-forms-registration-builder' ); ?></p>
                                </div>
                                 
                            </div>
                            
                            <div class="advrf-row">
						<div class="advrf-control-label">
							<label><?php esc_html_e( 'User Activation Email Body(Mail Merge and HTML Supported)', 'vedrixa-forms-registration-builder' ); ?></label>
						</div>
						<div class="advrf-control">
							<?php
							$editor_id                               = 'user_email_activation_content';
							$settings                                = array(
								'wpautop'           => false,
								'media_buttons'     => true,
								'textarea_name'     => 'meta[user_email_activation_content]',
								'textarea_rows'     => 20,
								'tabindex'          => '',
								'tabfocus_elements' => ':prev,:next',
								'editor_css'        => '',
								'editor_class'      => 'advrf-editor',
								'teeny'             => false,
								'dfw'               => false,
								'tinymce'           => true, // <-----
								'quicktags'         => true,
							);
                                                        $before_form = isset( $meta['user_email_activation_content'] )?$meta['user_email_activation_content']:'';
							wp_editor( $before_form, $editor_id, $settings );
							?>
							<p class="description">
								<?php esc_html_e( 'Content of the email to be sent to the activated user. You can use rich text and values the user submitted in the form for a more personalized message.', 'vedrixa-forms-registration-builder' ); ?><br/>
			
							</p>
						</div>  
					</div>
                            
                            <div class="group-title">
                            <?php esc_html_e( 'Notification Templates for Admin', 'vedrixa-forms-registration-builder' ); ?>				</div>
                            
                            <div class="advrf-row">
                                <div class="advrf-control-label">
                                    <label><?php esc_html_e( 'New Submission Email Subject (To Admin)', 'vedrixa-forms-registration-builder' ); ?></label>
                                </div>
                                <div class="advrf-control">
                                    <input type="text" class="advrf-input-field" name="meta[new_submission_email_subject]" value="<?php echo esc_attr( isset( $meta['new_submission_email_subject'] ) ? $meta['new_submission_email_subject'] : '' ); ?>">
                                    <p class="description"><?php esc_html_e( 'Subject of the email to be sent to admin on new submission.', 'vedrixa-forms-registration-builder' ); ?></p>
                                </div>
                                 
                            </div>
                            
                            <div class="advrf-row">
						<div class="advrf-control-label">
							<label><?php esc_html_e( 'New Submission Email Body (To Admin)(Mail Merge and HTML Supported)', 'vedrixa-forms-registration-builder' ); ?></label>
						</div>
						<div class="advrf-control">
							<?php
							$editor_id                               = 'new_submission_email_content';
							$settings                                = array(
								'wpautop'           => false,
								'media_buttons'     => true,
								'textarea_name'     => 'meta[new_submission_email_content]',
								'textarea_rows'     => 20,
								'tabindex'          => '',
								'tabfocus_elements' => ':prev,:next',
								'editor_css'        => '',
								'editor_class'      => 'advrf-editor',
								'teeny'             => false,
								'dfw'               => false,
								'tinymce'           => true, // <-----
								'quicktags'         => true,
							);
                                                        $before_form = isset( $meta['new_submission_email_content'] )?$meta['new_submission_email_content']:'';
							wp_editor( $before_form, $editor_id, $settings );
							?>
							<p class="description">
								<?php esc_html_e( 'Want to know what the user wrote to you earlier with contact form submission? Wish to have purchase history of your WooCommerce customer attached to a new support request? Need to see purchased downloads of Easy Digital Downloads buyer with form data? Time to add some intelligence to your submission notifications. Introducing, message shortcodes which dynamically fetch user information from their history on your site and provide you with deeper user insights attached to the submitted content.', 'vedrixa-forms-registration-builder' ); ?><br/>
			
							</p>
						</div>  
					</div>
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            </div>
                                
                                
                                
                                </div>


                  


                </fieldset>

                <p class="submit">
                    <input type="hidden" name="form_id" id="form_id" value="<?php echo esc_attr( $current_form_id ); ?>" />
                    <?php wp_nonce_field( 'save_wpefb_add_form' ); ?>
                    <input type="submit" value="<?php esc_attr_e( 'Save', 'vedrixa-forms-registration-builder' ); ?>" class="button button-primary" name="submit_form" id="submit_form" onClick="return add_form_validation()"/>
                    <a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . $current_form_id ) ); ?>"><?php esc_html_e( 'Cancel', 'vedrixa-forms-registration-builder' ); ?></a>
                    <div class="all_error_text" style="display:none;"></div>
                </p>
            </form>  
        </div>    

    </div>    
</div>

