<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php
$options = $this->options;
if (!isset($options->popupNoticeEnable) || $options->popupNoticeEnable !== '1') {
    return;
}

$title = isset($options->popupNoticeTitle) ? trim($options->popupNoticeTitle) : '站点公告';
$content = isset($options->popupNoticeContent) ? $options->popupNoticeContent : '';
if (empty($title) && empty($content)) {
    return;
}

$widthMap = ['small' => '360px', 'medium' => '480px', 'large' => '600px'];
$width = isset($options->popupNoticeWidth) && isset($widthMap[$options->popupNoticeWidth]) ? $widthMap[$options->popupNoticeWidth] : '480px';
$btnText = isset($options->popupNoticeBtnText) ? trim($options->popupNoticeBtnText) : '';
$btnUrl = isset($options->popupNoticeBtnUrl) ? trim($options->popupNoticeBtnUrl) : '';
$showClose = !isset($options->popupNoticeShowClose) || $options->popupNoticeShowClose !== '0';
$showOnce = isset($options->popupNoticeShowOnce) && $options->popupNoticeShowOnce === '1';
$expires = isset($options->popupNoticeExpires) ? intval($options->popupNoticeExpires) : 24;
?>
<div class="gt-popup-notice" id="popupNoticeModal" style="--popup-width:<?php echo $width; ?>">
    <div class="gt-popup-notice-overlay" onclick="closePopupNotice()"></div>
    <div class="gt-popup-notice-panel">
        <?php if ($showClose): ?>
        <button class="gt-popup-notice-close" onclick="closePopupNotice()" aria-label="关闭">
            <i class="ri-close-line"></i>
        </button>
        <?php endif; ?>
        <?php if ($title): ?>
        <div class="gt-popup-notice-header">
            <h3 class="gt-popup-notice-title"><?php echo htmlspecialchars($title); ?></h3>
        </div>
        <?php endif; ?>
        <?php if ($content): ?>
        <div class="gt-popup-notice-body">
            <?php echo $content; ?>
        </div>
        <?php endif; ?>
        <?php if ($btnText): ?>
        <div class="gt-popup-notice-footer">
            <button class="gt-popup-notice-btn" onclick="handlePopupNoticeBtn()"><?php echo htmlspecialchars($btnText); ?></button>
        </div>
        <?php endif; ?>
    </div>
</div>
<script>
(function() {
    var showOnce = <?php echo $showOnce ? 'true' : 'false'; ?>;
    var expires = <?php echo max(0, $expires); ?>;
    var btnUrl = <?php echo json_encode($btnUrl); ?>;

    function setCookie(name, value, hours) {
        var d = new Date();
        d.setTime(d.getTime() + hours * 60 * 60 * 1000);
        document.cookie = name + '=' + encodeURIComponent(value) + ';expires=' + d.toUTCString() + ';path=/';
    }

    function getCookie(name) {
        var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
        return match ? decodeURIComponent(match[2]) : null;
    }

    window.closePopupNotice = function() {
        var modal = document.getElementById('popupNoticeModal');
        if (modal) {
            modal.classList.remove('active');
            document.body.style.overflow = '';
        }
        if (showOnce && expires > 0) {
            setCookie('mirai_popup_notice', '1', expires);
        }
    };

    window.handlePopupNoticeBtn = function() {
        if (btnUrl) {
            window.location.href = btnUrl;
        } else {
            closePopupNotice();
        }
    };

    document.addEventListener('DOMContentLoaded', function() {
        if (showOnce && expires > 0 && getCookie('mirai_popup_notice')) {
            return;
        }
        setTimeout(function() {
            var modal = document.getElementById('popupNoticeModal');
            if (modal) {
                modal.classList.add('active');
                document.body.style.overflow = 'hidden';
            }
        }, 500);
    });
})();
</script>