<?php
global $wp_query;
$plan_id = $wp_query->get('sub_id');
$plan_info = yascode_vip::get_plan_info($plan_id);
$gateways = yascode_gateway::get_subscription_gateways();
$default_gateway = '';
if (!empty($gateways)) {
    foreach ($gateways as $gateway) {
        $gateway_id = (!empty($gateway['id'])) ? $gateway['id'] : '';
        if (!empty($gateway_id) && !empty($gateway['status']) && $gateway['status'] == 'active') {
            $default_gateway = $gateway_id;
            break;
        }
    }
}

$vip_active = yascode_vip::is_active();
if (!$vip_active) wp_redirect(PANEL_URL) and exit;
if (!empty($plan_info)) {
    $discounted = 0;
    $ID = (!empty($plan_info->ID)) ? $plan_info->ID : '';
    $time = (!empty($plan_info->time)) ? change_days_to_human($plan_info->time) : '';
    $real_price = (!empty($plan_info->price) && is_numeric($plan_info->price)) ? $plan_info->price : '';
    $discount_price = (!empty($plan_info->discount_price) && is_numeric($plan_info->discount_price)) ? $plan_info->discount_price : '';
    if (!empty($real_price) && !empty($discount_price) && $discount_price < $real_price) $discounted = ceil($real_price - $discount_price); ?>
    <div class="single-plans rt rt-center">
        <div class="ins rt">
            <div class="entery rt">
                <div class="main">
                    <form class="rt rt-bg rt-relative rt-15px cart" method="POST" action="<?php echo PANEL_URL . '/plans/checkout/' . $plan_id; ?>">
                        <?php wp_nonce_field('buy_subscription_security', 'security_nonce'); ?>
                        <div class="avatar rt-align rt">
                            <img src="<?php echo THEME_TEMPLATE_URL . '/assets/front/img/user.webp'; ?>" width="80" height="80" class="rt-50px">
                        </div>
                        <div class="copen rt rt-absolute rt-0s">
                            <div class="form rt rt-15px rt-bg">
                                <input type="text" class="right rt-14 rt-222 rt-5px input-discount" placeholder="کد تخفیف را وارد کنید...">
                                <span class="left rt-color rt-align rt-14 rt-400 rt-pointer rt-fff rt-5px submit-discount" data-active="true">اعمال</span>
                            </div>
                        </div>
                        <img src="<?php echo THEME_TEMPLATE_URL . '/assets/front/img/vip.png'; ?>" class="arm right rt-align">
                        <div class="right title rt-align rt-555 rt-bold rt-18">اشتراک <?php echo $time['number']; ?> <?php echo $time['type'] . 'ه'; ?> <?php echo YC_NAME; ?></div>
                        <div class="rt content rt-400 rt-15">
                            <div class="rt item discount-item" style="display: block">
                                <span class="right rt-limit">تخفیف اعمال شده:</span>
                                <span class="left rt-limit discount-price"><?php echo (!empty($discounted)) ? number_format($discounted) . ' تومان' : 'ندارد'; ?></span>
                            </div>
                        </div>
                        <div class="pay-out rt-align rt">
                            <samp class="rt-medium rt-15">مبلغ </samp>
                            <samp class="rt-bold rt-21 rt-444 pay-price"><?php echo (!empty($discounted)) ? number_format(ceil($real_price - $discounted)) : number_format($real_price); ?> تومان</samp>
                            <input type="submit" name="buy_subscription_plan" value="پرداخت آنلاین" class="sub rt-16 rt-bold rt-10px rt rt-pointer">
                        </div>
                        <div class="final-data">
                            <input type="hidden" name="plan_id" value="<?php echo $ID; ?>" class="plan_id">
                            <input type="hidden" name="discount_code" value="" class="discount_code">
                            <input type="hidden" name="gateway_id" value="<?php echo $default_gateway; ?>" class="gateway_id">
                        </div>
                    </form>
                    <div class="rt rt-align"><span id="offer-btn" class="offer-btn rt-pointer rt-14 rt-888"><i class="fa fa-gift"></i> ثبت کد تخفیف / کد هدیه</span></div>
                    <div class="result-discount rt rt-14 rt-align rt-444"></div>
                </div>
            </div>
        </div>
    </div>
<?php } else wp_redirect(PANEL_URL . '/plans') ?>