<?php
/**
 * Render View for PrimeKit Modern Post Grid
 */
if (!defined('ABSPATH')) exit; // Exit if accessed directly

$settings = $this->get_settings_for_display();
// Placeholder image as fallback
$fallback_image = PRIMEKIT_ELEMENTOR_ASSETS . '/img/img-hover-placeholder.jpg';
// Get the values from Elementor controls
$post_type = $settings['post_types']; // Get selected post types
$customized_posts_selection = $settings['customized_posts_selection']; // Get the customization option
$selected_categories = $settings['get_categories']; // Get selected categories
$selected_posts = $settings['get_posts_list']; // Get selected posts
$ignore_sticky_posts = $settings['ignore_sticky_posts'] === 'true'; // Get ignore sticky posts

$post_info_switch = !empty($settings['post_meta_switch']) ? $settings['post_meta_switch'] : 'false';
$post_info_display = !empty($settings['post_meta_display']) ? $settings['post_meta_display'] : [];
$categories_switch = isset($settings['display_category']) ? $settings['display_category'] : 'true';
$random_color_switch = isset($settings['category_random_color_switch']) ? $settings['category_random_color_switch'] : 'false';

// Get post limit
if('style2' == $settings['primekit_modern_post_grid_style']) {
    $post_limit = $settings['post_limit_for_style2']; // Get the post limit for style 2
}else{
    $post_limit = $settings['post_limit']; // Get the post limit
}


// Determine if a single post should be displayed as a full-width container
$fullWidth_class = (1 === $post_limit) ? ' primekit-modern-one-post' : '';

// Initialize WP_Query arguments based on the user selection
$args = [
    'post_type' => $post_type,
    'posts_per_page' => $post_limit,
    'ignore_sticky_posts' => $ignore_sticky_posts,
];

// Modify WP_Query based on the user selection
if ($customized_posts_selection === 'categories' && !empty($selected_categories)) {
    // If the user selected to display posts by categories
    $args['category__in'] = $selected_categories;
} elseif ($customized_posts_selection === 'specific_posts' && !empty($selected_posts)) {
    // If the user selected to display specific posts
    $args['post__in'] = $selected_posts;
}

if('style1' == $settings['primekit_modern_post_grid_style']) {
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style1.php';
}elseif('style2' == $settings['primekit_modern_post_grid_style']) {
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style2.php';
}elseif('style3' == $settings['primekit_modern_post_grid_style']) {
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style3.php';
}elseif('style4' == $settings['primekit_modern_post_grid_style']) {
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style4.php';
}elseif('style5' == $settings['primekit_modern_post_grid_style']) {
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style5.php';
}elseif('style6' == $settings['primekit_modern_post_grid_style']) {
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style6.php';
}elseif('style7' == $settings['primekit_modern_post_grid_style']) {
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style7.php';
}elseif('style8' == $settings['primekit_modern_post_grid_style']) {
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style8.php';
}
else{
    include PRIMEKIT_ELEMENTOR_PATH . 'Widgets/ModernPostGrid/Templates/style1.php';
}