<?php

use KaizenCoders\MagicLink\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

/* @var \KaizenCoders\MagicLink\Admin\Magic_Links_Table */
$object = Helper::get_data( $template_data, 'object', [] );
$title  = Helper::get_data( $template_data, 'title', '' );

?>

<div class="wrap">
	<h1 class="wp-heading-inline">
		<span class="text-2xl font-bold leading-7 text-gray-900 sm:text-2xl sm:leading-9 sm:truncate">
			<?php echo esc_html( $title ); ?>
		</span>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=magic-link-links&action=new' ) ); ?>" class="page-title-action"><?php esc_html_e( 'Generate New', 'magic-link' ); ?></a>
	</h1>

	<?php
	$message = isset( $_GET['message'] ) ? sanitize_text_field( wp_unslash( $_GET['message'] ) ) : '';

	if ( 'created' === $message ) :
		$notice_text = __( 'Magic Link created successfully.', 'magic-link' );

		if ( isset( $_GET['email_sent'] ) ) {
			$email_sent = absint( $_GET['email_sent'] );
			if ( 1 === $email_sent ) {
				$notice_text .= ' ' . __( 'Email sent to the user.', 'magic-link' );
			} else {
				$notice_text .= ' ' . __( 'Failed to send email.', 'magic-link' );
			}
		}
		?>
		<div class="notice notice-success is-dismissible">
			<p><?php echo esc_html( $notice_text ); ?></p>
		</div>
	<?php endif; ?>

	<div id="poststuff" class="kc-us-items-lists">
		<div id="post-body" class="metabox-holder column-1">
			<div id="post-body-content">
				<div class="meta-box-sortables ui-sortable">
					<form method="get">
						<input type="hidden" name="page" value="magic-link-links">
						<?php
						$object->prepare_items();
						?>
					</form>
					<form method="post">
						<?php
						$object->display();
						?>
					</form>
				</div>
			</div>
		</div>
		<br class="clear">
	</div>
</div>
