<?php
if (isset($_POST['username'])) {
    _selectRowNoParam(
        "SELECT id FROM teacher WHERE email='" . $_POST['username'] . "'",
        $rid
    );
    if (!empty($rid)) {
        $pass = rand(1111, 9999);

        $success = _exec(
            "UPDATE teacher SET pass = ? WHERE id = ?",
            'ss',
            [password_hash($pass, PASSWORD_BCRYPT, ["cost" => 8]), $rid],
            $count
        );
        $to = $_POST['username'];
        $subject = $pageTitle . ' - Нууц үг сэргээлээ';
        $message = 'Сайн байна уу! Нууц үг сэргээлээ. Таны нууц үг: ' . $pass;
        $headers = 'From: password@surgalt.uvcollege.edu.mn' . "\r\n" .
            'Reply-To: password@surgalt.uvcollege.edu.mn' . "\r\n";

        if (mail($to, $subject, $message, $headers)) {
            $_SESSION['errors'] = [$_POST['username']  . ' цахим шууданд нууц үг илгээлээ! Цахим шуудангаа шалгаад нэвтэрч орно уу! SPAM фолдероо шалгаж үзэхээ мартуузай'];
            redirect('/login');
            exit;
        } else {
            $_SESSION['errors'] = ['Имэйл илгээхэд алдаа гарлаа. Дахин оролдоно уу!'];
        }
    } else {
        $_SESSION['errors'] = [$_POST['username']  . ' цахим шуудан бүртгэлээс олдсонгүй!'];
    }
}
?>

<!DOCTYPE html>
<html lang="zxx">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="keyword" content="">
    <meta name="author" content="theme_ocean">
    <title><?= $pageTitle ?></title>
    <link rel="shortcut icon" type="image/x-icon" href="<?= $favi ?>">
    <link rel="stylesheet" type="text/css" href="/assets/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="/assets/vendors/css/vendors.min.css">
    <link rel="stylesheet" type="text/css" href="/assets/css/theme.min.css">
</head>

<body>
    <main class="auth-cover-wrapper">
        <div class="auth-cover-content-inner">
            <div class="auth-cover-content-wrapper">
                <div class="auth-img">
                    <img src="/images/reset-pass.svg" alt="" class="img-fluid">
                </div>
            </div>
        </div>
        <div class="auth-cover-sidebar-inner">
            <div class="auth-cover-card-wrapper">
                <div class="auth-cover-card p-sm-5">
                    <div class="wd-100 mb-5">
                        <img src="/images/logo.jpg" alt="" class="img-fluid">
                    </div>
                    <h2 class="fs-20 fw-bolder mb-4">Нууц үг сэргээх</h2>
                    <form class="w-100 mt-4 pt-2" method="post">
                        <div class="mb-4">
                            <input class="form-control" name="username" type="email" placeholder="Бүртгэлтэй цахим шуудангаа оруулна уу!" required>
                        </div>
                        <?php if (!empty($_SESSION['errors'])) : ?>
                            <div class="mb-3">
                                <ul>
                                    <?php foreach ($_SESSION['errors'] as $error) : ?>
                                        <li style="color:red; font-size: 12px;"><?= $error ?></li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        <?php unset($_SESSION['errors']);
                        endif; ?>
                        <div class="mt-5">
                            <button type="submit" class="btn btn-lg btn-primary w-100">СЭРГЭЭХ</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
</body>

</html>