<?php
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_services_quiz_content extends c_page_TXT
{
  public $content_view_id=0 ;
  public $is_vurtual_term=0 ;
  public function __construct()
  {
     $this->content_view_id=_CUR_ID ;
  }

  function show()
  { ob_start() ;
    // данные
    $rec_content_view=execSQL_van('select * from media_content_view where pkey='.$this->content_view_id) ;
    if ($rec_content_view['pkey'])
    {   // определяем откуда пришел запрос на контент - если виртуальный терминал - то видеo надо запускать сразу
        $arr_refferer=parse_url($_SERVER['HTTP_REFERER']) ;
        $this->is_vurtual_term=($arr_refferer['host']==_MAIN_DOMAIN)? 1:0 ;

        LOG_JSON('event_pass')->reg('get_media_content','Получен запрос на медиаконтент ID '.$this->content_view_id);

        ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
            <head>
                <title>ЭСМО</title>
                <SCRIPT type="text/javascript" src="<? echo _PATH_TO_SITE ?>/<? echo _CLASS_ ?>/ext/jquery/jquery-3.4.1.min.js"></SCRIPT>
                <SCRIPT type="text/javascript" src="<? echo _PATH_TO_SITE ?>/<? echo _CLASS_ ?>/ext/jquery/jquery.timers-1.2.js"></SCRIPT>
                <script type="text/javascript">let server_url = "<?echo _PATH_TO_SITE?>";</script>
                <style type="text/css">
                      div.current_time{text-align:center;font-size:18px;color:black;}
                      div.content{text-align:center;font-size:18px;color:black;}
                      table.table_text{width:100%;height:100%;}
                      table.table_text td{text-align:center;vertical-align:center;font-size:22px;color:#272727;}
                      table.table_image{width:100%;height:100%;}
                      table.table_image td{text-align:center;vertical-align:center;font-size:22px;color:#272727;}
                      table.table_image td img{max-width:100%;max-height:1000px;}
                </style>
            </head>
            <body><div class="content">
        <?

        $rec_content=execSQL_van('select * from media_content where pkey='.$rec_content_view['content_id']) ;
        if ($rec_content['type']=='text'){$this->show_text($rec_content,$rec_content_view);}
        if (strpos($rec_content['type'],'image')!==false){$this->show_image($rec_content,$rec_content_view);}
        if (strpos($rec_content['type'],'video')!==false)  $this->player_code_avi($rec_content['file_name'],array('pass_id'=>$rec_content_view['pass_id'],'content_id'=>$rec_content_view['content_id'],'content_view_id'=>$this->content_view_id)) ;
        //echo $rec_content['file_name'] ;
        ?></div></body>
          <script type="text/javascript" src="<?echo _PATH_TO_SITE?>/<?echo _CLASS_?>/ext/events_media/terminal-ipc.js?rand=<?echo rand(1,1000000)?>"></script>

          <script type="text/javascript">
              // Сообщение терминалу что страница готова.
              $j(document).ready(function()
              { sendAppEvent('media-content-ready') ;
              }) ;
          </script>

         </html>

        <?

        LOG_JSON('event_pass')->reg('get_media_content','Сформирован медиаконтент <strong>'.$rec_content['type'].'</strong>: <strong>'.$rec_content['file_name'].'</strong> <strong>'.$rec_content['manual'].'</strong><br>');
        LOG_JSON('event_pass')->flush('event_pass_'.$rec_content_view['pass_id']);


    }
    $text=ob_get_clean() ;
    echo $text ;
    LOGS()->reg_log('$rec_content_view',$text) ;
  }

  function show_text($rec_content,$rec_content_view)
  {  if (!$rec_content_view['length']) $rec_content_view['length']=10 ;
      ?><table class="table_text"><tr><td style="font-size:24px;"><?echo $rec_content['manual'] ;?></td></tr></table><?
      $this->show_JS_timer_mediacontenthidden($rec_content_view) ;
  }

  function show_image($rec_content,$rec_content_view)
  { if (!$rec_content_view['length']) $rec_content_view['length']=10 ;
    ?><table class="table_image"><tr><td><img src="<?echo _PATH_TO_SITE.''.$rec_content['file_name']?>"></td></tr></table><?
    $this->show_JS_timer_mediacontenthidden($rec_content_view) ;
  }

  function show_JS_timer_mediacontenthidden($rec_content_view)
  { ?><script type="text/javascript">
      $j(document).ready(function()
      {
          $j('body').oneTime('<?echo $rec_content_view['length']?>s','mediacontenthidden',function()
             {   console.log('image show ended') ;
                 var data={ event:'media-content-hidden',
                            payload:{ pass_id:<?echo $rec_content_view['pass_id']?>,
                                      media_id:<?echo $rec_content_view['content_id']?>,
                                      content_view_id:<?echo $rec_content_view['pkey']?>,
                                    }
                         } ;
                 var server_url="<?echo _PATH_TO_SITE?>" ;
                 $j.ajax({url:server_url+'/services/events_media/reg_event/',type:'POST',dataType:'xml',cache:false,data:data});
                 //sendAppEvent('media-content-done')
             })
      }) ;
      </script>
      <?
  }

   function player_code_avi($video_url,$options=array())
   {  $_style=array() ;
      $_style[]='max-width:100%' ;
      $_style[]='max-height:100%' ;
      if ($options['video_rotare']) $_style[]='transform:rotate('.$options['video_rotare'].'deg)' ;

      ?><video id=terminal_media style="<?echo implode(';',$_style)?>">
         <source src="<?echo $video_url?>">
       </video>
       <script type="text/javascript">
          var videoEl = document.getElementsByTagName('video')[0] ;
          function start()  { videoEl.play(); }
          function stop()   { videoEl.pause(); }
          // обработываем событие окончания показа видео
          videoEl.addEventListener("ended", function()
          { console.log('video ended') ;
            sendAppEvent('media-content-done')
            $j.ajax({url:server_url+'/services/events_media/reg_event/',type:'POST',dataType:'xml',cache:false,
                      data:{ event:'media-content-hidden',
                             payload:{pass_id:<?echo $options['pass_id']?>,
                                      media_id:<?echo $options['content_id']?>,
                                      content_view_id:<?echo $options['content_view_id']?>,
                                     }
                           }
                    });
          }, true);
          //v.currentTime = 10.0;
         <?if ($this->is_vurtual_term) {?>start() ;<?}?>
       </script>
       <style type="text/css">
           video#terminal_media {
           position: fixed; right: 0; bottom: 0;
           min-width: 100%; min-height: 100%;
           width: auto; height: auto; z-index: -100;
           }
       </style>

      <?
   }





}
