<?php
include_once(_DIR_TO_CLASS."/c_page.php");
class c_page_cab_esmo_magnit_instruction_lession extends c_page
{
  public $h1='Занятие по инструктажу' ;
  public $rec_event=array() ;
  public $rec_lession=array() ;
  public $obj_event;
  public $recs_themes;
  public $recs_docs_by_themes;

  function select_obj_info()
  { if (_CUR_ID)
    { $this->rec_lession=EVENTS()->get_lession_by_id(_CUR_ID) ;
      $this->rec_lession['__href']='/window/esmo_magnit_instruction/lession/'.$this->rec_lession['pkey'].'/' ;
      $this->rec_event=EVENTS()->get_event_by_id($this->rec_lession['event_id']) ;
      $this->obj_event=EVENTS()->get_event_obj_by_ext($this->rec_event['ext']) ;
      $this->recs_themes=EVENTS()->get_themes_of_lession($this->rec_lession['pkey']) ;
      //damp_array($this->recs_themes) ;
      if (_sizeof($this->recs_themes))
       { foreach($this->recs_themes as $rec_theme) $arr_themes_id[]=$rec_theme['theme_id'] ;
         $recs_docs=execSQL('select * from obj_site_events_themes_files where parent in ('.implode(',',$arr_themes_id).')',0,1) ;
         $this->recs_docs_by_themes=group_by_field('parent',$recs_docs,'pkey') ;
         //damp_array($this->recs_docs_by_themes) ;
       }
      // проверка, что если статус 5 (занятие завершено) то доллжен быть документ. Если документа нет - переводим в статус 4
      if ($this->rec_lession['status']==5)
      {   $doc_id=execSQL_value('select pkey from obj_site_docs where lession_id='.$this->rec_lession['pkey']) ;
          if (!$doc_id)
          { update_rec_in_table('obj_site_lession',array('status'=>4),'pkey='.$this->rec_lession['pkey'],array('debug'=>0)) ;
            LOGS()->reg_log('BAG','Возврат lession в статус 4, так как не найден документ (ведомость)',array('reffer'=>$this->rec_lession['_reffer'])) ;
            $this->rec_lession['status']=4 ;
          }

      }


      if ($this->rec_event['pkey']) $this->h1=$this->obj_event->name.' "'.$this->rec_event['obj_name'].'"' ;

      if (!$this->rec_lession['pkey']) $this->result=404 ;

    } else $this->result=404 ;
  }

  function menu_config()
  { if ($this->rec_event['pkey'])
       { if ($this->rec_lession['status']==1) $this->menu[$this->rec_lession['__href'].'register/']='Регистрация участиников' ;
         $this->menu[$this->rec_lession['__href']]='Занятие' ;
         if (_sizeof($this->recs_docs_by_themes)) $this->menu[$this->rec_lession['__href'].'docs/']='Методические материалы' ;
         if ($this->rec_lession['status']==4) $this->menu[$this->rec_lession['__href'].'sign/']='Подпись участников' ;

         $doc_rec=execSQL_van('select * from obj_site_docs where lession_id="'.$this->rec_lession['pkey'].'" and report_code="lession_personal_status" order by pkey desc limit 1') ;
         if ($this->rec_lession['status']>=4 and $doc_rec['pkey']) $this->menu[$this->rec_lession['__href'].'report/']=$doc_rec['obj_name'] ;

         //$doc_id=execSQL_value('select pkey from obj_site_docs where obj_reffer="'.$this->rec_lession['_reffer'].'" and report_code="lession_personal_status" order by pkey desc limit 1') ;
         //if ($this->rec_lession['status']>=4 and $doc_id) $this->menu[$this->rec_lession['__href'].'report/']='Ведомость прохождения' ;

         $this->menu[$this->rec_lession['__href'].'db/']='Исходные данные' ;
         $this->page_type='window' ;
       }
    else parent::menu_config() ;
  }

    function block_main()
      { $this->page_title() ;
        $this->panel_lession_info() ;
        $this->panel_lession_action() ;
        $this->panel_lession_themes() ;

        $usl=array() ;
        $usl[]='clss=211';
        $usl[]='event_id='.$this->rec_event['pkey'] ;
        $usl[]='lession_id='.$this->rec_lession['pkey'] ;
        $recs=execSQL('select * from view_lession_personals where '.implode(' and ',$usl)) ;
        if (_sizeof($recs))
        {  $this->panel_buttons() ;
           $this->page_title('Зарегистрированные на инструктаж сотрудники ['._sizeof($recs).']') ;
           $this->panel_lession_personals($recs) ;
        }
        //damp_array($this->rec_lession) ;


        include_once(_DIR_EXT.'/docs/report_docs.php') ;
        $report_obj=new report_docs() ;
        $report_obj->filter['lession_id']=$this->rec_lession['pkey'] ;
        $report_obj->view() ;
      }

      function panel_buttons($options=array())
       {   ?><link rel="stylesheet" href="/AddOns/FlipClock/compiled/flipclock.css">
              <script src="/AddOns/FlipClock/compiled/flipclock.min.js"></script>

           <?

           if (!is_array($options['disabled_filter'])) $options['disabled_filter']=array() ;
           ?><div id="panel_filter">
                   <table class="panel_buttons sky-form">
                       <tr>
                           <td><?if ($this->rec_lession['status']<3){?><button class="button v2" cmd="esmo_magnit_instruction/start_lession" lession_id=<?echo $this->rec_lession['pkey']?> style="margin:0;float:none;">Начать занятие</button><?}
                                 else echo 'Занятие начато<br><strong style="font-size:22px;">'.date('d.m.Y H:i',$this->rec_lession['time_from']).'</strong>' ;
                               ?></td>
                           <td><div class="clock" style="margin:2em;"></div><div class="message"></div></td>
                           <td><?
                               if ($this->rec_lession['status']==3){?><button class="button v2" cmd="esmo_magnit_instruction/stop_lession"  lession_id=<?echo $this->rec_lession['pkey']?> style="margin:0;float:none;">Закончить занятие</button><?}
                               else if ($this->rec_lession['status']>=4) echo 'Занятие закончено<br><strong style="font-size:22px;">'.date('d.m.Y H:i',$this->rec_lession['time_to']).'</strong>' ;
                           ?></td>
                       </tr>
                   </table>
             </div><br>

           <style type="text/css">
               table.panel_buttons{margin:0 auto;}
               table.panel_buttons td{text-align:center;vertical-align:middle;}
           </style>
           <script type="text/javascript">
           		var clock;

           		$j(document).ready(function()
                {	<? $lession_length=0 ;
                       if ($this->rec_lession['status']==3) $lession_length=time()-$this->rec_lession['time_from'] ;
                       if ($this->rec_lession['status']>=4) $lession_length=$this->rec_lession['time_to']-$this->rec_lession['time_from'] ;
                    ?>
                    clock = $j('.clock').FlipClock(<?echo $lession_length?>,
                        { clockFace: 'HourlyCounter',
                          autoStart:<?echo ($this->rec_lession['status']==3)? 'true':'false'?>,
                          language: 'ru'
           		        });
           		});
           	</script>
           <?
        }


      function panel_lession_personals($recs)
      {

               ?><table id="panel_lession_personals"><?
               $col=0 ;
               if (_sizeof($recs)) foreach($recs as $rec)
                   {  if (!$col) echo '<tr>' ; $col++ ;
                       ?><td class="item right">
                            <img class="photo" src="<?echo img_clone($rec,'200')?>" width="100">
                            <div class="name"><?echo $rec['obj_name']?></div>
                       </td>
                       <?
                       if ($col==5) { echo '</tr>' ; $col=0  ;}

                   }
               ?></table>
                 <style type="text/css">
                     #panel_lession_personals{width:auto;table-layout:fixed;}
                     #panel_lession_personals td{vertical-align:top;text-align:center;padding:10px 0 25px 0;width:100px;}
                     #panel_lession_personals td.item img{border:1px solid #aaaaaa;float:left;margin-bottom:10px;}
                     #panel_lession_personals td.item .name{font-size:12px;}
                 </style>
               <?
      }

    function panel_lession_themes()
     {   ?><h2>Темы занятия:</h2><ul class="list_themes"><?
         if (_sizeof($this->recs_themes)) foreach($this->recs_themes as $rec){?><li><?echo $rec['obj_name']?></li><?}
         ?></ul>
         <style type="text/css">
              .list_themes{font-size:14px;}
         </style>
         <?


     }

    function panel_lession_info()
     {   ?><div class="left">Место занятия: <span class="nowrap bold size16"><?echo $this->rec_lession['group_name']?>,</span>
                                Лектор:  <span class="nowrap bold size16"><?echo $this->rec_lession['account_name']?></span>
                                Статус занятия:  <span class="nowrap bold size16"><?echo $this->obj_event->arr_status_lession[$this->rec_lession['status']]?></span>
            </div>
         <?
     }

    function panel_lession_action()
    {?><div class="right"><?if ($this->rec_lession['status']<4){?><button class="button v2" cmd="esmo_magnit_instruction/cancel_lession" lession_id="<?echo $this->rec_lession['pkey']?>">Отмена занятия</button><?}?></div><?}
}



?>
