<?
include_once(_DIR_EXT.'/aspmo_rmv/report_mo_rmv.php') ;
class report_facecontrol_log extends report_mo_rmv
{
 var $title='Журнал Face control' ;
 var $view_only_model=0 ;
 var $mo_sort='pkey desc' ;
 var $recs_doctor=array() ;
 var $allow_export_to_csv=1 ;
 var $arr_intrusion=array('1'=>'FaceControl пройден','2'=>'FaceControl не пройден') ;


    function setting()
     {
         //$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() ;
         $arr_groups=ACCOUNTS()->get_arr_groups_select_options() ;

         list($arr_orgs_ids,$arr_otdel_ids,$arr_works_ids)=PERSONAL()->get_org_otdels_works_to_cur_account($this->filter) ;
         $arr_terminals=ESMO()->get_list_terminals(MEMBER()->cur_group_id) ;
         $arr_readers=execSQL_line('select distinct(reader) from obj_site_mo_session where reader!="" and reader!="0"') ;
         if (!$this->filter['data_from']) $this->filter['data_from']=date('d.m.Y') ;

         ?><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_multiple('group_ids',$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','ФИО:',4,array('auto_complete'=>"autocomplette_personal_name")) ;
                           $this->filter_section_input_text('propusk','Пропуск:',2) ;
                           $this->filter_section_select('intrusion','FaceControl:',2,$this->arr_intrusion) ;
                        ?>
                    </div>
                    <div class="row">
                        <? $this->filter_section_input_text('id_mo','ID МО:',2) ;
                           $this->filter_section_select('terminal_id','Терминал:',2,$arr_terminals,array('key_fname'=>'id')) ;
                           $this->filter_section_select('reader','Reader:',2,$arr_readers,array('value_is_id'=>1)) ;
                           $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">
                        <section class="col col-2">
                                <label class="label">K1=∑AF/∑DR (алкоголь):</label><label class="input"><input id=k1 name="filter[k1]" class="range-slider" type="hidden" value="0.0" /></label>
                        </section>
                        <section class="col col-2">
                                <label class="label">Среднее значение AF (алкоголь):</label><label class="input"><input id="avg_af_a" name="filter[avg_af_a]" class="range-slider" type="hidden" value="0.0" /></label>
                        </section>
                        <section class="col col-2">
                                <label class="label">Среднее значение DR (алкоголь):</label><label class="input"><input id=avg_dr_a name="filter[avg_dr_a]" class="range-slider" type="hidden" value="0.0" /></label>
                        </section>
                        <section class="col col-2">
                                <label class="label">K2=∑AF/∑DR (давление):</label><label class="input"><input id=k2 name="filter[k2]" class="range-slider" type="hidden" value="0.0" /></label>
                        </section>
                        <section class="col col-2">
                                <label class="label">Среднее значение AF (давление):</label><label class="input"><input id=avg_af_d name="filter[avg_af_d]" class="range-slider" type="hidden" value="0.0" /></label>
                        </section>
                        <section class="col col-2">
                                <label class="label">Среднее значение DR (давление):</label><label class="input"><input id="avg_dr_d" name="filter[avg_dr_d]" class="range-slider" type="hidden" value="0.0" /></label>
                        </section>
                    </div>


        <script type="text/javascript">
            $j(document).ready(function()
            {
                $j('.range-slider').jRange({
                  from: 0,
                  to: 1,
                  step: 0.01,
                  scale: [0, 0.2, 0.4, 0.6, 0.8, 1],
                  format: '%s',
                  width: '100%',
                  showLabels: true,
                  isRange: true
                });
                <? $this->range_select_set_value('k1');
                   $this->range_select_set_value('avg_af_a');
                   $this->range_select_set_value('avg_dr_a');
                   $this->range_select_set_value('k2');
                   $this->range_select_set_value('avg_af_d');
                   $this->range_select_set_value('avg_dr_d');
                ?>

          });
        </script>
                </fieldset>
                <?$this->panel_button_apply();?>
            </form>
         </div>
      <?
    }

    function range_select_set_value($fname)
    {  $cur_value=($this->filter[$fname])? $this->filter[$fname]:'0,1' ;
       list($from,$to)=explode(',',$cur_value) ;
       $js_value=$from.','.$to ;
       ?>$j('#<?echo $fname?>').jRange('setValue','<?echo $js_value?>');<?
    }


    function create_usl_select_obj($_params,&$options)
    {   $usl_select=array() ;
        //damp_array($usl_select,1,-1);
        if ($this->filter['k1'] and $this->filter['k1']!='0,1') $usl_select[]=$this->get_usl_select_by_filter('k1','ka') ;
        if ($this->filter['k2'] and $this->filter['k2']!='0,1') $usl_select[]=$this->get_usl_select_by_filter('k2','kd') ;
        if ($this->filter['avg_af_a'] and $this->filter['avg_af_a']!='0,1') $usl_select[]=$this->get_usl_select_by_filter('avg_af_a') ;
        if ($this->filter['avg_dr_a'] and $this->filter['avg_dr_a']!='0,1') $usl_select[]=$this->get_usl_select_by_filter('avg_dr_a') ;
        if ($this->filter['avg_af_d'] and $this->filter['avg_af_d']!='0,1') $usl_select[]=$this->get_usl_select_by_filter('avg_af_d') ;
        if ($this->filter['avg_dr_d'] and $this->filter['avg_dr_d']!='0,1') $usl_select[]=$this->get_usl_select_by_filter('avg_dr_d') ;
        if (_sizeof($usl_select))
        { $usl=implode(' and ',$usl_select) ;
          $mo_ids=execSQL_line('select mo_id from fc_metric where '.$usl.' and mo_id>0') ;
          if (_sizeof($mo_ids)) $options['mo_ids']=implode(',',$mo_ids) ;
        }
        //damp_array($usl_select,1,-1);
        return(parent::create_usl_select_obj($_params,$options)) ;
    }

    function get_usl_select_by_filter($field,$db_fname='')
    {   if (!$db_fname) $db_fname=$field ;
        list($k1_from,$k1_to)=explode(',',$this->filter[$field]) ;
        $usl=$db_fname.'>='.$k1_from.' and '.$db_fname.'<='.$k1_to ;
        return($usl) ;
    }


    // шаблон - вывод ссылками без разделитея
    function print_template_HTML($list_recs,$options=array())
    {  $options['id']='panel_last_mo' ;
       $options['base_dir']='' ;
       if (!isset($options['no_reverse_indx'])) $options['no_reverse_indx']=$this->filter['skoro'] ;

       //damp_array($this->filter,1,-1);
       $this->list_mo_sessions($list_recs,$options) ;

    }

    // шаблон - вывод ссылками без разделитея
      function list_mo_sessions($list_recs,$options=array())
      { $id=($options['id'])? 'id='.$options['id']:'' ;
       if (_sizeof($list_recs)) $fc_data=execSQL('select mo_id,ka,kd,avg_af_a,avg_dr_a,avg_af_d,avg_dr_d from fc_metric where mo_id in ('.implode(',',array_keys($list_recs)).')',array('debug'=>0,'use_fname_indx'=>'mo_id')) ;
        //print_2x_arr($fc_data);

        ?><div class=list_mo_sessions <?echo $id?>>
          <table class="basic fz_small full auto">

            <tr class="head">
                <th rowspan="2">№</th>
                <th rowspan="2">Дата/время</th>
                <th rowspan="2">Терминал</th>
                <th rowspan="2">Тип<br>контроля</th>
                <th rowspan="2">ФИО<br>сотрудника</th>
                <th rowspan="2">Фото</th>
                <th rowspan="2">Reader</th>
                <!--<th rowspan="2">Результат осмотра</th>
                <th rowspan="2">Допуск</th>-->
                <th colspan="3">Метрики Алкоголь</th>
                <th colspan="3">Метрики Давление</th>
                <th rowspan="2">Коммент</th>
            </tr>
            <tr>
                <th>K1=∑AF/∑DR</th>
                <th>Среднее значение AF</th>
                <th>Среднее значение DR</th>
                <th>K2=∑AF/∑DR</th>
                <th>Среднее значение AF</th>
                <th>Среднее значение DR</th>
            </tr>

              <?
            $i=($options['no_reverse_indx'])? 1:_sizeof($list_recs) ;
            if (_sizeof($list_recs)) foreach($list_recs as $rec)
              {   $fc_metric=$fc_data[$rec['pkey']] ;
                  if (!_sizeof($fc_metric)) $fc_metric=FACEID()->facecontrol_get_metric_from_file($rec['pkey']) ;

                  $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'] or $rec['_use_in_trassir']){?>&nbsp;<span class="v2" cmd="media_server/get_video_from_terminal" id_mo="<?echo $rec['pkey']?>"><i class="fa fa-video-camera black"></i></span><?}?>
                    </td>
                    <td><?echo $rec['_tk_name']?><!--<br><?echo $rec['model']?>--><br><?echo $rec['_model_name']?></td>
                    <td class="name">
                    <?  $arr_info=array() ;
                        if ($rec['member_id']) $arr_info[]='<a class="new_window" '.($rec['personal_danger_work']?'style="background-color:orange"':'').' target="_blank" title="win_second" href="'.$rec['__href'].'">'.$rec['member_name'].'</a>' ;
                        if ($rec['_org_name']) $arr_info[]=$rec['_org_name'] ;
                        if ($rec['_otdel_name']) $arr_info[]=$rec['_otdel_name'] ;
                        if (MEMBER()->rol==1 and $rec['propusk']) $arr_info[]='Пропуск: '.$rec['propusk'] ;
                        if (_sizeof($arr_info)) echo implode('<br>',$arr_info) ;
                    ?>
                    </td>
                    <td class="photo nowrap"><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="img"><? if ($rec['_member_img_alco']) {?><img width=100 src="<?echo $rec['_member_img_alco']?>"><?}?></div>
                        <div class="clear"></div></div>
                    </td>
                    <td><? if ($rec['reader']) echo $rec['reader']?></td>
                    <!--<td class="comment"><?  if ($rec['status']==2) echo '<div class=ok>'.$rec['_status_name'].'</div>' ;
                                            echo $rec['_comment'];
                                            $rec['terminal_comment']=str_replace($rec['comment'],'',$rec['terminal_comment']) ;  // убираем дублирующиемся надписи, пока временно, так
                                            if ($rec['terminal_comment']) {?><div class="terminal_comment"><i class="fa fa-desktop" aria-hidden="true"></i> <?echo $rec['terminal_comment']?></div><?}
                                        ?>
                        <?if ($rec['__dlit_mo']) echo '<br>'.$rec['__dlit_mo'].' c' ;?>
                        <?

                        ?>
                    </td>
                    <td>
                        <?



                        if (stripos('intrusion',$rec['reason'])!==false) echo '<div class=red>FaceControl не пройден</div>' ;
                        else                                             echo '<div class=green>FaceControl пройден</div>' ;
                        ?>


                    </td>-->
                   <td class="center"><?echo $fc_metric['ka']?></td>
                   <td class="center"><?echo $fc_metric['avg_af_a']?></td>
                   <td class="center"><?echo $fc_metric['avg_dr_a']?></td>
                   <td class="center"><?echo $fc_metric['kd']?></td>
                   <td class="center"><?echo $fc_metric['avg_af_d']?></td>
                   <td class="center"><?echo $fc_metric['avg_dr_d']?></td>
                  <td>
                      <a href="/cab/mo/<?echo $rec['pkey']?>/db/facecontrol/" class="new_window" title="fc_<?echo $rec['pkey']?>">Страница FC</a><br>
                      <a href="/cab/mo/<?echo $rec['pkey']?>/db/req/" class="new_window" title="mo_<?echo $rec['pkey']?>">Обмен</a><br>
                  </td>
                </tr>
                  <?

                  if ($options['no_reverse_indx']) $i++ ; else $i-- ;
              }


          ?></table><div id="panel_image_preview"></div></div>
          <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');}
                    ) ;
              }) ;
          </script>

          <?
      }



}

?>
