<?php
// pages/components/newsletter_collection_modal.php
// Thin wrapper delegating to generic CRUD controller (Collections = NewsletterCampaigns)

use Components\Resources\Controllers\CrudController;
use Components\Models\NewsletterCampaign;

ob_start();

// Support presetting the archived toggle when creating from the Archive section empty state
$mode = \mh_get('mode', 'new');
$id = (int)\mh_get('id', 0, 'int');

$pageConfig = \Components\Newsletter\PageConfig::fromRequest();
$fieldPresets = [];
if ($id <= 0 && $mode === 'new') {
    if ((int)\mh_get('preset_archived', 0, 'int') === 1) {
        $fieldPresets['newsletter_campaign_archived'] = 'True';
    }

    if ($pageConfig->mode === 'newsletter') {
        $fieldPresets['newsletter_campaign_applicability'] = 'newsletter';
    } elseif ($pageConfig->mode === 'automation') {
        $fieldPresets['newsletter_campaign_applicability'] = 'automation';
    }
}

mh_load_modules_language_files(MH_DIR_MODULE, 'mb_newsletter');

echo CrudController::handle([
    'modelClass'    => NewsletterCampaign::class,
    'selfRoute'     => 'newsletter_collection_modal',
    'oobListRoute'  => 'newsletter_collection_list',
    'oobListParams' => ['page_mode' => $pageConfig->mode],
    'event_prefix'  => $pageConfig->getEventName('campaigns'),
    'field_presets' => $fieldPresets,
]);

$__html = ob_get_clean();
echo $__html;
