<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=users.details.tags
 * [END_COT_EXT]
 */

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

require_once cot_incfile('pricelist', 'plug');
list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('pricelist', 'any', 'RWA');

$pricelist_edit = ($tab == 'pricelist' && $action == 'edit' && ($usr['isadmin'] || $usr['id'] == $urr['user_id']));
if($tab == 'pricelist' && $action == 'edit' && !$pricelist_edit) $action = '';

$t1 = new XTemplate(cot_tplfile(array('pricelist', ($pricelist_edit ? 'edit' : 'show')), 'plug'));

$sqllist_rowset = $db->query("SELECT * FROM $db_pricelist WHERE pl_uid=" . $urr['user_id'])->fetchAll();

/* === Hook === */
$extp = cot_getextplugins('pricelist.userdetails.loop');
/* ===== */

$urrpricelist = array();
$urrpricelist_sub = array();
$urrpricelist_desc = array();
$urrpricelist_desc_sub = array();
$urrpricelist_names = array();
$urrpricelist_names_sub = array();

$jj=0;
foreach($sqllist_rowset as $pl)
{
  $jj++;

	/* === Hook === */
	foreach ($extp as $pl)
	{
		include $pl;
	}
	/* ===== */

  if(empty($pl['pl_name'])) {
    $pl['pl_name'] = 'cat';
  } else {
    if($pl['pl_type'] == -1) {
      $urrpricelist_names[$pl['pl_cat']] = $pl['pl_name'];
    } else {
      $urrpricelist_names_sub[$pl['pl_cat']] = $pl['pl_name'];
    }
  }
  if(!empty($pl['pl_desc'])) {
    if($pl['pl_type'] == -1) {
      $urrpricelist_desc[$pl['pl_cat']] = $pl['pl_desc'];
    } else {
      $urrpricelist_desc_sub[$pl['pl_cat']] = $pl['pl_desc'];
    }
  }

  $urrpricelist[$pl['pl_cat']][$pl['pl_name']][$pl['pl_type']] = array(
    'min' => $pl['pl_cost_min'],
    'max' => $pl['pl_cost_max'],
    'desc' => $pl['pl_desc']
  );
  $urrpricelist_sub[$pl['pl_cat']][$pl['pl_type']] = array(
    'min' => $pl['pl_cost_min'],
    'max' => $pl['pl_cost_max']
  );
}

$cnt_pl_types = 0;
foreach($pricelis_types as $type => $data) {
  $cnt_pl_types++;
  $t1->assign(array(
    'PL_ROW_TYPE_TITLE' => $data['title']
  ));

  $t1->parse('MAIN.PL_TABLE_HEADER');
}

$pricelist_count_parsed=0;
$jjcat=0;
foreach($structure['market'] as $code => $cat) {
  if (mb_substr_count($cat['path'], ".") == 0) {
    $parsecat = ($pricelist_edit ? true : false);

    $minmax = array();
    $catminmax = array();
    foreach($pricelis_types as $type => $data) {
      $catminmax[$type] = array(
        'min' => 0,
        'max' => 0
      );
    }

    $t1->assign(array(
      'CAT_ROW_ID' => $cat['id'],
      'CAT_ROW_TITLE' => (!empty($urrpricelist_names[$code]) ? $urrpricelist_names[$code] : $cat['title']),
      'CAT_ROW_CODE' => $code,
      'CAT_ROW_DESC' => (!empty($urrpricelist_desc[$code]) ? $urrpricelist_desc[$code] : ''),
    ));

    $cat['subcnt_inuser'] = 0;
    $cat['subcnt'] = 0;
    foreach($structure['market'] as $subcode => $subcat) {
      if($subcat['parent'] == $code && $subcat['parent'] != $subcode) {
        $parsesubcat = ($pricelist_edit ? true : false);

        $t1->assign(array(
          'SUBCAT_ROW_ID' => $subcat['id'],
          'SUBCAT_ROW_TITLE' => (!empty($urrpricelist_names[$subcode]) ? $urrpricelist_names[$subcode] : $subcat['title']),
          'SUBCAT_ROW_CODE' => $subcode,
          'SUBCAT_ROW_DESC' => (!empty($urrpricelist_desc_sub[$subcode]) ? $urrpricelist_desc_sub[$subcode] : ''),
        ));

        foreach($pricelis_types as $type => $data) {
          $minmax = $urrpricelist_sub[$subcode][$type];
          $t1->assign(array(
            'PL_ROW_TYPE_ID' => $type,
            'PL_ROW_TYPE_VALUTA' => $data['valuta'],
            'PL_ROW_TYPE_FILED_MAX' => (in_array('max', $data['fields'])),
            'PL_ROW_VALUES_MIN' => ($minmax['min'] > 0 ? round($minmax['min']) : ''),
            'PL_ROW_VALUES_MAX' => ($minmax['max'] > 0 ? round($minmax['max']) : ''),
            'PL_ROW_VALUES_MIN_FRMT' => ($minmax['min'] > 0 ? number_format($minmax['min'], 0, '.', ' ') : ''),
            'PL_ROW_VALUES_MAX_FRMT' => ($minmax['max'] > 0 ? number_format($minmax['max'], 0, '.', ' ') : '')
          ));

          if($minmax['min'] > 0) {
            $parsesubcat = true;
            if($minmax['min'] < $catminmax[$type]['min'] || $catminmax[$type]['min'] == 0) $catminmax[$type]['min'] = $minmax['min'];
            if($minmax['min'] > $catminmax[$type]['max']) $catminmax[$type]['max'] = $minmax['min'];
            if($minmax['max'] > 0 && $minmax['max'] > $catminmax[$type]['max']) $catminmax[$type]['max'] = $minmax['max'];
          }

          $t1->parse('MAIN.PL_CATS_ROW.SUBCATS_ROW.SUB_PL_TYPES');
        }

        if($parsesubcat) {
          $parsecat = true;
          $t1->parse('MAIN.PL_CATS_ROW.SUBCATS_ROW');
        } else {
          $t1->reset('MAIN.PL_CATS_ROW.SUBCATS_ROW.SUB_PL_TYPES');
        }
      }
    }

    if (count((array)$urrpricelist[$code]) > 0) {
      foreach($urrpricelist[$code] as $pl_name => $pl_data) {
        $parsesubcat = ($pricelist_edit ? true : false);
        $parsesubcat_ext = false;

        $t1->assign(array(
          'PL_ROW_EXT_NAME' => $pl_name,
        ));

        foreach($pricelis_types as $type => $data) {
          $minmax = $pl_data[$type];
          $t1->assign(array(
            'PL_ROW_TYPE_ID' => $type,
            'PL_ROW_EXT_DESC' => $minmax['desc'],
            'PL_ROW_TYPE_VALUTA' => $data['valuta'],
            'PL_ROW_TYPE_FILED_MAX' => (in_array('max', $data['fields'])),
            'PL_ROW_VALUES_MIN' => ($minmax['min'] > 0 ? round($minmax['min']) : ''),
            'PL_ROW_VALUES_MAX' => ($minmax['max'] > 0 ? round($minmax['max']) : ''),
            'PL_ROW_VALUES_MIN_FRMT' => ($minmax['min'] > 0 ? number_format($minmax['min'], 0, '.', ' ') : ''),
            'PL_ROW_VALUES_MAX_FRMT' => ($minmax['max'] > 0 ? number_format($minmax['max'], 0, '.', ' ') : '')
          ));

          if($minmax['min'] > 0) {
            $parsesubcat_ext = true;
            $parsesubcat = true;
            if($minmax['min'] < $catminmax[$type]['min'] || $catminmax[$type]['min'] == 0) $catminmax[$type]['min'] = $minmax['min'];
            if($minmax['min'] > $catminmax[$type]['max']) $catminmax[$type]['max'] = $minmax['min'];
            if($minmax['max'] > 0 && $minmax['max'] > $catminmax[$type]['max']) $catminmax[$type]['max'] = $minmax['max'];
          }

          $t1->parse('MAIN.PL_CATS_ROW.PL_EXTRA_ROW.EXT_PL_TYPES_ADD');
        }

        if($parsesubcat && $parsesubcat_ext) {
          $parsecat = true;
          $t1->parse('MAIN.PL_CATS_ROW.PL_EXTRA_ROW');
        } else {
          $t1->reset('MAIN.PL_CATS_ROW.PL_EXTRA_ROW.EXT_PL_TYPES_ADD');
        }
      }
    }
    if($parsecat) {
      foreach($pricelis_types as $type => $data) {
        $t1->assign(array(
          'PL_ROW_TYPE_ID' => $type,
          'PL_ROW_TYPE_VALUTA' => $data['valuta'],
          'PL_ROW_TYPE_FILED_MAX' => (in_array('max', $data['fields'])),
          'PL_ROW_TYPE_MINMAX' => array(
            'min' => ($catminmax[$type]['min'] > 0 ? number_format($catminmax[$type]['min'], 0, '.', ' ') : ''),
            'max' => ($catminmax[$type]['max'] > 0 ? number_format($catminmax[$type]['max'], 0, '.', ' ') : ''),
          ),
        ));

        $t1->parse('MAIN.PL_CATS_ROW.CAT_PL_TYPES');
        $t1->parse('MAIN.PL_CATS_ROW.CAT_PL_TYPES_OTS');
        $t1->parse('MAIN.PL_CATS_ROW.CAT_PL_TYPES_ADD');
      }

      $pricelist_count_parsed++;
      $t1->parse('MAIN.PL_CATS_ROW');
    }
  }
}

/* === Hook === */
foreach (cot_getextplugins('pricelist.userdetails.tags') as $pl)
{
	include $pl;
}
/* ===== */

$t1->assign(array(
  'PL_CNT_TYPES' => $cnt_pl_types,
  'PL_USER_ID' => $urr['user_id'],
  'PL_EDIT_LINK' => (($usr['isadmin'] || $usr['id'] == $urr['user_id']) ? cot_url('users', 'm=details&u='.$urr['user_name'].'&tab=pricelist&action=edit') : ''),
  'PL_USER_LINK' => cot_url('users', 'm=details&u='.$urr['user_name'].'&tab=pricelist'),
));

$t1->parse("MAIN");

$t->assign(array(
	"USERS_DETAILS_PRICELIST_COUNT" => $pricelist_count_parsed,
  "USERS_DETAILS_PRICELIST_URL" => cot_url('users', 'm=details&u='.$urr['user_name'].'&tab=pricelist'),
  "USERS_DETAILS_PRICELIST_EDIT_LINK" => (($usr['isadmin'] || $usr['id'] == $urr['user_id']) ? cot_url('users', 'm=details&u='.$urr['user_name'].'&tab=pricelist&action=edit') : '')
));

$t->assign('PRICELIST', $t1->text("MAIN"));