<?php $_shouldextend[1]=1; ?>

<?php $this->startSection('content'); ?>
<?php
$activeCategory = (string)($activeCategory ?? '');
$currentPage = max(1, (int)($currentPage ?? 1));
$totalPages = max(1, (int)($totalPages ?? 1));
$posts = is_array($posts ?? null) ? $posts : [];
$recentPosts = is_array($recentPosts ?? null) ? $recentPosts : [];
$categories = is_array($categories ?? null) ? $categories : [];
$authorName = site_is_en() ? 'ITX Trade Editorial Team' : setting('blog_author_name', 'مهدی شفیعی');
$buildPageUrl = static function (int $pageNumber) use ($activeCategory): string {
 $query = [];
 if ($activeCategory !== '') $query['category'] = $activeCategory;
 if ($pageNumber > 1) $query['page'] = $pageNumber;
 return site_public_url('/blog').($query ? '?'.http_build_query($query) : '');
};
$sidebarFallback = site_is_en() ? ['Network Integration','Deep Learning Solutions','Transfer Learning','Model Evaluation','Real-time Prediction'] : ['ادغام شبکه','راهکارهای یادگیری عمیق','انتقال یادگیری','ارزیابی مدل','پیش‌بینی بلادرنگ'];
?>

<div class="blog-classic-layout-v4 blog-classic-wrapper">
 <section class="blog-classic-content">
 <div class="content-inner">
 <div class="archive-listing">
 <?php if(!$posts): ?>
 <div class="blog-classic-empty">
 <i class="fa-regular fa-newspaper"></i>
 <h2><?=e(site_is_en()?'No articles yet':'هنوز مطلبی منتشر نشده است')?></h2>
 <p><?=e(site_is_en()?'Published articles will appear here.':'نوشته‌های منتشرشده از بخش مدیریت وبلاگ در این قسمت نمایش داده می‌شوند.')?></p>
 </div>
 <?php else: ?>
 <div class="archive-listing-wrapper classic-listing">
 <?php foreach($posts as $post): ?>
 <div class="standard-blog-item-wrapper post">
 <article class="blog-item">
 <div class="post-media-wrapper">
 <a href="<?=e(site_public_url('/blog/'.$post['slug']))?>" aria-label="<?=e($post['title'])?>">
 <?php if(trim((string)($post['featured_image'] ?? '')) !== ''): ?>
 <img src="<?=e(page_block_href($post['featured_image']))?>" alt="<?=e($post['title'])?>" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" loading="lazy">
 <?php else: ?>
 <span class="classic-post-image-fallback"><i class="fa-regular fa-newspaper"></i></span>
 <?php endif; ?>
 </a>
 <div class="post-meta-header">
 <div class="post-meta-items-wrapper">
 <div class="post-meta-items">
 <div class="post-meta-item post-meta-item-date"><span><?=e(site_format_date((string)$post['created_at']))?></span></div>
 <div class="post-meta-item post-meta-item-author"><span><?=e($authorName)?></span></div>
 </div>
 </div>
 </div>
 </div>

 <h3 class="post-title"><a href="<?=e(site_public_url('/blog/'.$post['slug']))?>"><?=e($post['title'])?></a></h3>
 <div class="post-content"><?=e($post['excerpt'] ?: (site_is_en() ? 'Open the article to read the full story and details.' : 'برای مطالعه کامل این مطلب و مشاهده جزئیات بیشتر وارد صفحه مقاله شوید.'))?></div>
 <div class="post-labels">
 <div class="post-categories">
 <?php if(!empty($post['category_slug'])): ?>
 <a class="post-category-item" href="<?=e(site_public_url('/blog').'?category='.rawurlencode($post['category_slug']))?>"><?=e($post['category'] ?? site_ui('nav.blog'))?></a>
 <?php else: ?>
 <a class="post-category-item" href="<?=e(site_public_url('/blog'))?>"><?=e(site_ui('nav.blog'))?></a>
 <?php endif; ?>
 </div>
 </div>
 </article>
 </div>
 <?php endforeach; ?>
 </div>

 <?php if($totalPages > 1): ?>
 <div class="content-pagination">
 <nav class="navigation pagination" role="navigation" aria-label="<?=site_is_en()?'Blog pagination':'صفحه‌بندی وبلاگ'?>">
 <h2 class="screen-reader-text">Pagination</h2>
 <div class="nav-links">
 <?php for($page = 1; $page <= $totalPages; $page++): ?>
 <?php if($page === $currentPage): ?>
 <span aria-label="Page <?=e((string)$page)?>" aria-current="page" class="page-numbers current"><span class="button-inner"></span><?=e((string)$page)?></span>
 <?php else: ?>
 <a aria-label="Page <?=e((string)$page)?>" class="page-numbers" href="<?=e($buildPageUrl($page))?>"><span class="button-inner"></span><?=e((string)$page)?></a>
 <?php endif; ?>
 <?php endfor; ?>
 <?php if($currentPage < $totalPages): ?>
 <a class="next page-numbers" href="<?=e($buildPageUrl($currentPage + 1))?>"><?=e(site_is_en()?'Next':'بعدی')?><span class="button-inner"></span><span class="icon-button-icon"><i class="fa-solid <?=site_is_en()?'fa-arrow-right':'fa-arrow-left'?>"></i></span></a>
 <?php endif; ?>
 </div>
 </nav>
 </div>
 <?php endif; ?>
 <?php endif; ?>
 </div>
 </div>
 </section>

 <aside class="blog-classic-sidebar" id="simple-sidebar-default">
 <div class="widget widget_block widget-classic-links"><div class="widget-wrapper">
 <ul class="wp-block-list has-background">
 <?php if($categories): ?>
 <?php foreach(array_slice($categories, 0, 5) as $category): ?>
 <li><a class="<?= $activeCategory === (string)$category['slug'] ? 'active' : '' ?>" href="<?=e(site_public_url('/blog').'?category='.rawurlencode($category['slug']))?>"><?=e($category['name'])?></a></li>
 <?php endforeach; ?>
 <?php else: ?>
 <?php foreach($sidebarFallback as $label): ?><li><a href="<?=e(site_public_url('/blog'))?>"><?=e($label)?></a></li><?php endforeach; ?>
 <?php endif; ?>
 </ul>
 </div></div>

 <div class="widget widget_block widget-recent-posts"><div class="widget-wrapper">
 <h5 class="widget-title"><span><?=e(site_ui('blog.recent'))?></span></h5>
 <div class="wp-widget-group__inner-blocks"><ul class="wp-block-latest-posts__list has-dates wp-block-latest-posts">
 <?php foreach($recentPosts as $item): ?>
 <li>
 <div class="wp-block-latest-posts__featured-image alignleft"><a href="<?=e(site_public_url('/blog/'.$item['slug']))?>" aria-label="<?=e($item['title'])?>">
 <?php if(trim((string)($item['featured_image'] ?? '')) !== ''): ?><img src="<?=e(page_block_href($item['featured_image']))?>" alt="<?=e($item['title'])?>" loading="lazy"><?php else: ?><span class="recent-post-image-fallback"><i class="fa-regular fa-newspaper"></i></span><?php endif; ?>
 </a></div>
 <div class="wp-block-latest-posts__content"><a class="wp-block-latest-posts__post-title" href="<?=e(site_public_url('/blog/'.$item['slug']))?>"><?=e($item['title'])?></a><time datetime="<?=e((string)$item['created_at'])?>" class="wp-block-latest-posts__post-date"><?=e(site_format_date((string)$item['created_at']))?></time></div>
 </li>
 <?php endforeach; ?>
 </ul></div>
 </div></div>

 <div class="widget widget_block widget-classic-cover"><div class="widget-wrapper">
 <a class="wp-block-cover" href="<?=e(site_public_url('/contact-us'))?>">
 <?php if(!empty($sidebarBannerPost) && trim((string)($sidebarBannerPost['featured_image'] ?? '')) !== ''): ?><img class="wp-block-cover__image-background" alt="" src="<?=e(page_block_href($sidebarBannerPost['featured_image']))?>" loading="lazy"><?php endif; ?>
 <span aria-hidden="true" class="wp-block-cover__background"></span>
 <div class="wp-block-cover__inner-container"><h5 class="wp-block-heading"><?=e(site_ui('blog.sidebar_title'))?></h5><p class="wp-block-paragraph"><?=e(site_ui('blog.sidebar_text'))?></p></div>
 </a>
 </div></div>
 </aside>
</div>
<?php $this->stopSection(); ?>

<?php if (isset($_shouldextend[1])) { echo $this->runChild('layouts.main'); } ?>