<?php
ini_set("display_errors", 0);
error_reporting(0);
require_once 'db.php';
requireLogin();

header('Content-Type: application/json');

try {
    $input = json_decode(file_get_contents('php://input'), true);
    $ids = $input['ids'] ?? [];
    $hide = $input['hide'] ?? true; // true = verbergen, false = wieder anzeigen
    
    if (empty($ids)) {
        echo json_encode(['success' => false, 'error' => 'Keine IDs']);
        exit;
    }
    
    // IDs filtern
    $ids = array_filter($ids, function($id) {
        return is_numeric($id) || is_string($id) && ctype_digit($id);
    });
    $ids = array_map('intval', $ids);
    
    if (empty($ids)) {
        echo json_encode(['success' => false, 'error' => t('bulk_invalid_ids')]);
        exit;
    }
    
    // UPDATE Statement
    $hiddenValue = $hide ? 1 : 0;
    $placeholders = implode(',', array_fill(0, count($ids), '?'));
    $sql = "UPDATE wertsachen SET hidden = $hiddenValue WHERE id IN ($placeholders)";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute($ids);
    
    $affected = $stmt->rowCount();
    
    echo json_encode([
        'success' => true,
        'affected' => $affected,
        'message' => $affected . ' ' . ($hide ? t('bulk_entries_hidden') : t('bulk_entries_visible'))
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'error' => $e->getMessage()
    ]);
}
