<?php
/**
 * Basic Forms - Admin Forms List View
 *
 * @package BasicForms
 * @since 1.0.0
 */

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

use WP_PowerSuite\Admin\PageHero;

$list_table = new BasicForms\FormsListTable();
$list_table->prepare_items();

$meta_html = sprintf(
	'<a href="%1$s" class="button">%2$s</a> <a href="%3$s" class="button">%4$s</a> <a href="%5$s" class="button button-primary">%6$s</a>',
	esc_url( admin_url( 'admin.php?page=powersuite-basic-forms&view=entries' ) ),
	esc_html__( 'All Entries', 'wp-powersuite' ),
	esc_url( admin_url( 'admin.php?page=powersuite-basic-forms&view=settings' ) ),
	esc_html__( 'Settings', 'wp-powersuite' ),
	esc_url( admin_url( 'admin.php?page=powersuite-basic-forms&action=add' ) ),
	esc_html__( 'Add New Form', 'wp-powersuite' )
);
?>
<div class="wrap wpps-bf-wrap">
	<hr class="wp-header-end" style="margin:0;padding:0;border:none;height:0;overflow:hidden;">

	<?php if ( $forms_deleted > 0 ) : ?>
		<div class="notice notice-success is-dismissible"><p><?php echo esc_html( sprintf( _n( '%d form deleted.', '%d forms deleted.', $forms_deleted, 'wp-powersuite' ), $forms_deleted ) ); ?></p></div>
	<?php endif; ?>

	<?php
	PageHero::render_for_module(
		[
			'icon'      => 'dashicons-feedback',
			'title'     => __( 'AI Forms', 'wp-powersuite' ),
			'tagline'   => __( 'Create and manage contact and lead forms with email notifications and entry management.', 'wp-powersuite' ),
			'stats'     => [
				[
					'value' => number_format_i18n( $total_forms ),
					'label' => __( 'Forms', 'wp-powersuite' ),
				],
				[
					'value' => number_format_i18n( $total_entries ),
					'label' => __( 'Entries', 'wp-powersuite' ),
				],
				[
					'value' => number_format_i18n( $entries_today ),
					'label' => __( 'Today', 'wp-powersuite' ),
				],
				[
					'value' => number_format_i18n( $entries_week ),
					'label' => __( 'This week', 'wp-powersuite' ),
				],
			],
			'meta_html' => $meta_html,
		]
	);
	?>

	<div class="notice notice-info inline wpps-bf-privacy-notice">
		<p><?php esc_html_e( 'Form entries may include personal data such as email addresses, phone numbers, and IP addresses. Use export and delete controls responsibly, and disable IP storage in form settings when not required.', 'wp-powersuite' ); ?></p>
	</div>

	<div class="wpps-bf-layout-2col">
		<div class="wpps-bf-panel">
			<h2 class="wpps-bf-panel-title"><?php esc_html_e( 'Recent Entries', 'wp-powersuite' ); ?></h2>
			<div class="wpps-bf-panel-body">
				<?php if ( ! empty( $recent_entries ) ) : ?>
					<table class="widefat striped">
						<thead>
							<tr>
								<th><?php esc_html_e( 'Form', 'wp-powersuite' ); ?></th>
								<th><?php esc_html_e( 'Submitted', 'wp-powersuite' ); ?></th>
								<th><?php esc_html_e( 'Status', 'wp-powersuite' ); ?></th>
								<th><?php esc_html_e( 'Action', 'wp-powersuite' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $recent_entries as $entry ) : ?>
								<tr>
									<td><strong><?php echo esc_html( $entry->form_name ?? __( 'Unknown', 'wp-powersuite' ) ); ?></strong></td>
									<td><?php echo esc_html( human_time_diff( strtotime( $entry->date_created ), (int) current_time( 'timestamp' ) ) . ' ' . __( 'ago', 'wp-powersuite' ) ); ?></td>
									<td><span class="wpps-bf-badge <?php echo ( $entry->status ?? '' ) === 'unread' ? 'wpps-bf-badge-warning' : 'wpps-bf-badge-success'; ?>"><?php echo ( $entry->status ?? '' ) === 'unread' ? esc_html__( 'Unread', 'wp-powersuite' ) : esc_html__( 'Read', 'wp-powersuite' ); ?></span></td>
									<td><a href="<?php echo esc_url( admin_url( 'admin.php?page=powersuite-basic-forms&view=entries&form_filter=' . (int) $entry->form_id ) ); ?>" class="button button-small"><?php esc_html_e( 'View', 'wp-powersuite' ); ?></a></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php else : ?>
					<p class="wpps-bf-empty"><?php esc_html_e( 'Form entries will appear here once users start submitting forms.', 'wp-powersuite' ); ?></p>
				<?php endif; ?>
			</div>
		</div>
		<div class="wpps-bf-panel">
			<h2 class="wpps-bf-panel-title"><?php esc_html_e( 'Top Forms', 'wp-powersuite' ); ?></h2>
			<div class="wpps-bf-panel-body">
				<?php if ( ! empty( $top_forms ) ) : ?>
					<table class="widefat striped">
						<thead>
							<tr>
								<th><?php esc_html_e( 'Form Name', 'wp-powersuite' ); ?></th>
								<th><?php esc_html_e( 'Entries', 'wp-powersuite' ); ?></th>
								<th><?php esc_html_e( 'Action', 'wp-powersuite' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $top_forms as $form ) : ?>
								<tr>
									<td><strong><?php echo esc_html( $form->post_title ); ?></strong></td>
									<td><?php echo esc_html( number_format_i18n( (int) $form->entry_count ) ); ?></td>
									<td><a href="<?php echo esc_url( admin_url( 'admin.php?page=powersuite-basic-forms&view=entries&form_filter=' . (int) $form->ID ) ); ?>" class="button button-small"><?php esc_html_e( 'View Entries', 'wp-powersuite' ); ?></a></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php else : ?>
					<p class="wpps-bf-empty"><?php esc_html_e( 'Create your first form to start collecting entries!', 'wp-powersuite' ); ?></p>
				<?php endif; ?>
			</div>
		</div>
	</div>

	<div class="wpps-bf-panel wpps-bf-list-panel">
		<div class="wpps-bf-panel-body">
			<form method="get" id="forms-filter">
				<input type="hidden" name="page" value="powersuite-basic-forms">
				<?php $list_table->search_box( __( 'Search forms', 'wp-powersuite' ), 'form' ); ?>
				<?php $list_table->display(); ?>
			</form>
			<?php if ( ! $list_table->has_items() ) : ?>
				<div class="wpps-bf-empty-state">
					<p><?php esc_html_e( 'Create your first form to start collecting entries!', 'wp-powersuite' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=powersuite-basic-forms&action=add' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Create Your First Form', 'wp-powersuite' ); ?></a>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>
