<?php

/**
 * Template part for displaying posts.
 *
 * @package chic-lifestyle
 */

$view = isset($args['view']) ? $args['view'] : 'list-view';
$post_details = $args['post_details'] ?? [];

$section = isset($args['section']) ? $args['section'] : '';

$snipet_class = "col-12"; // Default full-width
$img_class = 'h-md-280'; // Default for list-view

if ($view === 'grid-view') {
  $snipet_class = "col-12 col-md-6";
  $img_class = 'h-md-280 h-lg-400';

  if ($section === 'featured') {
    $snipet_class = "col-12";
  }
} else if ($view === 'full-width-view') {
  $snipet_class = "col-12";
  $img_class = 'h-md-400 h-lg-600 d-block m-auto';
}

?>

<div class="<?php echo esc_attr($snipet_class); ?> mb-40">
  <div class="card h-p100 border-0 <?php echo ($view === 'list-view') ? 'flex-column flex-md-row gap-16' : ''; ?>">

    <a href="<?php echo esc_url(get_permalink()); ?>" rel="bookmark" class="d-block <?php echo ($view === 'list-view') ? 'w-p100 w-md-p35' : 'w-p100 pb-0 px-0'; ?>">
      <?php if (has_post_thumbnail()) : ?>
        <?php the_post_thumbnail('post-thumbs', ['class' => "card-img-top h-280 h-sm-320 $img_class rounded-0 object-fit-cover"]); ?>
      <?php else : ?>
        <img src="<?php echo esc_url(get_template_directory_uri() . '/images/no-image.jpg'); ?>" class="card-img-top h-280 h-sm-320 <?php echo $img_class; ?> rounded-0 object-fit-cover" alt="No image">
      <?php endif; ?>
    </a>

    <div class="card-body d-flex flex-column px-0 <?php echo ($view === 'list-view') ? 'w-p100 w-md-p65 py-0' : 'w-p100 float-start'; ?>">

      <h3 class="news-title mb-12 position-relative">
        <a href="<?php echo esc_url(get_permalink()); ?>" rel="bookmark" class="text-decoration-none">
          <?php the_title(); ?>
        </a>
      </h3>

      <?php if (! empty($post_details) && is_array($post_details)) : ?>
        <div class="info text-info mb-3">
          <ul class="list-unstyled d-flex flex-wrap small text-muted ms-0 gap-8 lh-1">
            <?php foreach ($post_details as $value) : ?>

              <?php if ($value == 'date') : ?>
                <?php
                $archive_year  = get_the_time('Y');
                $archive_month = get_the_time('m');
                $archive_day   = get_the_time('d');
                ?>
                <li class="text-info">
                  <i class="fa fa-clock-o me-2"></i>
                  <a class="text-info" href="<?php echo esc_url(get_day_link($archive_year, $archive_month, $archive_day)); ?>">
                    <?php echo get_the_date(); ?>
                  </a>
                </li>
              <?php endif; ?>

              <?php if ($value == 'categories') : ?>
                <?php $categories = get_the_category();
                if (! empty($categories)) :
                  foreach ($categories as $cat) : ?>
                    <li class="text-info">
                      <a class="text-info" href="<?php echo esc_url(get_category_link($cat->term_id)); ?>">
                        <?php echo esc_html($cat->name); ?>
                      </a>
                    </li>
                <?php endforeach;
                endif; ?>
              <?php endif; ?>

              <?php if ($value == 'tags') : ?>
                <?php $tags = get_the_tags();
                if (! empty($tags)) :
                  foreach ($tags as $tag) : ?>
                    <li class="text-info">
                      <a class="text-info" href="<?php echo esc_url(get_tag_link($tag->term_id)); ?>">
                        <?php echo esc_html($tag->name); ?>
                      </a>
                    </li>
                <?php endforeach;
                endif; ?>
              <?php endif; ?>

              <?php if ($value == 'number_of_comments') : ?>
                <li class="text-info">
                  <i class="fa fa-comments-o me-1"></i>
                  <?php comments_popup_link(
                    __('0 Comments', 'chic-lifestyle'),
                    __('1 Comment', 'chic-lifestyle'),
                    __('% Comments', 'chic-lifestyle'),
                    $css_class = "text-info"
                  ); ?>
                </li>
              <?php endif; ?>

              <?php if ($value == 'author') : ?>
                <li class="info text-info mb-3">
                  <div class="list-unstyled small text-muted">
                    <p class="m-0">
                      <a class="d-flex align-items-center text-decoration-none text-info" href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>">
                        <?php echo get_avatar(get_the_author_meta('ID'), 40, '', '', ['class' => 'rounded-circle me-4 w-20']); ?>
                        <span><?php echo esc_html(get_the_author()); ?></span>
                      </a>
                    </p>
                  </div>
                </li>
              <?php endif; ?>

            <?php endforeach; ?>
          </ul>
        </div>
      <?php endif; ?>

      <div class="excerpt mb-8">
        <?php echo esc_html(chic_lifestyle_custom_excerpt(250)); ?>
      </div>

      <a href="<?php echo esc_url(get_permalink()); ?>" style="letter-spacing: 3px;" class="readmore btn text-secondary border-0 bg-transparent ps-0 text-uppercase fs-85 align-self-start <?php echo ($view === 'list-view') ? 'm-0' : 'mt-auto'; ?>">
        <?php esc_html_e('Read more', 'default'); ?>
      </a>

    </div><!-- .card-body -->
  </div><!-- .news-snippet -->
</div>