<?php
include_once("c_setting_config.php");
class c_page_cab_setting_test_ws extends c_page_cab_setting_config
{
  public $h1='Проверка мониторинга' ;
  public $sign_uid="" ;

  function __construct($options=array())
  { parent::__construct($options);
    $this->sign_uid=ENGINE()->generate_UID() ;
  }


 function block_main()
 {   $groups=ACCOUNTS()->get_arr_groups_select_options() ;
     $this->page_title() ;
     FORM()->form_open() ;
     FORM()->row_open() ;
     FORM()->section_select(['name'=>'group_id','title'=>'Рабочая группа','col_count'=>4,'recs'=>$groups]);
     FORM()->row_close();
     FORM()->row_open() ;
     FORM()->button(['title'=>'Отправить предупреждение по алкоголю себе','cmd_method'=>'v2','cmd'=>'setting/test_send_ws_message_alco_alert','recipient'=>MEMBER()->id]);;
     FORM()->button(['title'=>'Отправить предупреждение по алкоголю фельдшеру','cmd_method'=>'v2','cmd'=>'setting/test_send_ws_message_alco_alert']);;
     FORM()->button(['title'=>'Закрыть предупреждение по алкоголю','cmd_method'=>'v2','cmd'=>'setting/test_send_ws_close_message_alco_alert']);;
     FORM()->button(['title'=>'Отправить на подпись пакет с МО','cmd_method'=>'v2','cmd'=>'setting/test_send_ws_mo_to_sign']);;
     FORM()->button(['title'=>'Отправить на подпись произвольный пакет','cmd_method'=>'v2','cmd'=>'setting/test_send_ws_data_to_sign']);;
     FORM()->row_close();
     FORM()->row_open() ;
     FORM()->section_textarea(['name'=>'ws_message','title'=>'Сообщение','col_count'=>6]);
     FORM()->row_close() ;
     FORM()->button(['title'=>'Отправить сообщение','cmd_method'=>'v2','cmd'=>'setting/test_send_ws_message']);;
     FORM()->form_close() ;



     ?><h3>Проверка обновления интерфейса через сокеты</h3><?
     FORM()->form_open() ;
     FORM()->row_open() ;
     FORM()->section_select(['name'=>'group_id','title'=>'Рабочая группа','col_count'=>4,'recs'=>$groups]);
     FORM()->row_close();
     FORM()->row_open() ;
     FORM()->section_input(['name'=>'div[update][test_001]','title'=>'div#test_001','col_count'=>1]);
     FORM()->section_input(['name'=>'div[update][test_002]','title'=>'div#test_002','col_count'=>1]);
     FORM()->section_input(['name'=>'div[update][test_003]','title'=>'div#test_003','col_count'=>1]);
     FORM()->section_textarea(['name'=>'div[append][test_004]','title'=>'div#test_004','col_count'=>6]);
     FORM()->row_close() ;
     FORM()->row_open() ;
     FORM()->section_open(['col_count'=>1]) ; ?><div id="test_001" style="border:1px solid green"></div><? FORM()->section_close() ;
     FORM()->section_open(['col_count'=>1]) ; ?><div id="test_002" style="border:1px solid green"></div><? FORM()->section_close() ;
     FORM()->section_open(['col_count'=>1]) ; ?><div id="test_003" style="border:1px solid green"></div><? FORM()->section_close() ;
     FORM()->section_open(['col_count'=>6]) ; ?><div id="test_004" style="border:1px solid green"></div><? FORM()->section_close() ;
     FORM()->row_close() ;
     FORM()->button(['title'=>'Проверить','cmd_method'=>'v2','cmd'=>'setting/test_send_ws_content']);;
     FORM()->form_close() ;
     ?>
     <div id="message_from_ws"></div>
     <script type="text/javascript">
         function connect_ws() {
             ws_protocol="<?if (_MAIN_PROTOCOL == 'https://') echo 'wss'; else echo 'ws';?>";
             let connect_url=ws_protocol + '://<?echo ($GLOBALS['LS_WS_address_port_JS'])?:_MAIN_DOMAIN?>/ws_connect?group_id=<?echo MEMBER()->cur_group_id?>';
             console.log('cab check WS connect_url:'+connect_url) ;
             ws = new WebSocket(connect_url);

             ws.onopen = function() {
                 console.log("Socket connect to group <?echo MEMBER()->cur_group_id?>");
             };

             ws.onmessage = function (evt) {
                 log_message(evt.data);
             };

             ws.onclose = function(evt) {
                 log_message('Socket is closed. Reconnect will be attempted in 1 second.' + '<br>' +  evt.reason);
                 setTimeout(function() {
                     connect_ws();
                 }, 1000);
             };

             ws.onerror = function(err) {
                 log_message('Socket encountered error: <br>' + err.message + '<br>Closing socket');
                 ws.close();
             };
         }

         $j('button#exec_mo_sign').click(function(){signMoAfterRequest('14E98336A37425B2CE12F3EE804D27326B537516',363925,0) ;}) ;

         function log_message(message) {
             var data=JSON.parse(message);
             if (data.host!==undefined && data.host!=location.hostname) return ;
             //console.log('data.host='+data.host);
             //console.log('location.hostname='+location.hostname);
             var data=JSON.parse(message);
             if (data.host!==undefined && data.host!=location.hostname && data.host!='localhost') return ;
             //console.log('data.host='+data.host);
             //console.log('location.hostname='+location.hostname);
             old_html = $j('#message_from_ws').html();
             date_now = new Date();
             date_str = leading_zeros(date_now.getHours()) + ":" + leading_zeros(date_now.getMinutes())+ ":" + leading_zeros(date_now.getSeconds());
             new_html = '<p>' + date_str + " - " +message + "</p><br>" + old_html;
             $j('#message_from_ws').html(new_html);
             clear_message();
         }

         function clear_message(max_count=10)
         {
             message_p = $j('#message_from_ws').children('p');
             // если больше 10 записей
             if (message_p.length > max_count) {
                 count = message_p.length - max_count;
                 for (i=1;i<=count;i++) {
                     //удаляем <p> и <br>
                     message_p[message_p.length-i].remove();
                     $j('#message_from_ws').children('br')[message_p.length-i].remove();
                 }
             }
         }

         function leading_zeros(str_val)
         {
             return (str_val < 10 ? '0' : '') + str_val;
         }

         connect_ws();
     </script>
     <style>
         #ws_message_text{width:100%;height:100px;font-size:16px;}
         #message_from_ws{background-color: lightgrey}
     </style>
     <?

 }

}



?>
