<?
include_once(_DIR_EXT.'/HTML/c_page_XML_AJAX.php') ;
//include_once('messages.php') ;
class c_page_ajax extends c_page_XML_AJAX
{

    function calculate_norm_auto()
    {
        if (_sizeof($_POST['personal_id']))
        {
            if (is_array($_POST['personal_id'])) {
                foreach ($_POST['personal_id'] as $personal_id) {
                    PP()->calculate_norm_auto($personal_id) ;
                }
            } else {
            PP()->calculate_norm_auto($_POST['personal_id']) ;
            }
            $this->add_element('update_page',1) ;
        } else {
            $this->add_element('show_notife','Пользователь не выбран') ;
        }
    }

  function calculate_norm()
  {  //if (_sizeof($_POST['check_popolom_id']))
     if (_sizeof($_POST['use_pp_norma']))
     { //$ids=implode(',',$_POST['check_popolom_id']) ;
       PP()->calculate_norm($_POST['use_pp_norma'],$_POST['personal_id']) ;
       $this->add_element('update_page',1) ;
     } else $this->add_element('show_notife','Не выбрана запись') ;
  }

  function set_norma_koof()
  { update_rec_in_table('obj_site_narko_data_out_303',array('koof_m'=>$_POST['koof_m'],'koof_p'=>$_POST['koof_p']),'user_id='.$_POST['personal_id'],array('debug'=>0)) ;
    $rec_norm=execSQL_van("select * from obj_site_narko_data_out_303 where user_id=".$_POST['personal_id']);
    PP()->calculate_norm($rec_norm['pp_id'],$_POST['personal_id']) ;
    //$this->add_element('show_modal_window','HTML') ;
      //$this->add_element('show_notife','Сохранено') ;
    $this->add_element('update_page',1) ;
  }

  function delete_norm()
  {
     execSQL_update('delete from obj_site_narko_data_out_303 where user_id='.$_POST['personal_id']) ;
     $this->add_element('update_page',1) ;

  }

  function delete()
  {  ob_start() ;
     ?><script type="text/javascript"><?
     if (_sizeof($_POST['check_popolom_id']))
     { $ids=implode(',',$_POST['check_popolom_id']) ;
       execSQL_update('delete from obj_site_narko_data where pkey in ('.$ids.')') ;
       foreach($_POST['check_popolom_id'] as $pp_id) {?>$j('tr#tr_<?echo $pp_id?>').remove();<?}
     }
     ?></script><?
     $this->add_element('JSCODE',ob_get_clean()) ;
     $this->add_element('graf_popilometr.HTML','') ;
  }

  function draw()
  { ob_start() ;
    if (_sizeof($_POST['check_popolom_id']))
    { $ids=implode(',',$_POST['check_popolom_id']) ;
      /*?><img src="/services/esmo_pp/graf_popilometr/?pp_id=<?echo $ids?>"><?*/
      ?><script type="text/javascript">
        var width = 1350;
        var newWin=window.open('<?echo _MAIN_PROTOCOL._MAIN_DOMAIN?>/cab/pp/?ids=<?echo $ids?>','PP_GRAF','resizable=1,scrollbars=1,fullscreen=0,height=1000,width=' + width + '  , left=' + screen.width + ', toolbar=0, menubar=0,status=1');
        //newWin.location = '' ;
        newWin.focus() ;
      </script><?
      $this->add_element('JSCODE',ob_get_clean()) ;
    }
    //$this->add_element('graf_popilometr.HTML',ob_get_clean()) ;
  }

  function draw_preview()
  { ob_start() ;
    if ($_POST['mo_id'])
    { $pp_id=execSQL_value('select pkey from obj_site_narko_data where id_mo='.$_POST['mo_id']) ;
      $panel_id='panel_PP_preview_'.$_POST['mo_id'] ;
    }
    else
    {  $pp_id=$_POST['pp_id'] ;
       $panel_id='panel_PP_preview_'.$_POST['pp_id'] ;
    }
    if ($pp_id) {
      include_once('panel_pp_fusion_charts.php');
      ?>
      <div class="center">
          <?
          panel_pp_fusion_charts($pp_id,'l',['width'=>'420','height'=>'300','parent_panel_id'=>$panel_id]);
          panel_pp_fusion_charts($pp_id,'r',['width'=>'420','height'=>'300','parent_panel_id'=>$panel_id]);
          ?>
      </div>
      <style type="text/css">
          div.dryPPEye {display: inline}
      </style>
      <?
    }
    $mo_id=$_POST['mo_id'] ;
    //$mo_id=11 ;
    $video_fname='pp_video_'.$mo_id.'.mp4' ;
    if (file_exists(_TEMP_DIR.'/'.$video_fname))
    {   $video_url=_PATH_TO_SITE.'/temp/'.$video_fname.'?rand='.rand(1,1000) ;
        //echo      $video_url ;
        $options=array() ;
        $video_height=($options['video_height'])? $options['video_height']:300 ;
        ?><video id="PP_video_player" controls width="100%" autoplay loop height="<?echo $video_height?>" <?if ($options['video_rotare']) echo 'style="transform:rotate('.$options['video_rotare'].'deg);"'?>>
             <source src="<?echo $video_url?>">
           </video>
           <script type="text/javascript">
               var video;

               //window.onload = function()
               //{
               	  video = document.getElementById("videoPlayer");
                   video.currentTime = 0;
                  video.play();
               //};
           </script>
        <?
    }
    $this->add_element($panel_id.'.HTML',ob_get_clean()) ;
  }

  function save_comment()
  { //damp_array($_POST,1,-1) ;
    update_rec_in_table('obj_site_narko_data',array('comment'=>$_POST['value']),'pkey='.$_POST['pp_id'],array('debug'=>0)) ;
    $this->add_element('show_notife','Сохранено') ;
    //$this->add_element('show_modal_window','HTML') ;
  }

    function save_dn_result()
    { //damp_array($_POST,1,-1) ;
        $value=floatval(str_replace(',','.',$_POST['value']));
        update_rec_in_table('obj_site_narko_data',array('dn_result'=>$value),'pkey='.$_POST['pp_id'],array('debug'=>0)) ;
        $this->add_element('show_notife','Сохранено') ;
        //$this->add_element('show_modal_window','HTML') ;
    }

    function save_tl_result()
    { //damp_array($_POST,1,-1) ;
        $value=floatval(str_replace(',','.',$_POST['value']));
        update_rec_in_table('obj_site_narko_data',array('tl_result'=>$value),'pkey='.$_POST['pp_id'],array('debug'=>0)) ;
        $this->add_element('show_notife','Сохранено') ;
        //$this->add_element('show_modal_window','HTML') ;
    }


 function search_personal()
 {  //$this->add_element('show_modal_window','HTML') ;
    //damp_array($_POST,1,-1) ;
    if ($_POST['search_text'])
    { $members_ids=PERSONAL()->search_personal_ids($_POST['search_text']) ;
      if (!_sizeof($members_ids)) $this->add_element('show_notife','Ничего не найдено',array('type'=>'info')) ;
    }
    //elseif (_sizeof($members_ids)==1) $this->add_element('go_url','/cab/personal/'.$members_ids[0].'.html') ;
    //else
    { ob_start() ;
      $_usl=array('clss=300') ;
      if ($members_ids) $_usl[]='personal_id in ('.implode(',',$members_ids).')' ;
      $usl=implode(' and ',$_usl) ;
      $recs_personal=execSQL_row('select personal_id,count(pkey) from obj_site_narko_data where '.$usl.' group by personal_id order by c_data desc') ;
      //if ($_SESSION['list_members_filter_park']) $_usl[]='parent='.$_SESSION['list_members_filter_park'] ;

      //PERSONAL()->show_list_items($usl,'aspmo_personal/list_personal_table_img',array('debug'=>0,'panel_select_pages'=>1,'order'=>$_SESSION['list_members_sort_mode'],'pages_base_url'=>'/cab/personal/')) ;
      include_once('panel_list_rec_popilumetr.php') ;
      panel_list_rec_popilumetr($recs_personal) ;
      $this->add_element('panel_list_recs.HTML',ob_get_clean()) ;
      $this->add_element('show_notife','Найдено '._sizeof($recs_personal).' сотрудников',array('type'=>'info')) ;
    }
 }

 function pp_journal_apply_filter()
 { $options['text_info']=$_POST['name'] ;
   $options['time_from']=$_POST['time_from'] ;
   $options['time_to']=$_POST['time_to'] ;
   $options['terminal_id']=$_POST['terminal'] ;
   $options['result']=$_POST['result'] ;
   include_once('panel_pp_journal.php') ;
   panel_pp_journal($options) ;
   $this->add_element('panel_filter_result.HTML',ob_get_clean()) ;
   //$this->add_element('show_modal_window','HTML') ;
 }

 function change_graph()
 {
     damp_array($_POST,1,-1);
     if ($_POST['value']) $filter['source_graph']=1;
     $url=REPORT()->prepare_url($filter,array('base_url'=>$_POST['filter_dir'])) ;
     $this->go_url=$url ;
 }


 function disregart_pp_result(){
     ?>
     <form class="sky-form no_box_shadow">
         <table>
             <tr>
                 <td><strong>Не учитывать результаты раньше чем:</strong></td>
                 <td width="120px"><input type="text" name="disregart_pp_before" class="datepicker" data-required value="<?if ($GLOBALS['LS_pp_disregartResultBefore']) echo date('d.m.Y',$GLOBALS['LS_pp_disregartResultBefore'])?>"></td>
             </tr>
             <tr>
                 <td colspan="2">Будут сброшены все нормы пупиллометрии сотрудников и перестанут учитываться все результаты до выбранного числа</td>
             </tr>
         </table>

        <input class="button v2" type="submit" cmd="aspmo_pp/disregart_pp_result_apply" value="Применить">
     </form>
     <style >

     </style>
     <script type="text/javascript">
         $j('input.datepicker').datepicker($j.datepicker.regional["ru"]);
     </script>
     <?
      $this->add_element('show_modal_window','HTML',array('title'=>'Не учитывать устаревшие результаты пупилометрии'));
 }

 function disregart_pp_result_apply() {
    if ($_POST['disregart_pp_before']) {
        $disregart_pp_before=strtotime($_POST['disregart_pp_before']);
    } else {
        $disregart_pp_before=0;
    }

    if (is_numeric($disregart_pp_before)) {
        update_site_setting('LS_pp_disregartResultBefore',$disregart_pp_before) ;
        if ($GLOBALS['LS_pp_disregartResultBefore']==$disregart_pp_before) {
            execSQL_update('update obj_site_narko_data set enabled=1');
            if ($GLOBALS['LS_pp_disregartResultBefore']>0) {
                $cnt_disable_pp=execSQL_update('update obj_site_narko_data set enabled=0 where c_data<'.$GLOBALS['LS_pp_disregartResultBefore']);
                $cnt_del_norm=execSQL_update('delete from obj_site_narko_data_out_303');
                $this->add_element('show_notife','Отключено измерений ПП: '.$cnt_disable_pp,['color'=>'green']);
                $this->add_element('show_notife','Удалено норм ПП: '.$cnt_del_norm,['color'=>'green']);
            }
        }
    }
    $this->update_page=1;
 }

}
