<?
include_once(_DIR_TO_MODULES.'/engine/i_form.php') ;
include_once(_DIR_EXT.'/HTML/c_page_XML_AJAX.php') ;
//include_once('messages.php') ;
//include_once (_DIR_EXT.'/metronix/i_metronix_form.php') ;
class c_page_ajax extends c_page_XML_AJAX
{
  var  $rec_theme ;
  var  $rec_question ;
  var  $recs_answers ;
  var  $recs_contents ;
  var  $h1 ;

  function edit()
  {  ob_start() ;
     $id=($_POST['id'])? $_POST['id']:$_GET['id'];
     list($a,$pkey,$tkey)=explode('_',$id) ;
     $fname=str_replace('el_'.$pkey.'_'.$tkey.'_','',$id) ;
     $rec=get_obj_info($pkey.'.'.$tkey) ;
     if ($rec['pkey'])
     { $text=_CLSS($rec['clss'])->generate_edit_element($rec,$fname) ;
       $td_text='<td id='.$_POST['id'].' reffer="'.$tkey.'.'.$pkey.'">'.$text.'</td>' ;
       $this->add_element($id,$td_text) ;
     }
    $text=ob_get_clean() ;
    $this->add_element('debug',$text) ;
  }

  function check()
  {
     $id=($_POST['id'])? $_POST['id']:$_GET['id'];
     list($a,$pkey,$tkey)=explode('_',$id) ;
     $fname=str_replace('el_'.$pkey.'_'.$tkey.'_','',$id) ;
     $rec=get_obj_info($pkey.'.'.$tkey) ;
     if ($rec['pkey'])
     { $obj[$tkey][$rec['clss']][$pkey][$fname]=($rec[$fname])? 0:1 ;
       save_list_obj($obj) ;
       $rec[$fname]=$obj[$tkey][$rec['clss']][$pkey][$fname] ;
       $this->add_element('value',$rec[$fname]) ;
       list($text,$td_attr)=_CLSS($rec['clss'])->generate_field_text($rec,$fname) ;
       $this->add_element($id.'.HTML',$text) ;
     }

  }

  function get_obj_panel()
  { ob_start() ;
    $rec=get_obj_info($_POST['item_id']) ;
    //damp_array(_CLSS($rec['clss'])->menu) ;
	?>
	<div class="card"><div class="card-header"><?echo _CLSS($rec['clss'])->name.' "<strong>'.$rec['obj_name'].'</strong>"'?>

	<div class="card-header-actions">
        <a class="card-header-action btn-setting" href="#"><i class="icon-settings"></i></a>
        <a class="card-header-action btn-minimize" href="#" data-toggle="collapse" data-target="#collapseExample" aria-expanded="true"><i class="icon-plus"></i></a>
        <!--<a class="card-header-action btn-close" href="#"><i class="icon-close"></i></a>-->
	</div>

	</div><div class="card-body"><?
  	//damp_array($_POST,1,-1) ;

  	//damp_array($rec,1,-1) ;
  	if ($rec['clss'])
    {   $arr_menu_items=array() ;
        if (_sizeof(_CLSS($rec['clss'])->menu)) foreach( _CLSS($rec['clss'])->menu as $rec_item)
        {  if ($rec_item['type']) switch($rec_item['type'])
            { case 'structure':   // получаем все классы дочерних элементов объекта
                	             $arr_clss=select_obj_childs_clss_cnt($rec['_reffer']) ;
                                 if (_sizeof($arr_clss)) foreach($arr_clss as $clss=>$count)  $arr_menu_items['clss_'.$clss]=array('name'=>_CLSS($clss)->name,'attr'=>array('clss'=>$clss,'action'=>'view_childs_objects')) ;
                                 break ;
              default:           $arr_menu_items['type_'.$rec_item['type']]=array('name'=>$rec_item['name'],'attr'=>array('action'=>$rec_item['type'])) ;  break ;
            }

            if ($rec_item['cmd'])    $arr_menu_items[$rec_item['cmd']]=array('name'=>$rec_item['name'],'attr'=>array('action'=>'exec_func','func'=>$rec_item['cmd'],'script'=>$rec_item['script'])) ;
            if ($rec_item['action']) $arr_menu_items['action_'.$rec_item['action']]=array('name'=>$rec_item['name'],'attr'=>array('action'=>'clss_func','action_id'=>$rec_item['action']))  ;
        }


        //$arr_menu_items['props']='Свойства' ;
        $arr_menu_items['dump']=array('name'=>'Дамп БД','attr'=>array('action'=>'dump_rec')) ;
         //damp_array($arr_menu_items) ;
    	$i=0 ;
    	?><ul class="nav nav-pills" reffer="<?echo $rec['_reffer']?>"><?
    	if (_sizeof($arr_menu_items))foreach($arr_menu_items as $id=>$item)
        {  if (!is_array($item)) $item=['name'=>$item] ;
           $arr_attr=array() ;
           $arr_attr[]='data-toggle="pill"' ;
           $arr_attr[]='role="tab"' ;
           $arr_attr[]='id="items_'.$id.'"' ;
           $arr_attr[]='href="#'.$id.'"' ;
           $arr_attr[]='aria-controls="#'.$id.'"' ;
           $arr_attr[]='aria-selected="true"' ;
           if (_sizeof($item['attr'])) foreach($item['attr'] as $attr_id=>$attr_value) $arr_attr[]=$attr_id.'="'.$attr_value.'"' ;
           $attr=(_sizeof($arr_attr))? implode(' ',$arr_attr):'' ;
           ?><li class="nav-item"><a class="nav-link <?if (!$i) echo "active show"?>" <?echo $attr?>><?echo $item['name']?></a></li><?
          $i++ ;
        }
        ?></ul><?

        $i=0 ;
        ?><div class="tab-content" id="pills-tabContent"><?
        if (_sizeof($arr_menu_items))foreach($arr_menu_items as $id=>$item)
        {   ?><div class="tab-pane fade <?if (!$i) echo "active show"?>" id="<?echo $id?>" role="tabpanel" aria-labelledby="item_<?echo $id?>"><?
            //damp_array($item,1,-1) ;
            ?></div><?
            $i++ ;
        }

      ?></div>
        <script type="text/javascript">
            $j(document).ready(function ()
            {
            $j('a.nav-link').on('click', function (e)
                {  var reffer=$j(this).closest('ul').attr('reffer') ;
                   var menu_id=e.target.id ;
                  console.log(menu_id) // newly activated tab
                  var arr_attr=convert_tag_attributes_to_array(this) ;
                  arr_attr['cmd']='class/get_menu_item_content' ;
                  arr_attr['reffer']=reffer ;
                  arr_attr['menu_id']=menu_id ;
                  console.log(arr_attr) ;
                  send_ajax_request(arr_attr) ;
                }) ;
            });
       </script>


        <?

    	 //damp_array($arr_clss) ;
    	//damp_array(_CLSS($rec['clss'])) ;
    }        //report_objects_clss_7
    //panel_report_objects_7

	?>
	  <?
  	$text=ob_get_clean() ;


  	$this->add_element('upload_contents.HTML',$text) ;

  }

  function get_menu_item_content()
  {  ob_start() ;
     //damp_array($_POST,1,-1) ;
     if ($_POST['action']) switch($_POST['action'])
     {  case 'view_childs_objects': $this->view_childs_objects($_POST) ; break ;
        case 'prop':                $this->view_object_prop($_POST) ; break ;
        case 'dump_rec':            $this->view_object_dump($_POST) ; break ;
        case 'exec_func':           $this->exec_func($_POST) ; break ;

     }
     $text=ob_get_clean() ;
     $menu_id=$_POST['menu_id'] ;
     $panel_id=str_replace('items_','',$menu_id) ;

     $this->add_element($panel_id.'.HTML',$text) ;
  }

  function view_childs_objects_old($in_data)
  { $obj_info=get_obj_info($in_data['reffer']) ;
    $options=array() ;
    _CLSS($obj_info['clss'])->view_childs_objects($obj_info,$options) ;
  }

  function view_childs_objects($in_data)
  { $clss=$in_data['clss'] ;
    list($pkey,$tkey)=explode('.',$in_data['reffer']) ;
    ?><div id="report_objects_clss_<?echo $clss?>"><?
      include_once(_DIR_EXT.'/class/report_objects.php') ;
      $report_obj=new report_objects() ;
      $report_obj->no_view_title=1 ;
      $report_obj->table_name=_DOT($tkey)->table_name ;
      $report_obj->root_id=$pkey ;
      $report_obj->view_clss=$clss ;
      //$report_obj->view_panel_info=0 ;
      $report_obj->setting() ;
      //$report_obj->panel_info_filter() ;
      $report_obj->view() ;
    ?></div><?
  }

  function view_object_prop($in_data)
  { $rec=get_obj_info($in_data['reffer']) ;
    _CLSS($rec['clss'])->show_props($rec) ;
  }

  function view_object_dump($in_data)
  { $rec=get_obj_info($in_data['reffer']) ;
    echo 'Таблица: <strong>'._DOT($rec['tkey'])->table_name.'</strong><br><br>' ;
    damp_array($rec,1,-1) ;
  }

  function exec_func($in_data)
  {  $rec=get_obj_info($in_data['reffer']) ;
     $clss=$rec['clss'] ;
     echo 'Подключаем класс '.$rec['clss'].'<br>' ;
     include_class($clss) ;
     // сначала ищем метод класса
     if (method_exists(_CLSS($clss),$in_data['func']))
     {   echo 'Выполяем метод '.$in_data['func'].' класса '.$clss.'<br>' ;
         _CLSS($clss)->$in_data['func']($in_data) ;
     }
     else
     {  if ($in_data['script'])
         {   $script_dir=_DIR_TO_ENGINE.'/'.$in_data['script'].'/'.$in_data['func'].'.php' ;
             if (!file_exists($script_dir)) echo 'Скрипт не найден по пути  <strong>'.hide_server_dir($script_dir).'</strong><br>' ;
         }
         if (!function_exists($in_data['func'])) echo 'Не найдена глобальная функция <strong>'.$in_data['func'].'</strong><br>' ;
         else  $in_data['func']($in_data) ;

     }
  }



  function update_report_objects()
    {   ob_start() ;
        //damp_array($_POST) ;
        include_once(_DIR_EXT.'/class/report_objects.php') ;
        $report_obj=new report_objects(['filter'=>$_POST['filter']]) ;
        $report_obj->no_view_title=1 ;
        $report_obj->table_name=$_POST['table_name'] ;
        $report_obj->root_id=$_POST['root_id']  ;
        $report_obj->view_clss=$_POST['view_clss'] ;
        //$report_obj->view_panel_info=0 ;
        $report_obj->setting() ;
        //$report_obj->panel_info_filter() ;
        $report_obj->view() ;
        $text=ob_get_clean() ;
        $this->add_element('report_objects_clss_'.$_POST['view_clss'].'.HTML',$text) ;

    }

    function save_objects()
    {
        //damp_array($_POST) ;
        save_list_obj($_POST['obj']) ;
        //$this->add_element('show_modal_window','HTML',array('title'=>'Сохранение данных объекта')) ;
        $this->update_report_objects() ;


    }



}
