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

global $db_x, $db_catcostplans, $db_catcostplans_attrs, $costplans_prices;
// Table names
cot::$db->registerTable('catcostplans');
cot::$db->registerTable('catcostplans_attrs');

$db_catcostplans = $db_x.'catcostplans';
$db_catcostplans_attrs = $db_x.'catcostplans_attrs';

$costplans_prices = array(
  1 => 100,
  2 => 200,
  3 => 300,
  4 => 400,
  5 => 500,
  6 => 1000,
  7 => 1500,
  8 => 2000,
  9 => 2500,
  10 => 3000,
  11 => 6000
);

function get_catcostplans_parent_attrs($parent = array(), $area = 'market', $bl = '', $attrs = array()) {
  global $cfg, $structure, $db, $db_structure, $db_catcostplans, $db_catcostplans_attrs;

  $return = array();

  if(is_array($parent) && $parent['structure_id'] > 0) {
    $cp = $db->query("SELECT * FROM $db_catcostplans WHERE p_area='" . $db->prep($area) . "' AND p_catid=".$parent['structure_id']." LIMIT 1")->fetch();
    if($cp['p_id'] > 0) {
      $bl = (!empty($bl) ? explode(',', $bl) : array());
      $options = $db->query("SELECT * FROM $db_catcostplans_attrs WHERE at_planid=".$cp['p_id']." ORDER BY at_id ASC")->fetchAll();
      foreach($options as $opt) {
        $opt['notdelete'] = 1;
        if(!in_array($opt['at_id'], $bl)) $return[] = $opt;
      }

      if($cp['p_onlyprice'] == 1 && $structure['market'][$parent['structure_code']]['parent'] != $parent['structure_code']) {
        $parentchk = $db->query("SELECT * FROM $db_structure WHERE structure_area='" . $db->prep($area) . "' AND structure_code='" . $structure['market'][$parent['structure_code']]['parent'] . "' LIMIT 1")->fetch();
        if($parentchk['structure_id'] > 0 && $parentchk['structure_id'] != $parent['structure_id']) $return = get_catcostplans_parent_attrs($parentchk, $area, $cp['p_bl_attrs'], $return);
      }
    }
  }
  if(count($attrs) > 0) {
    foreach($attrs as $opt) {
      $return[] = $opt;
    }
  }

  return $return;
}

function cot_get_costplan_bydata($costplan, $curtype = '') {
  $curtypes = (!empty($curtype) ? (in_array($curtype, array('standard', 'medium', 'premium')) ? $curtype : 'standard') : array('standard', 'medium', 'premium'));
  if(!is_array($costplan)) $costplan = (!empty($costplan) ? json_decode($costplan,1) : array());

  $cpinfo = array(
    'package' => 0,
    'days' => 1,
    'title' => '',
    'price' => 0,
    'price_dodji' => 0,
    'options' => array(),
    'bundle' => array(),
    'epackages' => array(),
    'price_format' => '',
    'price_dodji_format' => '',
    'days_format' => '',
  );

  if(is_array($costplan) && $costplan['bundle_type'] == 'package') {
    $cpinfo['package'] = 1;

    if(is_array($curtypes)) {
      foreach($curtypes as $type) {
        $tmparr = array(
          'duration' => $costplan['bundles'][$type]['duration'],
          'price' => $costplan['bundles'][$type]['price'],
          'price_dodji' => $costplan['bundles'][$type]['price_dodji']
        );

        if($tmparr['price'] > 0 && ($cpinfo['price'] == 0 || $cpinfo['price'] > $tmparr['price'])) $cpinfo['price'] = $tmparr['price'];
        if($tmparr['price_dodji'] > 0 && ($cpinfo['price_dodji'] == 0 || $cpinfo['price_dodji'] > $tmparr['price'])) $cpinfo['price_dodji'] = $tmparr['price_dodji'];

        if($tmparr['duration'] > 0 && ($cpinfo['days'] == 0 || $cpinfo['days'] > $tmparr['duration'])) $cpinfo['days'] = $tmparr['duration'];
      }
    } else {
      $cpinfo['price'] = $costplan['bundles'][$curtypes]['price'];
      $cpinfo['price_dodji'] = $costplan['bundles'][$curtypes]['price_dodji'];

      $cpinfo['days'] = $costplan['bundles'][$curtypes]['duration'];
      $cpinfo['bundle'] = $costplan['bundles'][$curtypes];
    }

    $cpinfo['epackages'] = $costplan['epackages'];
  } elseif($costplan['bundle_type'] == 'single') {
    if($costplan['bundles']['category']['time'] > 0) $cpinfo['days'] = $costplan['bundles']['category']['time'];
    if($costplan['bundles']['category']['price'] > 0) $cpinfo['price'] = $costplan['bundles']['category']['price'];
    if($costplan['bundles']['category']['price_dodji'] > 0) $cpinfo['price_dodji'] = $costplan['bundles']['category']['price_dodji'];
    $cpinfo['title'] = $costplan['bundles']['category']['title'];

    $cpinfo['options'] = $costplan['bundles']['custom'];
  }

  $cpinfo['price_format'] = number_format($cpinfo['price'], 0, '', ' ');
  $cpinfo['price_dodji_format'] = ($cpinfo['price_dodji'] > 0 ? number_format($cpinfo['price_dodji'], 0, '', ' ') : 0);

  $cpinfo['days_format'] = cot_declension($cpinfo['days'], 'день,дня,дней');

  return $cpinfo;
}
