<?php
/**
 * Enterprise HRM ERP System - Reset Admin Password
 * Use this to reset admin password if you forgot it
 * DELETE THIS FILE AFTER USE FOR SECURITY
 */

require_once __DIR__ . '/../includes/init.php';

$username = isset($_GET['username']) ? sanitize($_GET['username']) : 'admin';
$error = null;
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = sanitize($_POST['username'] ?? '');
    $newPassword = $_POST['new_password'] ?? '';
    $confirmPassword = $_POST['confirm_password'] ?? '';
    
    if (empty($username) || empty($newPassword)) {
        $error = 'All fields are required.';
    } elseif ($newPassword !== $confirmPassword) {
        $error = 'Passwords do not match.';
    } else {
        $passwordErrors = validatePassword($newPassword);
        if (!empty($passwordErrors)) {
            $error = implode(' ', $passwordErrors);
        } else {
            try {
                $db = getDB();
                $passwordHash = hashPassword($newPassword);
                
                $stmt = $db->prepare("UPDATE users SET password_hash = ? WHERE username = ? OR email = ?");
                $stmt->execute([$passwordHash, $username, $username]);
                
                if ($stmt->rowCount() > 0) {
                    $success = true;
                    logAudit('PASSWORD_RESET', 'users', null, null, ['username' => $username]);
                } else {
                    $error = 'User not found.';
                }
            } catch (Exception $e) {
                error_log("Error resetting password: " . $e->getMessage());
                $error = 'An error occurred while resetting the password.';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Reset Password</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 40px; background: #f5f5f5; }
        .box { background: white; padding: 30px; border-radius: 8px; max-width: 500px; margin: 0 auto; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #1e293b; }
        .alert { padding: 15px; border-radius: 6px; margin: 15px 0; }
        .alert-success { background: #d1fae5; color: #065f46; border-left: 4px solid #10b981; }
        .alert-error { background: #fee2e2; color: #991b1b; border-left: 4px solid #ef4444; }
        .form-group { margin-bottom: 20px; }
        .form-label { display: block; margin-bottom: 8px; font-weight: 500; }
        .form-control { width: 100%; padding: 10px; border: 1px solid #e2e8f0; border-radius: 6px; font-size: 14px; }
        .btn { padding: 12px 24px; background: #1e293b; color: white; border: none; border-radius: 6px; cursor: pointer; width: 100%; }
        .btn:hover { background: #1e40af; }
    </style>
</head>
<body>
    <div class="box">
        <h1>Reset Password</h1>
        
        <?php if ($success): ?>
        <div class="alert alert-success">
            <strong>Success!</strong> Password has been reset successfully.
            <br><br>
            <a href="<?php echo BASE_URL; ?>/login.php" style="color: #065f46; font-weight: bold;">Go to Login Page</a>
        </div>
        <?php else: ?>
        
        <?php if ($error): ?>
        <div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>
        
        <form method="POST">
            <div class="form-group">
                <label class="form-label" for="username">Username or Email</label>
                <input type="text" id="username" name="username" class="form-control" required 
                       value="<?php echo htmlspecialchars($username); ?>">
            </div>
            
            <div class="form-group">
                <label class="form-label" for="new_password">New Password</label>
                <input type="password" id="new_password" name="new_password" class="form-control" required>
                <small style="color: #64748b; font-size: 12px;">
                    Minimum 8 characters, must include uppercase, lowercase, number, and special character.
                </small>
            </div>
            
            <div class="form-group">
                <label class="form-label" for="confirm_password">Confirm Password</label>
                <input type="password" id="confirm_password" name="confirm_password" class="form-control" required>
            </div>
            
            <button type="submit" class="btn">Reset Password</button>
        </form>
        
        <div style="margin-top: 20px; text-align: center;">
            <a href="<?php echo BASE_URL; ?>/login.php">Back to Login</a>
        </div>
        <?php endif; ?>
    </div>
</body>
</html>

