<?php
include_once("c_personal_person_card.php");
class c_page_cab_personal_person_graf extends c_page_cab_personal_person_card
{
  public $h1='Карточка сотрудника - графики' ;
  public $use_fusioncharts=1 ;

 function get_cur_page_meta_tags()
 {
   if ($this->rec_personal['pkey']) $this->h1='Графики '.$this->rec_personal['obj_name'] ;
 }

 function block_main()
 { $this->page_title() ;
   $this->panel_filter() ;
   $this->panel_personal_graf_pressure2() ;
 }

 function panel_filter()
 { ?><div id="panel_filter">
           <form id="sky-form" class="sky-form"><input type="hidden" name="filter_dir" value="/cab/personal/person/<?echo $this->rec_personal['pkey']?>/graf/">
                <fieldset>
                    <div class="row">
                        <section class="col col-3">
                            <label class="radio"><input type="radio" name="filter[group]" value="no" <?if ($this->filter['group']=='no' or !$this->filter['group']) echo 'checked'?>><i></i>Без группировки</label>
                            <label class="radio"><input type="radio" name="filter[group]" value="days" <?if ($this->filter['group']=='days') echo 'checked'?>><i></i>По дням</label>
                            <label class="radio"><input type="radio" name="filter[group]" value="week" <?if ($this->filter['group']=='week') echo 'checked'?>><i></i>По неделям</label>
                            <label class="radio"><input type="radio" name="filter[group]" value="month" <?if ($this->filter['group']=='month') echo 'checked'?>><i></i>По месяцам</label>
                        </section>
                        <section class="col col-3">
                            <label class="radio"><input type="radio" name="filter[type]" value="sd"  <?if ($this->filter['type']=='sd' or !$this->filter['type']) echo 'checked'?>><i></i>Давление</label>
                            <label class="radio"><input type="radio" name="filter[type]" value="pr" <?if ($this->filter['type']=='pr') echo 'checked'?>><i></i>Пульс, разница давлений</label>
                            <label class="radio"><input type="radio" name="filter[type]" value="ball" <?if ($this->filter['type']=='ball') echo 'checked'?> ><i></i>Баллы</label>
                        </section>

                    </div>
                </fieldset>
                <input type="submit" class="v2" cmd="set_filter" value="Обновить данные">
            </form>
     </div><br>
   <?
 }


 function panel_personal_graf_pressure2()
 {
   if ($this->filter['type']=='ball')
   { $this->set_ball_pressure() ; // обновляем баллы
     $recs=execSQL('select pkey as id,c_data,systolic,diastolic,pulse,ball_s,ball_d,status,hand_check_user_id from obj_site_mo_session where systolic>0 and diastolic>0 and status in (2) and member_id='.$this->rec_personal['pkey'].' order by c_data') ;
   }
   else $recs=execSQL('select pkey as id,c_data,systolic,diastolic,pulse,ball_s,ball_d,status,hand_check_user_id from obj_site_mo_session where systolic>0 and diastolic>0 and member_id='.$this->rec_personal['pkey'].' order by c_data') ;
   $arr_category=array() ; $arr_data_s=array() ; $arr_data_d=array() ; $arr_data_r=array() ;  $pre_res_s=array() ; $pre_res_d=array() ;  $pre_res_r=array() ;
   $pre_res=array() ;
   if (!$this->filter['type']) $this->filter['type']='sd' ;
   $caption='' ;  $yAxisName='' ;

   if (_sizeof($recs)) foreach($recs as $rec)
   {   switch($this->filter['group'])
       { case 'month': $x=get_month_year($rec['c_data'],'month year') ; $x_text=$x ;break ;
         case 'week':  $x=date('W',$rec['c_data']) ;
                       $now=getdate($rec['c_data']) ;
                       $start_week=$rec['c_data']-(($now['wday'])? ($now['wday']-1)*86400:7*86400) ;// начало недели
                       $x_text=date('d.m.Y',$start_week) ;
                       break ;
         case 'day':   $x=date('d.m.y',$rec['c_data']) ;                $x_text=$x ; break ;
         default:      $x=date('d.m.y H:i',$rec['c_data']) ;            $x_text=$x ;
       }

       $arr_category[$x]=$x_text ;

       switch($this->filter['type'])
       { case 'sd':   $pre_res['Систолическое давление'][$x][]=$rec['systolic'] ;
                      $pre_res['Диастолическое давление'][$x][]=$rec['diastolic'] ;
                      $caption='Графики давления' ;
                      $yAxisName='Давление'  ;
                      break ;
         case 'pr':   $pre_res['Пульс'][$x][]=$rec['pulse'] ;
                      $pre_res['Разница давлений'][$x][]=$rec['systolic']-$rec['diastolic'] ;
                      $caption='Графики пульса и разницы давлений' ;
                      $yAxisName='Пульс, разница давлений'  ;
                      break ;
         case 'ball': $pre_res['Баллы за СД'][$x][]=$rec['ball_s'] ;
                      $pre_res['Баллы за ДД'][$x][]=$rec['ball_d'] ;
                      $caption='График здоровья' ;
                      $yAxisName='Баллы'  ;
                      break ;
       }
   }

   //damp_array($arr_category) ;
   //damp_array($pre_res) ;

   //return ;

   if (_sizeof($arr_category)) foreach($arr_category as $category_name=>$category_title)
   { $arr_category_str[]='{"label": "'.$category_title.'" }' ;
     if (_sizeof($pre_res)) foreach($pre_res as $line_name=>$line_sets)
     {
      if (_sizeof($line_sets[$category_name])>1)
             $res[$line_name][$category_name]=round(array_sum($line_sets[$category_name])/_sizeof($line_sets[$category_name]),2) ;
      else   $res[$line_name][$category_name]=$line_sets[$category_name][0] ;

      $arr_data[$line_name][]='{"value": "'.$res[$line_name][$category_name].'" }' ;
     }
   }

   //damp_array($arr_category_str) ;
   //damp_array($arr_data) ;
   //damp_array($res_d) ;
   //return ;

   ?><div id="chartContainer">FusionCharts XT will load here!</div>
     <script type="text/javascript">
     $j(document).ready(function(){
       FusionCharts.ready(function(){
         var revenueChart = new FusionCharts({
             type: 'zoomline',
             "renderAt": "chartContainer",
             "width": "1100",
             "height": "500",
             "dataFormat": "json",
             "dataSource":
             { "chart":
               {
                   "caption": "<?echo $caption?>",
                   //"subCaption": "(FY 2012 to FY 2013)",
                   "xAxisName": "Дата",
                   "yAxisName": "<?echo $yAxisName?>",
                   "showValues": "0",
                   "numberPrefix": "",
                   "showBorder": "0",
                   "showShadow": "0",
                   "bgColor": "#ffffff",
                   "paletteColors": "#0075c2,#1aaf5d,#a20606",
                   "showCanvasBorder": "0",
                   "showAxisLines": "1",
                   "showAlternateHGridColor": "0",
                   "divlineAlpha": "100",
                   "divlineThickness": "1",
                   "divLineIsDashed": "1",
                   "divLineDashLen": "1",
                   "divLineGapLen": "1",
                   "lineThickness": "1",
                   "flatScrollBars": "1",
                   "scrollheight": "10",
                   "numVisiblePlot": "20",
                   "showHoverEffect":"1"
               },
               "categories": [ {
                                  "category": [<?echo implode(',',$arr_category_str)?>]
                               }
                             ],
               "dataset":    [
                               <? if (_sizeof($arr_data)) foreach($arr_data as $data_name=>$data_arr)
                                   { ?> {  "seriesname": "<?echo $data_name?>",
                                           "data": [<?echo implode(',',$data_arr)?>]
                                        },
                                     <?
                                   }
                               ?>
                             ],
                 /*"trendlines": [
                                 {
                                     "line": [
                                         {
                                             "parentyaxis": "P",
                                             "startvalue": "85",
                                             "displayvalue": "Пульс",
                                             "thickness": "1",
                                             "color": "#0075c2",
                                             "dashed": "1"
                                         },
                                         {
                                             "parentyaxis": "P",
                                             "startvalue": "35",
                                             "displayvalue": "Разница",
                                             "thickness": "1",
                                             "color": "#1aaf5d",
                                             "dashed": "1"
                                         }
                                     ]
                                 }
                             ,
                         {
                             "line": [
                                 {
                                     "isTrendZone": "1",
                                     "startvalue": "30",
                                     "endValue": "40",
                                     "color": "#1aaf5d",
                                     "valueOnRight": "1",
                                     "alpha": "30",
                                     "tooltext": "Нормальное значение разницы давлений",
                                     "displayvalue": "Разница"
                                 }
                             ]
                         },
                         {
                             "line": [
                                 {
                                     "isTrendZone": "1",
                                     "startvalue": "80",
                                     "endValue": "90",
                                     "color": "#00ef8d",
                                     "valueOnRight": "1",
                                     "alpha": "30",
                                     "tooltext": "Нормальное значение пульса",
                                     "displayvalue": "Пульс"
                                 }
                             ]
                         }
                     ]   */

           }

       });
     revenueChart.render();
     })
      }) ;
     </script>
   <?




   //print_2x_arr($recs) ;


 }

function set_ball_pressure()
    {
        //$recs=execSQL('select model,count(pkey) from obj_site_mo_session group by model order by model',2) ;
        $recs=execSQL(//'select * from obj_site_mo_session'.
                      'select pkey,systolic,diastolic,model,ball_s,ball_d from obj_site_mo_session'.
                      ' where member_id='.$this->rec_personal['pkey'].' and
                              status in (2) and
                              systolic!=-1 and
                              diastolic!=-1
                              /*and (hand_check_user_id=0 or hand_check_user_id is null)*/
                              and (ball_s=0 or ball_s is null or ball_d=0 or ball_d is null)
                        order by pkey') ;


        //  критерии по S - _IL('IL_pressure_s')->recs
        //  критерии по D - _IL('IL_pressure_d')->recs
        if (_sizeof($recs))
        {  ?><table class="basic fz_small"><tr><th>systolic</th><th>Диапазон</th><th>Баллы</th><th>diastolic</th><th>Диапазон</th><th>Баллы</th></tr><?
            foreach($recs as $rec)
            {   $ball_s=0 ; $ball_d=0 ;
                ?><tr><?
                foreach(_IL('IL_pressure_s')->recs as $krit)
                { if (($krit['v1'] and $krit['v2'] and $rec['systolic']>=$krit['v1'] and $rec['systolic']<=$krit['v2']) or
                      (!$krit['v1'] and $krit['v2'] and $rec['systolic']<=$krit['v2']) or
                      ($krit['v1'] and !$krit['v2'] and $rec['systolic']>=$krit['v1'])
                     )
                  { $ball_s=$krit['ball'] ;
                    ?><td><?echo $rec['systolic']?></td><td><?echo $krit['v1'].'...'.$krit['v2']?></td><td><? echo $krit['ball']?></td><?
                  }

                }
                foreach(_IL('IL_pressure_d')->recs as $krit)
                { if (($krit['v1'] and $krit['v2'] and $rec['diastolic']>=$krit['v1'] and $rec['diastolic']<=$krit['v2']) or
                      (!$krit['v1'] and $krit['v2'] and $rec['diastolic']<=$krit['v2']) or
                      ($krit['v1'] and !$krit['v2'] and $rec['diastolic']>=$krit['v1'])
                     )
                  { $ball_d=$krit['ball'] ;
                    ?><td><?echo $rec['diastolic']?></td><td><?echo $krit['v1'].'...'.$krit['v2']?></td><td><? echo $krit['ball']?></td><?
                  }
                }
                update_rec_in_table('obj_site_mo_session',array('ball_s'=>$ball_s,'ball_d'=>$ball_d),'pkey='.$rec['pkey']) ;
                ?></tr><?

            }
        }
    }

}



?>
