<?php

use KaizenCoders\MagicLink\Helper;

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

$title       = Helper::get_data( $template_data, 'title', '' );
$button_text = Helper::get_data( $template_data, 'button_text', '' );
$form_action = Helper::get_data( $template_data, 'form_action', '' );
$form_data   = Helper::get_data( $template_data, 'form_data', [] );

$user_id            = Helper::get_data( $form_data, 'user_id', 0 );
$token_lifespan     = Helper::get_data( $form_data, 'token_lifespan', 5 );
$token_lifespan_unit = Helper::get_data( $form_data, 'token_lifespan_unit', 'minutes' );
$login_redirect     = Helper::get_data( $form_data, 'login_redirect', '' );

$users = get_users( [ 'orderby' => 'display_name', 'order' => 'ASC' ] );

$list_url = admin_url( 'admin.php?page=magic-link-links' );

?>

<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>
	</h1>

	<?php if ( ! empty( $template_data['message'] ) ) : ?>
		<div class="notice notice-<?php echo esc_attr( $template_data['message_type'] ?? 'error' ); ?> is-dismissible">
			<p><?php echo esc_html( $template_data['message'] ); ?></p>
		</div>
	<?php endif; ?>

	<div id="poststuff">
		<div id="post-body" class="metabox-holder column-1">
			<div id="post-body-content">
                <div class="bg-white rounded-lg shadow-md meta-box-sortables ui-sortable">
				<form method="post" action="<?php echo esc_url( $form_action ); ?>">
					<?php wp_nonce_field( 'kc_ml_add_magic_link', '_wpnonce' ); ?>
					<input type="hidden" name="submitted" value="submitted" />

                    <!-- User -->
                    <div class="flex flex-row border-b border-gray-100">
                        <div class="flex w-1/5">
                            <div class="pt-6 ml-4">
                                <label for="tag-link"><span
                                            class="block pt-1 pb-2 pr-4 ml-4 text-sm font-medium text-gray-600"><?php esc_html_e( 'User', 'magic-link' ); ?></span></label>
                            </div>
                        </div>
                        <div class="flex">
                            <div class="h-10 mt-4 mb-4 ml-16 mr-4">
                                <div class="relative h-10">
                        <select name="form_data[user_id]" id="form_data_user_id" class="regular-text">
                            <option value="0"><?php esc_html_e( '-- Select User --', 'magic-link' ); ?></option>
                            <?php foreach ( $users as $user ) : ?>
                                <option value="<?php echo esc_attr( $user->ID ); ?>" <?php selected( $user_id, $user->ID ); ?>>
                                    <?php echo esc_html( $user->display_name . ' (' . $user->user_email . ')' ); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                                </div>
                            </div>
                        </div>
                    </div>


                    <!-- Token Lifespan -->
                    <div class="flex flex-row border-b border-gray-100">
                        <div class="flex w-1/5">
                            <div class="pt-6 ml-4">
                                <label for="tag-link"><span
                                            class="block pt-1 pb-2 pr-4 ml-4 text-sm font-medium text-gray-600"><?php esc_html_e( 'Token Lifespan', 'magic-link' ); ?></span></label>
                            </div>
                        </div>
                        <div class="flex">
                            <div class="h-10 mt-4 mb-4 ml-16 mr-4">
                                <div class="relative h-10">
                                    <input type="number" name="form_data[token_lifespan]" id="form_data_token_lifespan"
                                           value="<?php echo esc_attr( $token_lifespan ); ?>" min="1" class="small-text" />
                                    <select name="form_data[token_lifespan_unit]" id="form_data_token_lifespan_unit">
                                        <option value="minutes" <?php selected( $token_lifespan_unit, 'minutes' ); ?>><?php esc_html_e( 'Minutes', 'magic-link' ); ?></option>
                                        <option value="hours" <?php selected( $token_lifespan_unit, 'hours' ); ?>><?php esc_html_e( 'Hours', 'magic-link' ); ?></option>
                                        <option value="days" <?php selected( $token_lifespan_unit, 'days' ); ?>><?php esc_html_e( 'Days', 'magic-link' ); ?></option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Send Email to User -->
                    <div class="flex flex-row border-b border-gray-100">
                        <div class="flex w-1/5">
                            <div class="pt-6 ml-4">
                                <label for="form_data_send_email"><span
                                            class="block pt-1 pb-2 pr-4 ml-4 text-sm font-medium text-gray-600"><?php esc_html_e( 'Send Email to User', 'magic-link' ); ?></span></label>
                            </div>
                        </div>
                        <div class="flex">
                            <div class="h-10 mt-4 mb-4 ml-16 mr-4">
                                <div class="relative h-10 flex items-center">
                                    <input type="checkbox" name="form_data[send_email]" id="form_data_send_email" class="form-checkbox" value="1" checked="checked" />
                                    <span class="ml-2 text-sm text-gray-500"><?php esc_html_e( 'Send the magic link to the user via email', 'magic-link' ); ?></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <p class="submit"><input type="submit" name="submit" id=""
                                             class="px-4 py-2 ml-6 mr-2 align-middle cursor-pointer kc-us-primary-button"
                                             value="<?php echo $button_text; ?>"/>
                        <a href="admin.php?page=magic-link-links"
                                                                                     class="px-4 py-2 mx-2 my-2 text-sm font-medium leading-5 align-middle transition duration-150 ease-in-out border border-indigo-600 rounded-md cursor-pointer hover:shadow-md focus:outline-none focus:shadow-outline-indigo">
                            <?php
                            _e( 'Cancel',
                                    'magic-link' );
                            ?>
                        </a></p>

				</form>
                </div>
			</div>
		</div>
		<br class="clear">
	</div>
</div>
