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

$packtype = $mdata['package_type'];
$checkcat = (!empty($item_cache['item_catattr']) ? $item_cache['item_catattr'] : $item_cache['item_cat']);

if(!empty($checkcat)) {
  $catsql = $db->query("SELECT * FROM $db_structure WHERE structure_area='market' AND structure_code='".$checkcat."' LIMIT 1")->fetch();

  if($catsql['structure_id'] > 0) {
    $plan = $db->query("SELECT * FROM $db_catcostplans WHERE p_area='market' AND p_catid=".$catsql['structure_id']." LIMIT 1")->fetch();
    $jj=0;
    while($jj < 5 && !$plan['p_id'] && $structure['market'][$catsql['structure_code']]['parent'] != $catsql['structure_code']) {
      $catsql = $db->query("SELECT * FROM $db_structure WHERE structure_area='market' AND structure_code='".$structure['market'][$catsql['structure_code']]['parent']."' LIMIT 1")->fetch();
      if($catsql['structure_id'] > 0) {
        $plan = $db->query("SELECT * FROM $db_catcostplans WHERE p_area='market' AND p_catid=".$catsql['structure_id']." LIMIT 1")->fetch();
      }
      $jj++;
    }

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

      if($plan['p_onlyprice'] == 1 && $structure['market'][$catsql['structure_code']]['parent'] != $catsql['structure_code']) {
        $parent = $db->query("SELECT * FROM $db_structure WHERE structure_area='market' AND structure_code='" . $structure['market'][$catsql['structure_code']]['parent'] . "' LIMIT 1")->fetch();
        if($parent['structure_id'] > 0 && $parent['structure_id'] != $catsql['structure_id']) $options = get_catcostplans_parent_attrs($parent, 'market', $plan['p_bl_attrs'], $options);
      }

      if(count($mdata['gextras_selected']['category']) > 0) foreach($options as $otp) {
        foreach($mdata['gextras_selected']['category'] as $popt) {
          if($popt['id'] == $otp['at_id']) {
            $t->assign(array(
              'DS_PACKAGE_DATA' => array(
                'id' => $otp['at_id'],
                'name' => $otp['at_title'],
                'hint' => $otp['at_hint'],
                'type' => ($otp['at_type'] ? 'text' : 'bool'),
                'value' => $popt['val']
              ),
            ));

            $t->parse('MAIN.INBOX.DS_ROW.DS_TYPE3_PACKAGE');
            $t->parse('MAIN.OUTBOX.DS_ROW.DS_TYPE3_PACKAGE');
            $t->parse('MAIN.DS_ROW.DS_TYPE3_PACKAGE');
            break;
          }
        }
      }

      if(count($item_catcostplans['epackages']) > 0) foreach($item_catcostplans['epackages'] as $otp) {
        $popval = $mdata['gextras_selected']['custom_val'][$otp['id']];
        if(!empty($mdata['gextras_selected']['custom_val'][$otp['id']])) {
          $otp['value'] = $mdata['gextras_selected']['custom_val'][$otp['id']];
          $t->assign('DS_PACKAGE_DATA', $otp);

          $t->parse('MAIN.INBOX.DS_ROW.DS_TYPE3_PACKAGE');
          $t->parse('MAIN.OUTBOX.DS_ROW.DS_TYPE3_PACKAGE');
          $t->parse('MAIN.DS_ROW.DS_TYPE3_PACKAGE');
        }
      }
    }
  }
}