<?php
defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/
if(isset($layout_params['fields']['showName_'.$field->db_field_name]) &&
  $layout_params['fields']['showName_'.$field->db_field_name] == 'on'){
  $layout_html = str_replace($field->db_field_name.'-label-hidden', '', $layout_html);
}
$offset_animation = get_field_offset_animation($field, $layout);
$fName = $field->db_field_name;

$icon_alias_prefix = (isset($field_from_params[$fName.'_add_icon_alias_prefix'])) ? $field_from_params[$fName.'_add_icon_alias_prefix'] : '';
if($icon_alias_prefix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_prefix_hidden', 'fa '.$icon_alias_prefix, $layout_html);
}
$icon_alias_suffix = (isset($field_from_params[$fName.'_add_icon_alias_suffix'])) ? $field_from_params[$fName.'_add_icon_alias_suffix'] : '';
if($icon_alias_suffix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_suffix_hidden', 'fa '.$icon_alias_suffix, $layout_html);
}

$sceditor_toolbar = (isset($field_from_params[$fName.'_sceditor_toolbar'])) ? $field_from_params[$fName.'_sceditor_toolbar'] : array();
if(!empty($sceditor_toolbar)){
    $sceditor_toolbar = implode(',', $sceditor_toolbar);
}else{
    $sceditor_toolbar = '';
}
//isset($layout_params['fields']['description_'.$fName]) && $layout_params['fields']['description_'.$fName]=='on' && !empty($layout_params['fields'][$fName.'_tooltip']
//var_dump((isset($layout_params['fields']['description_'.$fName]) && $layout_params['fields']['description_'.$fName]=='on' && !empty($layout_params['fields'][$fName.'_tooltip'])));
//var_dump($layout_params['fields'][$fName.'_tooltip']);
$class='';
$required = '';
if(isset($field_from_params[$fName.'_required']) && $field_from_params[$fName.'_required']=='on')
    $required = ' required ';

if(!$field_from_params[$fName.'_editor_field'] || $field_from_params[$fName.'_editor_field'] == '0'){
    $class = 'editor-area '; ?>
  <span <?php if(isset($layout_params['fields']['description_'.$fName]) && $layout_params['fields']['description_'.$fName]=='on' && !empty($layout_params['fields'][$fName.'_tooltip']))
        {?>
rel="tooltip" data-toggle="tooltip" data-placement="top" title="<?php echo $layout_params['fields'][$fName.'_tooltip'];?>"
    <?php } ?> >
    <textarea <?php echo $layout_params['field_styling']?> 
                placeholder="<?php echo $field_from_params[$fName.'_placeholder']?>" 
                class="<?php echo $class.$layout_params['custom_class'].$required . $field->db_field_name?> text_area" 
                <?php echo $offset_animation; ?>
                type="text" 
                name="fi_<?php echo $field->db_field_name?>"><?php echo $value?></textarea>
                </span>
<?php
}
else{
    
    //var_dump($editor);?>
<span <?php if(isset($layout_params['fields']['description_'.$fName]) && $layout_params['fields']['description_'.$fName]=='on' && !empty($layout_params['fields'][$fName.'_tooltip']))
        {?>
rel="tooltip" data-toggle="tooltip" data-placement="top" title="<?php echo $layout_params['fields'][$fName.'_tooltip'];?>"
    <?php } ?> >
    <div class="<?php echo $layout_params['custom_class'] . ' ' . $required; ?> " <?php echo $offset_animation; ?> data_field_name="fi_<?php echo $field->db_field_name?>">
        <?php
        $editor = JFactory::getConfig()->get('editor');
        
        if($field_from_params[$fName.'_editor_field'] =='1' && $editor != 'none'){
            $editor = JEditor::getInstance($editor);
            echo $editor->display( "fi_" . $field->db_field_name, $value, '95%', '250', '5', '5');
            ?>
            <!-- <script>
                window.addEventListener('load', function() {
                    var editor = tinyMCE.activeEditor.isEnabled
                    console.log('888888888888888888', editor)
                    //     editor.Event('show', function(){
                    //     console.log('888888888888888888')
                    // })
                    // console.log('55555555555555555', tinyMCE.activeEditor)
                    jQuerOs('#fi_<?php echo $fName; ?>').on('change', function(){
                        console.log('7777777777777777777')
                    })
                })
            </script> -->
            <?php
        }elseif($field_from_params[$fName.'_editor_field'] == '2'){
            $doc->addStyleSheet("https://cdn.jsdelivr.net/npm/sceditor@3/minified/themes/default.min.css");
            $doc->addScript("https://cdn.jsdelivr.net/npm/sceditor@3/minified/sceditor.min.js");
            ?>
            <textarea <?php echo $layout_params['field_styling']?> 
                placeholder="<?php echo $field_from_params[$fName.'_placeholder']?>" 
                class="editor-area <?php echo $layout_params['custom_class'].$required . $field->db_field_name?> text_area" 
                <?php echo $offset_animation; ?>
                type="text" 
                name="fi_<?php echo $field->db_field_name?>"
                id=<?php echo $field->db_field_name; ?>><?php echo htmlspecialchars($value)?></textarea>
                
                <script>
                    window.addEventListener('load', function() {
                        // var textarea = document.getElementById('example');
                        textarea = jQuerOs('textarea.<?php echo $field->db_field_name; ?>');
                        
                        var sceditor_toolbar = '<?php echo $sceditor_toolbar; ?>'
                        // sceditor_toolbar = ''
//                         console.log('777777777777', textarea)
                        textarea = textarea[0]
//                        console.log('444444444444444', textarea)
                        if(typeof text_area_arr == 'undefined'){
                            text_area_arr = []
                        }

                        if(typeof sceditors_obj == 'undefined'){
                            sceditors_obj = {};
                        }

                        
                        text_area_arr.push('<?php echo $field->db_field_name; ?>')
                        // console.log('11111111111111', textarea)
                        var <?php echo $field->db_field_name; ?>sceditor = sceditor
                        
                        // console.log('555555555555555', <?php echo $field->db_field_name; ?>)
                        <?php echo $field->db_field_name; ?>sceditor.create(textarea, {
                            format: 'bbcode',
                            style: 'https://cdn.jsdelivr.net/npm/sceditor@3/minified/themes/content/default.min.css',
                            height: '250px',
                            emoticonsRoot: '<?php echo JURI::root(); ?>components/com_os_cck/assets/images/' ,
                            autoUpdate: true,
                            <?php if($sceditor_toolbar != ''){ ?>
                            toolbar: sceditor_toolbar,
                            <?php } ?>
                        });
                        // <?php echo $field->db_field_name; ?>.changeTextarea
                        sceditors_obj['<?php echo $field->db_field_name; ?>'] = <?php echo $field->db_field_name; ?>sceditor
                    })
                </script>
                <?php
        }else{
            
        ?>
        <textarea <?php echo $layout_params['field_styling']?> 
                placeholder="<?php echo $field_from_params[$fName.'_placeholder']?>" 
                class="editor-area <?php echo $layout_params['custom_class'].$required . $field->db_field_name?> text_area" 
                <?php echo $offset_animation; ?>
                type="text" 
                name="fi_<?php echo $field->db_field_name?>"><?php echo $value?></textarea>
                
    <script src="//cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js"></script>
        <script>
            tinymce.init({
                selector: '.editor-area',
                height: 300,
                plugins: [
                  'advlist autolink lists link image charmap print preview anchor',
                  'searchreplace visualblocks fullscreen',
                  'insertdatetime table contextmenu paste'
                ],
                toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code',
            });
        </script>
        <?php } ?>
    </div>
</span>
    <?php
}

