<?php
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/common.php';

$user = get_user();
$tags = fetch_all("SELECT * FROM tags ORDER BY sort_order DESC, id DESC LIMIT 15");

$page = max(1, intval($_GET['page'] ?? 1));
$page_size = PAGE_SIZE;
$offset = ($page - 1) * $page_size;

list($items, $total) = mixed_feed($page_size, $offset);

header_html('AI');

$flash = get_flash_msg();
if ($flash): ?>
<script>layer.msg('<?php echo addslashes($flash['msg']); ?>', {icon: <?php echo $flash['code']; ?>});</script>
<?php endif; ?>

<div class="youshi">
    <p class="intit">一个让学员满足模型幻想的圣地</p>
    <p> 工作流下载 <span>·</span> 模型下载</p>
</div>
<div class="w1400">
    <div class="box galleryList">
        <ul class="databox">
            <?php foreach ($items as $it): ?>
            <?php if ($it['kind'] === 'work'): ?>
            <?php echo work_card_html($it['data']); ?>
            <?php else: $g = $it['data']; ?>
            <li>
                <div class="img-box">
                    <a href="/gallery/<?php echo $g['id']; ?>.html">
                        <img src="<?php echo htmlspecialchars($g['cover']); ?>" alt="<?php echo htmlspecialchars($g['title']); ?>">
                    </a>
                    <em class="num"><?php echo $g['page_count']; ?>P</em>
                    <em class="hot"><i class="layui-icon layui-icon-fire"> </i><?php echo format_number($g['views']); ?></em>
                    <div class="chujing">模特：<?php echo htmlspecialchars($g['model_name'] ?: '匿名'); ?></div>
                </div>
                <p class="rtitle"><a href="/jigou/<?php echo $g['agency_id']; ?>.html"><?php echo htmlspecialchars($g['agency_name'] ?: '未知'); ?></a></p>
                <p class="ztitle"><a target="_blank" href="/gallery/<?php echo $g['id']; ?>.html"><?php echo htmlspecialchars($g['title']); ?></a></p>
                <?php
                $gtags = fetch_all("SELECT t.* FROM tags t INNER JOIN gallery_tags gt ON t.id = gt.tag_id WHERE gt.gallery_id = ?", [$g['id']]);
                if ($gtags): ?>
                <p class="rtitle"><?php foreach ($gtags as $tg): ?><a href="/tags/<?php echo $tg['slug']; ?>.html"><?php echo htmlspecialchars($tg['name']); ?></a> <?php endforeach; ?></p>
                <?php endif; ?>
            </li>
            <?php endif; ?>
            <?php endforeach; ?>
        </ul>
    </div>
</div>
<div class="bk10"></div>
<div class="tagmore"><a href="/gallery/">查看更多</a></div>
<div class="bk70"></div>

<?php if (get_site_config('home_links_enabled', '1') !== '0'): ?>
<div class="w1400">
    <div class="layui-tab" lay-filter="TabBrief">
        <ul class="layui-tab-title">
            <li class="layui-this">友情链接</li>
            <li>标签</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <div class="link">
                    <?php
                    // 友情链接：后台「站点设置 → 首页设置」每行一条「名称|网址」，未配置时显示默认项
                    $fl_raw = (string)get_site_config('home_links', '');
                    $friend_links = [];
                    foreach (preg_split('/\r?\n/', $fl_raw) as $ln) {
                        $t = trim($ln);
                        if ($t === '' || $t[0] === '#' || $t[0] === ';') continue;
                        $p = array_map('trim', explode('|', $t, 2));
                        if ($p[0] === '') continue;
                        $friend_links[] = ['name' => $p[0], 'url' => isset($p[1]) ? $p[1] : ''];
                    }
                    if (!$friend_links) {
                        $friend_links[] = ['name' => '美图乐', 'url' => 'http://www.meitule.com'];
                    }
                    $fl_qq = trim((string)get_site_config('home_link_qq', '友接QQ：（要求权重相当）'));
                    foreach ($friend_links as $fl): ?>
                    <?php if ($fl['url'] !== ''): ?>
                    <a href="<?php echo htmlspecialchars($fl['url']); ?>" target="_blank" rel="nofollow"><?php echo htmlspecialchars($fl['name']); ?></a>
                    <?php else: ?>
                    <span><?php echo htmlspecialchars($fl['name']); ?></span>
                    <?php endif; ?>
                    <?php endforeach; ?>
                    <?php if ($fl_qq !== ''): ?>
                    <br /><?php echo htmlspecialchars($fl_qq); ?>
                    <?php endif; ?>
                </div>
            </div>
            <div class="layui-tab-item">
                <div class="tags">
                    <?php
                    $all_tags = fetch_all("SELECT * FROM tags ORDER BY sort_order DESC, id DESC LIMIT 50");
                    foreach ($all_tags as $tag): ?>
                    <a href="/tags/<?php echo $tag['slug']; ?>.html"><?php echo htmlspecialchars($tag['name']); ?></a>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<?php
// pagination disabled on homepage
footer_html();
?>
