<?php
use BLR\Backline\Core\Access;
use BLR\Backline\Core\Router;

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

$base = rtrim((string) ($ctx['base_url'] ?? Router::base_url()), '/') . '/';

// phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only redirect hint from the URL.
$redirect = sanitize_text_field((string) wp_unslash($_GET['redirect'] ?? 'dashboard'));
$redirect = trim(sanitize_text_field(wp_unslash($redirect)));
$redirect = ltrim($redirect, '/');
$redirect = preg_replace('~[^a-zA-Z0-9/_-]~', '', $redirect);
if ($redirect === '' || strpos($redirect, '..') !== false) {
    $redirect = 'dashboard';
}

$err = (string) ($ctx['login_error'] ?? '');
// phpcs:enable WordPress.Security.NonceVerification.Recommended
?>

<div class="card">
  <div class="card-body">
    <h1 class="h3 mb-3"><?php echo esc_html__('Backline', 'blr-backline'); ?></h1>
    <p class="text-muted mb-4"><?php echo esc_html__('Log in to access the dashboard.', 'blr-backline'); ?></p>

    <?php if ($err): ?>
      <div class="alert alert-danger"><?php echo esc_html($err); ?></div>
    <?php endif; ?>

    <form method="post" action="">
      <?php wp_nonce_field('blr_ba_login'); ?>

      <div class="mb-3">
        <label class="form-label"><?php echo esc_html__('Username or Email', 'blr-backline'); ?></label>
        <input class="form-control" type="text" name="log" autocomplete="username" required>
      </div>

      <div class="mb-3">
        <label class="form-label"><?php echo esc_html__('Password', 'blr-backline'); ?></label>
        <input class="form-control" type="password" name="pwd" autocomplete="current-password" required>
      </div>

      <div class="form-check mb-3">
        <input class="form-check-input" type="checkbox" value="1" id="rememberme" name="rememberme">
        <label class="form-check-label" for="rememberme"><?php echo esc_html__('Remember me', 'blr-backline'); ?></label>
      </div>

      <button class="btn btn-primary w-100" type="submit"><?php echo esc_html__('Log in', 'blr-backline'); ?></button>
    </form>

    <div class="mt-3 d-flex justify-content-between">
      <a class="text-muted" href="<?php echo esc_url(wp_lostpassword_url()); ?>"><?php echo esc_html__('Forgot password?', 'blr-backline'); ?></a>
      <a class="text-muted" href="<?php echo esc_url(home_url('/')); ?>"><?php echo esc_html__('Back to site', 'blr-backline'); ?></a>
    </div>
  </div>
</div>
