<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=market.jscfg
 * [END_COT_EXT]
 */

defined('COT_CODE') or die('Wrong URL.');

require_once cot_incfile('catcostplans', 'plug');
global $db_catcostplans, $db_catcostplans_attrs;

$sql = $db->query("SELECT * FROM $db_catcostplans WHERE p_area='market' ORDER BY p_catid ASC")->fetchAll();
foreach ($sql as $val) {
    $return['categoriesWithAttributes'][] = $val['p_catid'];
    $return['categoryPrices'][$val['p_catid']] = array(
        'standard' => 500,
        'medium' => 1000,
        'premium' => 1500
    );

    foreach (array('standard', 'medium', 'premium') as $type) {
        $tmpPrice = (!empty($val['p_price' . ucfirst($type)]) ? explode(',', $val['p_price' . ucfirst($type)]) : array());
        if (count($tmpPrice) == 1 && $tmpPrice[0] > 0) {
            $return['categoryPrices'][$val['p_catid']][$type] = $tmpPrice[0];
        }
    }

    $options = $db->query("SELECT * FROM $db_catcostplans_attrs WHERE at_planid=" . $val['p_id'] . " ORDER BY at_id ASC")->fetchAll();

    foreach ($options as $otp) {
        if ($otp['at_type'] == '1') {
            $otp['at_int_minmax'] = (!empty($otp['at_int_minmax']) ? json_decode($otp['at_int_minmax'], true) : array(0, 0));
            $return['categoryExtras'][$val['p_catid']][] = array(
                'id' => $otp['at_id'],
                'name' => $otp['at_title'],
                'hint' => $otp['at_hint'],
                'type' => 'integer',
                'min' => $otp['at_int_minmax'][0],
                'max' => $otp['at_int_minmax'][1],
                'can_lower' => ($otp['can_lower'] == 1 ? '1' : '0')
            );
        } else {
            $return['categoryExtras'][$val['p_catid']][] = array(
                'id' => $otp['at_id'],
                'name' => $otp['at_title'],
                'hint' => $otp['at_hint'],
                'type' => 'bool'
            );
        }
    }
}

if ((is_array($item) || $item instanceof Countable) && count($item) > 0 && !empty($item['item_catcostplans'])) {
    $bundles = json_decode($item['item_catcostplans'], true);

    if ($bundles['bundle_type'] == 'package') {
        $return['prdPackage'] = 'package';

        if (isset($bundles['epackages']) && is_array($bundles['epackages']) && count($bundles['epackages']) > 0) {
            foreach ($bundles['epackages'] as $epack) {
                $return['customBundleExtras'][] = array(
                    'id' => $epack['id'],
                    'prd_id' => $item['item_id'],
                    'name' => $epack['name'],
                    'min_value' => '1',
                    'max_value' => '1',
                    'hint' => $epack['hint'],
                    'type' => $epack['type'],
                    'can_lower' => 1
                );
            }
        }
    }

    if (isset($bundles['bundles']) && count($bundles['bundles']) > 0) {
        $jj = 1;
        foreach ($bundles['bundles'] as $type => $bund) {
            if (isset($bund['category']) && count($bund['category']) > 0) {
                foreach ($bund['category'] as $brow) {
                    $return['selectedPackageExtras'][$type]['category'][] = array(
                        'id' => $jj,
                        'value' => $brow['val'],
                        'item_id' => $brow['id'],
                    );
                    $jj++;
                }
            }
            if (isset($bund['custom_val']) && count($bund['custom_val']) > 0) {
                foreach ($bund['custom_val'] as $oid => $browval) {
                    $return['selectedPackageExtras'][$type]['custom'][] = array(
                        'id' => $jj,
                        'value' => $browval,
                        'item_id' => $oid,
                    );
                    $jj++;
                }
            }

            $return['prdPackages'][$type] = array(
                'id' => $jj,
                'type' => $type,
                'desc' => '',
                'price' => $bund['price'],
                'price_ctp' => ($market_crt > 0 ? round(($bund['price'] / 100) * $market_crt) : 0),
                'price_dodji' => $bund['price_dodji'],
                'price_dodji_ctp' => ($market_crt > 0 ? round(($bund['price_dodji'] / 100) * $market_crt) : 0),
                'duration' => $bund['duration'],
            );
        }
    } else {
        $return['prdPackage'] = 'single';

        $return['extras'] = array(
            'single' => array(
                'title' => $bundles['bundles']['category']['title'],
                'time' => $bundles['bundles']['category']['time'],
                'price' => $bundles['bundles']['category']['price'],
            ),
            'cat' => array(),
            'my' => array(),
        );
        if ($bundles['bundles']['category']['price'] > 0) {
            $return['prdPrice'] = $bundles['bundles']['category']['price'];
        }

        global $costplans_prices;
        $costplans_prices_flip = array_flip($costplans_prices);

        if (isset($bundles['bundles']['custom']) && count($bundles['bundles']['custom']) > 0) {
            foreach ($bundles['bundles']['custom'] as $cust) {
                $priceId = 0;
                $cost = $cust['price'];
                if ($cost > 0 && $cost < 100) {
                    $priceId = $cost;
                    $cost = $costplans_prices[$priceId];
                }
                $return['extras']['my'][] = array(
                    'id' => $cust['id'],
                    'name' => $cust['name'],
                    'price' => ($market_crt > 0 ? ($cost - (($cost / 100) * $market_crt)) : $cost),
                    'price_dodji' => (int)$cust['price_dodji'],
                    'payerPrice' => $cost,
                    'duration' => (int)$cust['duration'],
                    'priceId' => $priceId
                );
            }
        }
    }
}

global $costplans_prices;

$return['costplans_price'] = array();
if ($cfg['payments']['dodjirate']) {
    $return['costplans_price'][] = array(
        'id' => 0,
        'val' => 0
    );
}

foreach ($costplans_prices as $key => $val) {
    $return['costplans_price'][] = array(
        'id' => $key,
        'val' => number_format(($market_crt > 0 ? ($val - (($val / 100) * $market_crt)) : $val), 0, '', ' ')
    );
}
