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

if (isset($_GET['token'])) {
    $token = $_GET['token'];
    $result = mysqli_query($conn, "SELECT * FROM customer WHERE reset_token='$token'");
    $row = mysqli_fetch_assoc($result);

    if ($row && strtotime($row['reset_expiry']) > time()) {
        if (isset($_POST['submit'])) {
            $password = mysqli_real_escape_string($conn, $_POST['password']);
            $hash = password_hash($password, PASSWORD_DEFAULT);

            mysqli_query($conn, "UPDATE customer SET password='$hash', reset_token=NULL, reset_expiry=NULL WHERE reset_token='$token'");
            echo "<script>alert('Password berhasil diubah!'); window.location='user_login.php';</script>";
        }
    } else {
        echo "<script>alert('Link tidak valid atau sudah kedaluwarsa!'); window.location='forgot_password.php';</script>";
        exit;
    }
} else {
    header('Location: forgot_password.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Reset Password</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f6f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .card {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 10px rgba(0,0,0,0.1);
            width: 350px;
        }
        input {
            width: 100%;
            padding: 10px;
            margin-top: 10px;
            border-radius: 6px;
            border: 1px solid #ccc;
        }
        button {
            background: #28a745;
            color: white;
            border: none;
            padding: 10px;
            border-radius: 6px;
            margin-top: 15px;
            width: 100%;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="card">
        <h3>Atur Password Baru</h3>
        <form method="POST">
            <input type="password" name="password" placeholder="Masukkan Password Baru" required>
            <button type="submit" name="submit">Simpan Password</button>
        </form>
    </div>
</body>
</html>
