
<?php

require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/config.php';


$message = '';
$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $current = $_POST['current_password'] ?? '';
    $new = $_POST['new_password'] ?? '';
    $confirm = $_POST['confirm_password'] ?? '';

    if (!password_verify($current, ADMIN_PASS_HASH)) {
        $error = 'Current password is incorrect';
    } elseif (strlen($new) < 8) {
        $error = 'New password must be at least 8 characters';
    } elseif ($new !== $confirm) {
        $error = 'New passwords do not match';
    } else {
        $newHash = password_hash($new, PASSWORD_DEFAULT);

        $configPath = __DIR__ . '/config.php';
        $configContent = "<?php\n"
            . "define('ADMIN_USER', '" . ADMIN_USER . "');\n\n"
            . "define('ADMIN_PASS_HASH', '" . $newHash . "');\n";

        if (file_put_contents($configPath, $configContent)) {
    // Force logout after password change
    session_unset();
    session_destroy();

    header('Location: login.php?password_changed=1');
    exit;

        } else {
            $error = 'Failed to update password file';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Change Password</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body{
      font-family:system-ui,Arial,sans-serif;
      background:#f1f5f9;
      display:flex;
      align-items:center;
      justify-content:center;
      height:100vh;
    }
    .box{
      background:#fff;
      padding:32px;
      border-radius:14px;
      width:360px;
      box-shadow:0 20px 40px rgba(0,0,0,.1);
    }
    h2{margin:0 0 16px}
    input{
      width:100%;
      padding:12px;
      margin:8px 0;
      border-radius:8px;
      border:1px solid #e5e7eb;
    }
    button{
      width:100%;
      padding:12px;
      margin-top:10px;
      background:#16a34a;
      color:#fff;
      border:0;
      border-radius:8px;
      font-weight:600;
    }
    .error{
      background:#fee2e2;
      color:#991b1b;
      padding:10px;
      border-radius:8px;
      font-size:.85rem;
      margin-bottom:10px;
    }
    .success{
      background:#dcfce7;
      color:#166534;
      padding:10px;
      border-radius:8px;
      font-size:.85rem;
      margin-bottom:10px;
    }
  </style>
</head>
<body>

  <form class="box" method="post">
    <h2>Change Password</h2>

    <?php if ($error): ?>
      <div class="error"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <?php if ($message): ?>
      <div class="success"><?= htmlspecialchars($message) ?></div>
    <?php endif; ?>

    <input type="password" name="current_password" placeholder="Current Password" required>
    <input type="password" name="new_password" placeholder="New Password" required>
    <input type="password" name="confirm_password" placeholder="Confirm New Password" required>

    <button type="submit">Update Password</button>
  </form>

</body>
</html>
