<?php
include_once(_DIR_TO_CLASS."/c_page.php");
class c_page_cab_docs_put extends c_page
{
  public $h1='Выдача документа' ;
  public $page_type='window' ;
  var $rec_personal=array()  ;
  var $add_docs_ids=array()  ;
  var $rec_event_to_personal=array()  ;

  function menu_config()
   { $this->menu=array() ;
   }

  function block_main()
  { $this->page_title() ;
    //$this->rec_event_to_personal=EVENTS()->get_event_personal_rec($this->rec_event['pkey'],$this->filter['personal_id']) ;
    $this->put_event_docs_to_personal() ;
  }

 /* function panel_select_docs($options=array())
  { //  damp_array($this->rec_event)  ;
      //damp_array($this->filter)  ;
     $this->rec_personal=PERSONAL()->get_personal_by_id($this->filter['personal_id']) ;
     $this->rec_event_to_personal=EVENTS()->get_event_personal_rec($this->rec_event['pkey'],$this->filter['personal_id']) ;
      ?><div id="panel_filter">
              <form id="sky-form" class="sky-form">
                  <input type="hidden" name="filter_dir" value="<?echo _CUR_REPORT_DIR?>">
                  <input type="hidden" name="filter[event_id]" value="<?echo $this->rec_event['pkey']?>">
                  <input type="hidden" name="filter[personal_id]" value="<?echo $this->rec_personal['pkey']?>">
                   <fieldset>
                       <h2>Мероприятие</h2><br>
                       <? EVENT_PMO()->panel_event_info($this->rec_event);?>
                       <br><h2>Участник мероприятия</h2><br>
                           <div class="row person_info">

                              <section class="col col-2">
                                     <label class="label">Фото</label>
                                     <img src="<?echo img_clone($this->rec_personal,'200')?>">
                              </section>

                              <section class="col col-2">
                                     <label class="label">ФИО</label>
                                     <strong><?echo $this->rec_personal['obj_name']?></strong>
                              </section>

                              <section class="col col-2">
                                     <label class="label">Дата рождения</label>
                                     <strong><?echo $this->rec_personal['dr']?></strong>
                              </section>

                              <section class="col col-2">
                                     <label class="label">Место работы</label>
                                     <strong><?echo $this->rec_personal['_org_name'].'</strong><br><div class=_small>'.$this->rec_personal['_otdel_name']?></strong>
                              </section>

                              <section class="col col-2">
                                     <label class="label">Должность</label>
                                     <strong><?echo $this->rec_personal['_work_name']?></strong>
                              </section>

                              <section class="col col-2">
                                     <label class="label">Группы</label>
                                     <strong><?echo (_sizeof($this->rec_personal['_groups']))? implode('<br>',$this->rec_personal['_groups']):'-'?></strong>
                              </section>
                           </div>
                           <br><h2>Документы для мероприятия</h2>
                           <div class="row event_info">
                           <?//damp_array($this->rec_event['_ext_info']['event_docs']) ;
                             // список уже созданных документов
                             $rec_docs=execSQL('select report_code,pkey,c_data,obj_name,sign from view_docs where event_id='.$this->rec_event['pkey'].' and personal_id='.$this->rec_personal['pkey'].' order by pkey') ;

                             $cnt_pdf=0 ; $cnt_sign=0 ; $cnt_docs=_sizeof($this->rec_event['_ext_info']['event_docs']) ;
                             // список документов согласно мероприятия
                             // ищем скан с последним направлением где зафиксирован диагноз, что он не здоров
                             $rec_scan=execSQL_van('select * from obj_site_scan where personal_id='.$this->rec_personal['pkey'].' and diagnoz!="Здоров" and not diagnoz is null order by pkey desc limit 1') ;
                             if ($rec_scan['pkey']) $this->rec_event['_ext_info']['event_docs']['doc/esmo_magnit_pmo/napravlenie_ag']='Направление по АГ' ;
                             ///damp_array($this->rec_event['_ext_info']['event_docs']) ;




                             if (_sizeof($this->rec_event['_ext_info']['event_docs']))
                              {?><table id="panel_pdf_info" class="basic fz_small full auto" style="margin-left:10px;">
                                  <tr><th>Наименование документа</th><th>Подготовлен</th><th>Подписан</th><th>Выдан</th></tr>
                                  <? if (_sizeof($this->rec_event['_ext_info']['event_docs'])) foreach($this->rec_event['_ext_info']['event_docs'] as $doc_code=>$doc_name)
                                    { $rec_doc=(isset($rec_docs[$doc_code]))? $rec_docs[$doc_code]:array() ;
                                      // черех чекбок передаем код действия
                                      // 1 - регистрировать документ в системе документов, создать PDF
                                      // 2 - не регистрировать, не создавтаь PDF
                                      $checkbox_value=($doc_code=='doc/esmo_magnit_pmo/napravlenie_ag')? 2:1 ;
                                      ?><tr>
                                          <td class="left"><?echo ($rec_doc['pkey'])? $rec_doc['obj_name']:$doc_name?><input type="hidden" name="filter[docs][<?echo $doc_code?>]" value="<?echo $checkbox_value?>"></td>
                                          <td><?echo ($rec_doc['pkey'])? date('d.m.Y H:i',$rec_doc['c_data']):'-'?></td>
                                          <td><?echo ($rec_doc['sign'])? 'Да':'-'?></td>
                                          <td id="time_print"><?echo ($this->rec_event_to_personal['time_print'])? date('d.m.Y H:i',$this->rec_event_to_personal['time_print']):'-'?></td>
                                        </tr><?
                                      if ($rec_doc['pkey']) $cnt_pdf++ ;
                                      if ($rec_doc['sign']) $cnt_sign++ ;
                                      $this->add_docs_ids[]=$rec_doc['pkey'] ;
                                    }?>
                                </table>
                              <?}
                           ?></div>
                   </fieldset>
                  <? //$from=_DIR_TO_ROOT.'/docs/'.$this->rec_event_to_personal['fname'] ;
                     //$to=_DIR_TO_ROOT.'/docs/'.$this->rec_event_to_personal['fname'] ;
                     //copy($from,$to) ;
                  ?>
                  <div id="panel_buttons">
                       <? if ($cnt_pdf<$cnt_docs or 1){?><button class="button v2" cmd="events/put_event_docs_to_personal">Подготовить пакет документов</button><?}
                          if ($this->rec_event_to_personal['fname']){?><button class="button tt2 btnPrint" onclick="send_ajax_request({cmd:'events/fixed_print_doc',event_id:'<?echo $this->rec_event_to_personal['event_id']?>',personal_id:'<?echo $this->rec_event_to_personal['personal_id']?>'})"  id="print_doc" href="<? echo _MAIN_PROTOCOL._MAIN_DOMAIN.'/docs/'.$this->rec_event_to_personal['fname']?>">Печать пакета документов</button><?}
                       ?>
                  </div><br>
                  <? //if ($cnt_pdf==$cnt_docs and $cnt_sign<$cnt_docs) $this->sign_doc() ;

                  ?>
                  <?if ($this->rec_event_to_personal['fname']){?><iframe src="<?echo _MAIN_PROTOCOL._MAIN_DOMAIN?>/docs/<?echo $this->rec_event_to_personal['fname']?>" style="width:100%; height:700px;" frameborder="0" /></iframe><?}?>
               </form>
        </div><br>
      <script>
        //$j(document).ready(function() {
        //  $j(".btnPrint").printPage();
        //});
        </script>
        <style>
            div.event_info strong{font-size:14px;font-weight:normal;}
            div.person_info strong{font-size:14px;font-weight:normal;}
            h3{text-align:left;}
        </style>

      <?
      //print_r($this->add_docs_ids) ;
      //damp_array($this->rec_personal) ;
    }
  */

  function put_event_docs_to_personal()
  { //damp_array($this->rec_event_to_personal,1,-1) ;   return ;
      //doc_napravlenie.php
     //damp_array($this->filter) ;
     $res=DOCS()->doc_create($this->filter['doc_code'],
                             $this->filter,
                             array('obj_reffer'=>$this->filter['obj_reffer'],
                                   'report_seria'=>$this->filter['report_seria'],
                                   'pdf_options'=>$this->filter['pdf_options'])
                             ) ;

     if ($res['error'])
     {   ?><div class="alert"><?echo $res['error']?></div><?
         return ;
     }

     if ($_GET['debug']) damp_array($res) ;

     $url_page_doc_view=($this->filter['url_page_doc_view'])? $this->filter['url_page_doc_view']:_PATH_TO_SITE.'/window/docs/pdf/'.$res['doc_id'].'/' ;

     if (!$_GET['debug']) REPORT()->panel_view_pdf_convert($res['result_convert']['cmd_uid'],$url_page_doc_view) ;

          //$this->add_element('show_modal_window','HTML',array('width'=>'700','title'=>'Подготовка документа')) ;   // показываем модальное окно


       /*
     $res=DOCS()->doc_create('doc/esmo_magnit_pmo/union',
                             // filter
                             array('event_id'=>$this->rec_event['pkey'],
                                   'personal_id'=>$this->rec_event_to_personal['personal_id']
                                   ),
                             // options
                             array('no_reg_doc'=>1,
                                   'event_id'=>$this->rec_event['pkey'],
                                   'obj_reffer'=>$this->filter['obj_reffer'],
                                   'report_seria'=>$this->filter['report_seria'],
                                   'pdf_options'=>$this->filter['pdf_options'],
                                   'source_pkey'=>$this->rec_event_to_personal['event_person_id'], // ID персоны в мероприяии
                                   'source_tkey'=>$this->rec_event_to_personal['event_person_tkey'], // ID персоны в мероприяии
                                   'source_obj'=>'esmo_magnit_pmo/i_event_pmo' // объект, которые будет использоваться в эвентах
                                   )
                             ) ;
     //damp_array($res,1,-1) ;
     //damp_array($this->rec_event) ;
     //return ;
     if ($res['error']) {?><div class="alert"><?echo $res['error']?></div><?}
     else
     { $after_convert_go_url=_PATH_TO_SITE.'/docs/'.$res['result_convert']['PDF_file_name'] ;
       REPORT()->panel_view_pdf_convert($res['result_convert']['cmd_uid'],$after_convert_go_url) ;
       // сохраняем в событии имя файла PDF пакета документов - он после подписи будет показан и пойдет на печать
       $data=array('fname'=>$res['result_convert']['PDF_file_name'],'time_print'=>time()) ;
       update_rec_in_table('obj_site_events_pass',$data,'pkey='.$this->rec_event_to_personal['event_person_id']) ;
     }
       */
  }
  /*
  function put_event_docs_to_personal_old()
  { //damp_array($this->rec_event_to_personal,1,-1) ;   return ;

     $res=DOCS()->doc_create('doc/esmo_magnit_pmo/union',
                             // filter
                             array('event_id'=>$this->rec_event['pkey'],
                                   'personal_id'=>$this->rec_event_to_personal['personal_id']
                                   ),
                             // options
                             array('no_reg_doc'=>1,
                                   'event_id'=>$this->rec_event['pkey'],
                                   'obj_reffer'=>$this->filter['obj_reffer'],
                                   'report_seria'=>$this->filter['report_seria'],
                                   'pdf_options'=>$this->filter['pdf_options'],
                                   'source_pkey'=>$this->rec_event_to_personal['event_person_id'], // ID персоны в мероприяии
                                   'source_tkey'=>$this->rec_event_to_personal['event_person_tkey'], // ID персоны в мероприяии
                                   'source_obj'=>'esmo_magnit_pmo/i_event_pmo' // объект, которые будет использоваться в эвентах
                                   )
                             ) ;
     //damp_array($res,1,-1) ;
     //damp_array($this->rec_event) ;
     //return ;
     if ($res['error']) {?><div class="alert"><?echo $res['error']?></div><?}
     else
     { $after_convert_go_url=_PATH_TO_SITE.'/docs/'.$res['result_convert']['PDF_file_name'] ;
       REPORT()->panel_view_pdf_convert($res['result_convert']['cmd_uid'],$after_convert_go_url) ;
       // сохраняем в событии имя файла PDF пакета документов - он после подписи будет показан и пойдет на печать
       $data=array('fname'=>$res['result_convert']['PDF_file_name'],'time_print'=>time()) ;
       update_rec_in_table('obj_site_events_pass',$data,'pkey='.$this->rec_event_to_personal['event_person_id']) ;
     }
  } */

}



?>
