<?php
include_once("c_setting.php");
class c_page_cab_setting_interface extends c_page_cab_setting
{
  public $h1='Настройки' ;
  public $use_datepicker=1 ;


 function block_main()
 { $this->page_title() ;
   $this->panel_setting() ;
 }

 function panel_setting($section_name='',$options=array())
 { $_usl_settings=[] ;
   $_usl_settings[]='enabled=1' ;
   $_usl_settings[]='clss=99' ;
   $_usl_settings[]='hidden=0' ;
   // добавляем возможность прямого указания настоек, которые надо показать
   $_usl_settings_var_code=[] ;
   if (_sizeof($options['var_code'])) foreach($options['var_code'] as $var_code) $_usl_settings_var_code[]='comment="'.$var_code.'"' ;
   if (_sizeof($_usl_settings_var_code)) $_usl_settings[]='('.implode(' or ',$_usl_settings_var_code).')' ;
   $usl_settings=implode(' and ',$_usl_settings) ;
   $items=execSQL('select * from '.TM_SETTING.' where '.$usl_settings.' order by parent,indx') ;

   //check_settings_for_client(MEMBER()->cur_group_id,$items);
   // группируем настройки по разделам
   $items2=group_by_field('parent',$items) ;
   // получаем имена разделов
   $_usl=array() ;
   $_usl[]='enabled=1' ;
   $_usl[]='clss=1' ;
   $_usl[]='pkey in ('.implode(',',array_keys($items2)).')' ;
   if ($section_name) $_usl[]='obj_name like \''.$section_name.'\'' ;
   $usl=implode(' and ',$_usl) ;
   $sections=execSQL('select * from '.TM_SETTING.' where '.$usl.' order by obj_name') ;
   if (sizeof($sections)<=3) $options['no_hidden_items']=1 ;


   if (!$options['no_quick_search']) {?><div id="panel_fast_search" style="margin:10px 0;"><strong>Быстрый поиск по</strong>: <input type="text" id="fast_search_url" style="width:200px;font-size:14px;"> </div><?}

   ?>
   <table class="basic fz_small fixed setting_right" id="LS_editor"><?
   if (_sizeof($sections)) foreach($sections as $section_id=>$rec_section) if ($rec_section['obj_name']!='Настройки сайта')
   {  ?><tr class="title_setting title" dir="section_<?echo $rec_section['pkey']?>"><td colspan=3 class="section_name" id="section_<?echo $section_id?>"><h2><?echo $rec_section['obj_name']?></h2></td></tr><?
       if (_sizeof($items2[$rec_section['pkey']])) foreach($items2[$rec_section['pkey']] as $rec_item)
       {  $ext_info=unserialize($rec_item['ext_info']) ;
          $var_name=$rec_item['comment'] ;
          if ($ext_info['place']=='ini')
          { $arr_code=explode('/',$var_name) ;
            if (sizeof($arr_code)==1) $rec_item['value']=$GLOBALS['_SETTING']['LocalSettings'][$var_name] ;
            else                      $rec_item['value']=$GLOBALS['_SETTING'][$arr_code[0]][$arr_code[1]] ;

		  }

          if ($ext_info['type']=='int') $rec_item['value']=(int)$rec_item['value'] ;
          if ($ext_info['type']=='string') $rec_item['value']=(string)$rec_item['value'] ;
          if ($ext_info['type']=='numeric') $rec_item['value']=(float)$rec_item['value'] ;

          $view_save_button=1 ;
          ?><tr id="<?echo $var_name?>" class="page_item section_values <?if (!$options['no_hidden_items']) echo 'hidden'?> section_<?echo $section_id?>" page="<?echo $var_name?>" >
           <td class="left description"><span class="var_title"><?echo $rec_item['obj_name']?></span><?if ($ext_info['descr']) echo '<div class="descr">'.$ext_info['descr'].'</div>'?><div class="comment"><?echo $var_name?></div></td>
               <td class="left value" id="td_LS_<?echo $rec_item['pkey']?>_value">
                   <? switch($ext_info['el'])
                      {case 'checkbox': echo '<label class=text_value id=label_checkbox_'.$rec_item['pkey'].'>'.(($rec_item['value'])? 'Да':'Нет').'</label>' ;
                                        FORM()->element_ios7_switch($rec_item, 'value',['ajax_cmd'=>'aspmo_setting/edit_setting_toggle_checkbox','ajax_params'=>['id'=>$rec_item['pkey']]]);
                                        $view_save_button=0 ;
                                        break ;
                       case 'select':   $arr=ENGINE()->get_arr_to_edit_element($ext_info) ;
                                        if (!$rec_item['value']) $rec_item['value']=0 ;
                                        if (isset($arr[$rec_item['value']])) echo $arr[$rec_item['value']] ;
                                        break ;
                       case 'multiselect':
                                        $arr=ENGINE()->get_arr_to_edit_element($ext_info) ;
                                        $values=explode(',',$rec_item['value']) ;
                                        $arr_val=array() ;
                                        if (_sizeof($values))foreach($values as $id) $arr_val[]=$arr[$id] ;
                                        echo implode(', ',$arr_val) ;
                                        break ;
                       default:         if ($rec_item['crypt']) echo '*********' ;
                                        else {
                                            if (!$ext_info['shieldHTMLChar']) echo $rec_item['value'];
                                            else echo htmlspecialchars($rec_item['value']);
                                        }
               }
                   if ($ext_info['human']) echo '<span class=human> = '.ENGINE()->view_human_time_period($ext_info['human'],$GLOBALS[$var_name]).'</span>' ;
               ?></td>
               <td id="td_LS_<?echo $rec_item['pkey']?>_button">
                   <?if ($view_save_button and MEMBER()->rol==1 || MEMBER()->rol==7 || MEMBER()->rol==6) FORM()->button('Изменить','aspmo_setting/edit_setting',['id'=>$rec_item['pkey'], 'data-testId'=>'changeSetting']) ; ?>
               </td>
           </tr><?

       }
   }
       ?></table>
         <script type="text/javascript">
         $j(document).ready(function()
         {
             $j('td.section_name').on('click',function()
             {
                 var class_name=$j(this).attr('id') ;
                 $j('tr.'+class_name).toggleClass('hidden') ;
                 //$j(this).next('tr.head_item').toggleClass('hidden') ;
             })    ;

               var text='' ;
               $j('#fast_search_url').keyup(
                   function(e)
                   {   text=$j(this).val() ;
                       if (text!="")
                       { $j('.setting_right tr.head_item').hide().addClass('hidden') ;
                         $j('.setting_right tr.page_item').hide().addClass('hidden') ;
                         $j('.setting_right tr.page_item').each(function(el)
                         {   var class_page=$j(this).attr('page') ;
                             var title_page=$j(this).find('span.var_title').text() ;
                             console.log(title_page) ;
                             var re = new RegExp(text, 'g');
                             var re_title = new RegExp(text, 'gi');
                             var res_by_class=class_page.match(re);
                             var res_by_title=title_page.match(re_title);
                             //if (res!=null && res.length)   console.log(class_page) ;
                             if (res_by_class!=null && res_by_class.length)   $j(this).removeClass('hidden').show() ; //console.log(class_page) ;
                             if (res_by_title!=null && res_by_title.length)   $j(this).removeClass('hidden').show() ; //console.log(class_page) ;
                         }) ;
                         // скрываем title в котором все элементы hidden
                         $j('.setting_right tr.title').each(function(el)
                         {  var dir=$j(this).attr('dir') ;
                            var count_all=$j('.setting_right tr.page_item.'+dir).length ;
                            var count_hidden=$j('.setting_right tr.page_item.hidden.'+dir).length ;
                            //console.log(dir+' / '+count_all+' / '+count_hidden) ;
                            if (count_all==count_hidden)  $j(this).hide() ;
                            else $j(this).next('tr.head_item').removeClass('hidden').show() ;
                         });
                       }
                       else
                       { $j('.setting_right tr.title').show() ;
                         $j('.setting_right tr.page_item').addClass('hidden') ;
                       }

                   }) ;
         }) ;
         </script>

         <style type="text/css">
            table#LS_editor {min-width:1000px;}
            table#LS_editor td.section_name{border: solid 1px #aaaaaa; background:#E1DEDF;}
            table#LS_editor td:nth-child(1){width:300px;font-size:12px;}
            table#LS_editor td:nth-child(2){width:auto;}
            table#LS_editor td:nth-child(3){width:150px;}
            table#LS_editor td.value{font-size:16px;}
            table#LS_editor td.value span.human{font-size:12px;position:relative;top:-15px;}
            table#LS_editor td.value textarea{width:100%;height:100px;font-size:16px;}
            table#LS_editor td.value form > input{width:100%;font-size:16px;}
            table#LS_editor td.value select{_width:400px;font-size:12px;}
            table#LS_editor td div.comment{margin-top:10px;font-size:11px;}
            table#LS_editor td div.descr{font-style:italic;font-size:11px;margin-top:10px;}
         </style>
       <?
 }
}



?>
