<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>短信列表 - 短信转发接收端</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            animation: slideInDown 0.5s ease-out;
        }

        .header h1 {
            font-size: 24px;
            color: #333;
        }

        .nav {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .nav a {
            padding: 8px 16px;
            text-decoration: none;
            background: #667eea;
            color: white;
            border-radius: 5px;
            transition: background 0.3s;
        }

        .nav a.active {
            background: #764ba2;
        }

        .nav a:hover {
            background: #764ba2;
        }

        .nav .logout {
            background: #dc3545;
        }

        .nav .logout:hover {
            background: #c82333;
        }

        .stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }

        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            text-align: center;
            animation: fadeInUp 0.5s ease-out forwards;
            opacity: 0;
        }

        .stat-card:nth-child(1) { animation-delay: 0.1s; }
        .stat-card:nth-child(2) { animation-delay: 0.2s; }
        .stat-card:nth-child(3) { animation-delay: 0.3s; }
        .stat-card:nth-child(4) { animation-delay: 0.4s; }

        .stat-card h3 {
            font-size: 14px;
            color: #666;
            margin-bottom: 10px;
        }

        .stat-card .value {
            font-size: 28px;
            font-weight: bold;
            color: #333;
        }

        .from-numbers-filter {
            background: white;
            padding: 15px;
            border-radius: 10px;
            margin-bottom: 15px;
            animation: fadeInUp 0.5s ease-out forwards;
            opacity: 0;
            animation-delay: 0.5s;
        }

        .from-numbers-filter h3 {
            font-size: 14px;
            color: #666;
            margin-bottom: 10px;
        }

        .from-numbers-filter .tags {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .from-numbers-filter .tag {
            padding: 5px 10px;
            background: #f0f0f0;
            border-radius: 5px;
            font-size: 12px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .from-numbers-filter .tag:hover {
            background: #667eea;
            color: white;
        }

        .from-numbers-filter .tag.active {
            background: #667eea;
            color: white;
        }

        .toolbar {
            display: flex;
            gap: 10px;
            margin-bottom: 15px;
            flex-wrap: wrap;
            align-items: center;
            background: white;
            padding: 15px;
            border-radius: 10px;
            animation: fadeInUp 0.5s ease-out forwards;
            opacity: 0;
            animation-delay: 0.6s;
        }

        .toolbar input, .toolbar select {
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }

        .toolbar button {
            padding: 8px 16px;
            background: #667eea;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .toolbar button:hover {
            background: #764ba2;
        }

        .toolbar .refresh-btn {
            margin-left: auto;
        }

        .auto-refresh-info {
            display: flex;
            align-items: center;
            gap: 10px;
            color: #666;
            font-size: 14px;
        }

        .refresh-toggle {
            display: inline-flex;
            align-items: center;
            gap: 5px;
            padding: 5px 10px;
            background: #f0f0f0;
            border-radius: 5px;
            cursor: pointer;
            user-select: none;
        }

        .refresh-toggle.active {
            background: #d4edda;
            color: #155724;
        }

        .messages-list {
            background: white;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            animation: fadeInUp 0.5s ease-out forwards;
            opacity: 0;
            animation-delay: 0.7s;
        }

        .list-view {
            list-style: none;
        }

        .list-view li {
            padding: 15px;
            border-bottom: 1px solid #eee;
            transition: background 0.3s;
        }

        .list-view li:hover {
            background: #f9f9f9;
        }

        .list-view li:last-child {
            border-bottom: none;
        }

        .msg-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
        }

        .from-number {
            font-weight: bold;
            color: #667eea;
            font-size: 15px;
        }

        .msg-time {
            font-size: 12px;
            color: #999;
        }

        .msg-content {
            color: #333;
            font-size: 14px;
            line-height: 1.6;
            word-break: break-all;
        }

        .sign-badge {
            display: inline-block;
            padding: 2px 8px;
            border-radius: 10px;
            font-size: 12px;
            font-weight: bold;
            margin-left: 10px;
        }

        .sign-badge.yes {
            background: #d4edda;
            color: #155724;
        }

        .sign-badge.no {
            background: #f8d7da;
            color: #721c24;
        }

        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 10px;
            margin-top: 20px;
            padding: 20px;
            animation: fadeInUp 0.5s ease-out forwards;
            opacity: 0;
            animation-delay: 0.8s;
        }

        .pagination a, .pagination span {
            padding: 8px 12px;
            background: white;
            border-radius: 5px;
            text-decoration: none;
            color: #333;
        }

        .pagination a:hover {
            background: #667eea;
            color: white;
        }

        .pagination span.active {
            background: #667eea;
            color: white;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #999;
        }

        .empty-state h3 {
            margin-bottom: 10px;
            color: #666;
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes slideInDown {
            from {
                opacity: 0;
                transform: translateY(-30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                gap: 15px;
            }

            .stats {
                grid-template-columns: 1fr 1fr;
            }

            .toolbar {
                flex-direction: column;
                align-items: stretch;
            }

            .toolbar .refresh-btn {
                margin-left: 0;
            }

            .nav {
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📱 短信转发接收端</h1>
            <div class="nav">
                <a href="/" class="active">短信列表</a>
                <a href="/logs">接收日志</a>
                <a href="/statistics">统计信息</a>
                <a href="/logout" class="logout">退出登录</a>
            </div>
        </div>

        <div class="stats">
            <div class="stat-card">
                <h3>短信总数</h3>
                <div class="value">{{.stats.Total}}</div>
            </div>
            <div class="stat-card">
                <h3>今日</h3>
                <div class="value">{{.stats.Today}}</div>
            </div>
            <div class="stat-card">
                <h3>本周</h3>
                <div class="value">{{.stats.Week}}</div>
            </div>
            <div class="stat-card">
                <h3>签名验证</h3>
                <div class="value">{{.stats.Verified}} / {{add .stats.Verified .stats.Unverified}}</div>
            </div>
        </div>

        {{if gt (len .fromNumbers) 0}}
        <div class="from-numbers-filter">
            <h3>快捷筛选 (按号码)</h3>
            <div class="tags">
                <span class="tag {{if eq .selectedFrom ""}}active{{end}}" onclick="filterByNumber('')">全部</span>
                {{range .fromNumbers}}
                <span class="tag {{if eq $.selectedFrom .}}active{{end}}" onclick="filterByNumber('{{.}}')">{{.}}</span>
                {{end}}
            </div>
        </div>
        {{end}}

        <div class="toolbar">
            <input type="text" id="searchInput" placeholder="搜索内容或号码..." value="{{.search}}">
            <button onclick="search()">搜索</button>
            <button onclick="clearSearch()">清除</button>
            <div class="auto-refresh-info">
                <span class="refresh-toggle active" id="refreshToggle">
                    <input type="checkbox" id="autoRefresh" checked onchange="toggleAutoRefresh()">
                    <span>自动刷新</span>
                </span>
                <span id="refreshCountdown">30s</span>
            </div>
            <div class="refresh-btn">
                <button onclick="location.reload()">立即刷新</button>
            </div>
        </div>

        <div class="messages-list">
            {{if gt (len .messages) 0}}
            <ul class="list-view">
                {{range .messages}}
                <li>
                    <div class="msg-header">
                        <span class="from-number">{{.FromNumber}}</span>
                        <span class="msg-time">{{if .LocalTimestampStr}}{{.LocalTimestampStr}}{{else}}{{.CreatedAt.Format "2006-01-02 15:04:05"}}{{end}}</span>
                    </div>
                    <div class="msg-content">
                        {{.Content}}
                        {{if .SignVerified.Valid}}
                            {{if .SignVerified.Bool}}
                            <span class="sign-badge yes">已验证</span>
                            {{else}}
                            <span class="sign-badge no">未验证</span>
                            {{end}}
                        {{end}}
                    </div>
                </li>
                {{end}}
            </ul>
            {{else}}
            <div class="empty-state">
                <h3>暂无短信</h3>
                <p>等待接收短信...</p>
            </div>
            {{end}}
        </div>

        {{if gt .totalPages 1}}
        <div class="pagination">
            {{if gt .page 1}}
            <a href="/?page={{sub .page 1}}&from={{.selectedFrom}}&search={{.search}}">上一页</a>
            {{end}}

            {{range $p := (seq 1 .totalPages)}}
                {{if eq $p $.page}}
                <span class="active">{{$p}}</span>
                {{else if or (le $p 3) (ge $p (sub $.totalPages 2)) (and (ge $p (sub $.page 1)) (le $p (add $.page 1)))}}
                <a href="/?page={{$p}}&from={{$.selectedFrom}}&search={{$.search}}">{{$p}}</a>
                {{else if or (eq $p 4) (eq $p (sub $.totalPages 2))}}
                <span>...</span>
                {{end}}
            {{end}}

            {{if lt .page .totalPages}}
            <a href="/?page={{add .page 1}}&from={{.selectedFrom}}&search={{.search}}">下一页</a>
            {{end}}

            <span>共 {{.total}} 条，第 {{.page}} / {{.totalPages}} 页</span>
        </div>
        {{end}}
    </div>

    <script>
        let refreshInterval;
        let countdownInterval;
        let refreshCountdown = 30;
        const REFRESH_INTERVAL = 30;

        function search() {
            const query = document.getElementById('searchInput').value;
            window.location.href = '/?search=' + encodeURIComponent(query){{if .selectedFrom}}+ '&from={{.selectedFrom}}'{{end}};
        }

        function clearSearch() {
            window.location.href = '/'{{if .selectedFrom}}+ '?from={{.selectedFrom}}'{{end}};
        }

        function filterByNumber(number) {
            let url = '/';
            const params = new URLSearchParams();
            if (number) params.set('from', number);
            {{if .search}}params.set('search', '{{.search}}');{{end}}
            const qs = params.toString();
            if (qs) url += '?' + qs;
            window.location.href = url;
        }

        function toggleAutoRefresh() {
            const checkbox = document.getElementById('autoRefresh');
            const toggle = document.getElementById('refreshToggle');
            
            if (checkbox.checked) {
                toggle.classList.add('active');
                startAutoRefresh();
            } else {
                toggle.classList.remove('active');
                stopAutoRefresh();
            }
        }

        function startAutoRefresh() {
            refreshCountdown = REFRESH_INTERVAL;
            updateCountdown();

            refreshInterval = setInterval(() => {
                const params = new URLSearchParams();
                {{if .search}}params.set('search', '{{.search}}');{{end}}
                {{if .selectedFrom}}params.set('from', '{{.selectedFrom}}');{{end}}
                const qs = params.toString();
                window.location.href = '/?' + qs;
            }, REFRESH_INTERVAL * 1000);

            countdownInterval = setInterval(() => {
                refreshCountdown--;
                updateCountdown();
            }, 1000);
        }

        function stopAutoRefresh() {
            clearInterval(refreshInterval);
            clearInterval(countdownInterval);
            document.getElementById('refreshCountdown').textContent = '--s';
        }

        function updateCountdown() {
            document.getElementById('refreshCountdown').textContent = refreshCountdown + 's';
        }

        document.getElementById('searchInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                search();
            }
        });

        // 初始化自动刷新
        document.addEventListener('DOMContentLoaded', function() {
            startAutoRefresh();
        });
    </script>
</body>
</html>