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

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

$form_id              = isset( $current_form_id ) ? absint( $current_form_id ) : 0;
$submission_id        = absint( filter_input( INPUT_GET, 'submission_id', FILTER_SANITIZE_NUMBER_INT ) );
$dbhandler            = isset( $dbhandler ) ? $dbhandler : new WPEFB_DBhandler();
$helper               = new WPEFB_Helper();
$submission_url       = admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . $form_id . '&tab=submissions' );
$form_row             = $dbhandler->get_row( 'FORMS', $form_id );
$is_registration_form = is_object( $form_row ) && isset( $form_row->type ) && 1 === absint( $form_row->type );

if ( $form_id <= 0 ) {
	echo '<div class="advrf-wrapper wrap wef-forms-modern wef-dashboard-modern wef-submissions-modern">';
	echo '<p>' . esc_html__( 'Invalid form ID.', 'vedrixa-forms-registration-builder' ) . '</p>';
	echo '</div>';
	return;
}

$get_submission_payload = static function ( $submission_row ) {
	$payload = array();
	if ( ! empty( $submission_row->data ) ) {
		$decoded = json_decode( $submission_row->data, true );
		if ( is_array( $decoded ) ) {
			$payload = $decoded;
		}
	}
	return $payload;
};

$get_submission_user_id = static function ( $payload ) {
	if ( isset( $payload['wp_user_id'] ) ) {
		return absint( $payload['wp_user_id'] );
	}
	return 0;
};

$get_payload_value = static function ( $payload, $candidate_keys ) {
	if ( ! is_array( $payload ) || empty( $payload ) ) {
		return '';
	}

	$normalized_map = array();
	foreach ( $payload as $key => $value ) {
		$normalized_key                    = strtolower( trim( str_replace( array( '_', '-' ), ' ', (string) $key ) ) );
		$normalized_map[ $normalized_key ] = $value;
	}

	foreach ( $candidate_keys as $candidate_key ) {
		$normalized_candidate = strtolower( trim( str_replace( array( '_', '-' ), ' ', (string) $candidate_key ) ) );
		if ( isset( $normalized_map[ $normalized_candidate ] ) ) {
			$value = $normalized_map[ $normalized_candidate ];
			return is_scalar( $value ) ? sanitize_text_field( (string) $value ) : '';
		}
	}

	return '';
};

if ( current_user_can( 'manage_options' ) ) {
	$delete_id    = absint( filter_input( INPUT_GET, 'delete_submission', FILTER_SANITIZE_NUMBER_INT ) );
	$delete_nonce = sanitize_text_field( (string) filter_input( INPUT_GET, '_wpnonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
	if ( $delete_id > 0 && '' !== $delete_nonce && wp_verify_nonce( $delete_nonce, 'wpefb_delete_submission_' . $delete_id ) ) {
		$delete_row = $dbhandler->get_row( 'SUBMISSIONS', $delete_id );
		if ( is_object( $delete_row ) && isset( $delete_row->form_id ) && absint( $delete_row->form_id ) === $form_id ) {
			$dbhandler->remove_row( 'SUBMISSIONS', 'id', $delete_id );
		}

		wp_safe_redirect( add_query_arg( 'wpefb_deleted', 1, $submission_url ) );
		exit;
	}

	$bulk_action = sanitize_key( (string) filter_input( INPUT_POST, 'wpefb_bulk_action', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
	if ( '' !== $bulk_action ) {
		$bulk_nonce = sanitize_text_field( (string) filter_input( INPUT_POST, 'wpefb_submissions_bulk_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
		if ( '' !== $bulk_nonce && wp_verify_nonce( $bulk_nonce, 'wpefb_submissions_bulk_' . $form_id ) ) {
			$selected_ids = filter_input( INPUT_POST, 'selected', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
			$selected_ids = is_array( $selected_ids ) ? $selected_ids : array();
			$deleted      = 0;
			$updated      = 0;

			foreach ( $selected_ids as $selected_id ) {
				$selected_id = absint( $selected_id );
				if ( $selected_id <= 0 ) {
					continue;
				}

				$current_row = $dbhandler->get_row( 'SUBMISSIONS', $selected_id );
				if ( ! is_object( $current_row ) || ! isset( $current_row->form_id ) || absint( $current_row->form_id ) !== $form_id ) {
					continue;
				}

				if ( 'delete' === $bulk_action ) {
					$dbhandler->remove_row( 'SUBMISSIONS', 'id', $selected_id );
					++$deleted;
					continue;
				}

				if ( $is_registration_form && in_array( $bulk_action, array( 'set_active', 'set_inactive', 'set_pending' ), true ) ) {
					$payload = $get_submission_payload( $current_row );
					$user_id = $get_submission_user_id( $payload );
					if ( $user_id > 0 ) {
						$target_status = 'pending';
						if ( 'set_active' === $bulk_action ) {
							$target_status = 'active';
						} elseif ( 'set_inactive' === $bulk_action ) {
							$target_status = 'inactive';
						}
						$helper->update_user_account_status( $user_id, $target_status );
						++$updated;
					}
				}
			}

			wp_safe_redirect(
				add_query_arg(
					array(
						'wpefb_deleted' => $deleted,
						'wpefb_updated' => $updated,
					),
					$submission_url
				)
			);
			exit;
		}
	}
}

$is_image_url = static function ( $url ) {
	$path = wp_parse_url( $url, PHP_URL_PATH );
	$ext  = $path ? strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ) : '';
	return in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg' ), true );
};

$render_submission_value = static function ( $value ) use ( $is_image_url ) {
	$render_file_item = static function ( $item ) use ( $is_image_url ) {
		$attachment_id = isset( $item['attachment_id'] ) ? absint( $item['attachment_id'] ) : 0;
		$file_url      = '';
		if ( $attachment_id > 0 ) {
			$maybe_url = wp_get_attachment_url( $attachment_id );
			if ( is_string( $maybe_url ) && '' !== $maybe_url ) {
				$file_url = $maybe_url;
			}
		}
		if ( '' === $file_url && ! empty( $item['url'] ) ) {
			$file_url = esc_url_raw( (string) $item['url'] );
		}

		$filename = '';
		if ( ! empty( $item['filename'] ) ) {
			$filename = sanitize_file_name( (string) $item['filename'] );
		}
		if ( '' === $filename && '' !== $file_url ) {
			$path     = wp_parse_url( $file_url, PHP_URL_PATH );
			$filename = sanitize_file_name( wp_basename( (string) $path ) );
		}
		if ( '' === $filename ) {
			$filename = __( 'Attachment', 'vedrixa-forms-registration-builder' );
		}

		if ( '' === $file_url ) {
			return '<span>' . esc_html( $filename ) . '</span>';
		}

		$is_image = ( $attachment_id > 0 && wp_attachment_is_image( $attachment_id ) ) || $is_image_url( $file_url );
		if ( $is_image ) {
			if ( $attachment_id > 0 ) {
				$thumb = wp_get_attachment_image( $attachment_id, array( 52, 52 ), true, array( 'class' => 'wef-submission-file-thumb' ) );
				if ( ! is_string( $thumb ) || '' === $thumb ) {
					$thumb = '<img class="wef-submission-file-thumb" src="' . esc_url( $file_url ) . '" alt="" />';
				}
			} else {
				$thumb = '<img class="wef-submission-file-thumb" src="' . esc_url( $file_url ) . '" alt="" />';
			}
		} else {
			$icon_url = $attachment_id > 0 ? wp_mime_type_icon( $attachment_id ) : wp_mime_type_icon( $file_url );
			$thumb    = '<img class="wef-submission-file-icon" src="' . esc_url( $icon_url ) . '" alt="" />';
		}

		return '<div class="wef-submission-file-item">'
			. '<a class="wef-submission-file-preview" href="' . esc_url( $file_url ) . '" target="_blank" rel="noopener noreferrer">' . $thumb . '</a>'
			. '<div class="wef-submission-file-meta">'
			. '<div class="wef-submission-file-name">' . esc_html( $filename ) . '</div>'
			. '<a class="wef-submission-file-download" href="' . esc_url( $file_url ) . '" download>' . esc_html__( 'Download', 'vedrixa-forms-registration-builder' ) . '</a>'
			. '</div>'
			. '</div>';
	};

	if ( is_array( $value ) ) {
		$is_single_file = isset( $value['url'] ) || isset( $value['attachment_id'] ) || isset( $value['filename'] );
		if ( $is_single_file ) {
			return $render_file_item( $value );
		}

		$all_are_files = ! empty( $value );
		foreach ( $value as $item ) {
			if ( ! is_array( $item ) || ( ! isset( $item['url'] ) && ! isset( $item['attachment_id'] ) && ! isset( $item['filename'] ) ) ) {
				$all_are_files = false;
				break;
			}
		}

		if ( $all_are_files ) {
			$output = '<div class="wef-submission-file-list">';
			foreach ( $value as $item ) {
				$output .= $render_file_item( $item );
			}
			$output .= '</div>';
			return $output;
		}

		$flat_values = array_map(
			static function ( $item ) {
				return is_scalar( $item ) ? sanitize_text_field( (string) $item ) : '';
			},
			$value
		);
		return esc_html( implode( ', ', array_filter( $flat_values, 'strlen' ) ) );
	}

	return esc_html( (string) $value );
};

$is_sensitive_submission_label = static function ( $label ) {
	$normalized = strtolower( trim( (string) $label ) );
	$normalized = str_replace( array( '-', '_' ), ' ', $normalized );
	$normalized = preg_replace( '/\s+/', ' ', $normalized );
	return in_array( $normalized, array( 'password', 'confirm password', 'user pass', 'confirm pass' ), true );
};

if ( $submission_id ) {
	$submission = $dbhandler->get_row( 'SUBMISSIONS', $submission_id );

	echo '<div class="advrf-wrapper wrap wef-forms-modern wef-dashboard-modern wef-submissions-modern">';
	echo '<div class="advrf-page-title wef-submissions-page-title">';
	echo '<h1 class="wpefb-page-heading">' . esc_html__( 'Submission Details', 'vedrixa-forms-registration-builder' ) . '</h1>';
	echo '<ul class="advrf-nav"><li class="advrf-nav-item"><a href="' . esc_url( $submission_url ) . '">' . esc_html__( 'Back to Submissions', 'vedrixa-forms-registration-builder' ) . '</a></li></ul>';
	echo '</div>';

	if ( empty( $submission ) || (int) $submission->form_id !== $form_id ) {
		echo '<p>' . esc_html__( 'Submission not found.', 'vedrixa-forms-registration-builder' ) . '</p>';
		echo '</div>';
		return;
	}

	$payload = $get_submission_payload( $submission );
	$user_id = $get_submission_user_id( $payload );

	echo '<table class="widefat striped wef-submissions-table">';
	echo '<tbody>';
	echo '<tr><th>' . esc_html__( 'Submission ID', 'vedrixa-forms-registration-builder' ) . '</th><td>' . esc_html( $submission->id ) . '</td></tr>';
	echo '<tr><th>' . esc_html__( 'Email', 'vedrixa-forms-registration-builder' ) . '</th><td>' . esc_html( $submission->email ) . '</td></tr>';
	echo '<tr><th>' . esc_html__( 'Submitted On', 'vedrixa-forms-registration-builder' ) . '</th><td>' . esc_html( $submission->submitted_on ) . '</td></tr>';
	echo '<tr><th>' . esc_html__( 'Unique Token', 'vedrixa-forms-registration-builder' ) . '</th><td>' . esc_html( $submission->unique_token ) . '</td></tr>';

	if ( $user_id > 0 ) {
		$user           = get_user_by( 'id', $user_id );
		$account_status = $helper->get_user_account_status( $user_id );
		echo '<tr><th>' . esc_html__( 'Registered User', 'vedrixa-forms-registration-builder' ) . '</th><td>' . esc_html( $user ? $user->user_login : $user_id ) . '</td></tr>';
		echo '<tr><th>' . esc_html__( 'Account Status', 'vedrixa-forms-registration-builder' ) . '</th><td>' . esc_html( ucfirst( $account_status ) ) . '</td></tr>';
		if ( current_user_can( 'manage_options' ) ) {
			$activate_url   = add_query_arg(
				array(
					'user_action' => 'activate',
					'user_id'     => $user_id,
					'_wpnonce'    => wp_create_nonce( 'wpefb_user_status_' . $user_id ),
				),
				$submission_url
			);
			$deactivate_url = add_query_arg(
				array(
					'user_action' => 'deactivate',
					'user_id'     => $user_id,
					'_wpnonce'    => wp_create_nonce( 'wpefb_user_status_' . $user_id ),
				),
				$submission_url
			);
			echo '<tr><th>' . esc_html__( 'User Actions', 'vedrixa-forms-registration-builder' ) . '</th><td><a class="wef-secondary-btn" href="' . esc_url( $activate_url ) . '">' . esc_html__( 'Activate', 'vedrixa-forms-registration-builder' ) . '</a> <a class="wef-secondary-btn" href="' . esc_url( $deactivate_url ) . '">' . esc_html__( 'Deactivate', 'vedrixa-forms-registration-builder' ) . '</a></td></tr>';
		}
	}

	if ( ! empty( $payload ) ) {
		foreach ( $payload as $label => $value ) {
			if ( in_array( $label, array( 'wp_user_id', 'account_status' ), true ) || $is_sensitive_submission_label( $label ) ) {
				continue;
			}
			$rendered_value = $render_submission_value( $value );
			echo '<tr><th>' . esc_html( (string) $label ) . '</th><td>' . wp_kses_post( $rendered_value ) . '</td></tr>';
		}
	}
	echo '</tbody>';
	echo '</table>';
	echo '</div>';
	return;
}

$search_query   = sanitize_text_field( (string) filter_input( INPUT_GET, 'wpefb_q', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
$status_filter  = sanitize_key( (string) filter_input( INPUT_GET, 'wpefb_status', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
$date_from      = sanitize_text_field( (string) filter_input( INPUT_GET, 'wpefb_date_from', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
$date_to        = sanitize_text_field( (string) filter_input( INPUT_GET, 'wpefb_date_to', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
$pagenum        = max( 1, absint( filter_input( INPUT_GET, 'pagenum', FILTER_SANITIZE_NUMBER_INT ) ) );
$items_per_page = 20;

$submissions = $dbhandler->get_all_result( 'SUBMISSIONS', '*', array( 'form_id' => $form_id ), 'results', 0, false, 'id', 'DESC' );
$submissions = is_array( $submissions ) ? $submissions : array();

$submissions = array_values(
	array_filter(
		$submissions,
		static function ( $row ) use ( $search_query, $status_filter, $date_from, $date_to, $is_registration_form, $helper, $get_submission_payload, $get_submission_user_id, $get_payload_value ) {
			$payload = $get_submission_payload( $row );

			if ( '' !== $status_filter && $is_registration_form ) {
				$user_id = $get_submission_user_id( $payload );
				if ( $user_id <= 0 || $status_filter !== $helper->get_user_account_status( $user_id ) ) {
					return false;
				}
			}

			if ( '' !== $date_from || '' !== $date_to ) {
				$timestamp = isset( $row->submitted_on ) ? strtotime( (string) $row->submitted_on ) : false;
				if ( false === $timestamp ) {
					return false;
				}
				if ( '' !== $date_from ) {
					$from_ts = strtotime( $date_from . ' 00:00:00' );
					if ( false !== $from_ts && $timestamp < $from_ts ) {
						return false;
					}
				}
				if ( '' !== $date_to ) {
					$to_ts = strtotime( $date_to . ' 23:59:59' );
					if ( false !== $to_ts && $timestamp > $to_ts ) {
						return false;
					}
				}
			}

			if ( '' === $search_query ) {
				return true;
			}

			$name    = $get_payload_value( $payload, array( 'name', 'first name', 'user name', 'username' ) );
			$subject = $get_payload_value( $payload, array( 'subject' ) );
			$stack   = implode(
				' ',
				array(
					isset( $row->email ) ? (string) $row->email : '',
					isset( $row->submitted_on ) ? (string) $row->submitted_on : '',
					$name,
					$subject,
				)
			);

			return false !== stripos( $stack, $search_query );
		}
	)
);

$submissions = apply_filters( 'wpefb_submissions_list', $submissions, $form_id, $dbhandler );
$submissions = is_array( $submissions ) ? $submissions : array();

$total_items = count( $submissions );
$total_pages = max( 1, (int) ceil( $total_items / $items_per_page ) );
$pagenum     = min( $pagenum, $total_pages );
$offset      = ( $pagenum - 1 ) * $items_per_page;
$rows        = array_slice( $submissions, $offset, $items_per_page );

$pagination_links = paginate_links(
	array(
		'base'      => add_query_arg( 'pagenum', '%#%', $submission_url ),
		'format'    => '',
		'prev_text' => '&laquo;',
		'next_text' => '&raquo;',
		'total'     => $total_pages,
		'current'   => $pagenum,
		'add_args'  => array(
			'wpefb_q'         => $search_query,
			'wpefb_status'    => $status_filter,
			'wpefb_date_from' => $date_from,
			'wpefb_date_to'   => $date_to,
		),
	)
);

echo '<div class="advrf-wrapper wrap wef-forms-modern wef-dashboard-modern wef-submissions-modern">';
if ( isset( $_GET['wpefb_deleted'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$deleted_count = absint( filter_input( INPUT_GET, 'wpefb_deleted', FILTER_SANITIZE_NUMBER_INT ) );
	if ( $deleted_count > 0 ) {
		/* translators: %s: Number of submissions deleted. */
		echo '<div class="notice notice-success is-dismissible"><p>' . sprintf( esc_html__( '%s submission(s) deleted.', 'vedrixa-forms-registration-builder' ), esc_html( number_format_i18n( $deleted_count ) ) ) . '</p></div>';
	}
}
if ( isset( $_GET['wpefb_updated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$updated_count = absint( filter_input( INPUT_GET, 'wpefb_updated', FILTER_SANITIZE_NUMBER_INT ) );
	if ( $updated_count > 0 ) {
		/* translators: %s: Number of account status updates applied. */
		echo '<div class="notice notice-success is-dismissible"><p>' . sprintf( esc_html__( '%s account status update(s) applied.', 'vedrixa-forms-registration-builder' ), esc_html( number_format_i18n( $updated_count ) ) ) . '</p></div>';
	}
}

echo '<div class="advrf-page-title wef-submissions-page-title">';
echo '<h1 class="wpefb-page-heading">' . esc_html__( 'Form Submissions', 'vedrixa-forms-registration-builder' ) . '</h1>';
echo '<ul class="advrf-nav"><li class="advrf-nav-item"><a href="' . esc_url( admin_url( 'admin.php?page=wpefb_form_dashboard&id=' . $form_id ) ) . '">' . esc_html__( 'Back to Dashboard', 'vedrixa-forms-registration-builder' ) . '</a></li></ul>';
echo '</div>';

echo '<form method="get" action="' . esc_url( $submission_url ) . '" class="wef-submissions-filters" style="margin:16px 0 12px;display:flex;gap:8px;flex-wrap:wrap;">';
echo '<input type="hidden" name="page" value="wpefb_form_dashboard">';
echo '<input type="hidden" name="id" value="' . esc_attr( $form_id ) . '">';
echo '<input type="hidden" name="tab" value="submissions">';
echo '<input type="search" name="wpefb_q" value="' . esc_attr( $search_query ) . '" placeholder="' . esc_attr__( 'Search by keyword', 'vedrixa-forms-registration-builder' ) . '">';
if ( $is_registration_form ) {
	echo '<select name="wpefb_status">';
	echo '<option value="">' . esc_html__( 'All Status', 'vedrixa-forms-registration-builder' ) . '</option>';
	echo '<option value="active" ' . selected( $status_filter, 'active', false ) . '>' . esc_html__( 'Active', 'vedrixa-forms-registration-builder' ) . '</option>';
	echo '<option value="inactive" ' . selected( $status_filter, 'inactive', false ) . '>' . esc_html__( 'Inactive', 'vedrixa-forms-registration-builder' ) . '</option>';
	echo '<option value="pending" ' . selected( $status_filter, 'pending', false ) . '>' . esc_html__( 'Pending', 'vedrixa-forms-registration-builder' ) . '</option>';
	echo '</select>';
}
echo '<input type="date" name="wpefb_date_from" value="' . esc_attr( $date_from ) . '">';
echo '<input type="date" name="wpefb_date_to" value="' . esc_attr( $date_to ) . '">';
echo '<button type="submit" class="button button-secondary">' . esc_html__( 'Apply', 'vedrixa-forms-registration-builder' ) . '</button>';
echo '</form>';

do_action( 'wpefb_submissions_toolbar', $form_id, $submission_url );

echo '<form method="post" action="' . esc_url( $submission_url ) . '">';
wp_nonce_field( 'wpefb_submissions_bulk_' . $form_id, 'wpefb_submissions_bulk_nonce' );
echo '<div class="tablenav top">';
echo '<div class="alignleft actions">';
echo '<select name="wpefb_bulk_action">';
echo '<option value="">' . esc_html__( 'Bulk Actions', 'vedrixa-forms-registration-builder' ) . '</option>';
echo '<option value="delete">' . esc_html__( 'Delete', 'vedrixa-forms-registration-builder' ) . '</option>';
if ( $is_registration_form ) {
	echo '<option value="set_active">' . esc_html__( 'Set Active', 'vedrixa-forms-registration-builder' ) . '</option>';
	echo '<option value="set_inactive">' . esc_html__( 'Set Inactive', 'vedrixa-forms-registration-builder' ) . '</option>';
	echo '<option value="set_pending">' . esc_html__( 'Set Pending', 'vedrixa-forms-registration-builder' ) . '</option>';
}
echo '</select> ';
echo '<button type="submit" class="button action">' . esc_html__( 'Apply', 'vedrixa-forms-registration-builder' ) . '</button>';
echo '</div>';
if ( $pagination_links ) {
	echo '<div class="tablenav-pages">' . wp_kses_post( $pagination_links ) . '</div>';
}
echo '<br class="clear"></div>';

if ( empty( $rows ) ) {
	echo '<p>' . esc_html__( 'No submissions found for this form yet.', 'vedrixa-forms-registration-builder' ) . '</p>';
	echo '</form></div>';
	return;
}

echo '<table class="widefat striped wef-submissions-table wp-list-table">';
echo '<thead><tr>';
echo '<td class="manage-column column-cb check-column"><input type="checkbox" id="wpefb_select_all"></td>';
echo '<th>' . esc_html__( 'ID', 'vedrixa-forms-registration-builder' ) . '</th>';
echo '<th>' . esc_html__( 'Name', 'vedrixa-forms-registration-builder' ) . '</th>';
echo '<th>' . esc_html__( 'Email', 'vedrixa-forms-registration-builder' ) . '</th>';
echo '<th>' . esc_html__( 'Subject', 'vedrixa-forms-registration-builder' ) . '</th>';
if ( $is_registration_form ) {
	echo '<th>' . esc_html__( 'Status', 'vedrixa-forms-registration-builder' ) . '</th>';
}
echo '<th>' . esc_html__( 'Date', 'vedrixa-forms-registration-builder' ) . '</th>';
echo '<th>' . esc_html__( 'Actions', 'vedrixa-forms-registration-builder' ) . '</th>';
echo '</tr></thead>';
echo '<tbody>';

foreach ( $rows as $submission ) {
	$payload     = $get_submission_payload( $submission );
	$user_id     = $get_submission_user_id( $payload );
	$user_status = $is_registration_form && $user_id > 0 ? $helper->get_user_account_status( $user_id ) : '';
	$name        = $get_payload_value( $payload, array( 'name', 'first name', 'user name', 'username' ) );
	$subject     = $get_payload_value( $payload, array( 'subject' ) );
	$detail_link = add_query_arg( array( 'submission_id' => $submission->id ), $submission_url );
	$delete_link = add_query_arg(
		array(
			'delete_submission' => absint( $submission->id ),
			'_wpnonce'          => wp_create_nonce( 'wpefb_delete_submission_' . absint( $submission->id ) ),
		),
		$submission_url
	);

	echo '<tr>';
	echo '<th scope="row" class="check-column"><input type="checkbox" name="selected[]" value="' . esc_attr( $submission->id ) . '"></th>';
	echo '<td>' . esc_html( $submission->id ) . '</td>';
	echo '<td>' . esc_html( '' !== $name ? $name : '-' ) . '</td>';
	echo '<td>' . esc_html( $submission->email ) . '</td>';
	echo '<td>' . esc_html( '' !== $subject ? $subject : '-' ) . '</td>';
	if ( $is_registration_form ) {
		echo '<td>' . esc_html( '' !== $user_status ? ucfirst( $user_status ) : '-' ) . '</td>';
	}
	echo '<td>' . esc_html( $submission->submitted_on ) . '</td>';
	echo '<td>';
	echo '<a class="wef-secondary-btn" href="' . esc_url( $detail_link ) . '">' . esc_html__( 'View', 'vedrixa-forms-registration-builder' ) . '</a>';
	if ( $is_registration_form && $user_id > 0 ) {
		$toggle_action = 'active' === $user_status ? 'deactivate' : 'activate';
		$toggle_label  = 'active' === $user_status ? __( 'Deactivate User', 'vedrixa-forms-registration-builder' ) : __( 'Activate User', 'vedrixa-forms-registration-builder' );
		$toggle_url    = add_query_arg(
			array(
				'user_action' => $toggle_action,
				'user_id'     => $user_id,
				'_wpnonce'    => wp_create_nonce( 'wpefb_user_status_' . $user_id ),
			),
			$submission_url
		);
		echo ' <a class="wef-secondary-btn" href="' . esc_url( $toggle_url ) . '">' . esc_html( $toggle_label ) . '</a>';
	}
	if ( current_user_can( 'manage_options' ) ) {
		echo ' <a class="wef-secondary-btn" href="' . esc_url( $delete_link ) . '" onclick="return confirm(' . wp_json_encode( __( 'Delete this submission?', 'vedrixa-forms-registration-builder' ) ) . ');">' . esc_html__( 'Delete', 'vedrixa-forms-registration-builder' ) . '</a>';
	}
	echo '</td>';
	echo '</tr>';
}

echo '</tbody>';
echo '</table>';

echo '<div class="tablenav bottom">';
if ( $pagination_links ) {
	echo '<div class="tablenav-pages">' . wp_kses_post( $pagination_links ) . '</div>';
}
echo '<br class="clear"></div>';

echo '</form>';
echo '</div>';
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
	var selectAll = document.getElementById('wpefb_select_all');
	if (!selectAll) {
		return;
	}
	selectAll.addEventListener('change', function() {
		var checkboxes = document.querySelectorAll('input[name="selected[]"]');
		checkboxes.forEach(function(checkbox) {
			checkbox.checked = selectAll.checked;
		});
	});
});
</script>

