<?php
/*
Template Name: Shop Explorer Dashboard
*/

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

$error_message = '';
if ( isset( $_POST['se_login_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['se_login_nonce'] ) ), 'se_login_action' ) ) {
	if ( isset( $_POST['log'], $_POST['pwd'] ) ) {
		$creds = [
			'user_login'    => sanitize_user( wp_unslash( $_POST['log'] ) ),
			'user_password' => sanitize_text_field( wp_unslash( $_POST['pwd'] ) ),
			'remember'      => isset( $_POST['rememberme'] ),
		];

		$user = wp_signon( $creds, false );

		if ( is_wp_error( $user ) ) {
			$error_message = $user->get_error_message();
		} else {
			wp_safe_redirect( home_url( '/shop-explorer-dashboard' ) );
			exit;
		}
	}
}

?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
	<meta charset="<?php bloginfo( 'charset' ); ?>">
	<?php wp_head(); ?>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
</head>
<body>
	<?php
	if ( is_page_template( 'templates/shop-explorer-dashboard.php' ) ) {
		if ( is_user_logged_in() ) {
			$user_obj       = wp_get_current_user();
			$roles_to_check = [ 'administrator', 'shop_manager', 'editor' ];
			$has_permission = false;

			foreach ( $roles_to_check as $check_role ) {
				if ( in_array( $check_role, $user_obj->roles, true ) ) {
					$has_permission = true;
					break;
				}
			}

			if ( ! $has_permission ) {
				echo '<div class="error-message">';
				echo esc_html__( "You don't have the permission to access this page", 'shop-explorer' );
				echo '</div>';
				wp_die();
			}
			?>
			<div id="shopexplore_dashboard"></div>
			<?php
		} else {
			?>
			<div class="shop-explorer-login-form-wrapper">
				<div class="shop-explorer-login-form">
					<h2><?php echo esc_html__( 'Please log in to access the dashboard', 'shop-explorer' ); ?></h2>
					<?php
					if ( $error_message ) {
						echo '<div class="error-message">' . wp_kses_post( $error_message ) . '</div>';
					}

					$current_url = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
					?>
					<form name="loginform" id="loginform" action="<?php echo esc_url( $current_url ); ?>" method="post">
						<?php wp_nonce_field( 'se_login_action', 'se_login_nonce' ); ?>
						<p class="login-container">
							<label for="user_login"><?php esc_html_e( 'Username or Email Address', 'shop-explorer' ); ?></label>
							<input type="text" name="log" id="user_login" class="login-input" value="<?php echo esc_attr( isset( $_POST['log'] ) ? sanitize_user( wp_unslash( $_POST['log'] ) ) : '' ); ?>" size="20" autocapitalize="off" autocomplete="username" required>
						</p>
						<p class="login-container">
							<label for="user_pass"><?php esc_html_e( 'Password', 'shop-explorer' ); ?></label>
							<input type="password" name="pwd" id="user_pass" class="login-input" value="" size="20" autocomplete="current-password" required>
						</p>
						<p class="login-remember">
							<label>
								<input name="rememberme" type="checkbox" id="rememberme" value="forever">
								<?php esc_html_e( 'Remember Me', 'shop-explorer' ); ?>
							</label>
						</p>
						<p class="login-submit">
							<input type="submit" name="wp-submit" id="wp-submit" class="login-button" value="<?php esc_attr_e( 'Log In', 'shop-explorer' ); ?>">
							<input type="hidden" name="redirect_to" value="<?php echo esc_url( home_url( '/shop-explorer-dashboard' ) ); ?>">
						</p>
					</form>
					<p class="login-help">
						<a href="<?php echo esc_url( wp_lostpassword_url() ); ?>"><?php echo esc_html__( 'Forgot your password?', 'shop-explorer' ); ?></a>
					</p>
				</div>
			</div>
			<?php
		}
	}
	wp_footer();
	?>
</body>
</html>
