<?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';
$pagenum       = filter_input( INPUT_GET, 'pagenum', FILTER_SANITIZE_NUMBER_INT );
$pagenum       = isset( $pagenum ) ? absint( $pagenum ) : 1;
$filter_key    = filter_input( INPUT_GET, 'filter_key', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
$filter_key    = isset( $filter_key ) ? sanitize_text_field( $filter_key ) : '';
$limit         = 10; // number of rows in page
$offset        = ( $pagenum - 1 ) * $limit;
$additional    = '';
if ( $filter_key !== '' ) {
	global $wpdb;
	$like       = '%' . $wpdb->esc_like( $filter_key ) . '%';
	$additional = $wpdb->prepare( 'name LIKE %s', $like );
}

if ( $additional !== '' ) {
	$totalforms = (int) $dbhandler->get_all_result( $identifier, 'COUNT(id)', 1, 'var', 0, false, 'id', false, $additional );
	$forms      =  $dbhandler->get_all_result( $identifier, '*', 1, 'results', $offset, $limit, 'id', 'desc', $additional );
} else {
	$totalforms = $dbhandler->wpefb_count( $identifier );
	$forms      =  $dbhandler->get_all_result( $identifier, '*', 1, 'results', $offset, $limit, 'id', 'desc' );
}
$num_of_pages = ceil( $totalforms/$limit );
$pagination   = $dbhandler->wpefb_get_pagination( $num_of_pages, $pagenum );
if ( ! function_exists( 'wefb_get_or_create_preview_page_id' ) ) {
	function wefb_get_or_create_preview_page_id() {

        $page_id = (int) get_option( 'wpefb_preview_page_id', 0 );
		if ( $page_id && get_post_status( $page_id ) ) {
			return $page_id;
		}

        $query = new WP_Query(
            array(
				'post_type'      => 'page',
				'post_status'    => array( 'publish', 'private', 'draft' ),
				's'              => '[wpefb_registration',
				'fields'         => 'ids',
				'posts_per_page' => 1,
            )
        );
		if ( ! empty( $query->posts ) ) {
			$page_id = (int) $query->posts[0];
			update_option( 'wpefb_preview_page_id', $page_id );
			return $page_id;
		}

        $page_id = wp_insert_post(
            array(
				'post_type'    => 'page',
				'post_title'   => 'Form Preview',
				'post_status'  => 'private',
				'post_content' => '[wpefb_registration]',
            )
        );
		if ( $page_id && ! is_wp_error( $page_id ) ) {
			update_option( 'wpefb_preview_page_id', (int) $page_id );
			return (int) $page_id;
		}

        return 0;
	}
}

$preview_page_id = wefb_get_or_create_preview_page_id();

?>
<div class="advrf-wrapper wrap wef-forms-modern wef-dashboard-modern">
	<?php do_action( 'wpefb_manage_forms_before_page_title' ); ?>

<div  class=" wef-forms-admin-wrap wef-wrapper-bg wef-forms-modern">

    <div class="wef-page-title">
            <div class="wef-page-title-row">
                <div class="wef-title-block">
                    <h1 class="wpefb-page-heading"><?php esc_html_e( 'All Forms', 'vedrixa-forms-registration-builder' ); ?></h1>
                    <p class="wef-subtitle"><?php esc_html_e( 'Create, customize, and track your forms in one place.', 'vedrixa-forms-registration-builder' ); ?></p>
                </div>
                <div class="wef-title-actions">
                    <button type="button" class="wef-primary-btn" onclick="efb_show_form()"><?php esc_html_e( 'New Form', 'vedrixa-forms-registration-builder' ); ?></button>
                </div>
            </div>
            <div class="wef-page-menu">
                <div class="wef-search-form">
                <form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
                    <input type="hidden" name="page" value="wpefb_manage_forms">
                    <label class="wef-search-label">
                        <span><?php esc_html_e( 'Search', 'vedrixa-forms-registration-builder' ); ?></span>
                        <input type="text" value="<?php echo esc_attr( $filter_key ); ?>" name="filter_key" placeholder="<?php esc_attr_e( 'Form Name', 'vedrixa-forms-registration-builder' ); ?>">
                    </label>
                    <button type="submit" class="wef-secondary-btn"><?php esc_html_e( 'Search', 'vedrixa-forms-registration-builder' ); ?></button>
                    <?php
                    if ( $filter_key !== '' ) :
						?>
                        <a class="wef-secondary-btn wef-reset-btn" href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_manage_forms' ) ); ?>"><?php esc_html_e( 'Clear', 'vedrixa-forms-registration-builder' ); ?></a>
                    <?php endif; ?>
                </form>
                </div>
            </div>
        </div>
    


    <?php if ( '1' === filter_input( INPUT_GET, 'wefb_form_create_error', FILTER_SANITIZE_NUMBER_INT ) ) : ?>
        <div class="notice notice-error"><p><?php esc_html_e( 'Please enter form name and select form type.', 'vedrixa-forms-registration-builder' ); ?></p></div>
    <?php endif; ?>

    <div class="wef-form-cards">
        <?php
        if ( !empty( $forms ) ) :
			foreach ( $forms as $form ) :
				?>
        <div class="wef-form-card">
            <div class="wef-form-card-wrap">
                <div class="wef-card-header">
                    <div class="wef-card-title">
                        <a href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . absint( $form->id ) ) ); ?>" title="<?php echo esc_attr( $form->name ); ?>"><?php echo esc_html( $form->name ); ?></a>
                    </div>
                    <div class="wef-card-meta">
                        <?php $form_type_label = ( 1 === (int) $form->type ) ? __( 'Registration', 'vedrixa-forms-registration-builder' ) : __( 'Contact', 'vedrixa-forms-registration-builder' ); ?>
                        <span class="wef-pill wef-pill-type"><?php echo esc_html( $form_type_label ); ?></span>
								<?php $submission_count = (int) $dbhandler->wpefb_count( 'SUBMISSIONS', array( 'form_id' => absint( $form->id ) ) ); ?>
                        <a class="wef-pill wef-pill-link" href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . absint( $form->id ) . '&tab=submissions' ) ); ?>">
                            <span><?php esc_html_e( 'Submissions', 'vedrixa-forms-registration-builder' ); ?></span>
                            <strong><?php echo esc_html( $submission_count ); ?></strong>
                            <span class="dashicons dashicons-arrow-right-alt"></span>
                        </a>
                    </div>
                </div>
                <div class="wef-card-body">
                    <div class="wef-short-code-wrap">
								<?php $shortcode_value = '[wpefb_registration id="' . absint( $form->id ) . '"]'; ?>
                        <button type="button" class="wef-shortcode-btn" title="<?php esc_attr_e( 'Click to copy Shortcode', 'vedrixa-forms-registration-builder' ); ?>" data-shortcode="<?php echo esc_attr( $shortcode_value ); ?>">
                            <span class="wef-shortcode-text"><?php echo esc_html( $shortcode_value ); ?></span>
                            <span class="wef-shortcode-action"><?php esc_html_e( 'Copy', 'vedrixa-forms-registration-builder' ); ?></span>
                        </button>
                        <span style="display: none;" class="copy-message"><?php esc_html_e( 'Copied to Clipboard', 'vedrixa-forms-registration-builder' ); ?></span>
                    </div>
                </div>
                <div class="wef-card-actions">
                    <a class="wef-action wef-action-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . absint( $form->id ) ) ); ?>"><i class="material-icons">dashboard</i><span><?php esc_html_e( 'Dashboard', 'vedrixa-forms-registration-builder' ); ?></span></a>
							<?php
							$preview_url = $preview_page_id ? get_permalink( $preview_page_id ) : home_url( '/' );
							$preview_url = add_query_arg( 'id', absint( $form->id ), $preview_url );
							?>
                    <a class="wef-action" target="_blank" href="<?php echo esc_url( $preview_url ); ?>"><i class="material-icons">visibility</i><span><?php esc_html_e( 'Preview', 'vedrixa-forms-registration-builder' ); ?></span></a>
                    <a class="wef-action" href="<?php echo esc_url( admin_url( 'admin.php?page=wpefb_form_fields&id=' . absint( $form->id ) ) ); ?>"><i class="material-icons">edit</i><span><?php esc_html_e( 'Edit', 'vedrixa-forms-registration-builder' ); ?></span></a>
							<?php $delete_url = wp_nonce_url( admin_url( 'admin.php?page=wpefb_manage_forms&action=delete&form_id=' . absint( $form->id ) ), 'wpefb_delete_form_' . absint( $form->id ) ); ?>
                    <a class="wef-action wef-action-danger" href="<?php echo esc_url( $delete_url ); ?>" onclick="return confirm('<?php echo esc_js( __( 'Are you sure you want to delete this form? This will remove all its fields, sections, and submissions.', 'vedrixa-forms-registration-builder' ) ); ?>');"><i class="material-icons">delete</i><span><?php esc_html_e( 'Delete', 'vedrixa-forms-registration-builder' ); ?></span></a>
                </div>
            </div>
        </div>
				<?php
                endforeach;
else :
	?>
		<p class="wef-no-forms"><?php esc_html_e( 'No forms found. Click the "New Form" button to create your first form.', 'vedrixa-forms-registration-builder' ); ?></p>	
	<?php
endif;
?>
        
           

    </div> 
    

<div class="wef-pagination">
    <?php
    if ( !empty( $pagination ) ) {
		echo wp_kses_post( $pagination );}
	?>
</div>


</div>
    

<!-- add new form popup html start -->

<div id="wef-builder-modal" class="wef-dialog wef-forms-modern"  style="display: none;">
   <div class="wef-dialog-overlay wef-fade-in"></div>
   <div class="wef-dialog-content wef-bounce-in" aria-labelledby="dialogTitle" aria-describedby="dialogDescription" role="dialog">
      <div class="wef-box" role="document">
           <form name="wef_add_form" id="wef_add_form" method="post">
         <div class="wef-box-header">
            <h3 class="wef-box-title" id="dialogTitle"><?php esc_html_e( 'Create New Form', 'vedrixa-forms-registration-builder' ); ?></h3>
            <div class="wef-actions-right">
               <button type="button" class="wef-dialog-close wef-builder-builder-fields-close" aria-label="Close this dialog window">X</button>
            </div>
         </div>
         <div class="wef-box-body">
            <p><?php esc_html_e( 'Choose a field type to insert into your form.', 'vedrixa-forms-registration-builder' ); ?></p>
         </div>
         <div class="wef-new-form-wrap">
            
           <div class="wefrow">
        <div class="weffield">
          <?php esc_html_e( 'Form Name', 'vedrixa-forms-registration-builder' ); ?>
          <sup>*</sup></div>
        <div class="wefinput wef_required">
          <input type="text" name="name" class="wef-input-field" id="name" value="" required />
          <div class="errortext"></div>
        </div>
      </div>
            
            <div class="wefrow">
        <div class="weffield">
          <?php esc_html_e( 'Form Type', 'vedrixa-forms-registration-builder' ); ?>
        </div>
        <div class="wefinput wef_radio_required">
         <ul class="wefradio">
              <li>
                  <input type="radio"  name="type" id="type_registration" value="1" required>
                <?php esc_html_e( 'Registration Form', 'vedrixa-forms-registration-builder' ); ?>
              </li>
               <li>
                <input type="radio" name="type"  id="type_contact" value="2">
                <?php esc_html_e( 'Contact Form', 'vedrixa-forms-registration-builder' ); ?>
              </li>
          </ul>
        
        </div>
      </div>
            
        </div> 
        
         <div class="wef-box-footer">
             <input type="hidden" name="form_id" id="form_id" value="0" />
        <input type="hidden" name="redirect" value="none" />
        <?php wp_nonce_field( 'save_wpefb_add_form' ); ?>
            <button type="button" class="wef-button wef-button-ghost wef-builder-builder-fields-close"><?php esc_html_e( 'Cancel', 'vedrixa-forms-registration-builder' ); ?></button>
            <div class="wef-actions-right">
            
        <button type="submit" value="Save" id="wef-builder-builder-insert-fields" name="submit_form" class="wef-button wef-button-blue" onClick="return add_form_validation()" ><span class="wef-loading-text"><?php esc_html_e( 'Save', 'vedrixa-forms-registration-builder' ); ?></span><i class="wef-icon-loader wef-loading" aria-hidden="true"></i></button></div>
         <div class="all_error_text" style="display:none;"></div>
         </div>
           </form>
      </div>
   </div>
</div>
<!-- add new form popup html end -->  

</div>
