<?php
/**
 * Forgot Password Page
 * CMTC Shopping
 */
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/functions/helpers.php';

$database = new Database();
$db = $database->getConnection();

$error = "";
$success = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['reset'])) {
    $email = trim($_POST['email'] ?? '');
    
    if (empty($email)) {
        $error = "กรุณากรอกอีเมลของคุณ";
    } else {
        $stmt = $db->prepare("SELECT * FROM users WHERE email = ? LIMIT 1");
        $stmt->execute([$email]);
        $user = $stmt->fetch();
        
        if ($user) {
            // Log recovery request
            log_activity($db, 'edit', "ขอรหัสผ่านใหม่สำหรับอีเมล: $email");
            
            // Mock OTP/Recovery link
            $success = "ระบบได้ทำการส่งลิงก์กู้คืนรหัสผ่านไปยังอีเมล $email เรียบร้อยแล้ว (การจำลอง: รหัสผ่านของคุณถูกรีเซ็ตเป็น '123456' ชั่วคราว กรุณาใช้รหัสผ่านนี้เพื่อล็อกอินและเปลี่ยนทันที)";
            
            // Update password to 123456 for convenience in test environment
            $new_pass = password_hash('123456', PASSWORD_BCRYPT);
            $stmt_upd = $db->prepare("UPDATE users SET password = ? WHERE id = ?");
            $stmt_upd->execute([$new_pass, $user['id']]);
        } else {
            $error = "ไม่พบข้อมูลผู้ใช้นี้ในระบบ";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="th" data-bs-theme="light">
<head>
    <meta charset="UTF-8">
    <title>ลืมรหัสผ่าน - CMTC Shopping</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Sarabun', sans-serif;
        }
        .forgot-card {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(16px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            padding: 40px;
            width: 100%;
            max-width: 450px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
            color: #ffffff;
        }
        .form-control {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        .form-control:focus {
            background: rgba(255, 255, 255, 0.15);
            border-color: #0d6efd;
            color: #fff;
            box-shadow: none;
        }
    </style>
</head>
<body>
    <div class="forgot-card">
        <h4 class="text-center mb-4"><i class="bi bi-key text-primary"></i> ลืมรหัสผ่าน</h4>
        
        <?php if (!empty($error)): ?>
            <div class="alert alert-danger py-2 small" role="alert">
                <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <?php if (!empty($success)): ?>
            <div class="alert alert-success py-2 small" role="alert">
                <?php echo $success; ?>
            </div>
            <div class="text-center mt-3">
                <a href="login.php" class="btn btn-primary px-4 py-2 w-100">กลับหน้าล็อกอิน</a>
            </div>
        <?php else: ?>
            <p class="text-muted text-center small mb-4">กรอกที่อยู่อีเมลที่ผูกกับบัญชีผู้ใช้ของคุณเพื่อทำการกู้คืนรหัสผ่าน</p>
            <form method="POST">
                <div class="mb-4">
                    <label for="email" class="form-label small">ที่อยู่อีเมล</label>
                    <input type="email" name="email" id="email" class="form-control" required placeholder="example@email.com">
                </div>

                <button type="submit" name="reset" class="btn btn-primary w-100 py-2.5 mb-3">กู้คืนรหัสผ่าน</button>
                
                <div class="text-center small">
                    <a href="login.php" class="text-primary text-decoration-none">กลับไปหน้าเข้าสู่ระบบ</a>
                </div>
            </form>
        <?php endif; ?>
    </div>
</body>
</html>
