<?php
if (!defined('ABSPATH')) {
    exit;
}
$current_user_tier_id  = 0;
//get user subscription
if (is_user_logged_in()) {
    $user_id =  get_current_user_id();
    $current_user_tier_id = get_user_meta($user_id, 'chama_tier_id', true);
}

//setup tier list
$tiers_args = array(
    'post_type' => 'chama_tier',
    'post_status' => 'publish',
    'nopaging' => true,
    'orderby' => 'meta_value_num',
    'meta_query' => array(// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
        array(
            'key' => 'tier_price',
            'type' => 'NUMERIC',
        )),
    'order' => 'ASC',
);
$tiers_list = new WP_Query($tiers_args);
$require_login = get_option('chama_require_login');
?>

<?php
if ($tiers_list->have_posts()):

    while ($tiers_list->have_posts()): $tiers_list->the_post();
        $post_id = get_the_ID();
        $title = get_the_title();
        $content = get_the_content();
        $price = get_post_meta($post_id, 'tier_price', true);
        $price_cents = $price * CHAMA_CURRENCY_MINIMUM;
        $display_price = chama_get_formatted_price($price_cents);
        $limit = get_post_meta($post_id, 'member_limit', true);
        $is_highlighted = get_post_meta($post_id, 'tier_is_highlighted', true);
        $product_id = get_post_meta($post_id, 'tier_stripe_product_id', true);
        $price_id = get_post_meta($post_id, 'tier_stripe_price_id', true);
        $subscription_data = array('amount' => $price, 'type' => 'membership', 'tier' => $title, 'tier_id' => $post_id, 'product_id' => $product_id, 'price_id' => $price_id);
        $encoded_subscription_data = base64_encode(serialize($subscription_data));
        $subscription_link = add_query_arg(
            array(
                'meta' => $encoded_subscription_data,
            ),
            CHAMA_SUBSCRIPTION_URL
        );
        if ($require_login) {
            if (!is_user_logged_in()) {
                $subscription_link = add_query_arg(
                    array(
                        'redirect_url' => urlencode($subscription_link),
                    ),
                    CHAMA_LOGIN_URL
                );

            } 
        }

        ?>
								    <div class="chama-card <?php echo $is_highlighted ? esc_attr('chama-tier-highlight') : ''; ?>" >
		                            <div class="chama-tier-highlight-title"><strong>You might like</strong></div>
						            <div class="chama-card-body chama-tier-content">


		                            <?php
        if (has_post_thumbnail()) {
            the_post_thumbnail('full', array('class' => 'chama-rounded'));
        } else {
            ?>
				        <img class="chama-rounded" src="<?php echo esc_attr(CHAMA_ASSETS_URL . 'img/no-image.jpg'); ?>" />
				        <?php
        }
        ?>
		     <h4 class="chama-item-title chama-text-center"><?php echo esc_html($title); ?></h4>
		     <div class="chama-tier-price chama-text-center"><?php echo esc_html($display_price); ?><span>&nbsp;per month</span></div>
             <?php if($current_user_tier_id == $post_id):?>
                <a  class="chama-button chama-button-medium chama-block chama-join-tier chama-disabled-button">Joined</a>
                <?php else: ?>
                    <?php if ($limit): ?>
		     <a href="<?php echo esc_url($subscription_link) ?>" class="chama-button chama-button-medium chama-block chama-join-tier">Join</a>
		     <?php else: ?>
	        <a class="chama-button chama-button-medium chama-block chama-join-tier chama-disabled-button">We're full!</a>
	        <?php endif;?>
                <?php endif;?>
		  
            <?php if($current_user_tier_id == $post_id):?>
                <div class="chama-tier-notice">Your current tier</div>
                <?php else: ?>
                    <?php if ($limit): ?>
        <?php if ($limit > 0): ?>
        <div class="chama-tier-notice"><?php echo esc_html($limit); ?> places left</div>
        <?php endif;?>
        <?php else: ?>
            <div class="chama-tier-notice">No places left</div>
    <?php endif;?>
                    <?php endif;?>
   
    <?php echo wp_kses(wpautop($content), chama_allowed_tags()); ?>

                </div>
                </div>

        <?php
endwhile;
$tiers_list = null;
wp_reset_postdata();
endif;
?>





