<?php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/module_hub_helpers.php';
require_once __DIR__ . '/functions.php';

$db = mkt_db();
$campaignId = (int)($_GET['id'] ?? 0);
if ($campaignId <= 0) {
    set_flash('error', 'معرف الحملة غير صالح.');
    header('Location: index.php');
    exit;
}
[$ok, $message, $templateId] = mkt_save_campaign_as_template($db, $campaignId, function_exists('current_admin_id') ? current_admin_id() : 0);
if ($ok && $templateId > 0) {
    set_flash('success', $message);
    header('Location: template_edit.php?id=' . $templateId);
    exit;
}
set_flash('error', $message ?: 'تعذر حفظ الحملة كقالب.');
header('Location: view.php?id=' . $campaignId);
exit;
