<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>#loading-message{text-align:center;font-family:var(--custom-font-family)!important;color:var(--custom-text-color)!important;display:none;margin-top:30px;margin-bottom:40px}.info-container{margin-bottom:20px;text-align:center;font-family:var(--custom-font-family)!important;font-size:var(--custom-font-size);color:var(--custom-text-color);line-height:var(--custom-line-height)}.cover{max-width:150px;margin:0 auto 10px;margin-top:20px}.cover img{width:100%;height:auto;object-fit:cover;border-radius:8px;aspect-ratio:2 / 3}.details{text-align:center;font-family:var(--custom-font-family)!important;font-size:var(--custom-font-size);color:var(--custom-text-color);line-height:var(--custom-line-height)}.details h2,.s1{margin-top:2px;margin-bottom:0;font-family:var(--custom-font-family)!important;font-size:var(--custom-font-size)!important;color:var(--custom-text-color)!important;line-height:var(--custom-line-height)}.details p{margin:10px 0}.content-block[data-content]::before{content:attr(data-content);display:block;margin-bottom:12px;margin-top:12px}.genre-label{display:inline-block;padding:2px 6px;margin:5px;font-family:var(--custom-font-family);background:var(--custom-text-color);color:var(--custom-background-color);line-height:var(--custom-line-height)}.alert.info{text-align:justify}.related-posts{display:block;margin-top:10px}.post-title{display:block;margin-bottom:5px;text-decoration:none;color:#007bff}.post-title:hover{text-decoration:underline}.pRelate,a.post-title,.alert.outline{font-family:var(--custom-font-family)!important;font-size:var(--custom-font-size)!important;color:var(--custom-text-color)!important;line-height:var(--custom-line-height)!important}b{font-family:var(--custom-font-family)!important;font-size:var(--custom-font-size)!important;line-height:var(--custom-line-height)!important}.pRelate{font-style:italic}</style>
</head>
<body>
    <!-- Thông báo Loading -->
    <div id="loading-message">Đang tải...</div>

    <div class="info-container" id="info-container" style="display: none;">
        <div class="cover" id="cover"></div>
        <div class="details" id="details"></div>
    </div>
  
  <div class='alert outline' style="display: none;"></div>

    <div class="introduction-container" id="introduction" style="display: none;">
        <blockquote class="s1">
            <p id="intro-text" style="text-align: justify;"></p>
            <span id="author-name"></span>
        </blockquote>
    </div>
  
    <div id="chapterContent">
        <h3 id="contentTitle"></h3>
        <div id="contentText"></div>
    </div>
  
	      <div class='pRelate' style="display: none;">
        Thử đọc tác phẩm khác nè:
        <div class="related-posts">
            <!-- Các liên kết gợi ý sẽ được thêm vào đây từ API -->
        </div>
    </div>

<script>document.addEventListener('DOMContentLoaded',function(){const isValidHash=(hash)=>/^#[^#]*-.*$/.test(hash);function getValidHash(){const hash=window.location.hash;if(isValidHash(hash)){localStorage.setItem('lastTabName',hash);return hash}else{return localStorage.getItem('lastTabName')||''}}
function fetchData(url){document.getElementById('loading-message').style.display='block';document.getElementById("info-container").style.display='none';document.getElementById("introduction").style.display='none';document.getElementById("chapterContent").style.display='none';document.querySelector('.alert.outline').style.display='none';document.querySelector('.pRelate').style.display='none';fetch(url).then(response=>{if(!response.ok){throw new Error(`HTTP error! status: ${response.status}`)}
return response.json()}).then(data=>{if(data.error){document.getElementById("details").innerHTML=`<p>${data.error}</p>`;return}
updateUI(data)}).catch(error=>{console.error("Error fetching data:",error);document.getElementById("details").innerHTML="<p>Đã xảy ra lỗi khi tải dữ liệu. Vui lòng thử lại sau.</p>"})}
function updateUI(data){const story=data;const formattedIntroduction=`<span style="font-family: var(--custom-font-family) !important;">${(story['Giới thiệu'] || "").replace(/\n\n/g, '<br>')}</span>`;const genres=story['Thể loại']?story['Thể loại'].split(','):['Không rõ'];const rawTime=story['Thời gian']||'';let formattedTime='Không xác định';if(rawTime){const date=new Date(rawTime);if(!isNaN(date)){formattedTime=date.toLocaleString('vi-VN',{hour:'2-digit',minute:'2-digit',second:'2-digit',day:'2-digit',month:'2-digit',year:'numeric'})}}
document.getElementById("cover").innerHTML=`<img src="${story['Url bìa'] || ''}" alt="${story['Tác phẩm'] || 'Không có tiêu đề'}">`;document.getElementById("details").innerHTML=`
        <h2 style="font-family: var(--custom-font-family) !important;">${story['Tác phẩm'] || 'Không có tiêu đề'}</h2>
        <p style="font-family: var(--custom-font-family) !important;">
            <strong>Tác giả:</strong> ${story['Tác giả'] || 'Không rõ'} |
            <strong>Lượt xem: </strong>${story['Lượt xem'] || '0'} |
            <strong>Thời gian:</strong> ${formattedTime}
        </p>
        <p style="font-family: var(--custom-font-family) !important;">
            ${genres.map(genre => `<span class="genre-label">${genre.trim()}</span>`).join(' ')}
        </p>
    `;document.getElementById("intro-text").innerHTML=formattedIntroduction||"Không có giới thiệu.";document.getElementById("author-name").innerHTML=story['Tác giả']||'';const message=story['Lời nhắn']||'Không có';const alertElement=document.querySelector(".alert.outline");if(message==='Không có'){alertElement.style.display='none'}else{alertElement.innerHTML=`<b>★ Lời nhắn:</b> ${message}`;alertElement.style.textAlign="justify";alertElement.style.display='block'}
displayContent({content:story['Nội dung']||''});if(story['Gợi ý']&&Array.isArray(story['Gợi ý'])){const relatedPostsContainer=document.querySelector('.related-posts');relatedPostsContainer.innerHTML='';story['Gợi ý'].forEach(post=>{const postLink=document.createElement('a');postLink.href="javascript:void(0);";postLink.classList.add('post-title');postLink.setAttribute('data-tab',post);postLink.innerHTML=post;postLink.addEventListener('click',function(){const newTabName=post;const cleanURL=window.location.origin+window.location.pathname;window.location.replace(`${cleanURL}${newTabName}`)});relatedPostsContainer.appendChild(postLink)})}
document.getElementById('loading-message').style.display='none';document.getElementById("info-container").style.display='block';document.getElementById("introduction").style.display='block';document.getElementById("chapterContent").style.display='block';document.querySelector('.pRelate').style.display=''}
function displayContent(content){document.getElementById('contentText').innerHTML='';const paragraphs=content.content.split('\n').filter(p=>p.trim().length>0);paragraphs.forEach(paragraph=>{const formattedParagraph=paragraph.trim();const contentElement=document.createElement('div');contentElement.classList.add('content-block');contentElement.setAttribute('data-content',formattedParagraph);document.getElementById('contentText').appendChild(contentElement)})}
function handleHashChange(){const validHash=getValidHash();if(validHash){const apiUrl=`https://script.google.com/macros/s/AKfycbxhFe6QjOSovP3SyCQQnEy0PP1jicOBY_p4nqJqfDr0Tc1af-lyji9cHcCWbRYbHol9/exec?title=${encodeURIComponent(validHash)}`;fetchData(apiUrl)}}
handleHashChange();window.addEventListener('hashchange',handleHashChange)});</script>
</body>
</html>
