<?php
// pages/components/email_editor_lock_unlock_modal.php
use Components\Base;
use Components\Router;
use Components\Button;
use Components\Legacy\LegacyRouter;

$newsletterId = (int)mh_get('newsletter_id', 0, 'int');
$campaignId = (int)mh_get('campaign_id', 0, 'int');
$flowId = (int)mh_get('flow_id', 0, 'int');
$isFollowUp = (bool)mh_get('is_follow_up', false, 'bool');
$locked = (bool)mh_get('locked', false, 'bool');

mh_load_modules_language_files(MH_DIR_MODULE, 'mb_newsletter');


$scheduleSummary = '';
if ($newsletterId > 0 && class_exists('\\Components\\Models\\NewsletterItem')) {
    $item = \Components\Models\NewsletterItem::find($newsletterId);
    if ($item) {
        // Prefer the unified schedule summary from the model; fallback to a simple
        // date range label when summary is empty for any reason.
        if (method_exists($item, 'buildScheduleSummary')) {
            $scheduleSummary = (string)$item->buildScheduleSummary();
        }
        if ($scheduleSummary === '') {
            $scheduleSummary = \Components\CampaignFlow\DateRangeBadge::buildRangeLabel($item->newsletter_item_start, $item->newsletter_item_end);
        }
    }
}

// URLs for lock/unlock actions
$lockUrl = LegacyRouter::url('newsletterLock', ['newsletter_id' => $newsletterId, 'flow_id' => $flowId]);
$unlockUrl = LegacyRouter::url('newsletterUnlock', ['newsletter_id' => $newsletterId, 'flow_id' => $flowId]);
$copyUrl = LegacyRouter::url('newsletterCopy', ['newsletter_id' => $newsletterId, 'campaign_id' => $campaignId]);

ob_start();
?>
    <div class="space-y-4">
        <div class="text-sm text-gray-500">
            <?php if ($locked): ?>
                <?= \mh_lng('MAILBEEZ_NEWSLETTER_UNLOCK_WARNING_TEXT', 'Each Recipient will receive this Newsletter once - so when editing this Newsletter it will not be sent again.') ?>
            <?php else: ?>
                <?= \mh_lng('TABLE_HEADING_CAMPAIGNS_STATUS_HINT_TEXT', 'The Newsletter will be sent out')  ?>

            <div class="mt-8">
            <?=  Base::h($scheduleSummary) ?>
            </div>
            <?php endif; ?>
        </div>

        <?php
        $gridCss = $locked && !$isFollowUp ? 'sm:grid-cols-3' : 'sm:grid-cols-2';
        ?>

        <template x-teleport="#confirm-modal-footer">
            <div class="grid grid-cols-1 <?= $gridCss ?> gap-3 w-full">
                <?= Button::render(\mh_lng('MAILBEEZ_NEWSLETTER_LOCK_BUTTON_CANCEL', 'Cancel'), [
                        'variant' => 'outline',
                        'fullWidth' => true,

                        'attrs' => [
                                'command' => 'close',
                                'commandfor' => 'confirm-modal',
                        ],
                ]) ?>

                <?php if ($locked): ?>
                    <?php if (!$isFollowUp): ?>
                        <?= Button::render(\mh_lng('MAILBEEZ_NEWSLETTER_UNLOCK_BUTTON_COPY', 'Copy this Newsletter'), [
                                'variant' => 'secondary',
                                'fullWidth' => true,

                                'as' => 'a',
                                'href' => $copyUrl,
                        ]) ?>
                    <?php endif; ?>
                    <?= Button::render(\mh_lng('MAILBEEZ_NEWSLETTER_UNLOCK_BUTTON_OK', 'OK - unlock'), [
                            'variant' => 'danger',
                            'fullWidth' => true,

                            'modal_close' => 'confirm-modal',
                            'attrs' => [
                                    'hx-post' => $unlockUrl,
                                    'hx-target' => '#confirm-modal-body',
                            ],
                    ]) ?>


                <?php else: ?>
                    <?= Button::render(\mh_lng('MAILBEEZ_NEWSLETTER_LOCK_BUTTON_OK', 'OK - send'), [
                            'variant' => 'primary',
                            'fullWidth' => true,

                            'modal_close' => 'confirm-modal',
                            'attrs' => [
                                    'hx-post' => $lockUrl,
                                    'hx-target' => '#confirm-modal-body',
                            ],
                    ]) ?>
                <?php endif; ?>


            </div>
        </template>
    </div>
<?php
$html = ob_get_clean();
echo $html;
