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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// === WORLD CUP 2026 LIVE SECTION ===
|
| 2 |
+
// Auto-refreshes every 90s, shows on homepage
|
| 3 |
+
// News + Đường tới World Cup interleaved by newest
|
| 4 |
+
|
| 5 |
+
let _wc2026Data = null;
|
| 6 |
+
let _wcRefreshInterval = null;
|
| 7 |
+
|
| 8 |
+
async function loadWC2026Section() {
|
| 9 |
+
try {
|
| 10 |
+
const data = await fetch('/api/wc2026').then(r => r.json()).catch(() => null);
|
| 11 |
+
if (!data) return;
|
| 12 |
+
_wc2026Data = data;
|
| 13 |
+
renderWC2026();
|
| 14 |
+
} catch(e) {}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
function renderWC2026() {
|
| 18 |
+
const data = _wc2026Data;
|
| 19 |
+
if (!data) return;
|
| 20 |
+
const home = document.getElementById('view-home');
|
| 21 |
+
if (!home) return;
|
| 22 |
+
|
| 23 |
+
// Remove old WC section if exists
|
| 24 |
+
document.getElementById('wc2026-live-section')?.remove();
|
| 25 |
+
|
| 26 |
+
const section = document.createElement('div');
|
| 27 |
+
section.id = 'wc2026-live-section';
|
| 28 |
+
section.className = 'wc2026-section';
|
| 29 |
+
|
| 30 |
+
let h = '<div class="wc-header"><h2>🏆 World Cup 2026</h2><span class="wc-live-badge">● LIVE</span></div>';
|
| 31 |
+
|
| 32 |
+
// === Tabs ===
|
| 33 |
+
h += '<div class="wc-tabs">';
|
| 34 |
+
h += '<span class="wc-tab active" onclick="switchWCTab(\'news\')">📰 Tin tức</span>';
|
| 35 |
+
h += '<span class="wc-tab" onclick="switchWCTab(\'fixtures\')">📅 Lịch thi đấu</span>';
|
| 36 |
+
h += '<span class="wc-tab" onclick="switchWCTab(\'standings\')">🏆 BXH</span>';
|
| 37 |
+
h += '<span class="wc-tab" onclick="switchWCTab(\'stats\')">📊 Thống kê</span>';
|
| 38 |
+
h += '</div>';
|
| 39 |
+
h += '<div class="wc-content" id="wc-content"></div>';
|
| 40 |
+
|
| 41 |
+
section.innerHTML = h;
|
| 42 |
+
|
| 43 |
+
// Insert after livescore section
|
| 44 |
+
const lsSection = home.querySelector('.ls-section');
|
| 45 |
+
if (lsSection) lsSection.after(section);
|
| 46 |
+
else home.appendChild(section);
|
| 47 |
+
|
| 48 |
+
// Default: show news + road interleaved
|
| 49 |
+
switchWCTab('news');
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
function switchWCTab(tab) {
|
| 53 |
+
document.querySelectorAll('.wc-tab').forEach(t => t.classList.remove('active'));
|
| 54 |
+
document.querySelectorAll('.wc-tab').forEach(t => {
|
| 55 |
+
if ((tab === 'news' && t.textContent.includes('Tin')) ||
|
| 56 |
+
(tab === 'fixtures' && t.textContent.includes('Lịch')) ||
|
| 57 |
+
(tab === 'standings' && t.textContent.includes('BXH')) ||
|
| 58 |
+
(tab === 'stats' && t.textContent.includes('Thống')))
|
| 59 |
+
t.classList.add('active');
|
| 60 |
+
});
|
| 61 |
+
const el = document.getElementById('wc-content');
|
| 62 |
+
if (!el || !_wc2026Data) return;
|
| 63 |
+
|
| 64 |
+
if (tab === 'news') renderWCNews(el);
|
| 65 |
+
else if (tab === 'fixtures') renderWCFixtures(el);
|
| 66 |
+
else if (tab === 'standings') renderWCStandings(el);
|
| 67 |
+
else if (tab === 'stats') renderWCStats(el);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
function renderWCNews(el) {
|
| 71 |
+
const news = _wc2026Data.news || [];
|
| 72 |
+
const road = _wc2026Data.road || [];
|
| 73 |
+
|
| 74 |
+
// Interleave news + road by newest (alternate)
|
| 75 |
+
const combined = [];
|
| 76 |
+
let ni = 0, ri = 0;
|
| 77 |
+
while (ni < news.length || ri < road.length) {
|
| 78 |
+
if (ni < news.length) combined.push({...news[ni++], _type: 'news'});
|
| 79 |
+
if (ri < road.length) combined.push({...road[ri++], _type: 'road'});
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
if (!combined.length) { el.innerHTML = '<div class="loading">Đang cập nhật...</div>'; return; }
|
| 83 |
+
|
| 84 |
+
let h = '<div class="wc-news-grid">';
|
| 85 |
+
combined.slice(0, 20).forEach(a => {
|
| 86 |
+
const badge = a._type === 'road' ? '<span class="badge badge-wc">Đường tới WC</span>' : '<span class="badge badge-fpt">Tin WC</span>';
|
| 87 |
+
h += `<div class="wc-news-item" onclick="readArticle('${esc(a.link)}')">`;
|
| 88 |
+
if (a.img) h += `<div class="wc-news-img"><img src="${esc(a.img)}" onerror="this.style.display='none'"></div>`;
|
| 89 |
+
h += `<div class="wc-news-text">${badge}<div class="wc-news-title">${esc(a.title)}</div><div class="wc-news-via">${esc(a.source || '')}</div></div></div>`;
|
| 90 |
+
});
|
| 91 |
+
h += '</div>';
|
| 92 |
+
el.innerHTML = h;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
function renderWCFixtures(el) {
|
| 96 |
+
const fixtures = _wc2026Data.fixtures || [];
|
| 97 |
+
|
| 98 |
+
if (fixtures.length && fixtures[0].html) {
|
| 99 |
+
el.innerHTML = fixtures[0].html;
|
| 100 |
+
return;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
if (!fixtures.length) { el.innerHTML = '<div class="loading">Chưa có lịch thi đấu</div>'; return; }
|
| 104 |
+
|
| 105 |
+
let h = '<div class="wc-fixtures">';
|
| 106 |
+
let currentGroup = '';
|
| 107 |
+
fixtures.forEach(m => {
|
| 108 |
+
if (m.group && m.group !== currentGroup) {
|
| 109 |
+
currentGroup = m.group;
|
| 110 |
+
h += `<div class="wc-group-header">${esc(m.group)}</div>`;
|
| 111 |
+
}
|
| 112 |
+
const isLive = (m.status || '').toLowerCase().includes('live') || (m.status || '').includes('đang');
|
| 113 |
+
h += `<div class="wc-match ${isLive ? 'wc-match-live' : ''}">`;
|
| 114 |
+
h += `<div class="wc-match-date">${esc(m.date)}</div>`;
|
| 115 |
+
h += `<div class="wc-match-teams"><span class="wc-team-home">${esc(m.home)}</span><span class="wc-match-score">${esc(m.score || 'vs')}</span><span class="wc-team-away">${esc(m.away)}</span></div>`;
|
| 116 |
+
if (isLive) h += '<div class="wc-match-live-badge">🔴 LIVE</div>';
|
| 117 |
+
h += '</div>';
|
| 118 |
+
});
|
| 119 |
+
h += '</div>';
|
| 120 |
+
el.innerHTML = h;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
function renderWCStandings(el) {
|
| 124 |
+
const standings = _wc2026Data.standings || {};
|
| 125 |
+
|
| 126 |
+
if (standings.html) {
|
| 127 |
+
el.innerHTML = standings.html;
|
| 128 |
+
return;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
const groups = Object.entries(standings);
|
| 132 |
+
if (!groups.length) { el.innerHTML = '<div class="loading">Chưa có BXH</div>'; return; }
|
| 133 |
+
|
| 134 |
+
let h = '';
|
| 135 |
+
groups.forEach(([name, teams]) => {
|
| 136 |
+
h += `<div class="wc-group-header">${esc(name)}</div>`;
|
| 137 |
+
h += '<table class="wc-table"><tr><th>Đội</th><th>Tr</th><th>Đ</th></tr>';
|
| 138 |
+
(teams || []).forEach(t => {
|
| 139 |
+
h += `<tr><td class="wc-team-name">${esc(t.team)}</td><td>${esc(t.played)}</td><td class="wc-pts">${esc(t.pts)}</td></tr>`;
|
| 140 |
+
});
|
| 141 |
+
h += '</table>';
|
| 142 |
+
});
|
| 143 |
+
el.innerHTML = h;
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
function renderWCStats(el) {
|
| 147 |
+
const stats = _wc2026Data.stats || {};
|
| 148 |
+
|
| 149 |
+
if (stats.html) {
|
| 150 |
+
el.innerHTML = stats.html;
|
| 151 |
+
return;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
let h = '';
|
| 155 |
+
if (stats.top_scorers && stats.top_scorers.length) {
|
| 156 |
+
h += '<div class="wc-group-header">⚽ Vua phá lưới</div><div class="wc-stats-list">';
|
| 157 |
+
stats.top_scorers.forEach((p, i) => {
|
| 158 |
+
h += `<div class="wc-stat-row"><span class="wc-stat-rank">${i + 1}</span><span class="wc-stat-name">${esc(p.player)}</span><span class="wc-stat-team">${esc(p.team)}</span><span class="wc-stat-val">${esc(p.goals)}</span></div>`;
|
| 159 |
+
});
|
| 160 |
+
h += '</div>';
|
| 161 |
+
}
|
| 162 |
+
if (stats.top_assists && stats.top_assists.length) {
|
| 163 |
+
h += '<div class="wc-group-header">🎯 Kiến tạo</div><div class="wc-stats-list">';
|
| 164 |
+
stats.top_assists.forEach((p, i) => {
|
| 165 |
+
h += `<div class="wc-stat-row"><span class="wc-stat-rank">${i + 1}</span><span class="wc-stat-name">${esc(p.player)}</span><span class="wc-stat-team">${esc(p.team)}</span><span class="wc-stat-val">${esc(p.assists)}</span></div>`;
|
| 166 |
+
});
|
| 167 |
+
h += '</div>';
|
| 168 |
+
}
|
| 169 |
+
if (!h) h = '<div class="loading">Chưa có thống kê chi tiết</div>';
|
| 170 |
+
el.innerHTML = h;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// === LIVE AUTO-REFRESH (every 90s) ===
|
| 174 |
+
function startWCLiveRefresh() {
|
| 175 |
+
if (_wcRefreshInterval) clearInterval(_wcRefreshInterval);
|
| 176 |
+
_wcRefreshInterval = setInterval(async () => {
|
| 177 |
+
if (!document.getElementById('view-home')?.classList.contains('active')) return;
|
| 178 |
+
try {
|
| 179 |
+
const data = await fetch('/api/wc2026').then(r => r.json()).catch(() => null);
|
| 180 |
+
if (data) {
|
| 181 |
+
_wc2026Data = data;
|
| 182 |
+
// Only re-render if WC section is visible
|
| 183 |
+
const wcSection = document.getElementById('wc2026-live-section');
|
| 184 |
+
if (wcSection) {
|
| 185 |
+
const activeTab = document.querySelector('.wc-tab.active');
|
| 186 |
+
if (activeTab) {
|
| 187 |
+
const txt = activeTab.textContent;
|
| 188 |
+
if (txt.includes('Tin')) renderWCNews(document.getElementById('wc-content'));
|
| 189 |
+
else if (txt.includes('Lịch')) renderWCFixtures(document.getElementById('wc-content'));
|
| 190 |
+
else if (txt.includes('BXH')) renderWCStandings(document.getElementById('wc-content'));
|
| 191 |
+
else if (txt.includes('Thống')) renderWCStats(document.getElementById('wc-content'));
|
| 192 |
+
}
|
| 193 |
+
}
|
| 194 |
+
}
|
| 195 |
+
} catch(e) {}
|
| 196 |
+
}, 90000); // 90 seconds
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
// Load WC2026 after homepage loads
|
| 200 |
+
setTimeout(() => { loadWC2026Section(); startWCLiveRefresh(); }, 3000);
|