<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=api.getCatsRadio.extra
 * [END_COT_EXT]
 */

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

require_once cot_incfile('catextratypes', 'plug');

$epArr = array();
$sql = $db->query("SELECT * FROM $db_catextratypes WHERE e_area='market' AND e_catid=".$curCat['id']." ORDER BY e_title ASC")->fetchAll();
foreach($sql as $val) {
  $epArr[$val['e_id']] = $val['e_title'];
}

$epcfg = get_catextratypes_cfg($curCat['id']);

if(count($epArr) > 0 || ($epcfg['cetextra'] > 0)) {
  $return['html'] .= '
    <div class="prd-save-step__field-label">
      <label class="prd-save-step__field-label-name">'.$epcfg['cetlabel']['label'].'</label>'
      .(!empty($epcfg['cetlabel']['tooltip']) ? '<span class="tooltipster tooltip_circle tooltip_circle--hover tooltip_circle--light tooltip_circle--hover ml5" data-tooltip-text="'.$epcfg['cetlabel']['tooltip'].'" data-tooltip-theme="dark">?</span>' : '')
      .($epcfg['cetlimit'] > 0 ? '<div class="prd-save-step__field-hint">Доступен выбор до '.cot_declension($epcfg['cetlimit'], 'варианта,вариантов,вариантов').'</div>' : '').'
      <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-checkbox" data-parent-id="'.$curCat['id'].'" data-input-name="attribute_ep[]" data-required="1" data-allow-multiple="1" data-allow-custom="'.($epcfg['cetextra'] > 0 ? 1 : 0).'" data-multiple-max-count="'.($epcfg['cetlimit'] > 0 ? $epcfg['cetlimit'] : 0).'" data-custom-max-count="'.($epcfg['cetextra'] > 0 ? $epcfg['cetextra'] : 0).'">';

   foreach($epArr as $epId => $ep) {
     $return['html'] .= '<div class="attribute-item">
                           <input id="attribute_item_'.$epId.'" type="checkbox" class="styled-checkbox classification-checkbox" data-parent-id="'.$curCat['id'].'" name="attribute_ep[]" value="'.$epId.'"'.((count($catext) > 0 && in_array($epId, $catext)) ? ' checked="checked"' : '').' />
                           <label for="attribute_item_'.$epId.'">'.$ep.'</label>
                         </div>';
     $return['cntparams']++;
   }

   if(count($catext_other) > 0) {
     $jj=0;
     foreach($catext_other as $catid => $valarr) {
       foreach($valarr as $val) {
         $return['html'] .= '<div class="attribute-item-custom">
             <input type="checkbox" class="styled-checkbox" name="attribute_ep[]" id="attribute_'.$catid.'_item_custom_'.$jj.'" value="1" checked="checked" onclick="return false;">
             <label for="attribute_'.$catid.'_item_custom_'.$jj.'">
             <input type="text" name="new_custom_attribute_title['.$catid.']['.$jj.']" value="'.$val.'"><a href="#" onclick="return false;" class="js-remove-custom-attribute-btn">×</a></label>
           </div>';
           $jj++;
       };
     }
   }

   if($epcfg['cetextra'] > 0) $return['html'] .= '<a href="#" class="js-add-custom-attribute-btn" onclick="return false;" data-custom-count="0">Добавить свой вариант</a>';

   $return['html'] .= '</div>
      </div>';
}