<?php
if (!defined('ABSPATH')) {
    exit;
}
$templates = new Chama_Template_Loader;
$slug = get_query_var( 'slug' );
// check	
$current_url        = home_url(chama_get_current_request_uri());
$social_url = $social_title = $facebook_url = $x_url = $whatsapp_url = $linkedin_url = '' ;
$current_currency = CHAMA_CURRENCY;
$current_symbol = CHAMA_SYMBOL;
if($slug){
    $campaign_args = array(
        'name'           => $slug,
        'post_type' => 'chama_commission',
        'post_status' => 'publish',
        'posts_per_page' => 1
    );
    $campaign = new WP_Query($campaign_args);
      if ($campaign->have_posts()):

        while ($campaign->have_posts()): $campaign->the_post();
        $post_id = get_the_ID();
        $content = get_post_meta($post_id, 'commission_description', true);
        $add_ons = get_post_meta($post_id, 'commission_add_ons', true);
        $shipping_prices = get_post_meta($post_id, 'commission_shipping_prices', true);
        $init_amount = get_post_meta($post_id, 'commission_price', true);
        $init_amount_cents = $init_amount * CHAMA_CURRENCY_MINIMUM;
        $display_init_amount = chama_get_formatted_price($init_amount_cents);

            ?>
                <!--Card-->  
              <div class="chama-card">
                            <!--Card header-->  
            <div class="chama-card-header">
        <strong><?php echo esc_html(get_the_title()); ?></strong> 
           <span id="chama-commission-share-buttons" class="chama-share-commission">Share this: <?php $templates->get_template_part('content', 'socialsharebuttons'); ?></span>
            </div>
             <!--./Card header-->  
              <!--Card body-->  
              <div class="chama-card-body">
                      <!--Detail wrapper row-->   
             <div class="chama-commission-detail-wrapper">
                <div class="chama-commission-detail-description">
                    <?php if(get_post_meta($post_id, 'commission_limit', true)):?>
                <strong><em class="chama-slots"><?php echo esc_html(get_post_meta($post_id, 'commission_limit', true)); ?> slots left</em></strong>
                <?php endif;?>
                <div class="chama-commission-detail-content">
                <?php echo wp_kses(wpautop($content), chama_allowed_tags()); ?>
                </div>
                <?php
                ?>
                </div>
                 <!--Input column-->   
                <div class="chama-commission-detail-input">
                       <!--Form-->   
                       <form id="chama-commission-order-form" class="chama-form chama-commission-order-form" action="" method="POST">
      <!--Description input-->               
<div class="chama-input">
<label>Describe your request</label>
<textarea id="chama-commission-request" name="chama-commission-request" rows="3"  placeholder="Please provide a description and all the details needed to complete your request." required class="required" data-msg="Please enter your description" ></textarea>
</div>
 <!--./Description input-->
<!--Add ons input-->
<?php if($add_ons):?>
<div class="chama-input">
<label>Add-ons</label>
<?php $add_on_counter = 0;?>
<?php foreach ($add_ons as $add_on): ?>
    <?php
    $add_on_price = $add_on['commission_add_on_price'];
    $add_on_price_cents = $add_on_price * CHAMA_CURRENCY_MINIMUM;
    $display_add_on_price  = chama_get_formatted_price($add_on_price_cents);
    ?>
    <?php $add_on_id = 'add_on_checkbox_' . $add_on_counter;?>
    <div class="chama-checkbox-wrapper">
        <input id="<?php echo esc_attr($add_on_id);?>" name="<?php echo esc_attr($add_on_id);?>" class="chama-addon-price" type="checkbox" data-cost="<?php echo esc_attr($add_on['commission_add_on_price']) ?>" data-description="<?php echo esc_attr($add_on['commission_add_on_name']) ?>" value="true" />
        <label for="<?php echo esc_attr($add_on_id);?>">
        <span class="chama-addon-price-name"><?php echo esc_html($add_on['commission_add_on_name']) ?></span> 
        <span class="chama-addon-price-label">+ <?php echo esc_html($display_add_on_price); ?></span>
        </label>
    </div>
    <?php $add_on_counter++;?>
<?php endforeach;?>

</div>
<?php endif;?>
<!--./Add ons input-->
<!--Shipping input-->
<?php if($shipping_prices):?>
<div class="chama-input">
<label>Shipping Fee</label>
<select id="chama-shipping-price" required class="required" data-msg="Please select your shipping location" data-current="0">
    <option value="">Select your location</option>
<?php foreach ($shipping_prices as $shipping_price): ?>
    <?php
    $shipping_price_amount = $shipping_price['commission_shipping_price'];
    $shipping_price_amount_cents = $shipping_price_amount * CHAMA_CURRENCY_MINIMUM;
    $display_shipping_price_amount  = chama_get_formatted_price($shipping_price_amount_cents);
    $display_country = chama_get_country_from_code($shipping_price['commission_shipping_location']);
    ?>
    <option data-cost="<?php echo esc_attr($shipping_price_amount) ?>" data-location="<?php echo esc_attr($display_country) ?>" data-location-code="<?php echo esc_attr($shipping_price['commission_shipping_location']) ?>" value="<?php echo esc_html($shipping_price['commission_shipping_location']) ?>"><?php echo esc_html( $display_country . ' - ' . $display_shipping_price_amount) ?></option>

   
<?php endforeach;?>
</select>
</div>
<?php endif;?>
<!--./Shipping input-->
<!--Total input/summary-->
<?php if(get_post_meta($post_id, 'commission_pay_what_you_want', true)):?>
    <div class="chama-input">
    <label>How much would you like to pay?</label>

    <div class="chama-input-wrapper">
		                <div class="chama-input-prepend">
		                                    <span class="chama-input-prepend-text">
                                            <?php echo esc_html("$current_symbol"); ?>
		                                  </span>
		                                </div>
		                <input type="number" min="<?php echo esc_attr($init_amount); ?>"  max="999999" maxlength="7" pattern="[-+]?[0-9]*[.,]?[0-9]+" step="any" name="commission_amount" id="commission_amount" value="<?php echo esc_attr($init_amount); ?>" size="30" required class="required" data-msg-min="You must pay at least <?php echo esc_attr($display_init_amount) ?>!" />
		                </div>
                        </div>
<?php else:?>
    <input type="hidden" name="commission_amount" min="<?php echo esc_attr($init_amount); ?>" id="commission_amount" value="<?php echo esc_attr($init_amount); ?>" />
    <div class="chama-pay-what-you-want-wrapper">
                                        <div class="chama-pay-what-you-want-label">Total</div>
                                        <div class="chama-pay-what-you-want-amount"><span id="chama-pay-what-you-want-total"><?php echo esc_html($display_init_amount); ?></span></div>
                                    </div>
    <?php endif;?>
    <!--./Total input/summary-->
    <!--Submit-->
    <div class="chama-commission_order-submit">
        <input type="hidden" id="chama-symbol" name="chama-symbol"  value="<?php echo esc_attr(CHAMA_SYMBOL); ?>" />
        <input type="hidden" name="chama_action" value="commission"/>
        <input type="hidden" id="commission_id" name="commission_id"  value="<?php echo esc_attr($post_id); ?>" />
        <input type="hidden" id="commission_price" name="commission_price"  value="<?php echo esc_attr($init_amount); ?>" />
        <input type="hidden" id="commission_description" name="commission_description"  value="<?php echo esc_attr(get_the_title()); ?>" />
        <input type="hidden" id="commission_add_ons" name="commission_add_ons" value=""/>
        <input type="hidden" id="commission_shipping" name="commission_shipping" value=""/>
        <input type="hidden" name="chama_commission_nonce" value="<?php echo esc_attr(wp_create_nonce('chama-commission-nonce')); ?>"/>
        <?php wp_nonce_field('wp_rest')?>
        <input type="hidden" id="chama_base_rest_url" name="chama_base_rest_url" value="<?php echo esc_url(get_rest_url(null, '/')); ?>" />
        <div id="chama-payment-errors" role="alert" class="chama-alert chama-alert-error"></div>
        <input type="submit" class="chama-button chama-button-medium chama-block chama-commission-button" value="<?php esc_attr_e('Make Payment' , 'chama');?>"/>
    </div>
    <!--./Submit-->
</form>
   <!--./Form-->   
</div>
      <!--./Input column-->   
             </div>
                <!--./Detail wrapper row-->   
      </div>
        <!--Card body-->  
      </div>
       <!--Card-->  
            <?php
        endwhile;
        $campaign = null;
        wp_reset_postdata();
    endif;

}





