<?php
include_once('c_mo_db.php') ;
class c_page_cab_mo_db_facecontrol extends c_page_cab_mo_db
{
 public $h1='Дамп записи МО' ;
    public $use_fusioncharts=1 ;


 function get_cur_page_meta_tags()
 {
   if ($this->rec_personal['pkey']) $this->h1='Дамп facecontrol при проверке сотрудника '.$this->rec_personal['obj_name'] ;
 }

 function block_main()
 { $this->page_title() ;
   //damp_array($this->rec_mo,1,-1) ;
   $fname=_DIR_TO_ROOT.'/logs/examination_result_'.$this->rec_mo['pkey'].'.html' ;
   if (file_exists($fname))
   { $content=file_get_contents(_DIR_TO_ROOT.'/logs/examination_result_'.$this->rec_mo['pkey'].'.html') ;
     $data=json_decode($content,true) ;

     if ($data['args']['data']['face_control']['detector_data'])
     { $result=FACEID()->facecontrol_get_metric($data['args']['data']['face_control']['detector_data']) ;
       FACEID()->save_fc_metric($this->rec_mo['pkey'],$result) ;
     }



     damp_array($result,1,-1);


      $fc_events=$data['args']['data']['face_control']['detector_data']['events'] ;
      $fc_detector_data=$data['args']['data']['face_control']['detector_data']['frames'] ;

      // заполняем массив данными для корректного построения графиков
      foreach($fc_detector_data as $i=>$rec) $fc_detector_data[$i]['af']=($rec['af'])? 1:0 ;
      foreach($fc_events as $rec_event) $fc_detector_data[$rec_event['f']][$rec_event['n']]=2 ;
      for($i=$result['a_frm_start'];$i<=$result['a_frm_end'];$i++) $fc_detector_data[$i]['a']=3 ;
      for($i=$result['d_frm_start'];$i<=$result['d_frm_end'];$i++) $fc_detector_data[$i]['d']=4 ;

      echo '<strong>Алкотест</strong><br>' ;
      echo 'K1=AF/DR='.$result['ka'].'<br>' ;
      echo 'Среднее значение AF='.$result['avg_af_a'].'<br>' ;
      echo 'Среднее значение DR='.$result['avg_dr_a'].'<br>' ;

      echo '<strong>Измерение давления</strong><br>' ;
      echo 'K2=AF/DR='.$result['kd'].'<br>' ;
      echo 'Среднее значение AF='.$result['avg_af_d'].'<br>' ;
      echo 'Среднее значение DR='.$result['avg_dr_d'].'<br>' ;

      $this->report_graf($fc_detector_data,$fc_events) ;

      print_2x_arr($fc_events);

      damp_array($fc_detector_data);


   } else echo '<div class="alert">Лог обмена уже удален</div>' ;

 }

 function report_graf($frames,$events)
         {   //print_2x_arr($result);
             $caption='График фреймов' ;






             ?><div id="chart-container">Подождите, идет построение графика</div>
               <script type="text/javascript">
               $j(document).ready(function(){
                FusionCharts.ready(function(){
               var revenueChart2 = new FusionCharts({
                     renderAt: 'chart-container',
                     //type: 'line',
                     //type: 'zoomline',
                     type: 'mscombi2d',
                     width: '100%',
                     height: '400',
                     "showanchors": "0",
                     "showvalues": "0",
                     "drawcrossline": "1",
                     dataFormat: 'json',
                     dataSource: {
                         "chart": {
                             "caption": "<?echo $caption?>",
                             //"compactDataMode": "1",
                             //"dataSeparator": "|",

                             //"pixelsPerPoint": "0",
                             //"lineThickness": "1",

                             //"snumberPrefix": "$",
                             //"pYAxisMaxValue": "1500",
                             //"pYAxisMinValue": "900",
                             //"sYAxisMaxValue": "23000",
                             //"sYAxisMinValue": "15000",
                             //"theme": "fusion",

                            //Cosmetics
                          "paletteColors" : "#0075c2,#1aaf5d,#f2c500",
                           "baseFontColor" : "#333333",
                           "baseFont" : "Helvetica Neue,Arial",
                           "captionFontSize" : "14",
                           "subcaptionFontSize" : "14",
                           "subcaptionFontBold" : "0",
                           "showBorder" : "0",
                           "bgColor" : "#ffffff",
                           "showShadow" : "0",
                           "canvasBgColor" : "#ffffff",
                           "canvasBorderAlpha" : "0",
                           "divlineAlpha" : "100",
                           "divlineColor" : "#999999",
                           "divlineThickness" : "1",
                           "divLineIsDashed" : "1",
                           "divLineDashLen" : "1",
                           "divLineGapLen" : "1",
                           "usePlotGradientColor" : "0",
                           "showplotborder" : "0",
                           "showXAxisLine" : "1",
                           "xAxisLineThickness" : "1",
                           "xAxisLineColor" : "#999999",
                           "showAlternateHGridColor" : "0",
                           "showAlternateVGridColor" : "0",
                           "legendBgAlpha" : "0",
                           "legendBorderAlpha" : "0",
                           "legendShadow" : "0",
                           "legendItemFontSize" : "10",
                           "legendItemFontColor" : "#666666"





                         },
                         "categories":
                         [{

                             "category": <?echo $this->get_category_values($frames)?>
                         }],
                         "dataset":
                         [
                             {   "seriesName": "Подтвержденное лицо (af)",
                                 "color": "0075c2",
                                 "showValues": "0",
                                 "showanchors": "0",
                                 "renderAs": "line",
                                 "data": <? echo $this->get_arr_values_full($frames,'af','',array('shift'=>0.04))?>
                             },
                            {    "seriesName": "Результат (dr)",
                                 "color": "1aaf5d",
                                 "renderAs": "line",
                                "showanchors": "0",
                                 "showValues": "0",
                                 "data": <? echo $this->get_arr_values_full($frames,'dr')?>
                             },
                             {   "seriesName": "Количество лиц в кадре (fc)",
                                 "color": "f2c500",
                                 "renderAs": "area",
                                 "showValues": "0",
                                 "data": <? echo $this->get_arr_values_full($frames,'fc')?>
                             },
                             {   "seriesName": "tonometer_start",
                                 "color": "000000",
                                 "data": <? echo $this->get_arr_values_full($frames,'tonometer_start')?>
                             },
                             {   "seriesName": "alcotester_start",
                                 "color": "000000",
                                 "data": <? echo $this->get_arr_values_full($frames,'alcotester_start')?>
                             },
                             {   "seriesName": "alcotester_done",
                                 "color": "000000",
                                 "data": <? echo $this->get_arr_values_full($frames,'alcotester_done')?>
                             },
                             {   "seriesName": "alcotester_breath",
                                 "color": "000000",
                                 "data": <? echo $this->get_arr_values_full($frames,'alcotester_breath')?>
                             },
                             {   "seriesName": "alcotester_done",
                                 "color": "000000",
                                 "data": <? echo $this->get_arr_values_full($frames,'alcotester_done')?>
                             },
                             {   "seriesName": "alcotester",
                                 "color": "aaaaaa",
                                 "renderAs": "area",
                                 "showValues": "0",
                                 "data": <? echo $this->get_arr_values_full($frames,'a')?>
                             },
                             {   "seriesName": "tonometr",
                                 "color": "eeeeee",
                                 "renderAs": "area",
                                 "showValues": "0",
                                 "data": <? echo $this->get_arr_values_full($frames,'d')?>
                             },
                          ]
                     }
                 });

                 revenueChart2.render();
             });
               }) ;
                  </script>
                  <style type="text/css">#chart-container{margin:0 auto;width:100%;}</style>
                <?



         }

function get_category_values($arr)
{   $str='[' ;
    foreach($arr as $id=>$rec) $str.='{ "label" : "'.$id.'" },' ;
    $str.=']' ;
    return($str) ;
}

function get_category_values_shorm($arr)
{
//"category": "<?echo implode('|',array_keys($frames))"
}

function get_arr_values_full($arr,$fname,$label_fname='',$options=array())
{   $str='[' ;
    foreach($arr as $t=>$res) $str.='{ "value" : "'.$res[$fname].'" },' ;

    if ($label_fname) foreach($arr as $t=>$res)
    {   $value=($options['shift'])? ($res[$fname]+$options['shift']):$res[$fname] ;
        $str.='{ "label" : "'.$res[$label_fname].'","value" : "'.$value.'" },' ;
    }
    $str.=']' ;
    return($str) ;
}
function get_arr_values($arr,$fname)
{
    $arr2=[] ;
    if (_sizeof($arr)) foreach($arr as $id=>$rec) $arr2[]=$rec[$fname] ;
    $str=implode('|',$arr2) ;
    return($str) ;
}

}

?>
