<?php
if (!defined('ABSPATH')) {
    exit;
}
$commissions_args = array(
    'post_type' => 'chama_commission',
    'post_status' => 'publish',
    'nopaging' => true,
    'orderby' => 'post_date',
    'order' => 'DESC',
);
$commissions_list = new WP_Query($commissions_args);
$display_membership_page = get_option('chama_display_membership');
?>
<div class="chama-two-cols-container">
<?php
if ($commissions_list->have_posts()):
    
   
    while ($commissions_list->have_posts()): $commissions_list->the_post();
        $post_id = get_the_ID();
        $title = get_the_title();
        $content = get_post_meta($post_id, 'commission_description', true);
        $image_ids = ($image_ids = get_post_meta($post_id, 'commission_preview_images', true)) ? array_filter($image_ids) : array();
        $price = get_post_meta($post_id, 'commission_price', true);
        $price_cents = $price * CHAMA_CURRENCY_MINIMUM;
        $display_amount  = chama_get_formatted_price($price_cents);
       $available_to_everyone = get_post_meta($post_id, 'commission_can_request', true) === 'yes';
        $add_ons = get_post_meta($post_id, 'commission_add_ons', true);
        $slots = get_post_meta($post_id, 'commission_limit', true);
        ?>
		        <div class="chama-card">
		        <div class="chama-card-body">

		        <?php
        if (has_post_thumbnail()) {
            the_post_thumbnail('full', array('class' => 'chama-rounded chama-featured'));
        }

        ?>
		 <h4 class="chama-item-title"><?php echo esc_html($title); ?> <span class="chama-title-price"><span> <?php echo esc_html($display_amount); ?>+</span></span></h4>

		<?php echo esc_html($content); ?>
		<ul class="chama-commission-gallery" data-id="<?php echo esc_attr($post_id); ?>">
		  <?php
        foreach ($image_ids as $i => &$id) {
            $thumbnail_url = wp_get_attachment_image_url($id, array(140, 140));
            $full_url = wp_get_attachment_image_url($id, 'full');
            if ($thumbnail_url) {
                ?>
		                <li data-id="<?php echo esc_attr($id) ?>"   data-featherlight-gallery data-featherlight-filter="a">
		                <a class="chama-thumbnail chama-gallery-<?php echo esc_attr($post_id); ?>" href="<?php echo esc_url($full_url); ?>"><span class="chama-preview-image" style="background-image:url('<?php echo esc_url($thumbnail_url); ?>')"></span></a>
		                </li>
		            <?php
        } else {
                unset($image_ids[$i]);
            }
        }
        ?>
		  </ul>
		  <?php if ($add_ons || $slots): ?>
		    <div class="chama-commission-footer">
		    <?php if ($add_ons): ?>
		    <i class="fas fa-layer-group"></i> <span>Add-ons available</span>

		    <?php endif;?>
	    <?php if ($slots): ?>
	     <span class="chama-slots"><?php echo esc_html($slots); ?> slots left</span>

	    <?php endif;?>
    </div>
    <?php endif;?>
    <?php if($available_to_everyone):?>
    <a href="<?php echo esc_url(CHAMA_COMMISSIONS_URL . basename(get_permalink($post_id))); ?>" class="chama-button chama-button-medium chama-block chama-request-button <?php echo esc_attr($slots > 0 ? '' : 'chama-disabled-button'); ?>"><?php echo esc_html($slots > 0 ? 'Request this' : 'No slots left'); ?></a>
    <?php else:?>
        <div class="chama-alert chama-alert-info">
        <i class='fa-solid fa-lock fa-2x'></i> You need to have an active subscription.
        </div>
        <?php if($display_membership_page): ?>
        <a href="<?php echo esc_url(CHAMA_MEMBERSHIP_URL); ?>" class="chama-button chama-button-medium chama-block chama-request-button">Become a subscriber</a>
        <?php endif;?>
        <?php endif;?>
            </div>
        </div>
        <?php
endwhile;
$commissions_list = null;
wp_reset_postdata();
endif;
?>
</div>



