<?php
include_once("c_term.php");
class c_page_term_client extends c_page
{
 public $h1='Эмулятор терминала  клиента' ;
 public $terminal_id=0 ;
 //public $page_type='window' ;
 public $free_access=1 ;
 public $client_id=0 ;
 public $rec_client=array() ;
 public $client_personal_sections=array() ;
 public $client_personal_sections_ids='' ;
 public $rec_terminal=array() ;

 function set_head_tags()
 {  parent::set_head_tags() ;
    $this->HEAD['css'][]='/'._CLASS_.'/assets/mForm.css' ;
 }

 function menu_config()
 {  $this->menu['/term/client/'.$this->client_id.'/'.$this->terminal_id.'/']=array('title'=>'МО','class'=>'fa fa-user-md') ;
 }



 function select_obj_info()
 {
   $arr=explode('/',_CUR_PAGE_DIR) ;
   $this->terminal_id=$arr[4] ;
   if (isset($arr[3]))
   { $this->rec_client=ACCOUNTS()->get_member_by_id($arr[3]) ;
     if (!$this->rec_client['enabled'] or $this->rec_client['rol']!=6) $this->result=404 ;
     else
     { $this->client_id=$this->rec_client['pkey'] ;
       $this->client_personal_sections=array() ;
       $orgs_ids=execSQL_line('select pkey from obj_site_personals where clss=207 and client_id='.$this->client_id) ;
       $otdel_ids=execSQL_line('select pkey from obj_site_personals where clss=208 and parent in ('.implode(',',$orgs_ids).')') ;
       $this->client_personal_sections=array_merge($orgs_ids,$otdel_ids) ;
       if (_sizeof($this->client_personal_sections)) $this->client_personal_sections_ids=implode(',',$this->client_personal_sections) ;
     }
   }
   if (isset($arr[4]))
   { $this->rec_terminal=ESMO()->get_terminal_by_id($arr[4]) ;
     if (!$this->rec_terminal['enabled']) $this->result=404 ;
     else $this->terminal_id=$this->rec_terminal['id'] ;
   }

   $this->title='VTERM '.$this->terminal_id ;

 }

 function check_access() {return(1);}
 function member_menu_check(&$menu) {}

  function panel_main_menu_title()
  { ?><li aria-haspopup="true"><a>Клиент <?echo '<strong>'.$this->rec_client['obj_name'].'</strong><br>'.'Терминал № <strong>'.$this->rec_terminal['id'].'</strong>'?></a></li><?}

  function block_main()
  {   $status=($this->rec_terminal['virt_status'])? ' [auto]':'' ;
      $this->page_title('Эмулятор терминала № '.$this->terminal_id.$status.' для клиента '.$this->rec_client['obj_name']) ;

      //damp_array($this->rec_client,1,-1) ;
      if ($this->rec_terminal['pkey'])
      {  //update_rec_in_table('obj_site_account',array('status'=>2),'pkey='.$rec['pkey']) ;
         //if (!$this->rec_terminal['virt_status'])
         { ?><button class="v1" update_page=1 id="reset">Reset</button>&nbsp;&nbsp;
             <button class="v2" cmd="aspmo_term/send_power_on" terminal_id=<?echo $this->terminal_id?>>Power ON</button>&nbsp;&nbsp;<?
           if ($this->rec_terminal['virt_status']==0){?><button class="v2" cmd="aspmo_term/start_auto" terminal_id=<?echo $this->terminal_id?> client_id=<?echo $this->client_id?>>Start Auto</button>&nbsp;&nbsp;<?}
           if ($this->rec_terminal['virt_status']==1){?><button class="v2" cmd="aspmo_term/stop_auto" terminal_id=<?echo $this->terminal_id?> client_id=<?echo $this->client_id?>>Stop Auto</button>&nbsp;&nbsp;<?}
           if ($this->rec_terminal['virt_status']==2){?><button class="v2" cmd="aspmo_term/stop_auto" terminal_id=<?echo $this->terminal_id?> client_id=<?echo $this->client_id?>>Stop Auto</button>&nbsp;&nbsp;<?}
           ?><button class="v2" cmd="aspmo_term/new_terminal_window" terminal_id=<?echo $this->terminal_id?> client_id=<?echo $this->client_id?>>New Terminal</button>&nbsp;&nbsp;
             <br>
             <br>
             <div id="panel_message">
                  <h2>Имитация проведения осмотра</h2>
                  <div><form><input type="text" placeholder="Номер пропуска" name="user" data-required> <input id=submit type="submit" class="v2" cmd="aspmo_term/send_propusk_info" validate="form" terminal_id=<?echo $this->terminal_id?>  client_id=<?echo $this->client_id?> value="Приложить пропуск к считывателю"></form></div>
             </div>
           <?
         }
         if ($this->rec_terminal['virt_status'])
         { // получаем случайный пропуск, из тех что сейчас не проходят МО, вписываем его в input, имитируем нажатие клавиши
           $_usl=array() ;
           $_usl[]='clss=211' ;
           $_usl[]='enabled=1' ;
           $_usl[]='not pkey in(select member_id from obj_site_mo_session where status=1)' ;
           $_usl[]='parent in ('.$this->client_personal_sections_ids.')' ;
           $usl=implode(' and ',$_usl) ;

           $propusk=execSQL_value('select propusk from obj_site_personals where '.$usl.' order by rand() limit 1') ;
           ?><script type="text/javascript">$j(document).ready(function()
           { $j('input[name="user"]').val('<?echo $propusk?>') ;
             $j('body').oneTime(3000,'timer_submit',function(){$j('input#submit').trigger('click') ;})
           }) ;
           </script><?
         }
      }
      else
      { ?><div id="panel_select_group"><form>
          <h2>Терминал не зарегистрирован в системе. Выберите рабочую группу для регистрации терминала.</h2><?
          $groups=ACCOUNTS()->get_list_workgoups() ;
          ?><strong>Рабочая группа: </strong><select name="use_group" data-select><option></option><?if (_sizeof($groups)) foreach($groups as $rec){?><option value="<?echo $rec['pkey']?>"><?echo $rec['obj_name']?></option><?}?></select> <input type=submit class="v2" cmd="aspmo_term/register_vterm" validate=form terminal_id="<?echo $this->terminal_id?>" value="Зарегистрировать">
          </form></div><div id=panel_message></div><?
      }

      if (!$this->rec_terminal['virt_status']) // если статус = 2 и не авто режим - обновлям каждые 1 мин
       { ?><script type="text/javascript">$j(document).ready(function()
         { //$j('body').everyTime(60000,'check_status',function(){send_ajax_request({cmd:'aspmo_term/check_status_and_update',terminal_id:<?echo $this->terminal_id?>,client_id:<?echo $this->client_id?>})})
         }) ;
         </script><?
       }

      $this->panel_debug() ;
  }

  function panel_debug() {?><div id="panel_debug" class="hidden"></div><?}


}



?>
