false, 'error' => 'No autenticado']); exit; } // Verificar permiso if (!hasPermission('manage_templates', 'telegram')) { http_response_code(403); echo json_encode(['success' => false, 'error' => 'No tienes permiso para eliminar plantillas de Telegram.']); exit; } if ($_SERVER['REQUEST_METHOD'] !== 'POST') { jsonResponse(['success' => false, 'error' => 'Método no permitido'], 405); } $input = json_decode(file_get_contents('php://input'), true); if (!isset($input['id'])) { jsonResponse(['success' => false, 'error' => 'ID no proporcionado'], 400); } try { $db = getDB(); // Verificar que la plantilla existe $stmt = $db->prepare("SELECT * FROM plantillas_telegram WHERE id = ?"); $stmt->execute([$input['id']]); $plantilla = $stmt->fetch(); if (!$plantilla) { jsonResponse(['success' => false, 'error' => 'Plantilla no encontrada'], 404); } // Verificar permisos (admin o creador) if ($userData->rol !== 'Admin' && $plantilla['usuario_id'] != $userData->userId) { jsonResponse(['success' => false, 'error' => 'No tiene permisos para eliminar esta plantilla'], 403); } // Eliminar plantilla $stmt = $db->prepare("DELETE FROM plantillas_telegram WHERE id = ?"); $stmt->execute([$input['id']]); logToFile('telegram/templates.log', "Plantilla eliminada: ID={$input['id']}, Usuario={$userData->username}"); jsonResponse(['success' => true, 'message' => 'Plantilla eliminada correctamente']); } catch (Exception $e) { logToFile('telegram/errors.log', "Error eliminando plantilla: " . $e->getMessage(), 'ERROR'); jsonResponse(['success' => false, 'error' => 'Error del servidor'], 500); }