<?php
include 'config.php';
include 'manager_data.php';

// Check if slaves to delete are selected
if (isset($_POST['slaves']) && count($_POST['slaves']) > 0) {
    $slaves = $_POST['slaves'];

    // Begin transaction for deletion
    $dbh->beginTransaction();

    try {
        // Delete selected slaves from tbl_slave
        $placeholders = implode(',', array_fill(0, count($slaves), '?'));
        $sql = "DELETE FROM tbl_slave WHERE slavetrade IN ($placeholders)";
        $stmt = $dbh->prepare($sql);
        $stmt->execute($slaves);

        // Delete selected slaves from tbl_copytrade
        $sql2 = "DELETE FROM tbl_copytrade WHERE copytrade IN ($placeholders)";
        $stmt2 = $dbh->prepare($sql2);
        $stmt2->execute($slaves);

        // Commit transaction
        $dbh->commit();

        echo 'Slaves removed successfully';
    } catch (Exception $e) {
        // Rollback if any error occurs
        $dbh->rollBack();
        echo 'Error: ' . $e->getMessage();
    }
}
?>
