<?php
include_once('c_personals_otdel_card.php');
class c_page_cab_personals_otdel_card_editor extends c_page_cab_personals_otdel_card
{
    public $h1='Карточка организации - редактировать';
    var $use_textarea_autosize=0 ;
    var $use_ckeditor5=0 ;

    function block_main()
    {   $this->h1.=' - редактировать' ;
        $this->page_title();
        //$this->panel_personals_org() ;
        $this->panel_otdel_edit($this->rec_section) ;
    }

    function panel_personals_org()
    {
        $arr_events=execSQL_row('select pkey,obj_name from obj_site_events_schedule where ext like "%quiz%" and enabled=1 and status!=5');

        //damp_array($rec_rasdel);
        $form=new i_form();
        $form->form_open();
        $form->row_open();
        $form->filter_section_input_text('section[name]','Название раздела',4,array('cur_value'=>$this->rec_section['obj_name']));
        if ($this->rec_section['level']==2) $form->filter_section_select('section[event_id]','Мероприятие раздела',2,$arr_events,array('cur_value'=>$this->rec_section['event_id']));
        $form->row_close();
        $form->row_open();
        ?><h4>Документ знания</h4><br><?
        $this->panel_quiz_editor();
        //$ckeditor_id=$form->textarea_ckeditor5('section[manual]',array('value'=>$this->rec_section['manual'],'style'=>'height:400px;'));
        $form->row_close();
        ?><br><?
        $form->row_open(['class'=>'center']);
        $form->button('Сохранить','quiz/section_save',['item_id'=>$this->rec_section['pkey'],'item_type'=>'theme','id'=>'button_save']);
        if ($this->rec_section['parent']>0) $form->button('Удалить раздел','quiz/get_panel_theme_delete',['item_id'=>$this->rec_section['pkey'],'item_type'=>'theme']);
        $form->button('Экспортировать раздел','quiz/get_panel_theme_export',['id'=>$this->rec_section['pkey'],'item_type'=>'theme']);
        $form->row_close();
        ?><input type="hidden" name="json_data" value="" id="json_data"><?
        $form->form_close();

        ?><script type="text/javascript">
        $j('button#button_save').on('click',function()
               {  $j('input#json_data').val(window.editor.getData()) ;
                   //send_ajax_request({cmd:'quiz/section_save',section_id:<?echo $this->rec_section['pkey']?>,json_data:window.editor.getData()});
               }) ;

        </script>
        <?
    }

    function panel_otdel_edit($rec,$options=array())
        { ?><div id="panel_otdel_edit"><form  enctype="multipart/form-data">
            <table class="info">
              <tr><th colspan="2">Реквизиты подразделения</th></tr>
             <tr><td>Наименование:</td><td><input type="text" _data-required  name="org[obj_name]" value="<?echo htmlspecialchars($rec['obj_name'])?>"></td></tr>
             <tr><td></td><td><br><br><input type="submit" class="v1" cmd="save_org" value="Сохранить" org_id="<?echo $rec['pkey']?>" validate="form"></td></tr>
             </table></form>
             <style type="text/css">
                  table.info{}
                  div#panel_otdel_edit table input[type="text"]{width:100%;}
                  div#panel_otdel_edit table textarea{width:700px;height:100px;font-size:13px;}
             </style>
           </div><?
        }

        function CMD_save_org()
         { //damp_array($_FILES) ;
           //damp_array($_POST) ;
           $res=PERSONAL()->save_org($this->rec_section['pkey'],$_POST['org']) ;
           $this->append_img_to_org() ;
           reboot_engine() ;

           //damp_array($_FILES) ;
           if (!$res) echo '<div class="red">Изменений не обнаружено</div>' ;
           else { echo '<div class="green">Данные сохранены</div>' ;
                  $this->rec_section=PERSONAL()->get_otdel_by_id($this->rec_section['pkey']) ;
                }
         }

       function append_img_to_org()
       {   $res_img=array() ;
           ob_start() ;
           damp_array($_FILES,1,-1) ;
           $text_files=ob_get_clean() ;
           ob_start() ;
           if (_sizeof($_FILES))
           { $recs_files=conver_FILES_to_recs($_FILES['upload_file']) ;  //damp_array($recs_files) ;
             if (_sizeof($recs_files[0]))
               { if ($recs_files[0]['type']=='image/gif')
                 { ?><h2>Загружаем изображение из файла "<?echo $recs_files[0]['name']?>"</h2><?
                   include_once(_DIR_TO_ENGINE.'/admin/i_admin_system.php') ;
                   execSQL_update('delete from obj_site_personals_image where parent='.$this->rec_section['pkey'].' and alt="stamp"') ;
                   $res_img=obj_upload_image($this->rec_section['_reffer'],$recs_files[0],array('view_upload'=>1,'img_alt'=>'stamp')) ;
                   //damp_array($res_img) ;
                   $res_img=array('type'=>'success','no_default_panel'=>1) ;
                 }
                 else $res_img=array('type'=>'error','code'=>'failed_type','text'=>'Неверный тип файла. Загрузите файл в формате GIF.') ;
               }
             //else $res_img=array('type'=>'error','code'=>'not_uploaded_file','text'=>'Не был выбран файл') ;
             if ($res_img['type']=='error') echo '<div class="alert">'.$res_img['text'].'</div>' ;
           }
           $text=ob_get_clean()  ;
           echo $text ;
           LOGS()->reg_log('Добавление печати организации',$text_files.'<br>'.$text,array('data'=>$res_img,'reffers'=>array($this->rec_section['_reffer']))) ;
           //else $res_img=array('type'=>'error','code'=>'not_uploaded_file','text'=>'Не был выбран файл') ;
       }

}

?>
