<?php
$pageTitle = 'Create the administrator';
$adminOld = is_array($_SESSION['install_admin_old'] ?? null) ? $_SESSION['install_admin_old'] : [];
$adminValue = static fn(string $key): string => htmlspecialchars((string)($adminOld[$key] ?? ''), ENT_QUOTES, 'UTF-8');
require __DIR__ . '/layout_start.php';
?>
            <form method="post" id="installerForm" class="installer-form">
                <input type="hidden" name="_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">

                <div class="connected-banner">
                    <span><i class="bi bi-check2-circle"></i></span>
                    <div><small>Database connected</small><strong><?= htmlspecialchars((string)($dbSummary['dbname'] ?? 'Central database'), ENT_QUOTES, 'UTF-8') ?></strong><em><?= htmlspecialchars((string)($dbSummary['host'] ?? ''), ENT_QUOTES, 'UTF-8') ?>:<?= htmlspecialchars((string)($dbSummary['port'] ?? '3306'), ENT_QUOTES, 'UTF-8') ?></em></div>
                    <button type="submit" name="installer_action" value="back" formnovalidate><i class="bi bi-pencil"></i> Change</button>
                </div>

                <div class="form-section-label"><span>Identity</span><small>This account receives full platform administration rights.</small></div>
                <div class="installer-fields admin-grid">
                    <div class="installer-field">
                        <label for="adminUser">Admin username <b>*</b></label>
                        <div class="installer-input"><i class="bi bi-person-badge"></i><input id="adminUser" type="text" name="admin_user" value="<?= $adminValue('username') ?>" minlength="3" maxlength="50" pattern="[a-zA-Z0-9_.-]+" autocomplete="username" placeholder="admin" required></div>
                        <small>Letters, numbers, dots, dashes, and underscores.</small>
                    </div>
                    <div class="installer-field">
                        <label for="adminEmail">Admin email <b>*</b></label>
                        <div class="installer-input"><i class="bi bi-envelope"></i><input id="adminEmail" type="email" name="admin_email" value="<?= $adminValue('email') ?>" maxlength="100" autocomplete="email" placeholder="admin@example.com" required></div>
                        <small>Used for account ownership and security notices.</small>
                    </div>
                </div>

                <div class="form-section-label"><span>Security</span><small>Choose a strong password for the platform owner.</small></div>
                <div class="installer-fields admin-grid">
                    <div class="installer-field">
                        <label for="adminPass">Password <b>*</b></label>
                        <div class="installer-input"><i class="bi bi-lock"></i><input id="adminPass" type="password" name="admin_pass" minlength="8" autocomplete="new-password" required><button type="button" class="installer-password-toggle" data-password-toggle="#adminPass" aria-label="Show administrator password" aria-pressed="false"><i class="bi bi-eye"></i></button></div>
                    </div>
                    <div class="installer-field">
                        <label for="adminPassConfirm">Confirm password <b>*</b></label>
                        <div class="installer-input"><i class="bi bi-shield-lock"></i><input id="adminPassConfirm" type="password" name="admin_pass_confirm" minlength="8" autocomplete="new-password" required><button type="button" class="installer-password-toggle" data-password-toggle="#adminPassConfirm" aria-label="Show password confirmation" aria-pressed="false"><i class="bi bi-eye"></i></button></div>
                        <small class="password-match" id="passwordMatch" aria-live="polite"></small>
                    </div>
                </div>

                <div class="password-quality">
                    <div class="quality-head"><span>Password strength</span><strong id="passwordStrengthLabel">Not entered</strong></div>
                    <div class="quality-track"><span id="passwordStrengthBar"></span></div>
                    <div class="password-rules" id="passwordRules">
                        <span data-rule="length"><i class="bi bi-circle"></i> 8+ characters</span>
                        <span data-rule="case"><i class="bi bi-circle"></i> Upper &amp; lower case</span>
                        <span data-rule="number"><i class="bi bi-circle"></i> A number</span>
                        <span data-rule="symbol"><i class="bi bi-circle"></i> A symbol</span>
                    </div>
                </div>

                <div class="completion-summary">
                    <i class="bi bi-stars"></i><div><strong>Ready to finish</strong><span>PPPHP will create the system tables, secure this account, write the database profile, and open the login page.</span></div>
                </div>

                <div class="installer-actions installer-actions-final">
                    <button type="submit" name="installer_action" value="back" class="installer-secondary" formnovalidate><i class="bi bi-arrow-left"></i><span>Back</span></button>
                    <button type="submit" class="installer-primary installer-complete"><span>Complete installation</span><i class="bi bi-check2-circle"></i></button>
                </div>
            </form>
<?php require __DIR__ . '/layout_end.php'; ?>
