<?
class i_poll_questions_panel
{
    private $recs_questions ;
    private $recs_aswers ;
    private $recs_aswers_by_questions ;
    private $recs_contents ;
    private $recs_questions_by_parent ;
    private $recs_contents_by_questions ;

	function __construct($question_ids)
	{
      // получаем одним запросом основные и дополнииельные вопросы
      $this->recs_questions=execSQL('select * from view_poll_questions where pkey in ('.implode(',',$question_ids).')',0) ;
      //print_2x_arr($this->recs_questions);

      $this->recs_aswers=POLL()->get_answers_by_question_id(implode(',',$question_ids)) ;
      $this->recs_aswers_by_questions=group_by_field('question_id',$this->recs_aswers) ;

      $this->recs_contents=POLL()->get_contents_by_question_id(implode(',',$question_ids)) ;

      $this->recs_questions_by_parent=group_by_field('parent',$this->recs_questions) ;
      $this->recs_contents_by_questions=group_by_field('question_id',$this->recs_contents) ;

      // print_2x_arr($this->recs_questions_by_parent);

	}

	function panel_questions($options=[])
    {  $i=0 ;
       if(_sizeof($this->recs_questions)) foreach($this->recs_questions as $rec_question)
       {  $this->panel_question($rec_question,$i,$options);
          $i++ ;
       }
	}


	function panel_question($rec_question,$i,$options=[])
	{
		$content_exists=(isset($this->recs_contents_by_questions[$rec_question['pkey']][0]))? 1:0 ;
		$_class=[] ; $_attr=[] ;
		$_class[]='tiket' ;
		if (!$i) $_class[]='current' ;
		if ($i or $options['hide']) $_class[]='hidden' ; // на  second включена всегда
		if (!_sizeof($this->recs_aswers_by_questions[$rec_question['pkey']])) $_class[]='no_answers' ; // class no_answers для размещения по центру
		$_attr['class']=$_class ;
		$_attr['id']='tiket_'.$rec_question['level'].'_'.($i+1) ;
		if (($i+1)<_sizeof($this->recs_questions)) $_attr['next_tiket']='tiket_'.$rec_question['level'].'_'.($i+2) ;
		//else $_attr['next_tiket']=$_POST['next_tiket'] ;  // только для second


		?><div <?echo ENGINE()->echo_attr($_attr)?>>
					<div class="question"><h1 class="display-4" style="font-size:2em;"><? echo $rec_question['obj_name'] ?></h1></div>
					<!-- было только на первичных вопросах -->
					<?/*<div class="cnt_info"><?echo $i+1?> из <?echo _sizeof($question_ids)?></div>*/?>
					<? if ($rec_question['answer_type']==1) $this->panel_answer_by_ball($rec_question['pkey']) ;
					   else {?><table class="inner">
                                    <tr>
                                            <?if ($content_exists){?><td class="col col_content"><?$this->panel_content($this->recs_contents_by_questions[$rec_question['pkey']][0])?></td><?}?>
                                            <td class="col col_question"><?$this->panel_answers($this->recs_aswers_by_questions[$rec_question['pkey']])?></td>
                                    </tr>
                                 </table><?
                               }
		 ?></div><?
	}

   function panel_answer_by_ball($question_id)
   {
       ?><div id="panel_answer_by_ball"><input type="hidden" name="poll_balls[<? echo $question_id ?>]" value="0">
          <div class="items">
             <div class="item size_1" ball="1"></div>
             <div class="item size_2" ball="2"></div>
             <div class="item size_3" ball="3"></div>
             <div class="item size_4" ball="4"></div>
             <div class="item size_5" ball="5"></div>
          </div>
         </div>
         <script type="text/javascript">
             $j('div.items div').each(function(){
             	$j(this).addClass('selected2')  ;
                 $j('body').oneTime('2s', function() {$j(this).removeClass('selected2')  ;}) ;
             });
         </script>
       <?
   }

   function panel_answer_by_yes_no($question_id)
   {   ?><div id="panel_answer_by_ball"><input type="hidden" name="poll_balls[<? echo $question_id ?>]" value="0">
          <div class="items">
             <div class="item size_1" ball="1"></div>
             <div class="item size_2" ball="2"></div>
             <div class="item size_3" ball="3"></div>
             <div class="item size_4" ball="4"></div>
             <div class="item size_5" ball="5"></div>
          </div>
         </div>
       <?
   }

	function panel_answers($recs_aswers)
    {//  print_2x_arr($recs_aswers);

        if(_sizeof($recs_aswers)) foreach($recs_aswers as $answer)
        {  $child_questions=execSQL('select * from view_poll_tree where parent='.$answer['pkey'],0,1) ;


             $attr=[] ;
             $attr['class'][]='answer' ;
             if(_sizeof($recs_aswers)>=5) $attr['class'][]='small_margin' ;
             $attr['class'][]='btn' ;
             //$attr['class'][]='green' ;
             $attr['class'][]='light-green' ;
             //$attr['class'][]='blue' ;
             //if (_sizeof($this->recs_questions_by_parent[$answer['pkey']])) $attr['next_tiket']='tiket_'.$this->recs_questions_by_parent[$answer['pkey']][0]['pkey'] ;
             if (_sizeof($child_questions)) $attr['answer_id']=$answer['pkey'] ;
             if (_sizeof($child_questions)) $attr['next_tiket']='tiket_'.$child_questions[0]['level'].'_1'; ;
             ?>
                <div <?echo ENGINE()->echo_attr($attr)?>>
                                        <input type="hidden" name="poll_answers[<? echo $answer['parent'] ?>][<? echo $answer['pkey'] ?>]" value="0">
                                        <? echo $answer['obj_name'] ?>
                                        <!--<div class="comment"><?echo $attr['next_tiket']?></div>-->
                </div>
                <?
                //$i++;
        }
    }

  function panel_content($rec_content)
  {
      if ($rec_content['pkey'])
      { //if ($rec_content['type']=='text'){$this->show_text($rec_content);}
        if (strpos($rec_content['type'],'image')!==false){$this->show_image($rec_content);}
        //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)) ;
      }
  }


  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_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 show_image($rec_content,$rec_content_view=[])
  {
      //if(!$rec_content_view['length']) $rec_content_view['length']=10;
      ?><img src="<? echo _PATH_TO_SITE.$rec_content['dir'].$rec_content['file_name'] ?>"><?
      //$this->show_JS_timer_mediacontenthidden($rec_content_view);
  }

  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(); }
          <?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>

      <?
  }






}
?>
