<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! current_user_can( 'manage_options' ) ) {
    wp_die( esc_html__( 'You do not have permission to access this page.', 'vedrixa-forms-registration-builder' ) );
}

$dbhandler = new WPEFB_DBhandler();
$form_id   = absint( filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT ) );

if ( ! $form_id ) {
    echo '<div class="wrap"><h1>' . esc_html__( 'Form Preview', 'vedrixa-forms-registration-builder' ) . '</h1><p>' . esc_html__( 'Missing form ID.', 'vedrixa-forms-registration-builder' ) . '</p></div>';
    return;
}

$form = $dbhandler->get_row( 'FORMS', $form_id );
if ( ! $form ) {
    echo '<div class="wrap"><h1>' . esc_html__( 'Form Preview', 'vedrixa-forms-registration-builder' ) . '</h1><p>' . esc_html__( 'Form not found.', 'vedrixa-forms-registration-builder' ) . '</p></div>';
    return;
}

$form_data = ! empty( $form->form_options ) ? json_decode( $form->form_options, true ) : array();
$form_meta = ! empty( $form->meta ) ? json_decode( $form->meta, true ) : array();
if ( empty( $form_data ) || empty( $form_data['sections'] ) ) {
    echo '<div class="wrap"><h1>' . esc_html__( 'Form Preview', 'vedrixa-forms-registration-builder' ) . '</h1><p>' . esc_html__( 'Form has no fields to preview.', 'vedrixa-forms-registration-builder' ) . '</p></div>';
    return;
}
	$global_settings = array(
		'wpefb_show_asterisk'         => $dbhandler->get_global_option_value( 'wpefb_show_asterisk', 1 ),
		'wpefb_form_theme'            => $dbhandler->get_global_option_value( 'wpefb_form_theme', 'default' ),
		'wpefb_form_layout'           => $dbhandler->get_global_option_value( 'wpefb_form_layout', 'label_top' ),
		'wpefb_enable_recaptcha'      => $dbhandler->get_global_option_value( 'wpefb_enable_recaptcha', 0 ),
		'wpefb_recaptcha_version'     => $dbhandler->get_global_option_value( 'wpefb_recaptcha_version', 'v2' ),
		'wpefb_recaptcha_site_key'    => $dbhandler->get_global_option_value( 'wpefb_recaptcha_site_key', '' ),
		'wpefb_multiple_file_uploads' => $dbhandler->get_global_option_value( 'wpefb_multiple_file_uploads', 0 ),
	);

	wp_enqueue_style( 'vedrixa-forms-registration-builder-public', plugin_dir_url( WPEFB_PLUGIN_FILE ) . 'public/css/registration-form-builder-public.css', array(), WPEFB_PLUGIN_VERSION );
	wp_enqueue_script( 'easy-form-frontend-formbuilder', plugin_dir_url( WPEFB_PLUGIN_FILE ) . 'public/js/registration-form-builder-main.js', array( 'jquery' ), WPEFB_PLUGIN_VERSION, true );

	$renderer = new WPEFB_Form_Renderer(
        $form_data,
        array(),
        array(
			'form_meta' => $form_meta,
			'global'    => $global_settings,
        )
	);
	?>
<div class="wrap">
    <h1><?php esc_html_e( 'Form Preview', 'vedrixa-forms-registration-builder' ); ?></h1>
    <?php if ( ! empty( $form_meta['wpefb_form_description'] ) ) : ?>
        <div class="wefb-form-description"><?php echo wp_kses_post( wpautop( $form_meta['wpefb_form_description'] ) ); ?></div>
    <?php endif; ?>
    <?php if ( ! empty( $form_meta['before_form'] ) ) : ?>
        <div class="wefb-before-form"><?php echo wp_kses_post( $form_meta['before_form'] ); ?></div>
    <?php endif; ?>
    <?php echo wp_kses_post( $renderer->render_form( $form_id ) ); ?>
</div>
