<?php
/**
 * 顶部大屏幻灯片模块
 * 支持：Swiper 图片/视频幻灯片、搜索叠加组件、图标卡片导航
 */
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

$options = $this->options;

if (!isset($options->heroEnable) || $options->heroEnable !== '1') return;

if (isset($options->heroPageOnly) && $options->heroPageOnly === '1') {
    if (!$this->is('index')) return;
    $page = $this->request->get('page', 1);
    if ($page > 1) return;
}

function _hero_is_video($url) {
    $url = strtolower(trim($url));
    $ext = pathinfo(parse_url($url, PHP_URL_PATH) ?: '', PATHINFO_EXTENSION);
    return in_array($ext, ['mp4', 'webm', 'ogg', 'mov']);
}

$isMobile = Mirai_isMobile();

$slides = [];
if (!empty($options->heroSlides)) {
    $lines = explode("\n", trim($options->heroSlides));
    foreach ($lines as $line) {
        $line = trim($line);
        if (empty($line)) continue;
        $parts = array_map('trim', explode('|', $line));

        $mediaUrl = isset($parts[0]) ? $parts[0] : '';
        if (empty($mediaUrl)) continue;

        $isVideo = _hero_is_video($mediaUrl);

        if ($isVideo && $isMobile) continue;

        $slide = [
            'media'   => $mediaUrl,
            'isVideo' => $isVideo,
            'link'    => isset($parts[1]) ? $parts[1] : '',
            'title'   => isset($parts[2]) ? $parts[2] : '',
            'desc'    => isset($parts[3]) ? $parts[3] : '',
            'poster'  => isset($parts[4]) ? $parts[4] : '',
        ];
        $slides[] = $slide;
    }
}

if (empty($slides)) return;

$pcHeight   = !empty($options->heroPcHeight) ? intval($options->heroPcHeight) : 550;
$mHeight    = !empty($options->heroMobileHeight) ? intval($options->heroMobileHeight) : 280;
$autoplay   = !isset($options->heroAutoplay) || $options->heroAutoplay !== '0';
$interval   = !empty($options->heroInterval) ? intval($options->heroInterval) * 1000 : 5000;
$effect     = !empty($options->heroEffect) ? $options->heroEffect : 'fade';
$loop       = !isset($options->heroLoop) || $options->heroLoop !== '0';
$showSearch = isset($options->heroSearchEnable) && $options->heroSearchEnable === '1';
$showCards  = isset($options->heroCardsEnable) && $options->heroCardsEnable === '1';
$filterBlur = isset($options->heroFilterBlur) && $options->heroFilterBlur === '1';
$fogEffect  = isset($options->heroFogEffect) && $options->heroFogEffect === '1';
$heroDesc   = !empty($options->heroSearchDesc) ? $options->heroSearchDesc : '';

$searchTitle = '';
$searchKeywords = [];
if ($showSearch) {
    $searchTitle = !empty($options->heroSearchTitle) ? $options->heroSearchTitle : '';
    $searchPlaceholder = !empty($options->heroSearchPlaceholder) ? $options->heroSearchPlaceholder : '开启精彩搜索';
    if (!empty($options->heroSearchKeywords)) {
        $searchKeywords = array_slice(
            array_filter(array_map('trim', explode("\n", trim($options->heroSearchKeywords)))),
            0, 10
        );
    }
}

$cards = [];
if ($showCards && !empty($options->heroCards)) {
    $cardLines = explode("\n", trim($options->heroCards));
    foreach ($cardLines as $cardLine) {
        $cardLine = trim($cardLine);
        if (empty($cardLine)) continue;
        $parts = array_map('trim', explode('|', $cardLine));
        $card = [
            'icon'       => isset($parts[0]) ? $parts[0] : 'ri-apps-line',
            'icon_color' => isset($parts[1]) ? $parts[1] : 'blue',
            'title'      => isset($parts[2]) ? $parts[2] : '',
            'desc'       => isset($parts[3]) ? $parts[3] : '',
            'link'       => isset($parts[4]) ? $parts[4] : '',
            'badge'      => isset($parts[5]) ? $parts[5] : '',
        ];
        if (!empty($card['title'])) {
            $cards[] = $card;
        }
    }
}

$cardCount = count($cards);
$containerClass = 'hero-container';
if ($cardCount > 0) $containerClass .= ' has-cards card-' . $cardCount;
if ($filterBlur) $containerClass .= ' hero-blur';
if ($fogEffect) $containerClass .= ' hero-fog';
if ($showSearch) $containerClass .= ' has-search';
?>

<section class="<?php echo $containerClass; ?>" style="--hero-pc-h:<?php echo $pcHeight; ?>px;--hero-m-h:<?php echo $mHeight; ?>px;">

    <div class="hero-banner">
        <div class="hero-swiper swiper"
             data-effect="<?php echo htmlspecialchars($effect); ?>"
             data-loop="<?php echo $loop ? 'true' : 'false'; ?>"
             data-autoplay="<?php echo $autoplay ? 'true' : 'false'; ?>"
             data-interval="<?php echo $interval; ?>">

            <div class="swiper-wrapper">
                <?php foreach ($slides as $i => $slide): ?>
                <div class="swiper-slide<?php echo $slide['isVideo'] ? ' is-video' : ''; ?>">

                    <?php if ($slide['isVideo']): ?>

                    <video class="hero-slide-video"
                           <?php if (!empty($slide['poster'])): ?>poster="<?php echo htmlspecialchars($slide['poster']); ?>"<?php endif; ?>
                           src="<?php echo htmlspecialchars($slide['media']); ?>"
                           autoplay muted playsinline preload="metadata"></video>
                    <?php else: ?>

                    <?php if ($i === 0): ?>
                    <img class="hero-slide-img" src="<?php echo htmlspecialchars($slide['media']); ?>" alt="<?php echo htmlspecialchars($slide['title'] ?: '幻灯片'); ?>" fetchpriority="high">
                    <?php else: ?>
                    <img class="hero-slide-img lazyload" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-src="<?php echo htmlspecialchars($slide['media']); ?>" alt="<?php echo htmlspecialchars($slide['title'] ?: '幻灯片'); ?>">
                    <?php endif; ?>
                    <?php endif; ?>

                    <div class="hero-slide-mask"></div>

                    <?php if (!empty($slide['link'])): ?>
                    <a href="<?php echo htmlspecialchars($slide['link']); ?>" class="hero-slide-link" aria-label="<?php echo htmlspecialchars($slide['title'] ?: '查看详情'); ?>"></a>
                    <?php endif; ?>

                    <?php if (!empty($slide['title']) || !empty($slide['desc'])): ?>
                    <div class="hero-slide-caption">
                        <?php if (!empty($slide['title'])): ?>
                        <h3 class="hero-slide-title"><?php echo htmlspecialchars($slide['title']); ?></h3>
                        <?php endif; ?>
                        <?php if (!empty($slide['desc'])): ?>
                        <p class="hero-slide-desc"><?php echo htmlspecialchars($slide['desc']); ?></p>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
                <?php endforeach; ?>
            </div>

            <?php if (count($slides) > 1): ?>
            <div class="swiper-pagination"></div>
            <div class="swiper-button-prev"></div>
            <div class="swiper-button-next"></div>
            <?php endif; ?>
        </div>
    </div>

    <?php if ($showSearch): ?>

    <div class="hero-overlay">
        <div class="hero-overlay-inner">
            <?php if (!empty($searchTitle)): ?>
            <div class="hero-heading"><?php echo $searchTitle; ?></div>
            <?php endif; ?>

            <?php if (!empty($heroDesc)): ?>
            <div class="hero-subheading"><?php echo htmlspecialchars($heroDesc); ?></div>
            <?php endif; ?>

            <div class="hero-search-box">
                <form class="hero-search-form" method="get" action="<?php $options->siteUrl(); ?>">
                    <div class="hero-search-inner">
                        <i class="ri-search-line hero-search-icon"></i>
                        <input type="search" name="s" class="hero-search-input"
                               placeholder="<?php echo htmlspecialchars($searchPlaceholder); ?>"
                               autocomplete="off">
                        <button type="submit" class="hero-search-btn">
                            <i class="ri-search-line"></i>
                        </button>
                    </div>
                </form>
            </div>

            <?php if (!empty($searchKeywords)): ?>
            <div class="hero-keywords">
                <?php foreach ($searchKeywords as $keyword): ?>
                <a class="hero-keyword" href="<?php $options->siteUrl(); ?>?s=<?php echo urlencode($keyword); ?>">
                    <?php echo htmlspecialchars($keyword); ?>
                </a>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

    <?php if ($showCards && !empty($cards)): ?>

    <div class="hero-cards">
        <div class="hero-cards-row">
            <?php foreach ($cards as $card): ?>
            <div class="hero-card">
                <?php $tag = !empty($card['link']) ? 'a' : 'div'; ?>
                <<?php echo $tag; ?> <?php if (!empty($card['link'])): ?>href="<?php echo htmlspecialchars($card['link']); ?>"<?php endif; ?> class="hero-card-body">
                    <span class="hero-card-icon icon-<?php echo htmlspecialchars($card['icon_color']); ?>">
                        <i class="<?php echo htmlspecialchars($card['icon']); ?>"></i>
                    </span>
                    <span class="hero-card-text">
                        <strong class="hero-card-name">
                            <?php echo htmlspecialchars($card['title']); ?>
                            <?php if (!empty($card['badge'])): ?>
                            <em class="hero-card-badge badge-<?php echo htmlspecialchars($card['icon_color']); ?>"><?php echo htmlspecialchars($card['badge']); ?></em>
                            <?php endif; ?>
                        </strong>
                        <?php if (!empty($card['desc'])): ?>
                        <span class="hero-card-desc"><?php echo htmlspecialchars($card['desc']); ?></span>
                        <?php endif; ?>
                    </span>
                </<?php echo $tag; ?>>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    <?php endif; ?>

</section>