<?php
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_services_events_poll_content extends c_page_TXT
{ function show()
  { //$obj_event=EVENTS()->get_event_obj_by_id(_CUR_ID) ;
    //if (is_object($obj_event))
    {   ?><SCRIPT type="text/javascript" src="<?echo _PATH_TO_SITE?>/<?echo _CLASS_?>/ext/jquery/jquery-1.8.3.min.js"></SCRIPT>
          <SCRIPT type="text/javascript" src="<?echo _PATH_TO_SITE?>/<?echo _CLASS_?>/ext/jquery/jquery.form.min.js"></SCRIPT>
          <script type="text/javascript" src="<?echo _PATH_TO_SITE?>/<?echo _CLASS_?>/ext/events_media/terminal-ipc.js"></script>
        <?
        ?><!--<div class="current_time"><?echo date('h:i:s')?></div>-->
          <div class="content">
        <?
        $recs_qustions=execSQL('select * from poll_questions where event_id='._CUR_ID) ;
        $recs_answers=execSQL('select * from poll_answers where question_id in ('.implode(',',array_keys($recs_qustions)).')') ;
        $recs_answers_by_qustion=group_by_field('question_id',$recs_answers) ;
        //damp_array($recs_answers_by_qustion) ;
        ?><div id="panel_poll"><form><?
        foreach($recs_qustions as $rec_qustion)
        {   $panel_attr=array() ;
            $panel_attr[]=($rec_qustion['indx']>1)? 'class="panel_poll_question hidden"':'class="panel_poll_question"' ;
            $panel_attr[]='id=question_'.$rec_qustion['indx'] ;
            if ($rec_qustion['indx']>1)  $panel_attr[]='prev_indx='.($rec_qustion['indx']-1) ;
            if ($rec_qustion['indx']<_sizeof($recs_qustions))  $panel_attr[]='next_indx='.($rec_qustion['indx']+1) ;
            if ($rec_qustion['indx']==_sizeof($recs_qustions))  $panel_attr[]='send_result=1' ;

            ?><div <?echo implode(' ',$panel_attr)?>>
             <h2><?echo $rec_qustion['obj_name'] ?></h2><?

            if (_sizeof($recs_answers_by_qustion[$rec_qustion['pkey']])) foreach($recs_answers_by_qustion[$rec_qustion['pkey']] as $rec_answer)
            {?>
              <label><input type="radio" name="poll_qustion[<?echo $rec_qustion['pkey']?>][]" value="<?echo $rec_answer['pkey']?>"><?echo $rec_answer['obj_name']?></label><br>
             <?
            }
            if ($rec_qustion['indx']>1){?><div class="button_left"><button class="button" go_indx="<?echo $rec_qustion['indx']-1?>">Назад</button></div><?}
            if ($rec_qustion['indx']<_sizeof($recs_qustions)){?><div class="button_right"><button class="button" go_indx="<?echo $rec_qustion['indx']+1?>">Далее</button></div><?}
            ?><div class="clear"></div><?
            ?></div><?
        }
        ?></form></div>
        <style type="text/css">
             div#panel_poll{margin-top:200px;}
             div#panel_poll div.panel_poll_question.hidden{display:none;}
             div#panel_poll div.button_left{float:left;}
             div#panel_poll div.button_right{float:right;}
             div#panel_poll div.clear{clear:both;}
        </style>
        <script type="text/javascript">

           $j('div#panel_poll').on('click','button.button',function()
             {   let go_indx=$j(this).attr('go_indx') ;
                 $j('div.panel_poll_question').hide() ;
                 $j('div#question_'+go_indx).show() ;
                 return(false) ;
             }) ;
           $j('div#panel_poll').on('click','input[type=radio]',function()
             {   let go_indx=$j(this).closest('div.panel_poll_question').attr('next_indx') ;
                 if (go_indx>0)
                 { $j('div.panel_poll_question').hide() ;
                   $j('div#question_'+go_indx).show() ;
                 }
                 let send_result=$j(this).closest('div.panel_poll_question').attr('send_result') ;
                 if (send_result>0)
                 {   let form=$j(this).closest('form') ;
                     $j(form).submit(function(){return false; }); // блокируем отправку формы стандартным способом
                     let params={} ;
                     params.event_id=<?echo _CUR_ID?> ;
                     $j(form).ajaxSubmit({url:'<?echo _PATH_TO_SITE?>/services/events_poll/reg_result/',type:'POST',dataType:'xml',cache:false,error:null,success:function ()
                         {
                             sendAppEvent('media-content-ended',{test:123}) ;
                         },data:params})  ;

                 }
             }) ;
        </script>
        <?

        ?></div>
          <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;}
          </style>
        <?


    }
  }






}
