<?
include_once('report_mo3.php') ;
class report_mo_sess_tech extends report_mo3
{
 var $title='Журнал МО - сеансы МО - ошибки работы терминалов' ;
 var $view_only_model=0 ;
 var $mo_sort='pkey desc' ;
 var $recs_doctor=array() ;
 var $allow_export_to_csv=1 ;

    function setting()
    { 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']);

        $arr_accounts=ACCOUNTS()->get_list_members(array('group_id'=>MEMBER()->cur_group_id,'rol'=>'2,3')) ;
        $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) ;


        $arr_reason=execSQL('select distinct(reason) as obj_name from obj_site_mo_session');
        unset($arr_reason[0]); unset($arr_reason[1]);
        $arr_equipment=execSQL('select distinct(equipment) as obj_name from obj_site_mo_session');
        $arr_reader=execSQL('select distinct(reader) as obj_name from obj_site_mo_session where reader!="" and reader!="0"');
        unset($arr_equipment[0]); unset($arr_equipment[1]);
//        $arr_reason=['equipmentFailure'=>'equipmentFailure','equipmentError'=>'equipmentError','cancelTimeout'=>'cancelTimeout'];

        ?><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('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('name','ФИО:',2,array('auto_complete'=>"autocomplette_personal_name")) ;
                    //$this->filter_section_select('tk','Тип контроля:',2,$_SESSION['ARR_mo_tk_type'],array('use_id_plus_1'=>1)) ;
                    $this->filter_section_input_text('data_from','От:',2,array('class'=>'datetimepicker')) ;
                    $this->filter_section_input_text('data_to','До:',2,array('class'=>'datetimepicker')) ;
                    ?>
                </div>

                <div class="row">
                    <? if (MEMBER()->rol!=2) $this->filter_section_select('doctor_id','Фельдшер:',2,$arr_accounts,array('space_element'=>1)) ;
                    $this->filter_section_select('equipment','equipment:',2,$arr_equipment,array('space_element'=>1)) ;
                    $this->filter_section_select('reason','reason:',2,$arr_reason,array('space_element'=>1)) ;
                    $this->filter_section_select('reader','reader:',2,$arr_reader,array('space_element'=>1)) ;
                    ?>
                </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>
                        <?if (_sizeof($_SESSION['ARR_mo_tk_type'])) foreach($_SESSION['ARR_mo_tk_type'] as $id=>$value)
                        {?><label class="radio"><input type="radio" name="filter[tk]" value="<?echo $id+1?>" <?if ($this->filter['tk']==($id+1)) echo 'checked'?>><i></i><?echo $value?></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>
                        <?
                        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?>" <?if (($this->filter['dopusk'][$id])) 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>
                        <label class="radio"><input type="checkbox" name="filter[v]" value="1" <?if ($this->filter['v']) echo 'checked'?>><i></i>ТКТ</label>
                    </section>
                    <section class="col col-2">
                        <label class="label">Модель МО:</label>
                        <? $arr=[1,3,5,7,9] ;
                        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=[2,4,6,8,10] ;
                        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 print_template_HTML($list_recs,$options=array())
 {     //print_2x_arr($list_recs) ;
       $id=($options['id'])? 'id='.$options['id']:'' ;
       //$arr_member_views=
       ?><div class=list_mo_sessions_report <?echo $id?>>
         <table class="report_mo_sess" >
           <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>
               <?if (MEMBER()->rol==1){?><th>Сессия МО</th><?}?>
               <th>ФИО<br>сотрудника</th>
               <th>Модель</th>
               <th>Терминал/Врач</th>
               <th>Давление</th>
               <th>Пульс</th>
               <th>Температура</th>
               <th>Алкоголь</th>
               <th>ЦНС</th>
               <th>Жалобы</th>
               <th>Результат осмотра<br>на терминале</th>
               <th>Признаки заболевания<br>и пониженной<br>работоспособности</th>
               <th>Комментарий врача</th>
               <th>Результат осмотра</th>
               <th>Допуск</th>
           </tr><?
           $i=$this->first_obj ;
           if (_sizeof($list_recs)) foreach($list_recs as $rec)
           {  $this->view_tr_item($i,$rec) ;
              if ($rec['mo1_id'])
              { $rec_prev_mo=ESMO()->get_mo_by_id($rec['mo1_id']) ;
                if ($rec_prev_mo)
                { $this->view_tr_item('',$rec_prev_mo,array('tr_class'=>'history')) ;
                  if ($rec_prev_mo['mo1_id'])
                  { $rec_prev_mo2=ESMO()->get_mo_by_id($rec_prev_mo['mo1_id']) ;
                    if ($rec_prev_mo2) { $this->view_tr_item('',$rec_prev_mo2,array('tr_class'=>'history')) ;
                                         //unset($list_recs[$rec_prev_mo['mo1_id']]) ;
                                       }
                  }
                  ?><tr><td colspan="9"></td></tr><?
                  //unset($list_recs[$rec['mo1_id']]) ;
                }
              }
              $i++ ;
           }
         ?></table></div>
           <style>
              table.report_mo_sess{}
              table.report_mo_sess td.dopusk_1{color:green;}
              table.report_mo_sess td.dopusk_2{color:#D70300;}
              table.report_mo_sess td.name a{color:black;text-decoration:none;}
              table.report_mo_sess td.name a:hover{text-decoration:underline;}
              table.report_mo_sess tr.history td{background-color:rgba(185, 221, 253, 0.15);;}
           </style>
        <?
        //damp_array($rec,1,-1) ;
     }

     function view_tr_item($i,$rec,$options=array())
     {   if (!isset($recs_doctor[$rec['doctor_id']])) $this->recs_doctor[$rec['doctor_id']]=ACCOUNTS()->get_member_by_id($rec['hand_check_user_id']) ;

         ?><tr class="item <?echo $options['tr_class']?>">
            <td><?echo $i ;?></td>
            <td><?echo date('d.m.Y',$rec['c_data']).'<br>'.date('H:i:s',$rec['c_data'])?><br>
                <?echo $_SESSION['ARR_mo_tk_type'][$rec['tk']] ; ?><br>#<?echo '<a href="/cab/mo/'.$rec['pkey'].'/" target=_blank>'.$rec['pkey'].'</a>'?>

            </td>
            <?if (MEMBER()->rol==1){?><td><?echo $rec['session_id']?><br><?echo date('H:i:s',$rec['session_id'])?></td><?}?>
            <?/*<td><?echo $rec['_org_name'].'<br>'.$rec['_otdel_name']?></td>*/?>
            <td class="name"><?if ($rec['member_id'])
                { echo '<strong><a href="/cab/personal/person/'.$rec['member_id'].'/">'.$rec['member_name'].'</a></strong>'?><br><?
                  if ($rec['member_dr']) echo $rec['member_dr'].'<br>' ;
                  echo (($rec['member_pol'])? 'Женский':'Мужской').'<br>' ;
                  echo 'Пропуск: <strong>'.$rec['propusk'].'</strong>';
                }
                ?>
            </td>
            <td><?echo $_SESSION['ARR_model_osmotr'][$rec['model']]?></td>
            <td><?if ($rec['terminal_id']) echo $rec['terminal_id'] ;
                  if ($rec['hand_check_user_id']) echo $this->recs_doctor[$rec['hand_check_user_id']]['obj_name'] ;
                ?>
            </td>
            <td><?if ($rec['tonometer']) echo (($rec['systolic'])? $rec['systolic']:'-').' / '.(($rec['diastolic'])? $rec['diastolic']:'-')?></td>
            <td><?if ($rec['pulsometer']) echo ($rec['pulse'])? $rec['pulse']:'-';?></td>
            <td><?if ($rec['thermometer']) echo ($rec['temperature']>0)? $rec['temperature']:'-';?></td>
            <td><?echo ($rec['alko']>0)? '<span class="red bold">Есть</span>'.(($rec['alko_value']>0)? ' ('.$rec['alko_value'].')':''):($rec['alcotester']? 'Нет':'-');?></td>
            <td><?if ($rec['pupilometer']) echo ($rec['narko'])? '<span class="red bold">Не пройден</span>':'Нет';?></td>
            <td><? if ($rec['status']>=2 and $rec['status']<=3) echo ($rec['question'])? '<span class="red bold">Есть</span>':'Нет' ; else echo '-' ;
                   if ($rec['question_comment']) echo '<br>'.$rec['question_comment'] ;
                ?>
            </td>
            <td class="comment"><? if ($rec['_comment']) echo '<div class=red>'.$rec['_comment'].'</div>' ; ?></td>
            <td><?  if ($rec['doctor_fixed_comment']) echo $rec['doctor_fixed_comment'].'<br>' ;
                    echo ($rec['simptoms'])? '<span class="red bold">Есть</span>':''?><br><?echo _IL('IL_diagnoz')->recs[$rec['simptoms']]['obj_name']?></td>
            <td><?echo ($rec['doctor_comment'])? $rec['doctor_comment']:''?></td>
            <td><? echo $rec['_status_name'] ; ?></td>
            <td class="dopusk_<?echo $rec['dopusk']?>"><? echo $rec['_dopusk_status']?></td>
        </tr><?
     }

    function print_template_CSV($list_recs,$options=array())
         { $i=$options['from'] ;
           if (_sizeof($list_recs)) foreach($list_recs as $rec)
             { $data=array() ;
               $data['Дата']=date('d.m.Y',$rec['c_data']) ;
               $data['ID сесcии МО']=$rec['pkey'] ;
               $data['Тип контроля']=$_SESSION['ARR_mo_type'][$rec['mo_type']] ;
               $data['Модель']=$_SESSION['ARR_model_osmotr'][$rec['model']] ;
               $data['ФИО сотрудника']=$rec['member_name'] ;
               $data['ДатаРожд']=$rec['member_dr'] ;
               $data['Возраст']=$rec['age'] ;
               $data['Пол']=($rec['member_pol'])? 'Женский':'Мужской' ;
               $data['Пропуск']='"'.$rec['propusk'].'"';
               $data['Терминал/Врач']=($rec['hand_check_user_id'])? $this->recs_doctor[$rec['hand_check_user_id']]['obj_name']:$rec['terminal_id']  ;
               $data['Давление']=($rec['tonometer'])? (($rec['systolic'])? $rec['systolic']:'-').' / '.(($rec['diastolic'])? $rec['diastolic']:'-'):''  ;
               $data['Пульс']=($rec['pulsometer'])? (($rec['pulse'])? $rec['pulse']:'-'):'';  ;
               $data['Температура']=($rec['thermometer'])? (($rec['temperature']>0)? $rec['temperature']:'-'):'';  ;
               $data['Алкоголь']=($rec['alko']>0)? 'Есть'.(($rec['alko_value']>0)? ' ('.$rec['alko_value'].')':''):($rec['alcotester']? 'Нет':'-')  ;
               $data['Жалобы']=($rec['status']>=2 and $rec['status']<=3)? ($rec['question'])? 'Есть':'Нет':'-'   ;
                               if ($rec['question_comment']) $data['Жалобы'].=' '.$rec['question_comment'] ;
               $data['Результат осмотра на терминале']=($rec['_comment'])? strip_tags($rec['_comment']):'-'   ;
               $data['ПЗиПР']=(($rec['simptoms'])? 'Есть':'')._IL('IL_diagnoz')->recs[$rec['simptoms']]['obj_name'] ;
               $data['Комментарий врача']=($rec['doctor_comment'])? $rec['doctor_comment']:'' ;
               $data['Результат осмотра']=strip_tags($rec['_status_name']);
               $data['Допуск']=strip_tags($rec['_dopusk_name']);


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

         }


}

?>
