<?php
// специальный отчет для добавления сотрудников в группы
class report_rabbit extends c_report
{
 var $title='Пакеты Rabbit' ;
 var $view_by_space_filter=0 ;

 function setting($options=array())
 {  $arr_types=execSQL_row('select distinct(type),type from log_rabbit') ;
    ?><div id="panel_filter">
             <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                  <fieldset>
                      <div class="row">
                           <?
                           $this->filter_section_select('type','Тип пакета:',2,$arr_types,array('nospace_element'=>0)) ;
                           $this->filter_section_input_text('message_id','ID пакета:',2) ;
                           $this->filter_section_input_text('text','Ход обработки:',2) ;
                           $this->filter_section_input_text('date_from','От:',2,array('class'=>'datetimepicker')) ;
                           $this->filter_section_input_text('date_to','До:',2,array('class'=>'datetimepicker')) ;
                           //$this->filter_section_input_text('number','Номер машины:',2) ;
                           ?>
                      </div>
                  </fieldset>
                  <?$this->panel_buttons();?>
              </form>
       </div><br>
     <?
  }

  function _panel_report_buttons()
  {
    ?><button use_form="form_report" class="button v1" href="/cab/esmo_ruda/routers/new/">Добавить маршрут</button><?
  }


  function create_usl_select_obj($_params=array(),&$options=array())
  {  //damp_array($this->filter,1,-1) ;
     $usl=array() ;
     $usl[]='1' ;
     if ($this->filter['type'])            $usl[]='type="'.$this->filter['type'].'"' ;
     if ($this->filter['text'])            $usl[]='content like "%'.$this->filter['text'].'%"' ;
     if ($this->filter['message_id'])      $usl[]='message_id like "'.$this->filter['message_id'].'%"' ;
     if ($this->filter['date_from'])       $usl[]='c_data>='.strtotime($this->filter['date_from']) ;
     if ($this->filter['date_to'])         $usl[]='c_data<='.strtotime($this->filter['date_to']) ;
     $usl_res=implode(' and ',$usl) ;
     //damp_array($usl,1,-1) ;
     return($usl_res) ;
  }

 function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;

   if ($this->filter['type'])    $info['Тип пакетов']='"'.$this->filter['type'].'"' ;
   if ($this->filter['message_id'])    $info['ID сообщения']='"'.$this->filter['message_id'].'"' ;
   if ($this->filter['date_from'])    $info['От']='"'.$this->filter['date_from'].'"' ;
   if ($this->filter['date_to'])    $info['До']='"'.$this->filter['date_to'].'"' ;

   if (_sizeof($info)) foreach($info as $title=>$value) $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
   return $title ;
 }


  function get_cnt_items($usl,$options=array())
  { $count=execSQL_value('select count(pkey) from  log_rabbit where '.$usl) ;
    return($count) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    if ($this->filter['date_from']) $this->filter['order']='pkey' ;
    $order=($this->filter['order'])? ' order by '.$this->filter['order']:' order by pkey desc' ;
    $options['debug']=0 ;
    $recs=execSQL('select *  from  log_rabbit where '.$usl.$order.$limit,$options) ;
    //if (_sizeof($recs)) foreach($recs as $id=>$rec) ESMO_RUDA()->prepare_public_info_to_route($recs[$id]) ;
    return($recs) ;
  }

  function get_cnt_arr_levels($arr,&$cnt)
  {
    if (_sizeof($arr)) foreach($arr as $id=>$value)
    { $cnt++ ;
      if (is_array($value)) $this->get_cnt_arr_levels($value,$cnt) ;
    }
  }
  function show_th_headers($arr)
  {
    if (_sizeof($arr)) foreach($arr as $id=>$value)
    {   $cnt=0 ;
        if (is_array($value)) $this->get_cnt_arr_levels($value,$cnt) ;
        ?><th rowspan="<?echo $cnt?>"><?echo $id?></th><?

    }
  }


// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
  {   //damp_array($this->filter,1,-1) ;
      //print_2x_arr($list_recs) ;
      if ($this->filter['type'])
      {   $d=array() ;
          if (_sizeof($list_recs)) foreach($list_recs as $rec)
          { //$data=json_decode(base64_decode($rec['content']),1) ;
            //$d=array_merge($d,$data) ;
          }
          //damp_array($d,1,-1) ;
          $cnt=0 ;
          $this->get_cnt_arr_levels($d,$cnt) ;
          //echo '$cnt_level_1='.$cnt.'<br>' ;
      }


      ?><table class="basic fz_small full auto">
        <tr><th>ID</th><th>Дата</th><th>Направление</th><th>Тип</th><th>Message ID</th><th>Пакет</th><th>Ход обработки</th><th>Операции</th><?
          if ($this->filter['type'])
            {   $d=array() ;
                if (_sizeof($list_recs)) foreach($list_recs as $rec)
                { //$data=json_decode(base64_decode($rec['content']),1) ;
                  //$d=array_merge($d,$data) ;
                }

                $cnt=0 ;
                $this->get_cnt_arr_levels($d,$cnt) ;
                ?><th _rowspan="<?echo $cnt?>">Содержимое пакета</th><tr><?
            }
            else
            {?></tr><?}
          ?>

        <?
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
        {   $data=array() ;  $fname_msg='' ;  $fname_body='' ;  $comment='' ;
            if ($rec['uid']){   $fname_msg='rabbit_packet_'.$rec['uid'].'_msg.json' ;
                                $fname_body='rabbit_packet_'.$rec['uid'].'_body.json' ;
                                if ($this->filter['type'])
                                { if (file_exists(_TEMP_DIR.$fname_body))
                                  { $cont=file_get_contents(_TEMP_DIR.$fname_body) ;
                                    $data=json_decode($cont,1) ;
                                  } else $data=array('error'=>'Не найден файл '.$fname_body) ;
                                }
                                $comment=$rec['content'] ;
                            }
            elseif ($this->filter['type'] and $rec['content']) $data=json_decode(base64_decode($rec['content']),1) ;

            ?><tr class=item>
            <td><?echo $rec['pkey']?></td>
            <td><?echo date('d.m.Y H:i:s',$rec['c_data'])?></td>
            <td><?echo ($rec['direct']==1)? 'Входящий':'Исходящий'?></td>
            <td><?echo $rec['type']?></td>
            <td><?echo $rec['message_id']?></td>
            <td><?
                if ($rec['uid'])
                { if (file_exists(_TEMP_DIR.$fname_msg)) echo '<a href="/temp/'.$fname_msg.'" target="_blank">MSG</a> ' ;
                  if (file_exists(_TEMP_DIR.$fname_body)) echo '<a href="/temp/'.$fname_body.'" target="_blank">BODY</a>' ;
                }
                if ($this->filter['type'])
                {   if (file_exists(_TEMP_DIR.$fname_body))
                    {$cont=file_get_contents(_TEMP_DIR.$fname_body) ;
                     $data=json_decode($cont,1) ;
                     echo ' - '.strlen($cont).'байт<br>' ;
                    }
                    else echo 'Файл <strong>'.$fname_body.'</strong> - <span class="red">не найден</span><br>' ;
                }
                ?></td>
            <td class="left"><?echo $comment?></td>
            <? if ($this->filter['type'] and _sizeof($data)){?><td><?damp_array($data,1,-1)?></td><?}?>
            <td><?if ($rec['direct']==1){?><button class="v2" cmd="rabbit/resend_packet" packet_id="<?echo $rec['pkey']?>">Принять повторно</button><?}?></td>
            </tr><?
        }
      ?></table><?
  }

}

?>
