<div class="container-fluid causes">
    <div class="container py-5">
        <div class="section-title" id="section-news" style="scroll-margin-top: 100px;">
          <div class="line left-line"></div>
          <h1><?php echo $weblangs->trans("news"); ?></h1>
          <div class="line right-line"></div>
        </div>
        <div class="row g-4">
        <?php 
        $MAXNEWS = GETPOSTINT('maxnews') ? GETPOSTINT('maxnews') : 3;
        $allblogs = $websitepage->fetchAll($website->id, 'DESC', 'date_creation', 0, 0, array('type_container'=>'blogpost', 'status'=>1, 'lang'=>'null,'.$_SESSION['lang']['shortcode']));
        $countofblogs = count($allblogs);
        $arrayofblogs = $websitepage->fetchAll($website->id, 'DESC', 'date_creation', $MAXNEWS, 0, array('type_container'=>'blogpost', 'status'=>1, 'lang'=>'null, '.$_SESSION['lang']['shortcode']));
        $i=1;
        foreach($arrayofblogs as $blog)
        {
        ?>
            <div class="col-lg-6 col-xl-4 blog-item" id="blog-<?php echo $i; ?>">
                <div class="causes-item">
                    <div class="causes-img">
                        <?php
                            $urlforimage = getImageFromHtmlContent($blog->content);
                            if (empty($urlforimage)) {
                                $urlforimage = 'medias/image/'.$website->ref.'/noimage.jpg';
                            }
                        ?>
                        <img src="<?php echo $urlforimage; ?>" class="img-fluid w-100" style="height: 275px; object-fit: cover;" alt="Image">
                    </div>
                    <div class="progress progress-news">
                      <div class="progress-bar" role="progressbar" style="width: 100%;" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100">
                      </div>
                    </div>
                    <div class="causes-content p-4">
                        <span class="text-body">
                            <i class="fas fa-calendar-alt me-2"></i> <?php echo dol_print_date($blog->date_creation, 'daytext', 'tzuser', $weblangs); ?>
                            <span class="author-span"><i class="fas fa-user me-2"></i> <?php echo ($blog->author_alias ? $blog->author_alias : 'anonymous'); ?></span>
                        </span>
                        <h4 class="py-3"><?php echo $blog->title; ?></h4>
                        <hr>
                        <p class="mb-4 truncate3"><?php echo $blog->description; ?></p>
                        <a href="page.php?ref=<?php print $blog->pageurl; ?>&blog=<?php print $blog->id; ?>" class="btn flat-button text-white py-2 px-3 float-end"><?php echo $weblangs->trans("readMore"); ?></a>
                    </div>
                </div>
            </div>
            <?php 
            $i++;
        }
        ?>
        </div>
        
        <?php if ($MAXNEWS <= $countofblogs) { ?>
            <div class="row">
                <div class="col-lg-7 text-center py-5 section-title">
                    <div class="line left-line left-gradient"></div>
                    <a href="javascript:void(0);" class="btn flat-button text-white bgmaincolor" id="loadMoreNewsBtn"><?php echo $weblangs->trans("loadMore"); ?>...</a>
                    <div class="line right-line right-gradient"></div>
                </div>
            </div>
        <?php } ?>
        <script>
            document.getElementById('loadMoreNewsBtn').addEventListener('click', function() {
            const currentUrl = new URL(window.location.href);
            const maxNewsParam = <?php echo $MAXNEWS; ?>;
            const newMaxNews = parseInt(maxNewsParam) + 3;
            currentUrl.searchParams.set('maxnews', newMaxNews);
            currentUrl.hash = '#blog-' + (parseInt(maxNewsParam) + 1).toString();
            window.location.href = currentUrl.toString();
            });
        </script>
            
            
    </div>
</div>