<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Haberler</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        header {
            background-color: #007bff;
            color: white;
            padding: 20px;
            text-align: center;
        }

        .news-container {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            justify-content: center;
            padding: 20px;
        }

        .news-card {
            background-color: white;
            width: 300px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 15px;
            border-radius: 8px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .news-card img {
            width: 100%;
            height: 180px;
            object-fit: cover;
            border-radius: 8px;
        }

        .news-card h3 {
            font-size: 1.2em;
            color: #007bff;
        }

        .news-card p {
            font-size: 1em;
            color: #333;
        }

        .news-card .date {
            font-size: 0.8em;
            color: #666;
        }

        .news-card a {
            text-decoration: none;
            color: #007bff;
            font-weight: bold;
        }

    </style>
</head>
<body>
    <header>
        <h1 onclick="window.location.href='https://blindzer.com/user-login/'">Güncel Haberler</h1>

    </header>
    <div class="news-container" id="newsContainer">
    </div>

    <script>
        const sitemapUrl = "https://www.ahaber.com.tr/sitemap/news.xml";
        function timeAgo(date) {
            const now = new Date();
            const diffInSeconds = Math.floor((now - date) / 1000);
            const diffInMinutes = Math.floor(diffInSeconds / 60);
            const diffInHours = Math.floor(diffInMinutes / 60);
            const diffInDays = Math.floor(diffInHours / 24);

            if (diffInSeconds < 60) return "az önce";
            if (diffInMinutes < 60) return `${diffInMinutes} dakika önce`;
            if (diffInHours < 24) return `${diffInHours} saat önce`;
            if (diffInDays === 1) return "bir gün önce";
            if (diffInDays < 7) return `${diffInDays} gün önce`;
            return date.toLocaleDateString();
        }

        fetch(sitemapUrl)
            .then(response => response.text())
            .then(data => {
                const parser = new DOMParser();
                const xmlDoc = parser.parseFromString(data, "application/xml");
                const urls = xmlDoc.getElementsByTagName("url");
                const newsContainer = document.getElementById('newsContainer');
                Array.from(urls).forEach(url => {
                    const loc = url.getElementsByTagName("loc")[0].textContent;
                    const title = url.getElementsByTagName("news:title")[0].textContent;
                    const publicationDate = url.getElementsByTagName("news:publication_date")[0].textContent;
                    const imageLoc = url.getElementsByTagName("image:loc")[0]?.textContent || null;
                    const date = new Date(publicationDate);
                    const relativeTime = timeAgo(date);
                    const newsCard = document.createElement('div');
                    newsCard.classList.add('news-card');

                    if (imageLoc) {
                        const img = document.createElement('img');
                        img.src = imageLoc;
                        newsCard.appendChild(img);
                    }
                    const newsTitle = document.createElement('h3');
                    newsTitle.textContent = title;
                    newsCard.appendChild(newsTitle);
                    const newsDate = document.createElement('p');
                    newsDate.classList.add('date');
                    newsDate.textContent = `Yayınlanma Tarihi: ${relativeTime}`;
                    newsCard.appendChild(newsDate);
                    const newsLink = document.createElement('a');
                    newsLink.href = "#"; 
                    newsLink.textContent = "Detayları Gör";
                    newsCard.appendChild(newsLink);
                    newsContainer.appendChild(newsCard);
                });
            })
            .catch(error => {
                console.error("XML dosyası alınırken bir hata oluştu:", error);
            });
    </script>
</body>
</html>
