<?php
include 'header.php';
include 'koneksi/koneksi.php';

// Tambahkan timezone supaya waktu sesuai (Jakarta)
date_default_timezone_set('Asia/Jakarta');

if (isset($_POST['submit'])) {
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $result = mysqli_query($conn, "SELECT * FROM customer WHERE email='$email'");
    $row = mysqli_fetch_assoc($result);

    if ($row) {
        $token = bin2hex(random_bytes(16));
        $expiry = date("Y-m-d H:i:s", strtotime("+30 minutes"));
        mysqli_query($conn, "UPDATE customer SET reset_token='$token', reset_expiry='$expiry' WHERE email='$email'");

        $reset_link = "http://localhost/kasir/reset_password.php?token=$token";

        $subject = "Reset Password - UMKM Galeri 3D";
        $message = "
        Halo {$row['username']},<br><br>
        Klik link berikut untuk mengganti password Anda:<br>
        <a href='$reset_link'>$reset_link</a><br><br>
        Link ini berlaku selama 30 menit.<br><br>
        Salam,<br><b>UMKM Galeri 3D</b>
        ";
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= "From: UMKM Galeri 3D <no-reply@galeri3d.com>" . "\r\n";

        if (mail($email, $subject, $message, $headers)) {
            echo "<script>alert('Link reset password telah dikirim ke email Anda.'); window.location='user_login.php';</script>";
        } else {
            echo "<script>alert('Gagal mengirim email.'); window.location='forgot_password.php';</script>";
        }
    } else {
        echo "<script>alert('Email tidak terdaftar!'); window.location='forgot_password.php';</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Lupa Password - UMKM Galeri 3D</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        body {
            font-family: 'Poppins', sans-serif;
            background-color: #ffffff;
            margin: 0;
            height: 90vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            color: #333;
        }

        .container {
            width: 100%;
            max-width: 400px;
            text-align: center;
        }

        h3 {
            margin-bottom: 8px;
            color: #333;
            font-weight: 600;
        }

        p {
            font-size: 14px;
            color: #555;
            margin-bottom: 25px;
        }

        input[type="email"] {
            width: 100%;
            padding: 12px 14px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 14px;
            outline: none;
            color: #444;
            background-color: #fff;
            transition: border-color 0.3s ease;
        }

        input[type="email"]:focus {
            border-color: #ff7b00;
        }

        button {
            width: 100%;
            background: linear-gradient(90deg, #ff7b00, #ff9a2f);
            border: none;
            padding: 12px;
            border-radius: 8px;
            color: #fff;
            font-weight: 600;
            font-size: 15px;
            cursor: pointer;
            transition: background 0.3s ease;
            margin-top: 15px;
        }

        button:hover {
            background: linear-gradient(90deg, #ff9e32, #ffa94d);
        }

        .footer-text {
            margin-top: 25px;
            font-size: 12px;
            color: #666;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="container">
        <h3>Lupa Password</h3>
        <p>Masukkan email Anda untuk menerima link reset password.</p>

        <form method="POST">
            <input type="email" name="email" placeholder="Masukkan Email Terdaftar" required>
            <button type="submit" name="submit">Kirim Link Reset</button>
        </form>

        <a href="user_login.php"><i class="fas fa-arrow-left" style="margin-top: 30px;"></i> Kembali ke Login</a>
        <p class="footer-text">© 2025 Galeri 3D — UMKM Kreatif Indonesia</p>
    </div>
</body>
</html>
