<?
include_once(_DIR_EXT.'/HTML/c_page_XML_AJAX.php') ;
//include_once('messages.php') ;
class c_page_ajax extends c_page_XML_AJAX
{

  function set_filter_debug()
  {   ob_start() ;
      //damp_array($_POST) ;
      $_SESSION['filter']['debug']['cmd']=$_POST['req_cmd'] ;
      $_SESSION['filter']['debug']['terminal_id']=$_POST['terminal_id'] ;
      $_SESSION['filter']['debug']['mo_id']=$_POST['mo_id'] ;
      $_SESSION['filter']['debug']['personal_id']=$_POST['personal_id'] ;

      $options=array(   'count'=>50,
                        'mo_id'=>$_SESSION['filter']['debug']['mo_id'],
                        'personal_id'=>$_SESSION['filter']['debug']['personal_id'],
                        'terminal_id'=>$_SESSION['filter']['debug']['terminal_id'],
                        'cmd'=>$_SESSION['filter']['debug']['cmd'],
                        'text'=>$_POST['text']
      ) ;

      $list_log=LOGS()->get_log_request($options) ;
      print_template($options,'aspmo_debug/panel_filter_status') ;
      if (_sizeof($list_log)) print_template($list_log,'aspmo_debug/list_logs_request') ;
      else echo '<div class="alert">Ничего не найдено</div>' ;

      $text=ob_get_clean() ;
      $this->add_element('panel_log.HTML',$text) ;

  }

  function set_filter_debug_log()
  {   ob_start() ;
      //damp_array($_POST) ;
      $_SESSION['filter']['debug']['cmd']=$_POST['req_cmd'] ;
      $_SESSION['filter']['debug']['terminal_id']=$_POST['terminal_id'] ;
      $_SESSION['filter']['debug']['member_id']=$_POST['member_id'] ;

      $options=array(   //'count'=>50,
                        'member_id'=>$_SESSION['filter']['debug']['member_id'],
                        'terminal_id'=>$_SESSION['filter']['debug']['terminal_id'],
                        'cmd'=>$_SESSION['filter']['debug']['cmd'],
                        'text'=>$_POST['text']
      ) ;

      $list_log=LOGS()->get_log_events($options) ;
      print_template($options,'aspmo_debug/panel_filter_status') ;
      if (_sizeof($list_log)) print_template($list_log,'aspmo_debug/list_logs_events') ;
      else echo '<div class="alert">Ничего не найдено</div>' ;

      $text=ob_get_clean() ;
      $this->add_element('panel_log.HTML',$text) ;


  }

    function debug_db()
    { ob_start() ;
      if ($_POST['_debug_db']) $GLOBALS['debug_db']=$_POST['_debug_db'] ;
      ?><h2><?echo $_POST['key']?> "<?echo $_POST['str_value']?>"</h2><?

      $res=array() ;
      switch($_POST['key'])
      { case 'whois_personal': $res=PERSONAL()->whois_personal($_POST['str_value']) ;
                               ?><strong>Поиск персонала по номеру авто и номеру пропуска</strong><br><?
                               // damp_string($_POST['str_value']) ;

                                //damp_string($_POST['str_value']) ;
                               //damp_array(str_split($_POST['str_value'])) ;
                               damp_array($res,1,-1) ;
                               break ;

        case 'get_personal (active)': $res=$rec_member=PERSONAL()->get_personal(array('propusk'=>$_POST['str_value'],'tab_number'=>$_POST['str_value']),array('set_QR_code'=>$GLOBALS['LS_person_field_to_qrcode'],'set_rules'=>1,'dop_usl'=>'enabled=1','debug'=>1)) ; ;
                                      ?><strong>Поиск персонала по номеру пропуска и табельному номеру. Только действующие записи.</strong><br><?
                                      damp_array($res,1,-1) ;
                                      break ;

        case 'get_personal (any)': $res=PERSONAL()->get_personal(array('propusk'=>$_POST['str_value'],'tab_number'=>$_POST['str_value']),array('set_QR_code'=>$GLOBALS['LS_person_field_to_qrcode'],'set_rules'=>1,'debug'=>1)) ; ;
                                   ?><strong>Поиск персонала по номеру пропуска и табельному номеру. Все записи.</strong><br><?
                                   damp_array($res,1,-1) ;
                                   break ;

        case 'get_personal_by_driver': $res=PERSONAL()->get_personal_by_driver($_POST['str_value']) ; ;
                                   ?><strong>Поиск персонала по номеру ВУ. Все записи.</strong><br><?
                                   damp_array($res,1,-1) ;
                                   break ;

        case 'get_personals':      $res=PERSONAL()->get_personals(array('pkey'=>$_POST['str_value'],'propusk'=>$_POST['str_value'],'driving'=>$_POST['str_value']),array('set_QR_code'=>$GLOBALS['LS_person_field_to_qrcode'],'debug'=>0,'order'=>'parent, c_data desc')) ; ;
                                   ?><strong>Поиск персонала по ID, пропуску или номеру ВУ. Все записи.</strong><br><?
                                   //print_2x_arr($res) ;
                                   print_template($res,'aspmo_personal/list_personal_table_img') ;
                                   break ;

      }


      if (!_sizeof($res)) echo '<div class="alert">Ничего не найдено</div>' ;
      if ($_POST['_debug_db']) $GLOBALS['debug_db']=0 ;
      //$text=htmlspecialchars(ob_get_clean()) ;
      $text=ob_get_clean() ;

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

    function get_log_stats_info()
    {   if (!$_POST['event_name'] or !$_POST['line']) return ;
        $line_id=$_POST['line'] ;
        $event_name=$_POST['event_name'] ;$stats_from=0 ;$stats_to=0 ;
        $stats=execSQL_van('select min(pkey) as t_from,max(pkey) as t_to from obj_site_mo_log where obj_name="'.$event_name.'"') ;
        if ($stats['t_from']) $stats_from=execSQL_value('select c_data from obj_site_mo_log where pkey='.$stats['t_from'].'') ;
        if ($stats['t_to'])   $stats_to=execSQL_value('select c_data from obj_site_mo_log where pkey='.$stats['t_to'].'') ;

        $this->add_element('line_'.$line_id.'_from.HTML',($stats_from)? date('d.m.Y H:i',$stats_from):'');
        $this->add_element('line_'.$line_id.'_to.HTML',($stats_to)? date('d.m.Y H:i',$stats_to):'');
        $this->add_element('line_'.$line_id.'_day.HTML',($stats_to and $stats_from)? round(($stats_to-$stats_from)/(60*60*24)):'');
        $line_id++ ;
        $this->add_element('next_line',$line_id);
    }

    function toogle_debug_sql()
     { if (MEMBER()->info['debug_sql']) ACCOUNTS()->account_save(MEMBER()->id,['debug_sql'=>0]) ;
       else                             ACCOUNTS()->account_save(MEMBER()->id,['debug_sql'=>1]) ;
       $this->update_page=1 ;
       //$this->add_element('show_modal_window','HTML',array('title'=>'toogle_debug_sql','after_close_update_page'=>1)) ;
     }


    function calculate_table_schema_size()
    {
        $db_size=execSQL_van('SELECT table_schema AS "Имя базы данных",
       SUM(data_length + index_length) as size
       FROM information_schema.TABLES
       WHERE table_schema="'.$GLOBALS['_SETTING']['main_db']['db_name'].'"
       GROUP BY table_schema;');
        $this->add_element('table_schema_size.HTML',show_size($db_size['size']));

    }

    function exec_sql()
    {

    }


}
