['channels' => [], 'users' => []], 'telegram' => ['channels' => [], 'users' => []]]; try { $stmt = $pdo->query("SELECT id, name, type, platform FROM recipients ORDER BY platform, type, name ASC"); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { if (isset($recipients_by_platform[$row['platform']])) { if ($row['type'] === 'channel') { $recipients_by_platform[$row['platform']]['channels'][] = $row; } else { $recipients_by_platform[$row['platform']]['users'][] = $row; } } } } catch (PDOException $e) { die("Error: No se pudieron cargar los destinatarios: " . $e->getMessage()); } // Fetch recurrent message templates $templates = []; try { $stmt = $pdo->query("SELECT id, name, message_content FROM recurrent_messages ORDER BY name ASC"); $templates = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { die("Error: No se pudieron cargar las plantillas de mensajes recurrentes."); } // Initialize variables $isEditing = false; $scheduleId = null; $pageTitle = 'Crear Notificación'; $submitButtonText = 'Guardar y Enviar'; $submitButtonIcon = 'bi-send-fill'; $submitAction = 'send'; $platform = 'discord'; // Default platform $reuseContent = ''; $recipientId = ''; $recipientType = 'channel'; $scheduleType = 'now'; $recurringDays = []; // Set default times in Mexico City timezone $timezone = new DateTimeZone('America/Mexico_City'); $now = new DateTime('now', $timezone); $recurringTime = $now->format('H:i'); $scheduleDateTime = $now->format('Y-m-d\TH:i'); // Check for reuse action from sent_messages.php if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'reuse') { $reuseContent = $_POST['messageContent'] ?? ''; $recipientId = $_POST['recipientId'] ?? ''; $recipientType = $_POST['recipientType'] ?? 'channel'; // Set schedule to default for a new message $scheduleType = 'now'; // Set default schedule time to current time in Mexico City timezone $timezone = new DateTimeZone('America/Mexico_City'); $now = new DateTime('now', $timezone); $scheduleDateTime = $now->format('Y-m-d\TH:i'); $recurringDays = []; $recurringTime = $now->format('H:i'); } // Check for edit action if (isset($_GET['action']) && $_GET['action'] === 'edit' && isset($_GET['schedule_id'])) { $isEditing = true; $scheduleId = $_GET['schedule_id']; $pageTitle = 'Editar Notificación'; $submitButtonText = 'Actualizar Mensaje'; $submitButtonIcon = 'bi-pencil-square'; $submitAction = 'update'; $stmt = $pdo->prepare( "SELECT s.recipient_id, s.send_time, s.is_recurring, s.recurring_days, s.recurring_time, m.content, r.type as recipient_type, r.platform FROM schedules s JOIN messages m ON s.message_id = m.id JOIN recipients r ON s.recipient_id = r.id WHERE s.id = ?" ); $stmt->execute([$scheduleId]); $message_data = $stmt->fetch(PDO::FETCH_ASSOC); if ($message_data) { $reuseContent = $message_data['content']; $recipientId = $message_data['recipient_id']; $recipientType = $message_data['recipient_type']; $platform = $message_data['platform']; if ($message_data['is_recurring']) { $scheduleType = 'recurring'; $recurringDays = !empty($message_data['recurring_days']) ? explode(',', $message_data['recurring_days']) : []; $recurringTime = substr($message_data['recurring_time'], 0, 5); } else { $scheduleType = 'later'; if ($message_data['send_time']) { $scheduleDateTime = date('Y-m-d\TH:i', strtotime($message_data['send_time'])); } } } else { die("Error: Mensaje programado no encontrado."); } } require_once __DIR__ . '/templates/header.php'; ?>