<?php
require_once '../connection.php';

if (!isLoggedIn() || isAdmin()) {
    header('Location: login.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $client_id = $_SESSION['user_id'];
    $auto_debit = isset($_POST['auto_debit']) ? 1 : 0;
    
    $stmt = $pdo->prepare("UPDATE clients SET auto_debit = ? WHERE id = ?");
    $stmt->execute([$auto_debit, $client_id]);
    
    $_SESSION['success'] = $auto_debit ? "Auto-debit enabled successfully!" : "Auto-debit disabled successfully!";
    header('Location: dashboard.php');
    exit();
} else {
    header('Location: dashboard.php');
    exit();
}
?>