<?php
include_once(_DIR_EXT.'/personal/report_personals.php') ;
class report_master_personals extends report_personals
{
 var $title='Сотрудники мастера' ;
 var $view_by_space_filter=1 ;
 var $allow_org_ids=1 ;
 var $usl_select_org='' ;
 public $use_datepicker=1 ;
 var $report_script=_DIR_EXT.'/esmo_master/report_master_personals.php' ;
 var $recs_mess_to_personal=array() ;

 function panel_report_buttons()
  {
      ?><button class="button v2" cmd="aspmo_personal/send_mess_to_personal">Отправить сообщение всем</button><?
  }

  function get_items($usl,$options=array())
  { $list_rec=parent::get_items($usl,$options) ;
    $this->recs_mess_to_personal=array() ;
    if (_sizeof($list_rec))
       { $recs_mess=execSQL('select pkey,personal_id,enabled,view_cnt,view_data,view_mesto,status,view_from,view_to,block_from,block_to 
                             from view_personals_messages 
                             where personal_id in ('.implode(',',array_keys($list_rec)).') 
                             order by view_data desc,c_data desc',0,1) ;
         $this->recs_mess_to_personal=group_by_field('personal_id',$recs_mess) ;
       }

    //print_2x_arr($recs_mess_to_personal[13835]) ;
    //print_2x_arr($recs_mess_to_personal[13834]) ;

    return($list_rec) ;
  }



// шаблон - вывод ссылками без разделитея
  function print_template_HTML($list_recs,$options=array())
     { $id=($options['id'])? 'id='.$options['id']:'' ;


       ?><table class="list_personal_table_img basic fz_small full auto"<?echo $id?>>
           <colgroup><col id="c1"><col id="c2"><col id="c3"><col id="c4"></colgroup>
           <tr><th rowspan="2">Пропуск</th>
               <th rowspan="2">Фото</th>
               <th rowspan="2">ФИО</th>
               <th rowspan="2">Организация / Подразделение</th>
               <th rowspan="2">Должность</th>
               <th colspan="5">Сообщения</th>
               <th rowspan="2">Число показов / Последнее уведомление</th>
               <th rowspan="2">Операции</th>
           </tr>
           <tr>
               <th>Ожидает</th>
               <th>Новых</th>
               <th>Просмотрено</th>
               <th>Заблокировано</th>
               <th>Закрыто</th>
           </tr>
           <? ;
           if (_sizeof($list_recs)) foreach($list_recs as $rec)
           {
              $arr_status=$this->get_arr_mess_status($this->recs_mess_to_personal[$rec['pkey']]) ;

              ?><tr class=item>
                   <td class="id"><?echo $rec['propusk']?><?if ($rec['demo_access']){?><div class="demo_access" title="Сотрудник может проходить неограниченное число МО"></div><?}?></td>
                   <td class="img"><a  href="/window/personal/person/<?echo $rec['pkey']?>/" class="new_window" target="_blank" title="win_second"><img src="<?echo img_clone($rec,'small')?>"></a></td>
                   <td class="left"><a href="/window/personal/person/<?echo $rec['pkey']?>/" class="new_window" target="_blank" title="win_second"><?echo $rec['obj_name']?></a>
                       <br><?echo $rec['dr']?><br><?echo $rec['age'].' '.get_case_by_count_1_2_5($rec['age'],'год, года, лет')?>
                   </td>
                   <td class="left"><?echo $rec['_org_name'].'<br>'.$rec['_otdel_name']?></td>
                   <td class="left"><?echo _IL('IL_works')->recs[$rec['working']]['obj_name']?></td>
                   <td class="cnt_mess gray"><?$this->show_mess_count($rec['pkey'],$arr_status,5);?></td>
                   <td class="cnt_mess green"><?$this->show_mess_count($rec['pkey'],$arr_status,1);?></td>
                   <td class="cnt_mess green"><?$this->show_mess_count($rec['pkey'],$arr_status,2);?></td>
                   <td class="cnt_mess red"><?$this->show_mess_count($rec['pkey'],$arr_status,3);?></td>
                   <td class="cnt_mess gray"><?$this->show_mess_count($rec['pkey'],$arr_status,4);?></td>
                   <td class="cnt_mess">
                       <?if ($arr_status['cnt_view']){?><div class="view_cnt green"><? echo $arr_status['cnt_view']?></div><?}?>
                       <?if ($arr_status['last_data']){?><div class="view_data"><? echo date('d.m.Y H:i',$arr_status['last_data'])?></div><?}?>
                       <?if ($arr_status['last_mesto']){?><div class="view_mesto"><? echo $arr_status['last_mesto']?></div><?}?>
                       <? if (isset($arr_status[3]) and $arr_status[3]>0) echo  '<div class="mo_alert">Прохождение МО заблокировано</div>'?>
                   </td>
                   <td class="center"><button class="v2" cmd="aspmo_personal/send_mess_to_personal" personal_id="<?echo $rec['pkey']?>">Отправить сообщение</button></td>
               </tr><?
           }
         ?></table>
           <style type="text/css">
               table.list_personal_table_img{}
               table.list_personal_table_img td.cnt_mess{}
               table.list_personal_table_img div.view_cnt{font-size:24px;margin:5px 0 10px 0;}
               table.list_personal_table_img div.view_data{font-size:14px;margin:5px 0;}
               table.list_personal_table_img div.mo_alert{color:white;font-size:11px;margin:10px 80px 0 80px;background:darkred;padding:5px;}

           </style>
         <?
         //damp_array($rec) ;
     }

     function show_mess_count($personal_id,$arr_status,$status)
     { if ($arr_status[$status])
       {?><div class="view_cnt"><? echo $arr_status[$status]?></div><a href="/cab/esmo_master/messages/[personal_id=<?echo $personal_id?>,status=<?echo $status?>]/" target="_blank"><?echo get_case_by_count_1_2_5( $arr_status[$status],'Сообщение,Сообщения,Сообщений')?></a><?}
     }

     function get_arr_mess_status($recs_mess)
     { $arr_status=array(1=>0,
                         2=>0,
                         3=>0,
                         4=>0,
                         5=>0,
                         'last_data'=>0,
                         'last_mesto'=>'',
                         'view_cnt'=>0,
             ) ;

       if (_sizeof($recs_mess)) foreach($recs_mess as $rec_mess)
       { if ($rec_mess['status']<=3) $arr_status['cnt_view']+=$rec_mess['view_cnt'] ;
         $arr_status[$rec_mess['status']]++ ;
         if ($rec_mess['status']<=3 and $rec_mess['view_data']>$arr_status['last_data'])
         {   $arr_status['last_data']=$rec_mess['view_data'] ;
             $arr_status['last_mesto']=$rec_mess['view_mesto'] ;
         }
       }
       return($arr_status) ;
     }

    function print_template_CSV($list_recs,$options=array())
    { $i=$options['from'] ;
      if (_sizeof($list_recs)) foreach($list_recs as $rec)
      {   $arr_status=$this->get_arr_mess_status($this->recs_mess_to_personal[$rec['pkey']]) ;
          $data=array() ;
          $data['№']=$i+1 ;
          $data['Пропуск']=$rec['propusk']  ;
          $data['GUID']=$rec['uid']  ;
          $data['ФИО']=$rec['obj_name']  ;
          $data['Дата рождения']=$rec['dr']  ;
          $data['Возраст']=$rec['age']  ;
          $data['Пол']=($rec['pol'])? 'Женский':'Мужской'  ;
          $data['Организация']=$rec['_org_name'] ;
          $data['Подразделение']=$rec['_otdel_name'] ;
          $data['Должность']= _IL('IL_works')->recs[$rec['working']]['obj_name'] ;
          $data['Уволен']=($rec['enabled'])? '':'Да' ;
          $data['Новых сообщений']=$arr_status[1] ;
          $data['Просмотренных сообщений']=$arr_status[2] ;
          $data['Блокирующих сообщений']=$arr_status[3] ;
          $data['Закрытых сообщений']=$arr_status[4] ;
          // вносим данные в csv
          $this->put_data_to_CSV($data,$i) ;
          $i++ ;
      }
    }

}
?>
