<?php
include_once(_DIR_EXT.'/aspmo_reports/report_mo3.php') ;
class report_mo_session_time extends report_mo3
{
 var $title='Отчет по длительности сессии МО' ;
  public $view_by_space_filter=1 ;

 function _setting()
    { //$this->filter['model']=array('') ;
      //damp_array($this->filter) ;
      if (isset($this->filter['time_from'])) $this->filter['data_from']=date('d.m.Y',$this->filter['time_from']);
      if (isset($this->filter['time_to'])) $this->filter['data_to']=date('d.m.Y',$this->filter['time_to']);
      if (isset($this->filter['personal_id'])) $this->filter['name']=PERSONAL()->get_name_by_id($this->filter['personal_id']);
      $members=ACCOUNTS()->get_list_members() ;
      ?><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">
                    <?if (MEMBER()->rol!=2){?>
                        <section class="col col-3">
                            <label class="label">Врач:</label>
                            <label class="select">
                                <select id=doctor_id name="filter[doctor_id]"><option value="0">Все</option>
                                    <?if (_sizeof($members))foreach($members as $rec_member) if ($rec_member['rol']==2 or $rec_member['rol']==3)
                                      {?><option value="<?echo $rec_member['pkey']?>" <?if ($this->filter['doctor_id']==$rec_member['pkey']) echo 'selected'?>><?echo $rec_member['_rol_name'].' '.$rec_member['obj_name']?></option><?}
                                    ?>
                                </select>
                            </label>
                        </section><?}?>
                        <section class="col col-3">
                            <label class="label">От:</label>
                            <label class="input"><input type="text" name="filter[data_from]" class="datetimepicker" data-required value="<?echo $this->filter['data_from']?>"></label>
                        </section>
                        <section class="col col-3">
                            <label class="label">До:</label>
                            <label class="input"><input type="text" name="filter[data_to]" class="datetimepicker" data-required value="<?echo $this->filter['data_to']?>"></label>
                        </section>
                        <?if (MEMBER()->rol!=2){?>
                        <section class="col col-3">
                                                <label class="label">Группа:</label>
                                                <label class="select"><?
                                                 {   $list_group=execSQL_row('select pkey,obj_name from obj_site_account where clss=206 and enabled=1 and pkey!=1 order by obj_name') ;
                                                     ?><select name="filter[group_id]"><option value="0">Все</option><?
                                                     if (_sizeof($list_group)) foreach($list_group as $group_id=>$group_name) {?><option value="<?echo $group_id?>" <?if($this->filter['group_id']==$group_id) echo 'selected'?>><?echo $group_name?></option><?}
                                                     ?></select><?
                                                 }
                                                 ?>
                                                </label>
                                            </section><?}?>
                    </div>
                    <div class="row">
                        <section class="col col-3">
                            <label class="label">ФИО:</label>
                            <label class="input"><input type="text" name="filter[name]" value="<?echo $this->filter['name']?>" id="personal_name" SetAutoComplete cmd="autocomplette_personal_name"></label>
                        </section>
                        <!--<section class="col col-3">
                            <label class="label">Пропуск:</label>
                            <label class="input"><input type="text" name="filter[propusk]" value="<?echo $this->filter['propusk']?>"></label>
                        </section>-->
                        <section class="col col-3">
                    	<label class="label">Организация:</label>
                                <label class="select">
                                        <select name="filter[org_ids][]" multiple="multiple">
                                            <? $list_org=PERSONAL()->get_list_personal_section() ;
                                               foreach($list_org as $rec)  {?><option value="<?echo $rec['pkey']?>" <?if (isset($this->filter['org_ids'][$rec['pkey']])) echo 'selected'?>><?echo $rec['obj_name']?></option><?}?>
                                	    </select>
                                </label>
                        </section>

                    </div>
                    <div class="row">
                        <section class="col col-2">
                            <label class="label">Тип контроля:</label>
                            <label class="radio"><input type="radio" name="filter[tk]" value="0" <?if (!$this->filter['tk']) echo 'checked'?>><i></i>Не важно</label>
                            <label class="radio"><input type="radio" name="filter[tk]" value="1" <?if ($this->filter['tk']==1) echo 'checked'?>><i></i>предрейсовый</label>
                            <label class="radio"><input type="radio" name="filter[tk]" value="2" <?if ($this->filter['tk']==2) echo 'checked'?>><i></i>послерейсовый</label>
                        </section>
                        <section class="col col-2">
                            <label class="label">Результат МО:</label>
                            <label class="radio"><input type="radio" name="filter[status]" value="0" <?if (!$this->filter['status']) echo 'checked'?>><i></i>Не важно</label>
                            <? if (_sizeof($_SESSION['ARR_status_sess'])) foreach($_SESSION['ARR_status_sess'] as $id=>$value)  if ($id>0)
                               {?><label class="radio"><input type="radio" name="filter[status]" value="<?echo $id?>" <?if ($this->filter['status']==$id) echo 'checked'?>><i></i><?echo $value?></label><?}
                            ?>
                        </section>
                        <section class="col col-2">
                            <label class="label">Допуск к смене:</label>
                            <label class="radio">
                            <? if (_sizeof($_SESSION['ARR_status_dopusk'])) foreach($_SESSION['ARR_status_dopusk'] as $id=>$value)
                               {?><label class="radio"><input type="checkbox" name="filter[dopusk][]" value="<?echo $id+1?>" <?if (($this->filter['dopusk'][$id+1])) echo 'checked'?>><i></i><?echo $value?></label><?}
                            ?>
                        </section>
                        <section class="col col-2">
                            <label class="label">Причина отказа:</label>
                            <label class="radio"><input type="checkbox" name="filter[a]" value="1" <?if ($this->filter['a']) echo 'checked'?>><i></i>Алкоголь</label>
                            <label class="radio"><input type="checkbox" name="filter[d]" value="1" <?if ($this->filter['d']) echo 'checked'?>><i></i>Давление</label>
                            <label class="radio"><input type="checkbox" name="filter[p]" value="1" <?if ($this->filter['p']) echo 'checked'?>><i></i>Пульс</label>
                            <label class="radio"><input type="checkbox" name="filter[t]" value="1" <?if ($this->filter['t']) echo 'checked'?>><i></i>Температура</label>
                            <label class="radio"><input type="checkbox" name="filter[n]" value="1" <?if ($this->filter['n']) echo 'checked'?>><i></i>ПП</label>
                            <label class="radio"><input type="checkbox" name="filter[q]" value="1" <?if ($this->filter['q']) echo 'checked'?>><i></i>Жалобы</label>
                        </section>
                        <section class="col col-2">
                            <label class="label">Модель МО:</label>
                            <? $arr=[3,5,7] ;
                               foreach($arr as $model_id)
                               {?><label class="radio"><input type="checkbox" name="filter[model][]" value="<?echo $model_id?>" <?if ($this->filter['model'][$model_id]) echo 'checked'?>><i></i><?echo $_SESSION['ARR_model_osmotr'][$model_id]?></label><?}
                            ?>
                        </section>
                        <section class="col col-2">
                            <label class="label">&nbsp;</label>
                            <? $arr=[4,6,8] ;
                               foreach($arr as $model_id)
                               {?><label class="radio"><input type="checkbox" name="filter[model][]" value="<?echo $model_id?>" <?if ($this->filter['model'][$model_id]) echo 'checked'?>><i></i><?echo $_SESSION['ARR_model_osmotr'][$model_id]?></label><?}
                            ?>
                        </section>
                    </div>
                </fieldset>
                <?$this->panel_buttons();?>
            </form>
         </div>
      <?
    }

    function create_usl_select_obj($_params,&$options)
    {
        $res=parent::create_usl_select_obj($_params,$options) ;
        $res.=' and not model in (1,2,9,10)' ;
        //echo $res ;

        return($res) ;
    }


function print_template_HTML($list_recs,$options=array())
{

    $id=($options['id'])? 'id='.$options['id']:'' ;
       ?>
         <table class="basic fz_small full auto">
           <colgroup><col id="c0"><col id="c1"><col id="c3"><col id="c4"><col id="c11"><col id="c12"><col id="c13"></colgroup>
           <tr><th>№</th>
               <th>Модель МО</th>
               <th>Дата МО</th>
               <th>Время МО1</th>
               <th>Интервал</th>
               <th>Время МО2</th>
               <th>Интервал</th>
               <th>Время РМВ</th>
               <th>Группа</th>
               <th>Фамилия, имя, отчество</th>
               <th>Жалобы</th>
               <th>Заключение о результатах мед.осмотра</th>
             </tr>
           <?
           $i=$this->first_obj ; ;
           if (_sizeof($list_recs)) foreach($list_recs as $rec)
           {   $rec_prev_mo1=array() ;      $rec_prev_mo2=array() ; $times=array() ;
               if ($rec['mo1_id'])          $rec_prev_mo1=execSQL_van('select c_data,mo1_id,terminal_id from obj_site_mo_session where pkey='.$rec['mo1_id']) ;
               if ($rec_prev_mo1['mo1_id']) $rec_prev_mo2=execSQL_van('select c_data,mo1_id,terminal_id from obj_site_mo_session where pkey='.$rec_prev_mo1['mo1_id']) ;
               $time_mo1=$rec_prev_mo1['c_data'] ;
               $time_mo2=$rec_prev_mo2['c_data'] ;
               if ($time_mo2) $times['mo1']=$time_mo2 ;
               if ($time_mo1 and !$times['mo1']) $times['mo1']=$time_mo1 ; else $times['mo2']=$time_mo1 ;
               if (!$rec['terminal_id'])  $times['rmv']=$rec['c_data'] ; else $times['mo2']=$rec['c_data']  ;

               ?><tr class=item>
                   <td><?echo '<a href="/cab/mo/'.$rec['pkey'].'/" target=_blank>'.$i.'</a>' ;  $i++ ;?></td>
                   <td><?echo $_SESSION['ARR_model_osmotr'][$rec['model']]?></td>
                   <td><?echo date('d.m.Y',$rec['c_data'])?></td>
                   <td><?if ($times['mo1']) echo date('H:i',$times['mo1'])?></td>
                   <td><?if ($times['mo1'] and $times['mo2']) echo $this->get_interval($times['mo2'],$times['mo1']) ; ?></td>
                   <td><?if ($times['mo2']) echo date('H:i',$times['mo2'])?></td>
                   <td><?if ($times['rmv'] and $times['mo2']) echo $this->get_interval($times['rmv'],$times['mo2']) ;
                         else if ($times['rmv'] and $times['mo1']) echo $this->get_interval($times['rmv'],$times['mo1']) ;
                       ?></td>
                   <td><?if ($times['rmv']) echo date('H:i',$times['rmv'])?></td>
                   <td class="left"><?echo $rec['group_name']?></td>
                   <td class="left"><?echo $rec['member_name']?>
                                    <?if ($rec['member_dr']) echo '<br>'.$rec['member_dr'].' г.р.';?>
                   </td>
                   <td><?echo $rec['_question'] ; if ($rec['question_comment']) echo '<br>'.$rec['question_comment']?></td>
                   <td  class="left"><?echo ($rec['dopusk']==1)? 'Допущен':'<strong class="red">Не допущен</strong>' ;?>
                        <div><?if ($rec['status']>=2 and $rec['status']<=3 and $rec['simptoms']) echo _IL('IL_diagnoz')->recs[$rec['simptoms']]['obj_name']?></div><?
                        if ($rec['comment']) { $arr=explode('<br>',$rec['comment']) ; echo '<ul>' ; if (_sizeof($arr)) foreach($arr as $value)  echo '<li>'.$value.'</li>' ; echo '</ul>' ; }
                        //if ($rec['doctor_fixed_comment']) echo '<div>'.$rec['doctor_fixed_comment'].'</div>' ;
                        if ($rec['doctor_comment']) echo $rec['doctor_comment'];
                       ?>
                   </td>
               </tr><?
           }
         ?></table><?

        //damp_array($rec,1,-1) ;

}

   function get_interval($t1,$t2)
   {
       $d1=new DateTime(date('d.m.Y H:i:s',$t1));
       $d2=new DateTime(date('d.m.Y H:i:s',$t2));
       $diff=$d2->diff($d1);
       echo '+ ' ;
       $arr=array() ;
       $arr[]=vsprintf('%02d',$diff->h) ;
       $arr[]=vsprintf('%02d',$diff->i) ;
       $arr[]=vsprintf('%02d',$diff->s) ;
       return(implode(':',$arr)) ;
   }


    function print_template_CSV($list_recs,$options=array())
                { $i=$options['from'] ;
                  if (_sizeof($list_recs)) foreach($list_recs as $rec)
                    {
                       $rec_prev_mo1=array() ;      $rec_prev_mo2=array() ; $times=array() ;
                       if ($rec['mo1_id'])          $rec_prev_mo1=execSQL_van('select c_data,mo1_id,terminal_id from obj_site_mo_session where pkey='.$rec['mo1_id']) ;
                       if ($rec_prev_mo1['mo1_id']) $rec_prev_mo2=execSQL_van('select c_data,mo1_id,terminal_id from obj_site_mo_session where pkey='.$rec_prev_mo1['mo1_id']) ;
                       $time_mo1=$rec_prev_mo1['c_data'] ;
                       $time_mo2=$rec_prev_mo2['c_data'] ;
                       if ($time_mo2) $times['mo1']=$time_mo2 ;
                       if ($time_mo1 and !$times['mo1']) $times['mo1']=$time_mo1 ; else $times['mo2']=$time_mo1 ;
                       if (!$rec['terminal_id'])  $times['rmv']=$rec['c_data'] ; else $times['mo2']=$rec['c_data']  ;


                      $data=array() ;
                      $data['mo_id']=$rec['pkey'] ;
                      $data['Дата']=date('d.m.Y H:i:s',$rec['c_data']) ;
                      $data['Модель МО']=$_SESSION['ARR_model_osmotr'][$rec['model']] ;
                      $data['Время МО1']=($times['mo1'])? date('d.m.Y H:i',$times['mo1']):'' ;
                      $data['Интервал1']=($times['mo1'] and $times['mo2'])? $this->get_interval($times['mo2'],$times['mo1']):'' ;
                      $data['Время МО2']=($times['mo2'])? date('d.m.Y H:i',$times['mo2']):'' ;
                      $data['Интервал2']=($times['rmv'] and $times['mo2'])? $this->get_interval($times['rmv'],$times['mo2']):(($times['rmv'] and $times['mo1'])? $this->get_interval($times['rmv'],$times['mo1']):'') ;
                      $data['Время РМВ']=($times['rmv'])? date('d.m.Y H:i',$times['rmv']):'' ;
                      $data['ФИО сотрудника']=$rec['member_name'] ;
                      $data['ДатаРожд']=$rec['member_dr'] ;
                      $data['Место работы']=$rec['_org_name'].' '.$rec['_otdel_name'] ;
                      $data['GUID сотрудника']=$rec['member_uid'] ;
                      $data['Жалобы']=($rec['status']==2 or $rec['status']==3)? ($rec['question'])? 'Есть':'Нет':'-'   ; if ($rec['question_comment']) $data['Жалобы'].=' '.$rec['question_comment'] ;
                      $data['Допуск']=$rec['_dopusk_name'];


                      $this->put_data_to_CSV($data,$i) ;
                      $i++ ;
                    }

                }


}


?>
