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

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

global $structure;

$prdId = cot_import('prdId', 'G', 'INT');
$categoryId = cot_import('categoryId', 'G', 'INT');
$attributeId = cot_import('attributeId', 'G', 'INT');

if($attributeId > 0) $categoryId = $attributeId;

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

if($categoryId > 0) {
  $catLabels = array();
  if(!empty($cfg['market']['catslabels'])) foreach(explode("\n", $cfg['market']['catslabels']) as $val) {
    $val = explode('=', $val);
    if(!empty($val[1])) {
      $val[0] = explode(',', $val[0]);
      $val[1] = explode(';', $val[1]);
      foreach($val[0] as $vv) {
        if($vv > 0) {
          $catLabels[$vv] = array(
            'label' => $val[1][0],
            'tooltip' => $val[1][1]
          );
        }
      }
    }
  }

  $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'],
        'typelabel' => (is_array($catLabels[$cat['id']]) && !empty($catLabels[$cat['id']]['label']) ? $catLabels[$cat['id']]['label'] : 'Тип'),
        'typetooltip' => (is_array($catLabels[$cat['id']]) && !empty($catLabels[$cat['id']]['tooltip']) ? $catLabels[$cat['id']]['tooltip'] : '')
      );

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

      break;
    }
  }

  $extp1 = cot_getextplugins('api.getCatsRadio.getcats');
  $extp2 = cot_getextplugins('api.getCatsRadio.parsecats');

  if($curCat['id'] > 0) {
    $catsArr = array();

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

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

    $return['html'] = '';
    $return['cntparams'] = 0;

    $item = array();
    if($prdId > 0 && $usr['id'] > 0) {
      require_once cot_incfile('market', 'module');
      $item = $db->query("SELECT * FROM $db_market WHERE item_id=".$prdId.(!$usr['isadmin'] ? ' AND item_userid='.$usr['id'] : ''))->fetch();
    } elseif(is_array($_SESSION['market_add'])) {
      $item = $_SESSION['market_add'];
    }

    $catattrs = array();
    $catext = array();
    $catext_other = array();

    if(count($item) > 0) {
      $catattrs = array();
      if($item['item_catattr'] != $item['item_cat'] && $structure['market'][$item['item_catattr']]['id'] > 0) $catattrs[] = $structure['market'][$item['item_catattr']]['id'];

      if($structure['market'][$item['item_catattr']]['parent'] != $item['item_catattr']) {
        $tempcatarr = $structure['market'][$item['item_catattr']]['parent'];
        $catattrs[] = $structure['market'][$tempcatarr]['id'];
        if($structure['market'][$tempcatarr]['parent'] != $tempcatarr) {
          $tempcatarr = $structure['market'][$tempcatarr]['parent'];
          $catattrs[] = $structure['market'][$tempcatarr]['id'];
          if($structure['market'][$tempcatarr]['parent'] != $tempcatarr) {
            $tempcatarr = $structure['market'][$tempcatarr]['parent'];
            $catattrs[] = $structure['market'][$tempcatarr]['id'];
          }
        }
      }

      $catext = (!empty($item['item_catextratypes']) ? explode(',', $item['item_catextratypes']) : array());
      $catext_other = (!empty($item['item_catextratypes_other']) ? json_decode($item['item_catextratypes_other'], 1) : array());
    }

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

    $parseparams = false;
    if(count($catsArr) > 0) {
      $return['html'] .= '
        <div class="prd-save-step__field-label">
          <label class="prd-save-step__field-label-name">'.$curCat['typelabel'].'</label>
          '.(!empty($curCat['typetooltip']) ? '<span class="tooltipster tooltip_circle tooltip_circle--hover tooltip_circle--light tooltip_circle--hover ml5" data-tooltip-text="'.$curCat['typetooltip'].'" data-tooltip-theme="dark">?</span>' : '').'
          <div class="js-prd-save-field-error prd-save-step__field-error hidden"></div>
        </div>
        <div class="prd-save-step__field-value">
          <div class="attribute-list prd-save-field-radiobox" data-parent-id="'.$curCat['id'].'" data-input-name="new_custom_attribute['.$curCat['id'].']" data-required="1" data-allow-multiple="" data-allow-custom="" data-multiple-max-count="0" data-custom-max-count="0">';

        foreach($catsArr as $cat) {
          /* === Hook === */
          foreach ($extp2 as $pl)
          {
          	include $pl;
          }
          /* ===== */
          //data-is-prd-links-sites="1"
          $return['html'] .= '<div class="attribute-item">
                                <input id="attribute_item_'.$cat['id'].'" type="radio" class="styled-radio js-bindable-attribute" data-parent-id="'.$curCat['id'].'" data-is-prd-links-sites="0" data-is-prd-links-example-images="0" name="attribute['.$curCat['id'].']" value="'.$cat['id'].'"'.((count($catattrs) > 0 && in_array($cat['id'], $catattrs)) ? 'checked="checked"' : '').' />
                                <label for="attribute_item_'.$cat['id'].'">'.$cat['name'].'</label>
                              </div>';
          $return['cntparams']++;
        }
        if($return['cntparams'] > 0) $parseparams = true;
        $return['html'] .= '</div>
                          </div>';
    }

    if($parseparams) $return['html'] = '<div class="js-field-block prd-save-step__field-block" data-parent-id="">'.trim(str_replace("\r\n", '', $return['html'])).'<div class="js-attribute-section-block" data-id="'.$curCat['id'].'"></div></div>';
  }
}

?>
