<?php
defined( 'ABSPATH' ) || exit;

function formulate_pricing_forms_list_page() {
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( esc_html__( 'You do not have sufficient permissions.', 'formulate-pricing' ) );
	}

	// ── Inline action handlers ─────────────────────────────────────────────────
	if ( isset( $_GET['formulate_pricing_action'] ) ) {
		$action  = sanitize_text_field( wp_unslash( $_GET['formulate_pricing_action'] ) );
		$form_id = isset( $_GET['id'] ) ? absint( wp_unslash( $_GET['id'] ) ) : 0;
		check_admin_referer( 'formulate_pricing_form_action_' . $form_id );

		if ( 'delete' === $action && $form_id ) {
			Formulate_Pricing_Form::delete( $form_id );
			wp_safe_redirect( admin_url( 'admin.php?page=formulate-pricing&deleted=1' ) );
			exit;
		}
	}

	// phpcs:disable WordPress.Security.NonceVerification.Recommended -- read-only admin list filters.
	$search   = sanitize_text_field( isset( $_GET['s'] ) ? wp_unslash( $_GET['s'] ) : '' );
	$paged    = max( 1, isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 1 );
	// phpcs:enable WordPress.Security.NonceVerification.Recommended
	$per_page = 20;

	$forms = Formulate_Pricing_Form::get_all(
		array(
			'search'   => $search,
			'page'     => $paged,
			'per_page' => $per_page,
		)
	);
	$total  = Formulate_Pricing_Form::count( array( 'search' => $search ) );
	$pages  = (int) ceil( $total / $per_page );

	$admin_nonce = wp_create_nonce( 'formulate_pricing_admin_nonce' );
	$new_url     = wp_nonce_url( admin_url( 'admin.php?page=formulate-pricing-new' ), 'formulate_pricing_create_form' );
	?>
	<div class="wrap bf-admin-wrap">

	<?php if ( isset( $_GET['deleted'] ) ) : // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>
		<div class="bf-notice success">
			<i class="hgi-stroke hgi-checkmark-circle-01"></i>
			<?php esc_html_e( 'Form deleted successfully.', 'formulate-pricing' ); ?>
		</div>
	<?php endif; ?>

		<!-- Page header -->
		<div class="bf-page-header">
			<div class="bf-page-header-left">
				<h1><?php esc_html_e( 'Formulate Pricing', 'formulate-pricing' ); ?></h1>
				<?php
			// translators: %d: number of forms.
			$forms_count_text = sprintf( _n( '%d form', '%d forms', $total, 'formulate-pricing' ), $total );
			?>
			<p><?php echo esc_html( $forms_count_text ); ?></p>
			</div>
		<a href="<?php echo esc_url( $new_url ); ?>" class="bf-btn-primary">
			<i class="hgi-stroke hgi-plus-sign"></i>
			<?php esc_html_e( 'New Form', 'formulate-pricing' ); ?>
		</a>
		</div>

		<!-- Toolbar -->
		<form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
			<input type="hidden" name="page" value="formulate-pricing">
			<div class="bf-toolbar">
			<div class="bf-search-field">
				<i class="hgi-stroke hgi-search-01"></i>
				<input type="search" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Search forms…', 'formulate-pricing' ); ?>">
				<button type="submit" aria-label="<?php esc_attr_e( 'Search', 'formulate-pricing' ); ?>">
					<i class="hgi-stroke hgi-arrow-right-01"></i>
				</button>
			</div>
			</div>
		</form>

		<!-- Forms table -->
		<div class="bf-card">
			<table class="bf-table">
				<thead>
					<tr>
						<th style="width:36px"><input type="checkbox" id="bf-check-all"></th>
						<th><?php esc_html_e( 'Form', 'formulate-pricing' ); ?></th>
						<th><?php esc_html_e( 'Shortcode', 'formulate-pricing' ); ?></th>
						<th><?php esc_html_e( 'Entries', 'formulate-pricing' ); ?></th>
						<th><?php esc_html_e( 'Created', 'formulate-pricing' ); ?></th>
						<th><?php esc_html_e( 'Actions', 'formulate-pricing' ); ?></th>
					</tr>
				</thead>
				<tbody>
				<?php if ( empty( $forms ) ) : ?>
					<tr>
						<td colspan="6">
							<div class="bf-empty-state">
						<div class="bf-empty-icon">
								<i class="hgi-stroke hgi-file-01" style="font-size:36px;color:#9ca3af"></i>
							</div>
								<?php if ( $search ) : ?>
									<h3><?php esc_html_e( 'No forms found', 'formulate-pricing' ); ?></h3>
									<p><?php esc_html_e( 'No forms match your search. Try different keywords.', 'formulate-pricing' ); ?></p>
								<?php else : ?>
									<h3><?php esc_html_e( 'No forms yet', 'formulate-pricing' ); ?></h3>
									<p><?php esc_html_e( 'Create your first pricing form and embed it anywhere with a shortcode.', 'formulate-pricing' ); ?></p>
									<a href="<?php echo esc_url( $new_url ); ?>" class="bf-btn-primary">
										<?php esc_html_e( 'Create your first form', 'formulate-pricing' ); ?>
									</a>
								<?php endif; ?>
							</div>
						</td>
					</tr>
				<?php else : ?>
					<?php foreach ( $forms as $form ) :
						$edit_url    = admin_url( 'admin.php?page=formulate-pricing-edit&form_id=' . $form->id );
						$entries_url = admin_url( 'admin.php?page=formulate-pricing-entries&form_id=' . $form->id );
						$delete_url  = wp_nonce_url( admin_url( 'admin.php?page=formulate-pricing&formulate_pricing_action=delete&id=' . $form->id ), 'formulate_pricing_form_action_' . $form->id );
						$prev_url    = add_query_arg(
							array(
								'action'  => 'formulate_pricing_preview_form',
								'form_id' => $form->id,
								'nonce'   => wp_create_nonce( 'formulate_pricing_preview_' . $form->id ),
							),
							admin_url( 'admin-ajax.php' )
						);
						$entry_count = Formulate_Pricing_Entry::count( array( 'form_id' => $form->id ) );
						$shortcode   = '[formulate_pricing_form id="' . $form->id . '"]';
					?>
					<tr>
						<td><input type="checkbox" class="bf-form-check" value="<?php echo esc_attr( $form->id ); ?>"></td>
						<td>
							<a class="bf-form-name" href="<?php echo esc_url( $edit_url ); ?>"><?php echo esc_html( $form->title ); ?></a>
							<div class="bf-form-meta">ID: <?php echo esc_html( $form->id ); ?></div>
						</td>
						<td>
						<span class="bf-sc-copy" data-sc="<?php echo esc_attr( $shortcode ); ?>" title="<?php esc_attr_e( 'Click to copy', 'formulate-pricing' ); ?>">
							<i class="hgi-stroke hgi-copy-01" style="font-size:12px"></i>
							<?php echo esc_html( $shortcode ); ?>
						</span>
						</td>
						<td>
							<?php if ( $entry_count > 0 ) : ?>
								<a class="bf-count-pill" href="<?php echo esc_url( $entries_url ); ?>"><?php echo esc_html( $entry_count ); ?></a>
							<?php else : ?>
								<span class="bf-count-pill zero">0</span>
							<?php endif; ?>
						</td>
						<td class="bf-date-cell">
							<?php echo esc_html( date_i18n( 'M j, Y', strtotime( $form->created_at ) ) ); ?>
						</td>
						<td>
							<div class="bf-row-acts">
								<a href="<?php echo esc_url( $edit_url ); ?>" class="bf-btn-sm"><?php esc_html_e( 'Edit', 'formulate-pricing' ); ?></a>
								<button class="bf-btn-sm bf-duplicate-btn" data-id="<?php echo esc_attr( $form->id ); ?>" data-nonce="<?php echo esc_attr( $admin_nonce ); ?>"><?php esc_html_e( 'Duplicate', 'formulate-pricing' ); ?></button>
								<a href="<?php echo esc_url( $prev_url ); ?>" class="bf-btn-sm" target="_blank"><?php esc_html_e( 'Preview', 'formulate-pricing' ); ?></a>
								<a href="<?php echo esc_url( $delete_url ); ?>" class="bf-btn-sm danger bf-confirm-delete"><?php esc_html_e( 'Delete', 'formulate-pricing' ); ?></a>
							</div>
						</td>
					</tr>
					<?php endforeach; ?>
				<?php endif; ?>
				</tbody>
			</table>

			<?php if ( $pages > 1 ) : ?>
			<div class="bf-pagination">
				<span>
					<?php
					/* translators: 1: current page, 2: total pages */
					echo esc_html( sprintf( __( 'Page %1$d of %2$d', 'formulate-pricing' ), $paged, $pages ) );
					?>
				</span>
				<div>
					<?php
					$pagination_base = add_query_arg(
						array(
							'page' => 'formulate-pricing',
							's'    => $search,
							'paged' => '%#%',
						),
						admin_url( 'admin.php' )
					);
					echo wp_kses_post(
						paginate_links(
							array(
								'base'      => $pagination_base,
								'format'    => '',
								'total'     => $pages,
								'current'   => $paged,
								'prev_text' => '&#8592;',
								'next_text' => '&#8594;',
							)
						)
					);
					?>
				</div>
			</div>
			<?php endif; ?>
		</div>
	</div>

	<?php
}
