<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>EcoBot — Forgot Password</title>
    <link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="/admin/css/style.css">
    <style>
        .info-box {
            background: #e8f5e9;
            border-left: 4px solid #28a745;
            padding: 14px 16px;
            border-radius: 4px;
            margin-bottom: 22px;
            color: #2e7d32;
            font-size: 14px;
            line-height: 1.6;
        }
        .back-link {
            display: block;
            text-align: center;
            font-size: 13px;
            color: #888;
            text-decoration: none;
            margin-top: 18px;
        }
        .back-link:hover { color: #28a745; }
        .login-subtitle {
            color: #888;
            font-size: 13px;
            text-align: center;
            margin-top: 4px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-box">
            <div class="logo">
                <h1>EcoBot</h1>
                <p class="login-subtitle">Forgot Password</p>
            </div>

            <div id="sentBox" class="info-box" style="display:none;">
                <span class="material-icons" style="font-size:16px;vertical-align:middle;margin-right:6px;">mark_email_read</span>
                If that email is registered, you'll receive a reset link shortly. Check your inbox (and spam folder).
            </div>

            <div id="errorMsg" class="error-message" style="display:none;"></div>

            <form id="forgotForm" class="login-form">
                <div class="form-group">
                    <label for="email">Email</label>
                    <input type="email" id="email" name="email" placeholder="your@email.com" required autocomplete="email">
                </div>
                <button type="submit" class="btn-login">Send Reset Link</button>
            </form>

            <a href="/admin/login" class="back-link">
                <span class="material-icons" style="font-size:14px;vertical-align:middle;">arrow_back</span>
                Back to login
            </a>
        </div>
    </div>

    <script>
        document.getElementById('forgotForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const email = document.getElementById('email').value;
            const btn = this.querySelector('button');
            btn.disabled = true;
            btn.textContent = 'Sending...';

            fetch('/admin/api/forgot-password', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ email })
            })
            .then(res => res.json())
            .then(() => {
                document.getElementById('forgotForm').style.display = 'none';
                document.getElementById('sentBox').style.display = 'block';
            })
            .catch(() => {
                const errorMsg = document.getElementById('errorMsg');
                errorMsg.textContent = 'Connection error. Please try again.';
                errorMsg.style.display = 'block';
                btn.disabled = false;
                btn.textContent = 'Send Reset Link';
            });
        });
    </script>
</body>
</html>
