<?
// вниамение!!!
// во всех фильтрах:
// data_from, data_to - дата в текстовом формате
// time_from, time_to - дата в UNIX формате

class report_mo_rmv_speed extends c_report
{
 var $title='Журнал МО - скорость прохождения МО' ;
 var $view_only_model=1 ;
 var $mo_sort='pkey' ;
 public $view_by_space_filter=0 ;

 function init($options=array())
 {
    if (!$this->filter['group_id']) $this->filter['group_id']=MEMBER()->cur_group_id ;
 }

 function setting()
 {
     $arr_groups=ACCOUNTS()->get_arr_groups_to_cur_account() ;
     list($arr_orgs_ids,$arr_otdel_ids,$arr_works_ids)=PERSONAL()->get_org_otdels_works_to_cur_account($this->filter) ;

     ?><div id="panel_filter">
       <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="<?echo $this->url_report?>">
            <fieldset>
                <div class="row">
                    <? $this->filter_section_select('group_id',$GLOBALS['LS_person_group_title'].':',2,$arr_groups) ;
                       $this->filter_section_select_multiple('org_ids','Организация:',2,$arr_orgs_ids) ;
                       $this->filter_section_select_multiple('otdel_ids','Подразделение:',2,$arr_otdel_ids) ;
                       $this->filter_section_input_text('data_from','От:',2,array('class'=>'datetimepicker')) ;
                       $this->filter_section_input_text('data_to','До:',2,array('class'=>'datetimepicker')) ;
                    ?>
                </div>
            </fieldset>
            <?$this->panel_button_apply();?>
        </form>
     </div>
  <?
}

 function panel_info_filter()
 { $_str=array() ; $info=array() ; $title='' ;     //damp_array($this->filter,1,-1) ;

   // начало смены или авторизация пользователя
   if ($this->filter['data_from'])                       $info['c']=$this->filter['data_from'] ;
   if ($this->filter['data_to'])                         $info['по']=$this->filter['data_to'] ;
   if ($this->filter['time_from'])                       $info['c']=date('d.m.Y H:i',$this->filter['time_from']) ;
   if ($this->filter['time_to'])                         $info['по']=date('d.m.Y H:i',$this->filter['time_to']) ;
   if ($this->filter['group_id'])                        $info[$GLOBALS['LS_person_group_title'].':']=ACCOUNTS()->get_member_name_by_id($this->filter['group_id'])  ;
   if ($this->filter['org_id'])                          $info['Организация']=PERSONAL()->get_name_by_id($this->filter['org_id'])  ;
   if ($this->filter['otdel_id'])                        $info['Подразделение']=PERSONAL()->get_name_by_id($this->filter['otdel_id'])  ;
   if ($this->filter['org_ids'])                         $info['Организация']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['org_ids']))  ;
   if ($this->filter['otdel_ids'])                       $info['Подразделение']=PERSONAL()->get_org_names_by_ids(array_keys($this->filter['otdel_ids']))  ;
   if ($this->filter['dep_id'])                          $info['Подразделение']=PERSONAL()->get_full_dep_name_by_dep_id($this->filter['dep_id'])  ;
   if (_sizeof($info)) foreach($info as $title=>$value)   if (is_array($value)) $_str[]=$title.': <strong>'.implode(', ',$value).'</strong>' ; else $_str[]=$title.': <strong>'.$value.'</strong>' ;
   if (_sizeof($_str)) $title='<p class=center>'.implode(' ',$_str).'</p>' ;
   return $title ;

 }

 function create_usl_select_obj($_params,&$options)
 { //damp_array($this->filter,1,-1) ;
   $options_select=array() ;
   $options_select['status']='2,3' ;  // только завершенные МО
   $options_select['use_usl']='e_data>0' ;  // только завершенные МО
   $options_select['use_usl']='e_data>c_data' ;  // только завершенные МО
   $options_select['use_usl']='(terminal_virt=0 or terminal_virt is null)' ;  // только завершенные МО
   $options_select['use_usl']='terminal_id>0' ;  // только завершенные МО
   $options_select['use_usl']='mo_dlit>10' ;  // только завершенные МО


   if ($this->filter['group_id'])  $options_select['group_id']=$this->filter['group_id'] ;
   if ($this->filter['time_from']) $options_select['time_from']=$this->filter['time_from'] ;
   if ($this->filter['time_to'])   $options_select['time_to']=$this->filter['time_to'] ;
   if ($this->filter['data_from']) $options_select['data_from']=$this->filter['data_from'] ;
   if ($this->filter['data_to'])   $options_select['data_to']=$this->filter['data_to'] ;
   if ($this->filter['dep_id'])        $options_select['dep_id']=$this->filter['dep_id'] ;
   if ($this->filter['org_id'])    $options_select['org_id']=$this->filter['org_id'] ;
   if ($this->filter['org_id'])    $options_select['org_id']=$this->filter['org_id'] ;
   if ($this->filter['otdel_id'])  $options_select['otdel_id']=$this->filter['otdel_id'] ;
   if ($this->filter['org_ids'])  $options_select['org_ids']=$this->filter['org_ids'] ;
   if ($this->filter['otdel_ids'])  $options_select['otdel_ids']=$this->filter['otdel_ids'] ;

   $options_select['use_member_personal_filter']=1 ; // учитывать персональные настройки аккаунта по отбору персонала

   // было OR, но это не позволяло делать корректные выборки при наличии нескольких условий
   if (_sizeof($options_select['_use_usl'])) $options_select['use_usl']='('.implode(' and ',$options_select['_use_usl']).')' ;
   //damp_array($this->filter) ;
   //damp_array($options_select) ;
   $options_select['debug']=0 ;
   $usl_res=ESMO()->get_usl_select_mo_by_filter($options_select) ;
   //if (_IS_SYSTEM_IP) echo $usl_res ;
   return($usl_res) ;
 }

  function get_cnt_items($usl,$options=array())
  { //$options['debug']=2 ;
    if (isset($this->filter['org_ids']) or isset($this->filter['otdel_ids']) or isset($this->filter['name'])) $this->cnt_items=execSQL_value('select count(pkey) from '.ESMO()->view_mo_session.' where '.$usl.' limit 50',$options) ;
    else                                                                                                      $this->cnt_items=execSQL_value('select count(pkey) from '.ESMO()->view_mo_session.' where '.$usl.' limit 50',$options) ;
    if ($this->cnt_items>50)  $this->cnt_items=50 ;
    return($this->cnt_items) ;
  }

  function get_items($usl,$options=array())
  { $limit=($options['limit'])? ' '.$options['limit']:'' ;
    //$order=($options['order'])? ' order by '.$options['order']:' order by '.$this->mo_sort ;
    $order=' order by mo_dlit' ; //.$this->mo_sort ;
    if ($options['order']) $order=' order by '.$options['order'] ;
    $options['debug']=0 ;
    $recs=execSQL('select * from '.ESMO()->view_mo_session.' where '.$usl.$order.$limit,$options) ;
    if (_sizeof($recs) and !$options['no_public_info']) foreach($recs as $id=>$rec) ESMO()->prepare_public_info_to_mo($recs[$id],$options) ;
    return($recs) ;
  }

  function view_as_HTML($options=array())
  { if (!$this->no_view_title){?><h2 class="center"><?echo $this->title?></h2><?}
    if ($this->view_panel_info) echo $this->panel_info_filter() ;
      $options['count']=50 ;
      $options['no_any_panels']=1 ;
    return($this->HTML_report_body($options)) ;

  }


  function print_template_HTML($list_recs,$options=array())
  {  ?><div id=report_rmv_speed class=list_mo_sessions>
      <table>
        <tr class="head"><th>№</th><th>Дата/время</th><th>Терминал</th><th>Тип<br>контроля</th><th>ФИО<br>сотрудника</th><th>Фото</th><th>Результат осмотра</th><th>Время прохождения МО, сек</th></tr><?
        $i=1 ;
        if (_sizeof($list_recs)) foreach($list_recs as $rec)
          {   $options['i']=$i ;
              ?><tr class=item id="mo_tr_<?echo $rec['pkey']?>">
                  <td><?echo $options['i'] ; ?><br><br><a class="new_window"  target="_blank" title="win_second" href="<?echo $rec['__href']?>"><?echo $rec['pkey']?></a><?if ($rec['member_demo_access']){?><div class="demo_access" title="Сотрудник может проходить неограниченное число МО"></div><?}?></td>
                  <td><?echo $rec['_data']?><br><?echo $rec['_doctor_name']?></td>
                  <td><?echo $rec['group_name'].'<br>' ; echo ($rec['hand_check_user_id'])? '<img src="/images/doctor2.png" title="'.$rec['_hand_check_user_name'].'"><br>'.$rec['_hand_check_user_name']:$rec['terminal_name']?>

                      <?if ($rec['_term_cam_1'] or $rec['video']){?>&nbsp;<span class="v2" cmd="media_server/get_video_from_terminal" id_mo="<?echo $rec['pkey']?>"><i class="fa fa-video-camera"></i></span><?}?>
                  </td>
                  <td><?echo $rec['_tk_name']?><!--<br><?echo $rec['model']?>--><br><?echo $rec['_model_name']?></td>
                  <td class="name"><a class="new_window"  target="_blank" title="win_second" href="<?echo $rec['__href']?>"><?echo $rec['member_name']?></a><br><?echo $rec['_otdel_name']?><? if (MEMBER()->rol==1) echo '<br>Пропуск: '.$rec['propusk']?></td>
                  <td class="photo"><div>
                      <div class="img"><? if ($rec['_member_img']) { ?><img width=100 src="<?echo $rec['_member_img']?>"><?}?></div>
                      <div class="img"><? if ($rec['_member_img_auth']) {?><img width=100 src="<?echo $rec['_member_img_auth']?>"><?}?></div>
                      <div class="clear"></div></div>
                  </td>
              <td class="result">
                <? $arr=array() ;
                         if ($rec['alcotester'])  $arr[]='Алкоголь' ;
                         if ($rec['pupilometer'])  $arr[]='Пупилометр' ;
                         if ($rec['tonometer'])  $arr[]='Артериальное давление' ;
                         if ($rec['pulsometer'])  $arr[]='Пульс' ;
                         if ($rec['thermometer'])  $arr[]='Температура' ;
                         if ($rec['vcheck'])  $arr[]='ТКТ' ;
                         if (_sizeof($arr)) echo implode('<br>',$arr) ;
                ?>
                </td>
                  <td>
                      <?if ($rec['__dlit_mo']) echo '<span style=font-size:30px;">'.$rec['__dlit_mo'].'</span>' ;?>
                  </td>
              </tr>
        <?

               $i++ ;
          }


      ?></table><div id="panel_image_preview"></div></div><?
      if (!$options['no_back_update']){
      ?>
      <script type="text/javascript">
          $j(document).ready(function()
          {    $j('div.list_mo_sessions td.photo div.img img').hover(
                  function() { $j('div#panel_image_preview').addClass('active').html('<img src="'+$j(this).attr('src')+'">');},
                  function() {  $j('div#panel_image_preview').removeClass('active');}
                ) ;
          }) ;
          $j('body').everyTime(2000,'update_report_back',function()
          {   send_ajax_request({cmd:'report/get_report_contents',code:'report_mo_rmv_speed',panel_id:'report_rmv_speed',filter:"<?echo _CUR_FILTER?>"}) ;
          }) ;

      </script>
      <?}
  }


}
?>
