<?php
include_once(_DIR_EXT."/esmo_magnit_instruction/i_event_instruction.php");
include_once(_DIR_TO_CLASS."/c_page.php");
class c_page_cab_esmo_magnit_instruction_card extends c_page
{
  public $h1='Карточка инструктажа' ;
  var $rec_event ;
  var $obj_event ;
  var $rec_theme ;

 function select_obj_info()
 { if (_CUR_ID)
   { $this->rec_event=EVENTS()->get_event_by_id(_CUR_ID) ;
     if ($this->rec_event['pkey'])
     { if ($this->rec_event['ext']=='esmo_magnit_instruction/i_event_instruction') $this->rec_event['ext']='esmo_magnit_instruction/i_event_instruction1' ;
       $this->obj_event=EVENTS()->get_event_obj_by_ext($this->rec_event['ext']) ;
       $this->obj_event->rec_event=$this->rec_event ;
       $this->h1=$this->obj_event->name.' "'.$this->rec_event['obj_name'].'"' ;
     }
     if (!$this->rec_event['pkey']) $this->result=404 ;
   } else $this->result=404 ;

 }


 function menu_config()
 { if ($this->no_event_menu) { parent::menu_config() ; return ; }
   // вкладки для сотрудника

 //and group_id in ('.ACCOUNTS()->tree[MEMBER()->cur_group_id]->get_list_child().')
     //$GLOBALS['debug_db']=2;
     ob_start() ;
   $arr_cnt=EVENTS()->get_event_personals_status($this->rec_event['pkey'],array('only_active'=>1)) ;
   $this->text_get_event_personals_status=ob_get_clean() ;
     //$GLOBALS['debug_db']=0;
   //damp_array($arr_cnt,1,-1);
   $arr_cnt[2]=$arr_cnt[2]+$arr_cnt[3]+$arr_cnt[4]+$arr_cnt[5];

   if ($this->rec_event['pkey'] and $this->rec_event['enabled'])
      { $this->menu[$this->rec_event['__href']]='Карточка инструктажа' ;
        $this->menu[$this->rec_event['__href'].'edit/step1/']='Редактировать' ;
        //$this->menu[$this->rec_event['__href'].'new_lession/']=array('title'=>'Новое занятие','id'=>'show_window_monitor') ;
        $this->menu[$this->rec_event['__href'].'lessions/']=array('title'=>'Занятия') ;

        $this->menu[$this->rec_event['__href'].'personals/free/']=array('title'=>'Подлежат инструктажу','info_value'=>(($arr_cnt[1])? '<span>'.$arr_cnt[1].'</span>':'')) ;
        $this->menu[$this->rec_event['__href'].'personals/select/']=array('title'=>'Направлены на инструктаж','info_value'=>(($arr_cnt[2])? '<span>'.$arr_cnt[2].'</span>':'')) ;
        //$this->menu[$this->rec_event['__href'].'personals/in/']=array('title'=>'Проходят инструктаж','info_value'=>(($arr_cnt[4])? '<span>'.$arr_cnt[4].'</span>':'')) ;
        $this->menu[$this->rec_event['__href'].'personals/process/']=array('title'=>'Прошли инструктаж частично','info_value'=>(($arr_cnt[6])? '<span>'.$arr_cnt[6].'</span>':'')) ;
        $this->menu[$this->rec_event['__href'].'personals/completed/']=array('title'=>'Прошли инструктаж','info_value'=>(($arr_cnt[7])? '<span>'.$arr_cnt[7].'</span>':'')) ;
        $this->menu[$this->rec_event['__href'].'personals/']='Все участники' ;
        $this->menu[$this->rec_event['__href'].'report/1/']='Журнал' ;
        //$this->menu[$this->rec_event['__href'].'log/']='История' ;
        $this->menu[$this->rec_event['__href'].'db/']='Исходные данные' ;
        $this->page_type='window' ;
      }
   elseif ($this->rec_event['pkey'])
      { $this->menu[$this->rec_event['__href'].'edit/step1/']='Параметры инструктажа' ;
        $this->page_type='window' ;
      }
   else parent::menu_config() ;
 }

 function second_menu_config()
 { $this->second_menu[$this->rec_event['__href']]=array('title'=>'Карточка инструктажа') ;
   //$this->second_menu[$this->rec_event['__href'].'edit/step1/']=array('title'=>'Редактировать') ;
   //$this->second_menu[$this->rec_event['__href'].'lessions/']=array('title'=>'Занятия') ;
   //$this->second_menu[$this->rec_event['__href'].'new_lession/']=array('title'=>'Новое занятие','id'=>'show_window_monitor') ;
   $this->second_menu[$this->rec_event['__href'].'log/']='История' ;
   //$this->second_menu[$this->rec_event['__href'].'db/']='Исходные данные' ;
   //$this->second_menu[$this->rec_event['__href'].'db_actions/']='Проверки' ;
 }


 function block_main()
 { $this->page_title() ;
   //damp_array($this->rec_event) ;
   //damp_array($this->obj_event) ;



   ?><table class="col_2x">
     <tr>
         <td><?$this->obj_event->panel_event_info();?></td>
         <td><?$this->panel_events_themes() ;?></td>
     </tr>
     </table>
     <?if (MEMBER()->rol==1){?><div class="sky-form no_box_shadow"><button class="button v2" cmd="events/event_check_personals" event_id="<?echo $this->rec_event['pkey']?>">Обновить список и статусы подлежащих мероприятию</button><div class="clear"></div><?}?>
     </div>
   <?
   $this->panel_stats_by_atp() ;
   $this->panel_stats_by_org() ;
   //$this->panel_lession_themes() ;
   ?><style type="text/css">
           table.col_2x{width:100%;table-layout:fixed;}
           table.col_2x > tbody > tr > td{vertical-align:top;text-align:left;}
     </style>
   <?
 }

 function panel_stats_by_atp()
 { ?><h2>Статистика прохождения по АТП:</h2><?
   include_once(_DIR_EXT.'/esmo_magnit_instruction/report_event_instruktion_info.php') ;
   $report_obj=new report_event_instruktion_info() ;
   $report_obj->event_id=$this->rec_event['pkey'] ;
   $report_obj->filter['group_id']=MEMBER()->cur_group_id ;
   //$report_obj->setting() ;
   $report_obj->view() ;
 }

 function panel_stats_by_org()
 { ?><h2>Статистика прохождения по организациям:</h2><?
   include_once(_DIR_EXT.'/esmo_magnit_instruction/report_event_info_by_orgs.php') ;
   $report_obj=new report_event_info_by_orgs() ;
   $report_obj->event_id=$this->rec_event['pkey'] ;
   $report_obj->filter['group_id']=MEMBER()->cur_group_id ;
   //$report_obj->setting() ;
   $report_obj->view() ;
 }

  function panel_events_themes()
  { ?><div id="panel_events_themes"><h2><?echo $this->obj_event->card_theme_title?>:</h2><?
      $recs_docs_by_themes=array() ;
      $recs_themes=execSQL('select * from obj_site_events_themes where parent='.$this->rec_event['pkey'].' and clss=225 order by indx') ;
      if (_sizeof($recs_themes))
       { $recs_docs=execSQL('select pkey,parent,obj_name,file_name from obj_site_events_themes_files where parent in ('.implode(',',array_keys($recs_themes)).')',0,1) ;
         $recs_docs_by_themes=group_by_field('parent',$recs_docs) ;
       }

       ?><table class="basic fz_small full auto"><tr class="head"><th>№</th><th>Наименование</th><th>Длительность, мин.</th><th>Методические материалы</th></tr><?
           if (_sizeof($recs_themes)) foreach($recs_themes as $rec)
           { ?><tr>
                 <td style="width:10%;" class="indx center"><?echo $rec['indx']?></td>
                 <td style="width:35%;" class="left"><?echo $rec['obj_name']?></td>
                 <td style="width:5%;" class="center"><?echo $rec['length']?> мин.</td>
                 <td style="width:20%;text-align:left;"><?
                 if (_sizeof($recs_docs_by_themes[$rec['pkey']]))
                 {  ?><ul><?
                    foreach($recs_docs_by_themes[$rec['pkey']] as $rec_doc) {?><li><a href="<?echo _MAIN_PROTOCOL._MAIN_DOMAIN?>/public/instruktions_docs/<?echo $rec_doc['file_name']?>" class="new_window"><?echo $rec_doc['obj_name']?></a></li><?}
                    ?></ul><?
                 }
                 ?></td>
               </tr>
             <?
           }
           ?>
            </table>
            <style type="text/css">
               table.basic td.indx{text-align:center;}
               table.basic td.indx:hover i{display:inline;cursor:pointer;}
               table.basic ul{margin:0 0 0 20px;;padding:0;}
               table.basic ul li{padding:0;margin:0;}
            </style>
            </div>
           <?
  }

  function panel_lession_themes()
     {   $recs_themes=EVENTS()->get_themes_of_events($this->rec_event['pkey']) ;
         $recs_lessions=execSQL('select org_group_id,lession_id,count(personal_id) as cnt 
                                 from view_lession_personals 
                                 where lession_person_status=4 and event_id='.$this->rec_event['pkey'].'
                                 group by org_group_id,lession_id',0,1) ;
         if (_sizeof($recs_lessions)) foreach($recs_lessions as $rec) $arr[$rec['org_group_id']][$rec['lession_id']]=$rec['cnt'] ;
         //$arr=group_by_field('org_group_id',$recs_lessions,'lession_id') ;
         //damp_array($arr,1,-1) ;
         $recs_themes_of_lessions=execSQL('select parent as lession_id,theme_id from obj_site_lession_themes where  event_id='.$this->rec_event['pkey'],0,1) ;

         $stats_by_group=execSQL_row('select org_group_id,count(pkey) as cnt from view_event_personals where event_id='.$this->rec_event['pkey'].' group by org_group_id ') ;
         //$arr=group_by_field('org_group_id',$stats,'event_status') ;


         ?><h2>Статистика прохождения по темам инструктажа:</h2><table class="basic fz_small">
         <tr><!--<tr><th>ID</th>--><th>Наименование темы</th>
         <?if (_sizeof($arr)) foreach($arr as $group_id=>$lession_stats) echo '<th>'.ACCOUNTS()->get_group_name($group_id).'</th>'?>
         </tr><?
         ?><tr><td class="right bold">ВСЕГО:</td><?
         if (_sizeof($arr)) foreach($arr as $group_id=>$lession_stats) echo '<td>'.$stats_by_group[$group_id].'</td>'?>
         </tr><?

         $summ=array() ;
         if (_sizeof($recs_themes)) foreach($recs_themes as $rec)
         {?><tr>
             <!--<td class="left"><?echo $rec['pkey']?></td>-->
             <td class="left"><?echo $rec['obj_name']?></td>
             <?if (_sizeof($arr)) foreach($arr as $group_id=>$lession_stats)
              { // по ID темы смотрим, в каких занятиях она читалась
                $cnt=0 ;
                foreach($recs_themes_of_lessions as $rec_lession_themes) if ($rec_lession_themes['theme_id']==$rec['pkey'])
                {   $lession_id=$rec_lession_themes['lession_id'] ;
                    $cnt+=$arr[$group_id][$lession_id] ;
                }
                $summ[$group_id]+=$cnt ;
                ?><td><?echo ($cnt)? $cnt:'-'?></td><?

              }
             ?>
            </tr>
          <?
         }

         ?><tr><td class="right bold">ПРОЦЕНТ ДОВЕДЕНИЯ ИНСТРУКТАЖА:</td><?
                  if (_sizeof($arr)) foreach($arr as $group_id=>$lession_stats)
                  {  $cnt_full=_sizeof($recs_themes)*$stats_by_group[$group_id] ;
                     $proc=$summ[$group_id]/$cnt_full*100;
                     echo '<td>'.round($proc,2).' %</td>' ;

                  }
                  ?></tr><?

         ?></table><br>
         <style type="text/css">
              .list_themes{font-size:14px;}
         </style>
         <?
     }

 function panel_event_info()
   {  ?><div class="row event_info">
           <section class="col col-3">
             <label class="label">Переодичность:<br>&nbsp;</label>
             <strong><?echo $_SESSION['ARR_events_mode'][$this->rec_event['mode']]?></strong>
            </section>

            <section class="col col-2">
              <label class="label">Дата<br>начала уведомлений:</label>
               <strong><?echo date('d.m.Y',$this->rec_event['alert_from'])?></strong>
           </section>

           <!--<section class="col col-2">
              <label class="label">Дата<br>окончания уведомлений:</label>
               <strong><?echo date('d.m.Y',$this->rec_event['alert_to'])?></strong>
           </section>-->

           <section class="col col-2">
              <label class="label">Дата<br>начала блокировки:</label>
               <strong><?echo date('d.m.Y',$this->rec_event['block_from'])?></strong>
           </section>

           <section class="col col-2">
              <label class="label">Дата<br>окончания блокировки:</label>
               <strong><?echo date('d.m.Y',$this->rec_event['block_to'])?></strong>
           </section>
	  </div><?
   }

    function panel_personal_info()
    {   // данные по участникам мероприятия с учетом группы текущего аккаунта
        $arr_personals_status=EVENTS()->get_event_personals_status($this->rec_event['pkey']) ;
        $personals_count=array_sum($arr_personals_status) ;

        ?><div class="row event_info">
            <section class="col col-3">
                   <label class="label">Условие отбора участников</label>
                   <?PERSONALS_FILTER()->get_personals_filter_info_to_item($this->rec_event['_reffer'])?>
            </section>
            <section class="col col-3">
                   <label class="label">Сотрудники</label>
                   <strong><?echo $personals_count.' '.get_case_by_count_1_2_5($personals_count,'сотрудник,сотрудника,сотрудников')?></strong>
            </section>
 	  </div><?
    }




}



?>
