<?php
ini_set("display_errors", 0);
require '../../app/Controllers/sessions.php';
require '../../app/Controllers/security_admin.php';
require '../../app/Controllers/adminInfos.php';
require '../../app/Controllers/addProducts.php';
require '../../config/db.php';

$toastMessage = $_SESSION['toast_message'] ?? null;
$toastType = $_SESSION['toast_type'] ?? null;
unset($_SESSION['toast_message'], $_SESSION['toast_type']);

$token = filter_input(INPUT_GET, "token", FILTER_SANITIZE_SPECIAL_CHARS);
$is_active = filter_input(INPUT_GET, "is_active", FILTER_VALIDATE_INT);

if ($is_active == 1) {
    $alter = 0;
} 

if ($is_active == 0) {
    $alter = 1;
} 

$stmt = $pdo->prepare("SELECT * FROM investment_products WHERE token = :token LIMIT 1");
$stmt->execute([
    ':token' => $token
]);

if ($stmt->fetchColumn()) {
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
} else {
    $_SESSION['toast_message'] = $toastMessage = 'Produto não encontrado!';
    $_SESSION['toast_type'] = $toastType = 'error';
    header("Location: administer_products.php");
    exit;
}

$stmtUpdateProduct = $pdo->prepare("UPDATE investment_products SET is_active = :is_active WHERE token = :token");
$stmtUpdateProduct->execute([
    ':is_active' => $alter,
    ':token' => $token
]);

$_SESSION['toast_message'] = $toastMessage = 'Status do produto alterado!';
$_SESSION['toast_type'] = $toastType = 'success';
header("Location: administer_products.php");
exit;

?>

