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

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

$client_id = $_GET['id'] ?? 0;

if ($client_id) {
    try {
        // Get client PPPoE username
        $stmt = $pdo->prepare("SELECT pppoe_username FROM clients WHERE id = ?");
        $stmt->execute([$client_id]);
        $client = $stmt->fetch();
        
        if ($client) {
            // Disable client in database
            $stmt = $pdo->prepare("UPDATE clients SET is_active = 0 WHERE id = ?");
            $stmt->execute([$client_id]);
            
            // Disable active subscription
            $stmt = $pdo->prepare("UPDATE subscriptions SET is_active = 0 WHERE client_id = ? AND is_active = 1");
            $stmt->execute([$client_id]);
            
            // Disable in MikroTik (uncomment when API is ready)
            // disableMikroTikUser($client['pppoe_username']);
            
            $_SESSION['success'] = "Client disabled successfully!";
        } else {
            $_SESSION['error'] = "Client not found!";
        }
    } catch (Exception $e) {
        $_SESSION['error'] = "Error: " . $e->getMessage();
    }
}

header('Location: dashboard.php');
exit();
?>