<?php
/*
 * pages/lost.php
 * PHP logic: 100% INTACT - copiat exact din CMS, doar HTML inlocuit cu Nostalgia2
 * HTML: design Nostalgia2
 */
?>

<!-- Header -->
<nav class="navbar navbar-dark bg-news-header mb-5">
    <div class="container-fluid">
        <h4 class="h4 small m-0 text-uppercase">
            <?php echo ($message == 7) ? strtoupper($lang['change-password']) : strtoupper($lang['account-recovery']); ?>
        </h4>
    </div>
</nav>

<div class="main_board_darker_background p-4">
<form role="form" method="post" action="">

<?php
// ---- LOGICA PHP 100% INTACT din CMS original ----
if(isset($_GET['email']) && isset($_GET['code']) && !empty($_GET['email']) && !empty($_GET['code']) && isValidEmail($_GET['email'])) {
    if($message == 6) {
        echo '<div class="alert alert-danger alert-dismissible fade show mb-3">
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            '.$lang['incorrect-recovery'].'</div>';
    } else if(isset($_POST['password']) && isset($_POST['rpassword']) && $message == 9) {
        $message = 7;
        echo '<div class="alert alert-danger alert-dismissible fade show mb-3">
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            '.$lang['no-password-r'].'</div>';
    } else if(isset($_POST['password']) && isset($_POST['rpassword']) && $message == 8) {
        $message = 11;
        echo '<div class="alert alert-success alert-dismissible fade show mb-3">
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            '.$lang['success-change-password'].'</div>';
    } else if(isset($_POST['password']) && isset($_POST['rpassword']) && $message == 10) {
        $message = 7;
        echo '<div class="alert alert-danger alert-dismissible fade show mb-3">
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            '.$lang['incorrect-password'].'</div>';
    }
} else if(isset($_POST['username']) && isset($_POST['email']) && isset($_POST['captcha'])) {
    if($message == 5) {
        echo '<div class="alert alert-danger alert-dismissible fade show mb-3">
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            '.$lang['incorrect-security'].'</div>';
    } else {
        echo '<div class="alert alert-info alert-dismissible fade show mb-3">
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            '.$lang['email-recovery'].'</div>';
        if($message == 1) {
            $alt_message = $lang['code-delete-chars'];
            $subject     = $lang['account-recovery'];
            $sendName    = $_POST['username'];
            $sendEmail   = $_POST['email'];
            $code        = generateSocialID(32);
            update_passlost_token($_POST['username'], $code);
            $html_mail   = recoveryPassword($code, $_POST['email'], $_POST['username']);
            include 'include/functions/sendEmail.php';
        }
    }
}

require 'include/captcha/simple.php';
$_SESSION['captcha_lost'] = simple_php_captcha();

if($message != 11) { ?>

<div class="col-7 mx-auto">
    <?php if($message == 7) { ?>
    <!-- Schimba parola -->
    <div class="left-inner-addon input-container mt-2">
        <img src="<?php echo $site_url; ?>assets/img/icon-pass.png" alt="">
        <input name="password" id="password" type="password"
            class="form-control-input form-control form-input text-white small"
            placeholder="<?php echo $lang['password']; ?>"
            pattern=".{5,16}" maxlength="16" required>
    </div>
    <div class="left-inner-addon input-container mt-3">
        <img src="<?php echo $site_url; ?>assets/img/icon-pass.png" alt="">
        <input name="rpassword" id="rpassword" type="password"
            class="form-control-input form-control form-input text-white small"
            placeholder="<?php echo $lang['rpassword']; ?>"
            pattern=".{5,16}" maxlength="16" required>
    </div>
    <p class="text-danger small" id="checkpassword"></p>

    <?php } else { ?>
    <!-- Recuperare cont -->
    <div class="left-inner-addon input-container mt-2">
        <img src="<?php echo $site_url; ?>assets/img/icon-user.png" alt="">
        <input name="username" type="text"
            class="form-control-input form-control form-input text-white small"
            placeholder="<?php echo $lang['user-name']; ?>..."
            pattern=".{5,16}" maxlength="16" autocomplete="off" required>
    </div>
    <div class="left-inner-addon input-container mt-3">
        <input name="email" type="email"
            class="form-control-input form-control form-input text-white small"
            placeholder="<?php echo $lang['email-address']; ?>"
            pattern=".{7,64}" maxlength="64" required>
    </div>
    <div class="d-flex align-items-center gap-3 mt-3">
        <img src="<?php echo $site_url; ?>include/captcha/simple.php<?php echo $_SESSION['captcha_lost']['image_src']; ?>" alt="captcha">
        <input name="captcha" type="text"
            class="form-control-input form-control form-input text-white"
            placeholder="<?php echo $lang['captcha-code']; ?>"
            style="height:70px; font-size:30px;"
            pattern=".{4,6}" maxlength="5" required>
    </div>
    <?php } ?>
</div>

<div class="sider-btn mt-3">
    <button class="my-4 mx-auto text-center sign-in-button btn bg-button small fw-bold"
        type="submit" style="display:block;">
        <?php echo ($message == 7) ? $lang['change-password'] : $lang['account-recovery']; ?>
    </button>
</div>

<?php } ?>

</form>
</div>
