<?php
require_once 'db/config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $card_id = $_POST['card_id'] ?? null;
    $column_id = $_POST['column_id'] ?? null;
    
    if ($card_id && $column_id) {
        $pdo = db();
        
        // Update the card's column
        $stmt = $pdo->prepare('UPDATE info_productos SET column_id = :column_id WHERE id = :card_id');
        $stmt->execute([':column_id' => $column_id, ':card_id' => $card_id]);

        // Optional: Handle card order within the new column
        // For simplicity, we are not reordering, but you could add that logic here.
        // For example, set the moved card to be the last in the new column.
        $stmt_max_order = $pdo->prepare('SELECT MAX(orden) FROM info_productos WHERE column_id = :column_id');
        $stmt_max_order->execute([':column_id' => $column_id]);
        $max_order = $stmt_max_order->fetchColumn() ?? 0;

        $stmt_order = $pdo->prepare('UPDATE info_productos SET orden = :orden WHERE id = :card_id');
        $stmt_order->execute([':orden' => $max_order + 1, ':card_id' => $card_id]);

        $_SESSION['success_message'] = 'Tarjeta movida exitosamente.';
    } else {
        $_SESSION['error_message'] = 'Faltan datos para mover la tarjeta.';
    }
}

header('Location: kanban.php');
exit;