<?php

/**
 * @var string $stage
 * @var string $email
 * @var string $tenantHint
 * @var array $tenantCandidates
 * @var int $selectedTenantId
 * @var array|null $selectedTenant
 * @var array $selectedTenantMethods
 */

use MintyPHP\Buffer;
use MintyPHP\Session;

Buffer::set('title', t('Login'));

$stage = (string) ($stage ?? 'resolve_email');
$email = trim((string) ($email ?? ''));
$tenantHint = trim((string) ($tenantHint ?? ''));
$tenantCandidates = is_array($tenantCandidates ?? null) ? $tenantCandidates : [];
$selectedTenantId = (int) ($selectedTenantId ?? 0);
$selectedTenant = is_array($selectedTenant ?? null) ? $selectedTenant : null;
$selectedTenantMethods = is_array($selectedTenantMethods ?? null) ? $selectedTenantMethods : ['local' => false, 'microsoft' => false];
$selectedTenantSlug = (string) ($selectedTenant['slug'] ?? '');
$loginHeading = t('Login');
$loginSubheading = t('Please enter your credentials');
if ($stage === 'resolve_email') {
    $loginHeading = t('Login');
    $loginSubheading = t('Enter your email to continue');
} elseif ($stage === 'choose_tenant') {
    $loginHeading = t('Select tenant');
    $loginSubheading = t('Select your tenant to continue');
} else {
    $loginHeading = t('Login options');
    $loginSubheading = t('Choose how you want to sign in');
}
?>

<article>
    <header>
        <hgroup>
            <h1><?php e($loginHeading); ?></h1>
            <p><?php e($loginSubheading); ?></p>
        </hgroup>
    </header>

    <?php if ($stage === 'resolve_email'): ?>
        <form method="post">
            <input type="hidden" name="stage" value="resolve_email">
            <input type="hidden" name="tenant_hint" value="<?php e($tenantHint); ?>">
            <label for="email">
                <span><?php e(t('Email')); ?></span>
                <input required type="email" name="email" id="email" value="<?php e($email); ?>" autocomplete="email" autofocus />
            </label>
            <p>
                <button type="submit"><?php e(t('Continue')); ?></button>
            </p>
            <?php Session::getCsrfInput(); ?>
        </form>
    <?php elseif ($stage === 'choose_tenant'): ?>
        <form method="post" class="login-tenant-select-form">
            <input type="hidden" name="stage" value="choose_tenant">
            <input type="hidden" name="email" value="<?php e($email); ?>">
            <input type="hidden" name="tenant_hint" value="<?php e($tenantHint); ?>">
            <div class="login-tenant-choice-grid" role="radiogroup"
                aria-label="<?php e(t('Which tenant do you want to sign in to?')); ?>">
                <?php foreach ($tenantCandidates as $tenantCandidate): ?>
                    <?php
                    $tenantId = (int) ($tenantCandidate['id'] ?? 0);
                    if ($tenantId <= 0) {
                        continue;
                    }
                    $tenantLabel = (string) ($tenantCandidate['description'] ?? '');
                    $tenantAvatarUrl = (string) ($tenantCandidate['avatar_url'] ?? '');
                    $tenantInitial = strtoupper(substr(trim($tenantLabel), 0, 1));
                    if ($tenantInitial === '') {
                        $tenantInitial = '?';
                    }
                    ?>
                    <label class="app-field login-tenant-choice" data-tooltip="<?php e($tenantLabel); ?>" data-tooltip-pos="top">
                        <input type="radio" name="tenant_id" value="<?php e((string) $tenantId); ?>" <?php e($tenantId === $selectedTenantId ? 'checked autofocus' : ''); ?> aria-label="<?php e($tenantLabel); ?>">
                        <span class="login-tenant-choice-body">
                            <?php if ($tenantAvatarUrl !== ''): ?>
                                <img class="login-tenant-choice-avatar" src="<?php e($tenantAvatarUrl); ?>" alt="" loading="lazy">
                            <?php else: ?>
                                <span class="login-tenant-choice-avatar-placeholder"><?php e($tenantInitial); ?></span>
                            <?php endif; ?>
                        </span>
                    </label>
                <?php endforeach; ?>
            </div>
            <p>
                <button type="submit"><?php e(t('Continue')); ?></button>
            </p>
            <?php Session::getCsrfInput(); ?>
        </form>
        <form method="post">
            <input type="hidden" name="stage" value="reset">
            <p>
                <button type="submit" class="secondary outline"><?php e(t('Back')); ?></button>
            </p>
            <?php Session::getCsrfInput(); ?>
        </form>
    <?php else: ?>
        <?php if (!empty($selectedTenantMethods['local'])): ?>
            <form method="post">
                <input type="hidden" name="stage" value="login_password">
                <input type="hidden" name="email" value="<?php e($email); ?>">
                <input type="hidden" name="tenant_hint" value="<?php e($tenantHint); ?>">
                <input type="hidden" name="tenant_id" value="<?php e((string) $selectedTenantId); ?>">
                <label for="password">
                    <span><?php e(t('Password')); ?></span>
                    <input required type="password" name="password" id="password" autocomplete="current-password" />
                </label>
                <p>
                    <label class="app-field inline">
                        <input type="checkbox" name="remember" value="1">
                        <span><?php e(t('Remember me')); ?></span>
                    </label>
                </p>
                <p>
                    <button type="submit"><?php e(t('Login')); ?></button>
                </p>
                <p>
                    <a href="<?php e(lurl('password/forgot')); ?>"><?php e(t('Forgot password')); ?>?</a>
                </p>
                <?php Session::getCsrfInput(); ?>
            </form>
        <?php endif; ?>

        <?php if (!empty($selectedTenantMethods['local']) && !empty($selectedTenantMethods['microsoft'])): ?>
            <div class="login-alt-separator" aria-hidden="true">
                <span><?php e(t('Or')); ?></span>
            </div>
        <?php endif; ?>

        <?php if (!empty($selectedTenantMethods['microsoft']) && $selectedTenantSlug !== ''): ?>
            <a role="button" style="width:100%;" class="primary"
                href="<?php e(lurl('auth/microsoft/start?tenant=' . rawurlencode($selectedTenantSlug))); ?>">
                <i class="bi bi-microsoft"></i> <?php e(t('Sign in with Microsoft')); ?>
            </a>
        <?php endif; ?>

        <?php if (empty($selectedTenantMethods['local']) && empty($selectedTenantMethods['microsoft'])): ?>
            <div class="notice" data-variant="warning">
                <?php e(t('No login method is available for this tenant')); ?>
            </div>
        <?php endif; ?>
        <hr>
        <form method="post">
            <input type="hidden" name="stage" value="resolve_email">
            <input type="hidden" name="email" value="<?php e($email); ?>">
            <input type="hidden" name="tenant_hint" value="<?php e($tenantHint); ?>">
            <p>
                <button type="submit" class="secondary outline"><?php e(t('Back')); ?></button>
            </p>
            <?php Session::getCsrfInput(); ?>
        </form>
    <?php endif; ?>

    <?php if (allowRegistration()): ?>
        <hr>
        <p class="text-align-center"><?php e(t('No account yet')); ?>? <a href="<?php e(lurl('register')); ?>"><?php e(t('Register')); ?></a></p>
    <?php endif; ?>
</article>
