<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals

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

$dbhandler       = new WPEFB_DBhandler();
$wef_activator   = new Registration_Form_Builder_Activator();
$wefrequests     = new WPEFB_Request();
$identifier      = 'FORMS';
$current_form_id = absint( filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT ) );
$row             = $dbhandler->get_row( $identifier, $current_form_id );
$active_tab      = sanitize_key( (string) filter_input( INPUT_GET, 'tab', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
$active_type     = sanitize_key( (string) filter_input( INPUT_GET, 'type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
if ( is_object( $row ) && isset( $row->meta ) && '' !== $row->meta ) {
	$meta = json_decode( $row->meta, true );
} else {
	$meta = array();
}

if ( '' !== $active_tab ) {
	if ( 'configure' === $active_tab ) {
		$allowed_types = WPEFB_Features::get_configure_types();
		if ( in_array( $active_type, $allowed_types, true ) ) {
			$config_file = __DIR__ . '/form-settings-configure-' . $active_type . '.php';
			if ( file_exists( $config_file ) ) {
				include $config_file;
			} else {
				/**
				 * Render configure screen content from add-ons such as pro module.
				 *
				 * @param string $active_type Active configure type.
				 * @param int    $current_form_id Current form ID.
				 * @param object $row Form row object.
				 * @param array  $meta Form meta.
				 */
				do_action( 'wpefb_render_configure_screen', $active_type, $current_form_id, $row, $meta );
			}
		}
	} elseif ( 'build' !== $active_tab ) {
		$tab_file = __DIR__ . '/form-settings-' . $active_tab . '.php';
		if ( file_exists( $tab_file ) ) {
			include $tab_file;
		}
	}
} else {
	?>
	<?php
	$preview_page_id = (int) get_option( 'wpefb_preview_page_id', 0 );
	$preview_url     = home_url( '/' );
	if ( $preview_page_id > 0 ) {
		$maybe_preview_url = get_permalink( $preview_page_id );
		if ( is_string( $maybe_preview_url ) && '' !== $maybe_preview_url ) {
			$preview_url = $maybe_preview_url;
		}
	}
	$preview_url     = add_query_arg( 'id', absint( $current_form_id ), $preview_url );
	$form_name       = isset( $row->name ) ? $row->name : '';
	$upgrade_url     = apply_filters( 'wpefb_pro_page_url', admin_url( 'admin.php?page=wpefb_pro' ) );
	$configure_items = array(
		array(
			'type'  => 'general',
			'icon'  => 'dashicons-admin-generic',
			'label' => __( 'General Settings', 'vedrixa-forms-registration-builder' ),
		),
		array(
			'type'  => 'accounts',
			'icon'  => 'dashicons-admin-users',
			'label' => __( 'Accounts', 'vedrixa-forms-registration-builder' ),
		),
		array(
			'type'  => 'post_submission',
			'icon'  => 'dashicons-welcome-write-blog',
			'label' => __( 'Post Submission', 'vedrixa-forms-registration-builder' ),
		),
		array(
			'type'  => 'auto_responder',
			'icon'  => 'dashicons-email-alt',
			'label' => __( 'Auto Responder', 'vedrixa-forms-registration-builder' ),
		),
		array(
			'type'  => 'limits',
			'icon'  => 'dashicons-shield-alt',
			'label' => __( 'Limits', 'vedrixa-forms-registration-builder' ),
		),
		array(
			'type'  => 'email_templates',
			'icon'  => 'dashicons-email',
			'label' => __( 'Email Templates', 'vedrixa-forms-registration-builder' ),
		),
	);

	/**
	 * Filters configure cards displayed on the form dashboard.
	 *
	 * @param array  $configure_items Default configure items.
	 * @param int    $current_form_id Current form ID.
	 * @param object $row             Current form row.
	 * @param array  $meta            Current form meta.
	 */
	$configure_items    = (array) apply_filters( 'wpefb_form_dashboard_configure_items', $configure_items, $current_form_id, $row, $meta );
	$locked_pro_modules = array();
	if ( ! WPEFB_Features::is_pro_active() ) {
		$locked_module_icons = array(
			'advanced_workflow'     => 'dashicons-randomize',
			'progressive_forms'     => 'dashicons-controls-repeat',
			'validation_ux'         => 'dashicons-yes-alt',
			'file_upload_controls'  => 'dashicons-upload',
			'email_workflows'       => 'dashicons-email-alt',
			'user_lifecycle'        => 'dashicons-admin-users',
			'submission_management' => 'dashicons-clipboard',
			'templates_styling'     => 'dashicons-art',
			'export_filters'        => 'dashicons-download',
			'priority_support'      => 'dashicons-sos',
		);
		foreach ( WPEFB_Features::get_pro_modules() as $module_type => $module_label ) {
			$locked_pro_modules[] = array(
				'type'  => sanitize_key( (string) $module_type ),
				'icon'  => isset( $locked_module_icons[ $module_type ] ) ? $locked_module_icons[ $module_type ] : 'dashicons-admin-generic',
				'label' => (string) $module_label,
			);
		}
	}
	?>
<div class="advrf-wrapper wrap wef-forms-modern wef-dashboard-modern">
    <div class="advrf-page-title">
        <h1 class="wpefb-page-heading">
			<?php echo esc_html( $form_name ) . ' - ' . esc_html__( 'Form Settings', 'vedrixa-forms-registration-builder' ); ?>
        </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 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="advrforms-new-form clearfix">
        <div class="advrf-form-dashboard">
            <!-- Section: Manage -->
            <div class="advrf-dashboard-section">
                <div class="advrf-section-title"><?php esc_html_e( 'Manage', 'vedrixa-forms-registration-builder' ); ?></div>
                <div class="advrf-section-item-container">
                    <div class="advrf-section-item">
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_fields&id=' . esc_attr( $current_form_id ) ) ); ?>">
                            <div>
                                <span class="dashicons dashicons-edit"></span>
                                <span><?php esc_html_e( 'Fields', 'vedrixa-forms-registration-builder' ); ?></span>
                            </div>
                        </a>
                    </div>

                    <div class="advrf-section-item">
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . esc_attr( $current_form_id ) . '&tab=submissions' ) ); ?>">
                            <div>
                                <span class="dashicons dashicons-clipboard"></span>
                                <span><?php esc_html_e( 'Submissions', 'vedrixa-forms-registration-builder' ); ?></span>
                            </div>
                        </a>
                    </div>
                </div>
            </div>
            
            <!-- Section: Configure -->
            <div class="advrf-dashboard-section">
                <div class="advrf-section-title"><?php esc_html_e( 'Configure', 'vedrixa-forms-registration-builder' ); ?></div>
                <div class="advrf-section-item-container">
					<?php foreach ( $configure_items as $configure_item ) : ?>
						<?php
						$item_type = isset( $configure_item['type'] ) ? sanitize_key( (string) $configure_item['type'] ) : '';
						$icon      = isset( $configure_item['icon'] ) ? sanitize_html_class( (string) $configure_item['icon'] ) : 'dashicons-admin-generic';
						$label     = isset( $configure_item['label'] ) ? (string) $configure_item['label'] : '';
						if ( '' === $item_type || '' === $label ) {
							continue;
						}
						?>
						<div class="advrf-section-item">
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . esc_attr( $current_form_id ) . '&tab=configure&type=' . $item_type ) ); ?>">
								<span class="dashicons <?php echo esc_attr( $icon ); ?>"></span>
								<span><?php echo esc_html( $label ); ?></span>
							</a>
						</div>
					<?php endforeach; ?>
					<?php foreach ( $locked_pro_modules as $locked_module ) : ?>
						<div class="advrf-section-item advrf-section-item-locked">
							<a href="<?php echo esc_url( $upgrade_url ); ?>">
								<span class="dashicons <?php echo esc_attr( $locked_module['icon'] ); ?>"></span>
								<span><?php echo esc_html( $locked_module['label'] ); ?></span>
								<small class="wefb-locked-note">
									<span class="dashicons dashicons-lock" aria-hidden="true"></span>
									<?php esc_html_e( 'Pro', 'vedrixa-forms-registration-builder' ); ?>
								</small>
							</a>
						</div>
					<?php endforeach; ?>
                </div>
            </div>
            
            <!-- Section: Add-ons -->
            <div class="advrf-dashboard-section" style="display: none;">
                <div class="advrf-section-title"><?php esc_html_e( 'Add-ons', 'vedrixa-forms-registration-builder' ); ?></div>
                <div class="advrf-section-item-container"></div>
            </div>
        </div>
        
        <!-- Sidebar: Shortcode and Contact -->
        <div class="dashboard-sidebar">
            <div class="advrf-sidebar-column-wrap short-code-column">
                <div class="advrf-section-title"><?php esc_html_e( 'Shortcode', 'vedrixa-forms-registration-builder' ); ?></div>
                <div class="advrf-section-body">
                    <div class="advrf-short-code-wrap advrf-pop-wrap">
                        <?php $shortcode_value = '[wpefb_registration id="' . absint( $current_form_id ) . '"]'; ?>
                        <input type="text" class="advrf-shortcode" value="<?php echo esc_attr( $shortcode_value ); ?>" data-shortcode="<?php echo esc_attr( $shortcode_value ); ?>" readonly="">
                        <span style="display: none;" class="copy-message"><?php esc_html_e( 'Copied to Clipboard', 'vedrixa-forms-registration-builder' ); ?></span>
                    </div>
                </div>
            </div>
            <!-- Contact Information -->
            <div class="advrf-sidebar-column-wrap">
                <div class="advrf-section-title"><?php esc_html_e( 'Contact', 'vedrixa-forms-registration-builder' ); ?></div>
                <div class="advrf-section-body">
                    <div class="advrf-contact-info">
                        <a target="_blank" href="https://registrationformbuilder.com/documentation/"><?php esc_html_e( 'Documentation', 'vedrixa-forms-registration-builder' ); ?></a><br>
                        <a target="_blank" href="https://registrationformbuilder.com/support/"><?php esc_html_e( 'Support', 'vedrixa-forms-registration-builder' ); ?></a><br>
                        <a target="_blank" href="https://registrationformbuilder.com/contact/"><?php esc_html_e( 'Contact', 'vedrixa-forms-registration-builder' ); ?></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
	<?php
}
