<?php
defined('COT_CODE') or die('Wrong URL');

$return['success'] = 1;
$return['prices'] = '';

global $structure, $cfg;

if(!$usr['isadmin'] && ($usr['maingrp'] == 5 || $usr['maingrp'] == 6)) $usr['isadmin'] = true;

$categoryId = cot_import('categoryId', 'G', 'INT');
$attributeIds = cot_import('attributeIds', 'G', 'ARR');

$catIds = (count($attributeIds) > 0 ? array_reverse($attributeIds) : array());
if($categoryId > 0) $catIds[] = $categoryId;

$planFind = false;
foreach($catIds as $categoryId) {
  if($planFind) break;

  if($categoryId > 0 && cot_plugin_active('catcostplans')) {
    $curCat = array('id' => 0);

    foreach($structure['market'] as $seo => $cat) {
      if($categoryId == $cat['id']) {
        $curCat = array(
          'id' => $cat['id'],
          'name' => $cat['title'],
          'seo' => $seo,
          'path' => $cat['path']
        );

        /* === Hook === */
        foreach (cot_getextplugins('api.getCatsPrices.catfind') as $pl)
        {
        	include $pl;
        }
        /* ===== */

        break;
      }
    }

    if($curCat['id'] > 0) {
      require_once cot_incfile('catcostplans', 'plug');

      $plan = $db->query("SELECT * FROM $db_catcostplans WHERE p_area='market' AND p_catid=".$curCat['id']." LIMIT 1")->fetch();

      if($plan['p_id'] > 0) {
        $typicalPriceGradation = array(100,200,300,400,500,600,700,800,900,1000,1200,1500,1800,2000,2500,3000,3500,4000,4500,5000,6000,7000,8000,9000,10000,12000,13500,15000,17000,19000,20000,21000,22000,23000,24000,25000);
        if($cfg['payments']['dodjirate']) {
          array_unshift($typicalPriceGradation, 0);
        }

        $return['prices'] = array(
          'priceGradation' => array(
            'priceStandard' => 500,
            'priceMedium' => 1000,
            'pricePremium' => 1500,
          ),
          'minPrice' => ($plan['p_priceMin'] > 0 ? $plan['p_priceMin'] : 0),
          'maxPrice' => ($plan['p_priceMax'] > 0 ? $plan['p_priceMax'] : 0),
          'typicalPriceGradation' => $typicalPriceGradation
        );

        foreach(array('priceStandard', 'priceMedium', 'pricePremium') as $type) {
          $tmpPrice = (!empty($plan['p_'.$type]) ? explode(',', $plan['p_'.$type]) : array());
          if(count($tmpPrice) == 1 && $tmpPrice[0] > 0) {
            $return['prices']['priceGradation'][$type] = $tmpPrice[0];
          } elseif(count($tmpPrice) > 1) {
            $return['prices']['priceGradation'][$type] = $tmpPrice;//(',', $tmpPrice);
          } else {
            $return['prices']['priceGradation'][$type] = '';
          }
        }

        if(is_array($return['prices']['priceGradation']['priceStandard'])) {
          $typicalPriceGradation = $return['prices']['priceGradation']['priceStandard'];
          foreach(array('priceMedium', 'pricePremium') as $type) {
            if(is_array($return['prices']['priceGradation'][$type])) {
               foreach($return['prices']['priceGradation'][$type] as $val) {
                 if($val > 0 && !in_array($val, $typicalPriceGradation)) $typicalPriceGradation[] = $val;
               }
            }
          }
          asort($typicalPriceGradation);
          if($cfg['payments']['dodjirate']) {
            array_unshift($typicalPriceGradation, 0);
          }
        }

        /* === Hook === */
        foreach (cot_getextplugins('api.getCatsPrices.extra') as $pl)
        {
        	include $pl;
        }
        /* ===== */

        $planFind = true;
      }
    }
  }
}

?>
