<?php

require_once __DIR__ . '/functions.php';

$pdo = app_db_pdo();
$errors = [];
$values = [
    'tenant_name' => trim((string)($_POST['tenant_name'] ?? '')),
    'tenant_slug' => trim((string)($_POST['tenant_slug'] ?? '')),
    'display_name' => trim((string)($_POST['display_name'] ?? '')),
    'email' => trim((string)($_POST['email'] ?? '')),
];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    app_require_csrf();

    $result = saas_register_tenant_owner($pdo, [
        'tenant_name' => $values['tenant_name'],
        'tenant_slug' => $values['tenant_slug'],
        'display_name' => $values['display_name'],
        'email' => $values['email'],
        'password' => (string)($_POST['password'] ?? ''),
        'password_confirm' => (string)($_POST['password_confirm'] ?? ''),
    ]);

    if ($result['ok']) {
        saas_session_login($result['identity']);
        header('Location: konto.php?registered=1');
        exit;
    }

    $errors = $result['errors'];
}

include 'header.php';
include 'headerline.php';
include 'nav.php';
?>

<section id="banner">
    <div class="content">
        <h2>Registrierung</h2>

        <?php if ($errors !== []): ?>
            <div class="hint-box error">
                <?php foreach ($errors as $error): ?>
                    <p><?php echo saas_html($error); ?></p>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>

        <form method="post" action="register.php">
            <?php echo app_csrf_field(); ?>
            <div class="row">
                <div class="col-6 col-12-small">
                    <label for="tenant_name">Kundenname</label>
                    <input type="text" name="tenant_name" id="tenant_name" value="<?php echo saas_html($values['tenant_name']); ?>" required>
                </div>
                <div class="col-6 col-12-small">
                    <label for="tenant_slug">Kundenkuerzel</label>
                    <input type="text" name="tenant_slug" id="tenant_slug" value="<?php echo saas_html($values['tenant_slug']); ?>" placeholder="kaffeeliste-team" required>
                </div>
                <div class="col-6 col-12-small">
                    <label for="display_name">Dein Name</label>
                    <input type="text" name="display_name" id="display_name" value="<?php echo saas_html($values['display_name']); ?>" required>
                </div>
                <div class="col-6 col-12-small">
                    <label for="email">E-Mail</label>
                    <input type="email" name="email" id="email" value="<?php echo saas_html($values['email']); ?>" required>
                </div>
                <div class="col-6 col-12-small">
                    <label for="password">Passwort</label>
                    <input type="password" name="password" id="password" minlength="8" required>
                </div>
                <div class="col-6 col-12-small">
                    <label for="password_confirm">Passwort wiederholen</label>
                    <input type="password" name="password_confirm" id="password_confirm" minlength="8" required>
                </div>
            </div>
            <ul class="actions">
                <li><button type="submit">Kundenkonto anlegen</button></li>
                <li><a href="login.php" class="button alt">Zum Login</a></li>
            </ul>
        </form>
    </div>
</section>

<?php include 'footer.php'; ?>
