<?php
include_once('c_quiz_theme_card.php');
class c_page_cab_quiz_theme_card_editor extends c_page_cab_quiz_theme_card
{
    public $h1='Контроль знаний - карточка темы - редактировать';
    public $title=' - редактировать раздел';

    var $use_textarea_autosize=0 ;
    var $use_ckeditor5=0 ;

        function set_bottom_head_tags()
        {
            parent::set_bottom_head_tags() ;
            $this->HEAD['css'][]=_PATH_TO_ADDONS.'/ckeditor5-build-classic2/build/style.css' ;
            //$this->HEAD['css'][]=_PATH_TO_EXT.'/quiz/ckeditor_knowledgebase_local.css' ;
            $this->HEAD['js'][]=_PATH_TO_ADDONS.'/ckeditor5-build-classic2/build/ckeditor.js?rand='.ENGINE()->generate_UID() ;
        }


    function block_main()
    {
        $this->page_title();
        $this->panel_quiz_theme() ;
    }

    function panel_quiz_theme()
    {
        $arr_events=execSQL_row('select pkey,obj_name from obj_site_events_schedule where ext like "%quiz%" and enabled=1 and status!=5');
        if (!$this->rec_section['closing_perc']) $this->rec_section['closing_perc']=100 ;

        //damp_array($rec_rasdel);
        ?><div id="panel_filter"><?
        $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']));
        $form->filter_section_input_text('section[closing_perc]','Минимальное покрытие раздела, %',4,array('cur_value'=>$this->rec_section['closing_perc']));
        //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->button('Импортировать раздел','quiz/get_panel_theme_import',['id'=>$this->rec_section['pkey'],'item_type'=>'theme']);
        $form->row_close();
        ?><input type="hidden" name="json_data" value="" id="json_data"><?
        $form->form_close();
        ?></div><?
        ?><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_quiz_editor()
            {  ?><!-- This container will become the editable. -->
               <div class="document-editor">
                   <div id="container">
                       <div class="document-editor__editable-container">
                           <div id="editor" class="document-editor__editable"><?
                               if (!$this->rec_section['json_data']) echo $this->rec_section['manual'] ;

                               //echo $this->rec_section['manual']
                               ?></div>
                       </div>
                   </div>
               </div>
               <?echo ($this->rec_section['json_data'])? 'Используются данные из json_data':'Используются данные из manual'?>
                <script type="text/javascript">
                var section_id=<?echo $this->rec_section['pkey']?> ;
                $j(document).ready(function()
                { // Application data will be available under a global variable `appData`.

                  //import PasteFromOffice from '@ckeditor/ckeditor5-paste-from-office/src/pastefromoffice';

                  ClassicEditor.create( document.getElementById( 'editor' ),
                          {   language: 'ru',
                              toolbar: {
                                      items: [
                                        'heading', 'insertTable', 'bold', 'italic', 'link', 'undo', 'redo',  'bulletedList', 'numberedList',
                                        //'PasteFromOffice'
                                      ]
                                    },
                              table: {
                                contentToolbar: ['tableColumn', 'tableRow', 'mergeTableCells']
                              },
                              restrictedEditing: {
                                      allowedCommands: ['knowledgebase'],
                                      allowedAttributes: ['kbarticle']
                                    },
                              //blockToolbar: ['knowledgebase'],
                              //extraPlugins: [ 'PasteFromOffice' ],
                              //plugins: [ 'PasteFromOffice']
                          }
                   		)
                   			.then( editor => {
                   				window.editor = editor;
                   				console.log('then') ;
                                <?if ($this->rec_section['json_data']) {?>editor.setData(JSON.parse('<?echo addslashes($this->rec_section['json_data'])?>')) ;<?}?>
                   			} )
                   			.catch( error => {
                   				console.error( 'There was a problem initializing the editor.', error );
                   			} );

                }) ;

                </script><?

            }
}

?>
