@extends('layouts.home')

@push('css')
<style>
    /* Reset and Base Styles */
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    :root {
        --primary: #1a73e8;
        --secondary: #34a853;
        --accent: #f9345e;
        --light: #f8f9fa;
        --dark: #202124;
        --text: #3c4043;
        --shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    body {
        color: var(--text);
        line-height: 1.6;
        background-color: #f9f9f9;
        overflow-x: hidden;
    }

    a {
        text-decoration: none;
        text-underline-offset: none;
    }

    .container {
        width: 100%;
        max-width: 1200px;
        margin: 0 auto;
        padding: 0 20px;
    }


    /* Header Styles */
    header {
        background-color: white;
        box-shadow: var(--shadow);
        position: sticky;
        top: 0;
        z-index: 1000;
    }

    .header-container {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 15px 0;
        position: relative;
    }

    .logo {
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .logo:hover {
        transform: scale(1.05);
        transition: transform 0.5s;
        cursor: pointer;

    }

    .logo i {
        color: var(--primary);
        font-size: 2rem;
    }

    .logo a {
        text-decoration: none;
        text-underline-offset: none;
    }

    .logo h1 {
        font-size: 1.8rem;
        color: var(--primary);
    }

    .nav-menu {
        display: flex;
        list-style: none;
        gap: 30px;
    }

    .nav-menu li {

        padding: 5px;
    }

    .nav-menu li:hover {
        position: relative;
        transform: scale(1.09);
        transition: transform 0.1s;
        border-radius: 5px;
        background: #ddd6d681;

    }

    .nav-menu a {
        text-decoration: none;
        color: var(--text);
        font-weight: 500;
        transition: color 0.3s;
    }

    .nav-menu a:hover {
        color: var(--primary);
    }

    .header-actions {
        display: flex;
        gap: 15px;
        align-items: center;
    }

    .mobile-actions {
        display: none;
        flex-direction: column;
        gap: 15px;
        width: 100%;
        padding: 20px;
    }

    .btn {
        padding: 10px 20px;
        border-radius: 50px;
        border: none;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s;
        text-align: center;
    }

    .btn-primary {
        background-color: var(--primary);
        color: white;
    }

    .btn-primary:hover {
        background-color: #0d5bbb;
    }

    .btn-outline {
        background-color: transparent;
        border: 2px solid var(--primary);
        color: var(--primary);
    }

    .btn-outline:hover {
        background-color: var(--primary);
        color: white;
    }

    /* Dropdown specific styles */
    .nav-menu li.dropdown {
        position: relative;
    }

    .dropdown-menu {
        position: absolute;
        top: 100%;
        left: 0;
        background: white;
        width: 200px;
        border-radius: 8px;
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        opacity: 0;
        visibility: hidden;
        transform: translateY(10px);
        transition: all 0.3s ease;
        z-index: 1000;
        padding: 10px 0;
    }

    .dropdown:hover .dropdown-menu {
        opacity: 1;
        visibility: visible;
        transform: translateY(0);
    }

    .dropdown-menu li {
        padding: 0;
        margin: 0;
    }

    .dropdown-menu li:hover {
        background: #f5f5f5;
        transform: none;
    }

    .dropdown-menu a {
        display: block;
        padding: 10px 20px;
        color: var(--text);
        font-weight: 500;
    }

    .dropdown-menu a:hover {
        color: var(--primary);
    }

    .dropdown>a::after {
        content: '\f107';
        font-family: 'Font Awesome 6 Free';
        font-weight: 900;
        margin-left: 5px;
        font-size: 0.9rem;
    }

    /* Mobile Navigation */
    .hamburger {
        display: none;
        flex-direction: column;
        cursor: pointer;
        padding: 5px;
    }

    .hamburger span {
        width: 25px;
        height: 3px;
        background-color: var(--primary);
        margin: 3px 0;
        transition: all 0.3s ease;
        border-radius: 2px;
    }

    /* Blog Hero Section */
    .blog-hero {
        background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url('https://images.unsplash.com/photo-1579684385127-1ef15d508118?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1180&q=80');
        background-size: cover;
        background-position: center;
        color: white;
        padding: 100px 0;
        text-align: center;
    }

    .blog-content {
        padding: 80px 0;
    }

    .blog-container {
        display: grid;
        grid-template-columns: 2fr 1fr;
        gap: 40px;
    }

    .blog-posts {
        display: flex;
        flex-direction: column;
        gap: 40px;
    }

    .blog-post {
        background-color: white;
        border-radius: 10px;
        overflow: hidden;
        box-shadow: var(--shadow);
        transition: transform 0.3s, box-shadow 0.3s;
    }

    .blog-post:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
    }

    .post-image {
        height: 300px;
        overflow: hidden;
        position: relative;
    }

    .post-image img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: transform 0.5s;
    }

    .blog-post:hover .post-image img {
        transform: scale(1.05);
    }

    .post-content {
        padding: 30px;
    }

    .post-meta {
        display: flex;
        gap: 15px;
        margin-bottom: 15px;
        font-size: 0.9rem;
        color: #666;
    }

    .post-meta i {
        color: var(--primary);
        margin-right: 5px;
    }

    .post-content h3 {
        font-size: 1.5rem;
        margin-bottom: 15px;
        color: var(--dark);
        transition: color 0.3s;
    }

    .blog-post:hover .post-content h3 {
        color: var(--primary);
    }

    .post-excerpt {
        margin-bottom: 20px;
        color: var(--text);
    }

    .read-more {
        display: inline-flex;
        align-items: center;
        color: var(--primary);
        font-weight: 600;
        transition: all 0.3s;
    }

    .read-more i {
        margin-left: 5px;
        transition: transform 0.3s;
    }

    .read-more:hover {
        color: var(--secondary);
    }

    .read-more:hover i {
        transform: translateX(5px);
    }

    .sidebar {
        background-color: white;
        border-radius: 10px;
        padding: 30px;
        box-shadow: var(--shadow);
        height: fit-content;
    }

    .sidebar-widget {
        margin-bottom: 40px;
    }

    .sidebar-widget:last-child {
        margin-bottom: 0;
    }

    .sidebar-widget h3 {
        font-size: 1.3rem;
        margin-bottom: 20px;
        padding-bottom: 10px;
        border-bottom: 2px solid var(--light);
        color: var(--dark);
    }

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

    .categories-list li {
        margin-bottom: 10px;
        padding-bottom: 10px;
        border-bottom: 1px solid #eee;
        transition: padding-left 0.3s;
    }

    .categories-list li:hover {
        padding-left: 5px;
    }

    .categories-list li:last-child {
        margin-bottom: 0;
        padding-bottom: 0;
        border-bottom: none;
    }

    .categories-list a {
        color: var(--text);
        display: flex;
        justify-content: space-between;
        transition: color 0.3s;
    }

    .categories-list a:hover {
        color: var(--primary);
    }

    .recent-post {
        display: flex;
        gap: 15px;
        margin-bottom: 15px;
        padding-bottom: 15px;
        border-bottom: 1px solid #eee;
        transition: transform 0.3s;
    }

    .recent-post:hover {
        transform: translateX(5px);
    }

    .recent-post:last-child {
        margin-bottom: 0;
        padding-bottom: 0;
        border-bottom: none;
    }

    .recent-post-image {
        width: 80px;
        height: 80px;
        border-radius: 5px;
        overflow: hidden;
        flex-shrink: 0;
    }

    .recent-post-image img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: transform 0.3s;
    }

    .recent-post:hover .recent-post-image img {
        transform: scale(1.1);
    }

    .recent-post-content h4 {
        font-size: 0.9rem;
        margin-bottom: 5px;
        transition: color 0.3s;
    }

    .recent-post:hover .recent-post-content h4 {
        color: var(--primary);
    }

    .recent-post-content span {
        font-size: 0.8rem;
        color: #666;
    }

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

    .tag {
        background-color: var(--light);
        color: var(--text);
        padding: 5px 15px;
        border-radius: 50px;
        font-size: 0.9rem;
        transition: all 0.3s;
        cursor: pointer;
    }

    .tag:hover,
    .tag.active {
        background-color: var(--primary);
        color: white;
    }

    .search-form {
        position: relative;
    }

    .search-form input {
        width: 100%;
        padding: 12px 15px;
        border: 1px solid #ddd;
        border-radius: 5px;
        font-size: 1rem;
        padding-right: 45px;
    }

    .search-form button {
        position: absolute;
        right: 5px;
        top: 50%;
        transform: translateY(-50%);
        background: transparent;
        border: none;
        color: var(--primary);
        font-size: 1.2rem;
        cursor: pointer;
        padding: 5px;
    }

    .newsletter-form {
        display: flex;
        flex-direction: column;
        gap: 15px;
    }

    .newsletter-form input {
        padding: 12px 15px;
        border: 1px solid #ddd;
        border-radius: 5px;
        font-size: 1rem;
    }

    .pagination {
        display: flex;
        justify-content: center;
        gap: 10px;
        margin-top: 50px;
    }

    .page-link {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        border-radius: 5px;
        background-color: white;
        color: var(--text);
        font-weight: 600;
        transition: all 0.3s;
        box-shadow: var(--shadow);
    }

    .page-link:hover,
    .page-link.active {
        background-color: var(--primary);
        color: white;
        transform: translateY(-2px);
    }

    /* Appointment CTA */
    .appointment-cta {
        padding: 80px 0;
        background: linear-gradient(to right, var(--primary), var(--secondary));
        color: white;
        text-align: center;
    }

    .appointment-cta h2 {
        font-size: 2.5rem;
        margin-bottom: 20px;
    }

    .appointment-cta button:hover {
        transform: perspective(100px) scale(1.05);
    }

    /* Footer */
    footer {
        background-color: var(--dark);
        color: white;
        padding: 60px 0 30px;
    }

    .footer-content {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 40px;
        margin-bottom: 40px;
    }

    .footer-column h3 {
        font-size: 1.3rem;
        margin-bottom: 20px;
        color: white;
    }

    .footer-column ul {
        list-style: none;
    }

    .footer-column ul li {
        margin-bottom: 10px;
    }

    .footer-column a {
        color: #ddd;
        text-decoration: none;
        transition: color 0.3s;
    }

    .footer-column a:hover {
        color: white;
    }

    .social-links {
        display: flex;
        gap: 15px;
        margin-top: 20px;
    }

    .social-links a {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background-color: rgba(255, 255, 255, 0.1);
        color: white;
        transition: background-color 0.3s;
    }

    .social-links a:hover {
        background-color: var(--primary);
    }

    .copyright {
        text-align: center;
        padding-top: 30px;
        border-top: 1px solid rgba(255, 255, 255, 0.1);
    }

    /* Responsive Styles */
    @media (max-width: 992px) {
        .blog-container {
            grid-template-columns: 1fr;
        }

        .sidebar {
            order: -1;
        }
    }

    @media (max-width: 768px) {
        .hamburger {
            display: flex;
            z-index: 1001;
        }

        .hamburger.active span:nth-child(1) {
            transform: rotate(45deg) translate(5px, 5px);
        }

        .hamburger.active span:nth-child(2) {
            opacity: 0;
        }

        .hamburger.active span:nth-child(3) {
            transform: rotate(-45deg) translate(7px, -6px);
        }

        .nav-menu {
            position: fixed;
            top: 0;
            right: -100%;
            height: 100vh;
            width: 70%;
            flex-direction: column;
            justify-content: flex-start;
            align-items: center;
            background-color: white;
            transition: right 0.3s ease;
            box-shadow: -5px 0 15px rgba(0, 0, 0, 0.1);
            gap: 20px;
            z-index: 999;
            padding-top: 80px;
        }

        .nav-menu.active {
            right: 0;
        }

        .header-actions.desktop-actions {
            display: none;
        }

        .mobile-actions {
            display: flex;
        }

        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s ease;
            z-index: 998;
        }

        .overlay.active {
            opacity: 1;
            visibility: visible;
        }

        .post-image {
            height: 200px;
        }
    }

    @media (max-width: 576px) {
        .post-meta {
            flex-direction: column;
            gap: 5px;
        }

        .recent-post {
            flex-direction: column;
        }

        .recent-post-image {
            width: 100%;
            height: 120px;
        }
    }
</style>
@endpush


@section('content')
<!-- Blog Hero Section -->
<section class="blog-hero">
    <div class="container hero-content">
        <h2>Blog & News</h2>
        <p>Stay updated with the latest health information, medical breakthroughs, and hospital news from TGP Hospital.
        </p>
    </div>
</section>

<!-- Blog Content Section -->
<section class="blog-content">
    <div class="container blog-container">
        <div class="blog-posts">


            @foreach($blogs as $blog)
            <article class="blog-post">
                <div class="post-image">
                    <img src="{{ asset($blog->image) }}" alt="Breakthrough in Heart Surgery">
                </div>
                <div class="post-content">
                    <div class="post-meta">
                        <span><i class="far fa-calendar"></i> {{ date('F d, Y', strtotime($blog->created_at)) }}</span>
                        <span><i class="far fa-user"></i> {{ $blog->author }}</span>
                        <span><i class="far fa-folder"></i> {{ $blog->category->name }}</span>
                    </div>
                    <h3>{{ $blog->title }}</h3>
                    <p class="post-excerpt">{!! \Illuminate\Support\Str::words($blog->content, 100) !!}</p>
                    <a href="#" class="read-more">Read More <i class="fas fa-arrow-right"></i></a>
                </div>
            </article>
            @endforeach

        </div>

        <!-- Sidebar -->
        <div class="sidebar">
            <!-- Search Widget -->
            <div class="sidebar-widget">
                <h3>Search</h3>
                <div class="search-form">
                    <input type="text" id="search-input" placeholder="Search articles...">
                    <button id="search-btn"><i class="fas fa-search"></i></button>
                </div>
            </div>

            <!-- Categories Widget -->
            <div class="sidebar-widget">
                <h3>Categories</h3>
                <ul class="categories-list">
                    @foreach($categories as $key => $category)
                    <li><a href="#">{{ $category->name }}<span>({{ $category->research()->count() }})</span></a></li>
                    @endforeach

                </ul>
            </div>

            <!-- Recent Posts Widget -->
            <div class="sidebar-widget">
                <h3>Recent Posts</h3>
                <div class="recent-posts">


                    @foreach ($recently as $blog)
                    <div class="recent-post">
                        <div class="recent-post-image">
                            <img src="{{ $blog->image }}" alt="Recent Post">
                        </div>
                        <div class="recent-post-content">
                            <h4>{{ $blog->title }}</h4>
                            <span><i class="far fa-calendar"></i> {{ date('F d, Y', strtotime($blog->created_at))
                                }}</span>
                        </div>
                    </div>
                    @endforeach

                </div>
            </div>

            <!-- Tags Widget -->
            <div class="sidebar-widget">
                <h3>Tags</h3>
                <div class="tags-cloud">
                    @foreach($tags as $key => $tag)
                    <span class="tag">{{ $tag->name }}</span>
                    @endforeach
                </div>
            </div>

            <!-- Newsletter Widget -->
            <div class="sidebar-widget">
                <h3>Newsletter</h3>
                <p>Subscribe to our newsletter to get the latest health news and updates.</p>
                <form class="newsletter-form">
                    <input type="email" placeholder="Your Email Address">
                    <button class="btn btn-primary">Subscribe</button>
                </form>
            </div>
        </div>
    </div>
</section>

<!-- Appointment CTA -->
<section class="appointment-cta">
    <div class="container">
        <h2>Need Medical Assistance?</h2>
        <p>Our team of healthcare professionals is ready to help you with your health concerns.</p>
        <a href="{{ route('booking') }}"><button class="btn btn-outline"
                style="background-color: white; color: var(--primary); margin-top: 30px;">Book an Appointment &nbsp; <i
                    class="fa-regular fa-calendar-check"></i></button></a>
    </div>
</section>
@endsection


@push('js')
<script>
    // Mobile Navigation Menu
        const hamburger = document.querySelector('.hamburger');
        const navMenu = document.querySelector('.nav-menu');
        const overlay = document.querySelector('.overlay');
        const body = document.body;

        hamburger.addEventListener('click', () => {
            hamburger.classList.toggle('active');
            navMenu.classList.toggle('active');
            overlay.classList.toggle('active');
            body.style.overflow = body.style.overflow === 'hidden' ? '' : 'hidden';
        });

        overlay.addEventListener('click', () => {
            hamburger.classList.remove('active');
            navMenu.classList.remove('active');
            overlay.classList.remove('active');
            body.style.overflow = '';
        });

        // Close menu when clicking on a link
        document.querySelectorAll('.nav-menu a').forEach(link => {
            link.addEventListener('click', () => {
                hamburger.classList.remove('active');
                navMenu.classList.remove('active');
                overlay.classList.remove('active');
                body.style.overflow = '';
            });
        });

        // Search functionality
        document.getElementById('search-btn').addEventListener('click', function() {
            const searchTerm = document.getElementById('search-input').value.toLowerCase();
            const blogPosts = document.querySelectorAll('.blog-post');
            let resultsFound = false;

            blogPosts.forEach(post => {
                const title = post.querySelector('h3').textContent.toLowerCase();
                const excerpt = post.querySelector('.post-excerpt').textContent.toLowerCase();

                if (title.includes(searchTerm) || excerpt.includes(searchTerm)) {
                    post.style.display = 'block';
                    resultsFound = true;
                    // Highlight the matching text
                    highlightText(post, searchTerm);
                } else {
                    post.style.display = 'none';
                }
            });

            if (!resultsFound && searchTerm) {
                alert('No articles found matching your search.');
                // Show all posts again
                blogPosts.forEach(post => {
                    post.style.display = 'block';
                    removeHighlights(post);
                });
            } else if (!searchTerm) {
                // Show all posts if search is empty
                blogPosts.forEach(post => {
                    post.style.display = 'block';
                    removeHighlights(post);
                });
            }
        });

        // Function to highlight matching text
        function highlightText(element, searchTerm) {
            removeHighlights(element);

            const textNodes = getTextNodes(element);
            textNodes.forEach(node => {
                const text = node.nodeValue;
                const regex = new RegExp(`(${searchTerm})`, 'gi');
                const newText = text.replace(regex, '<mark>$1</mark>');

                if (newText !== text) {
                    const newElement = document.createElement('span');
                    newElement.innerHTML = newText;
                    node.parentNode.replaceChild(newElement, node);
                }
            });
        }

        // Function to remove highlights
        function removeHighlights(element) {
            const marks = element.querySelectorAll('mark');
            marks.forEach(mark => {
                const parent = mark.parentNode;
                parent.replaceChild(document.createTextNode(mark.textContent), mark);
                parent.normalize();
            });
        }

        // Function to get all text nodes in an element
        function getTextNodes(element) {
            const textNodes = [];
            const walk = document.createTreeWalker(
                element,
                NodeFilter.SHOW_TEXT,
                null,
                false
            );

            let node;
            while (node = walk.nextNode()) {
                textNodes.push(node);
            }

            return textNodes;
        }

        // Tag filtering functionality
        document.querySelectorAll('.tag').forEach(tag => {
            tag.addEventListener('click', function() {
                const tagText = this.textContent.toLowerCase();
                const blogPosts = document.querySelectorAll('.blog-post');
                let resultsFound = false;

                blogPosts.forEach(post => {
                    const categories = post.querySelector('.post-meta').textContent.toLowerCase();

                    if (categories.includes(tagText)) {
                        post.style.display = 'block';
                        resultsFound = true;
                    } else {
                        post.style.display = 'none';
                    }
                });

                if (!resultsFound) {
                    alert('No articles found with this tag.');
                    // Show all posts again
                    blogPosts.forEach(post => {
                        post.style.display = 'block';
                    });
                }

                // Highlight active tag
                document.querySelectorAll('.tag').forEach(t => t.classList.remove('active'));
                this.classList.add('active');
            });
        });
</script>
@endpush