<?php
// ============================================================
// FILE: reset_password.php
// Buat/reset akun admin langsung di server Anda.
// ⚠️  HAPUS file ini setelah digunakan!
// Akses: http://localhost/budijaya_ecommerce/reset_password.php
// ============================================================
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/db.php';

$done = false;
$info = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pdo      = db();
    $password = $_POST['password'] ?? 'admin123';
    $nama     = trim($_POST['nama']  ?? 'Pak Budi');
    $email    = trim($_POST['email'] ?? 'admin@budijaya.com');

    // Generate hash di server INI (bukan hash pre-made)
    $hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);

    // Cek apakah akun sudah ada
    $existing = $pdo->prepare("SELECT id FROM users WHERE email = ? LIMIT 1");
    $existing->execute([$email]);
    $existing = $existing->fetch();

    if ($existing) {
        $pdo->prepare("UPDATE users SET password = ?, nama = ?, role = 'admin' WHERE email = ?")
            ->execute([$hash, $nama, $email]);
        $info[] = "✅ Password akun <strong>$email</strong> berhasil di-reset!";
    } else {
        $pdo->prepare("INSERT INTO users (nama, email, password, role) VALUES (?, ?, ?, 'admin')")
            ->execute([$nama, $email, $hash]);
        $info[] = "✅ Akun admin baru <strong>$email</strong> berhasil dibuat!";
    }

    // Verifikasi langsung
    $check = $pdo->prepare("SELECT password FROM users WHERE email = ?");
    $check->execute([$email]);
    $row   = $check->fetch();

    if (password_verify($password, $row['password'])) {
        $info[] = "✅ Verifikasi berhasil — password cocok dengan hash baru.";
        $done = true;
    } else {
        $info[] = "❌ Verifikasi GAGAL. Ada masalah dengan fungsi password_hash di server ini.";
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <title>Reset Password Admin</title>
  <style>
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body {
      font-family: 'Segoe UI', sans-serif;
      background: #F5EDE2;
      min-height: 100vh;
      display: flex; align-items: center; justify-content: center;
      padding: 2rem;
    }
    .card {
      background: white; border-radius: 16px;
      padding: 2.5rem; max-width: 440px; width: 100%;
      box-shadow: 0 8px 32px rgba(139,94,60,.15);
    }
    h2 { color: #2D1B0E; margin-bottom: .25rem; font-size: 1.4rem; }
    .sub { color: #92756A; font-size: .85rem; margin-bottom: 1.75rem; }
    label { display: block; font-weight: 700; font-size: .82rem; color: #3D2B1F; margin-bottom: .35rem; }
    input {
      width: 100%; padding: .65rem .9rem;
      border: 1.5px solid #E8D9CC; border-radius: 8px;
      font-size: .95rem; margin-bottom: 1rem;
      font-family: inherit;
    }
    input:focus { outline: none; border-color: #8B5E3C; }
    button {
      width: 100%; padding: .8rem;
      background: #8B5E3C; color: white;
      border: none; border-radius: 10px;
      font-size: 1rem; font-weight: 700; cursor: pointer;
    }
    button:hover { background: #6B4526; }
    .msg { padding: .7rem 1rem; border-radius: 8px; margin-bottom: .75rem; font-size: .9rem; }
    .msg-ok  { background: #DCFCE7; color: #166534; border: 1px solid #86EFAC; }
    .msg-err { background: #FEE2E2; color: #991B1B; border: 1px solid #FCA5A5; }
    .login-btn {
      display: block; text-align: center; margin-top: 1.25rem;
      background: #10B981; color: white; padding: .75rem;
      border-radius: 10px; text-decoration: none; font-weight: 700;
    }
    .warn {
      background: #FEF9C3; border: 1px solid #FDE047;
      border-radius: 8px; padding: .75rem 1rem;
      font-size: .8rem; color: #713F12; margin-top: 1.25rem;
    }
    .divider { border: none; border-top: 1px solid #EDD9C4; margin: 1.5rem 0; }
  </style>
</head>
<body>
<div class="card">
  <h2>🔧 Reset Password Admin</h2>
  <p class="sub">Buat atau reset akun admin langsung di server ini.</p>

  <?php foreach ($info as $i): ?>
    <div class="msg <?= str_starts_with($i,'✅') ? 'msg-ok' : 'msg-err' ?>"><?= $i ?></div>
  <?php endforeach; ?>

  <?php if (!$done): ?>
    <form method="POST">
      <label for="fNama">Nama Admin</label>
      <input type="text" id="fNama" name="nama" value="Pak Budi" required>

      <label for="fEmail">Email Login</label>
      <input type="email" id="fEmail" name="email" value="admin@budijaya.com" required>

      <label for="fPass">Password Baru</label>
      <input type="text" id="fPass" name="password" value="admin123" required>

      <button type="submit">🔑 Buat / Reset Akun Admin</button>
    </form>
  <?php else: ?>
    <a href="<?= BASE_URL ?>/login.php" class="login-btn">→ Login Sekarang</a>
  <?php endif; ?>

  <hr class="divider">
  <div class="warn">
    <strong>⚠️ PENTING:</strong> Hapus file <code>reset_password.php</code> dari server
    setelah akun berhasil dibuat! File ini memungkinkan siapa saja mereset password admin.
  </div>
</div>
</body>
</html>