<?php
$pageTitle = 'Connect your database';
$dbOld = is_array($_SESSION['install_db_old'] ?? null)
    ? $_SESSION['install_db_old']
    : (is_array($dbSummary ?? null) ? $dbSummary : []);
$field = static fn(string $key, string $default = ''): string => htmlspecialchars((string)($dbOld[$key] ?? $default), 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="engine-banner">
                    <span class="engine-logo"><i class="bi bi-database"></i></span>
                    <div><strong>MySQL / MariaDB</strong><small>Central application database</small></div>
                    <span class="engine-ready"><i class="bi bi-check-circle"></i> PDO ready</span>
                </div>

                <div class="form-section-label"><span>Server</span><small>Where should PPPHP store its central data?</small></div>
                <div class="installer-fields connection-grid">
                    <div class="installer-field field-host">
                        <label for="dbHost">Host or server <b>*</b></label>
                        <div class="installer-input"><i class="bi bi-hdd-network"></i><input id="dbHost" type="text" name="db_host" value="<?= $field('host', '127.0.0.1') ?>" maxlength="190" autocomplete="off" placeholder="127.0.0.1" required></div>
                        <small>Use <code>127.0.0.1</code> when localhost sockets are unavailable.</small>
                    </div>
                    <div class="installer-field field-port">
                        <label for="dbPort">Port <b>*</b></label>
                        <div class="installer-input"><i class="bi bi-ethernet"></i><input id="dbPort" type="number" name="db_port" value="<?= $field('port', '3306') ?>" min="1" max="65535" inputmode="numeric" required></div>
                    </div>
                    <div class="installer-field field-database">
                        <label for="dbName">Database name <b>*</b></label>
                        <div class="installer-input"><i class="bi bi-database"></i><input id="dbName" type="text" name="db_name" value="<?= $field('dbname') ?>" maxlength="100" autocomplete="off" placeholder="pp_php_core" required></div>
                        <small>Create this empty database in your hosting panel first.</small>
                    </div>
                </div>

                <div class="form-section-label"><span>Credentials</span><small>A database user with table-creation permission.</small></div>
                <div class="installer-fields credential-grid">
                    <div class="installer-field">
                        <label for="dbUser">Database username <b>*</b></label>
                        <div class="installer-input"><i class="bi bi-person"></i><input id="dbUser" type="text" name="db_user" value="<?= $field('user') ?>" maxlength="100" autocomplete="username" required></div>
                    </div>
                    <div class="installer-field">
                        <div class="installer-label-row"><label for="dbPass">Database password</label><span>Stored server-side</span></div>
                        <div class="installer-input"><i class="bi bi-key"></i><input id="dbPass" type="password" name="db_pass" autocomplete="current-password"><button type="button" class="installer-password-toggle" data-password-toggle="#dbPass" aria-label="Show database password" aria-pressed="false"><i class="bi bi-eye"></i></button></div>
                    </div>
                </div>

                <div class="connection-preview" aria-live="polite">
                    <i class="bi bi-diagram-2"></i><div><small>Connection preview</small><strong id="connectionPreview">127.0.0.1:3306 / database</strong></div><span><i class="bi bi-lock"></i> Encrypted session</span>
                </div>

                <div class="installer-actions">
                    <span class="action-note"><i class="bi bi-info-circle"></i> The connection is tested before anything is written.</span>
                    <button type="submit" class="installer-primary"><span>Test connection &amp; continue</span><i class="bi bi-arrow-right"></i></button>
                </div>
            </form>
<?php require __DIR__ . '/layout_end.php'; ?>
