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

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

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

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

    function panel_quiz_editor()
        {  ?><!-- This container will become the editable. -->
           <div class="document-editor">
               <div id="container">
                   <div id="sidebar"><div style="display:none;"><div id="root"></div></div></div>
                   <div class="document-editor__editable-container">
                       <div id="editor" class="document-editor__editable"><? if (!$this->rec_section['json_data']) echo $this->rec_section['manual'] ; ?></div>
                   </div>
                   <div class="clear"></div>
               </div>
               <div class="clear"></div>
           </div>
            <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`.
              ClassicEditor.create( document.getElementById( 'editor' ),
                      {   language: 'ru',
                          toolbar: {
                                  items: [
                                    'heading', 'insertTable', 'knowledgebase', 'bold', 'italic', 'link', 'undo', 'redo',  'bulletedList', 'numberedList'
                                  ]
                                },
                          table: {
                            contentToolbar: ['tableColumn', 'tableRow', 'mergeTableCells']
                          },
                          restrictedEditing: {
                                  allowedCommands: ['knowledgebase'],
                                  allowedAttributes: ['kbarticle']
                                },
                          blockToolbar: ['knowledgebase'],
                          readonly: true
                          //extraPlugins: [ 'ConvertDivAttributes' ],
                      }
               		)
               			.then( editor => {
               				window.editor = editor;
               				console.log('then') ;
               				var json_text='<?echo addslashes($this->rec_section['json_data'])?>' ;
                            console.log(json_text) ;

                            <?if ($this->rec_section['json_data']) {?>editor.setData(JSON.parse(json_text)) ;<?}


                            $known_recs=execSQL('select t1.*,t2.json_data,(select count(pkey) from quiz_questions t3 where t3.parent=t1.pkey) as count_questions 
                                                 from quiz_themes t1 
                                                 left join quiz_themes_text t2 on t2.parent=t1.pkey
                                                 where t1.parent'.$this->rec_section['level'].'='.$this->rec_section['pkey'].' and t1.clss=481 and t1.section_id='.$this->rec_section['pkey'].'
                                                 and t2.json_data!=""
               				                    ') ;
                            if (_sizeof($known_recs)) foreach($known_recs as $rec)
                            {   ?>var range=Range.fromJSON(JSON.parse('<?echo $rec['json_data']?>'),editor.model.document)
                                  editor.addSidebarLabel('<?echo $rec['obj_name']?> [<?echo $rec['count_questions']?>]',range,<?echo $rec['pkey']?>) ;
                                <?
                            }
                            ?>

               				editor.on('addArticle',editor_addArticle) ;

               			} )
               			.catch( error => {
               				console.error( 'There was a problem initializing the editor.', error );
               			} );

            }) ;

            </script><?
        }
}

?>
