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

/**
* @package OS CCK
* @copyright 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
*/

require_once(JPATH_SITE . "/components/com_os_cck/functions.php");
 
?>

<script type="text/javascript">
  document.addEventListener('DOMContentLoaded', function(){ 
    
    // document.getElementById('jform_link').value = 'index.php?option=com_os_cck&view=cck_layout'
    // console.log('11111111111111')
    
    if(!document.getElementById('instance').value){
      document.getElementById('instance').parentNode.parentNode.style.display = 'none';
    }
    if(!document.getElementById('category').value){
      document.getElementById('category').parentNode.parentNode.style.display = 'none';
    }
    document.getElementById('jform_params_layout_type').parentNode.parentNode.style.display = 'none';
    document.getElementById('jform_params_entity_type').parentNode.parentNode.style.display = 'none';


    if(document.getElementById('jform_params_layout_type').value == 'user_instances'){
      document.getElementById('userModal_jform_params_user_choise_class').parentNode.parentNode.style.display = '';
      document.getElementById('jform_params_user_choise_class-lbl').parentNode.parentNode.style.display = '';
      jQuery('#jform_params_user_choise_class_id').parents('.control-group').show()
    }else{
      
      if(document.getElementById('userModal_jform_params_user_choise_class')){
        document.getElementById('userModal_jform_params_user_choise_class').parentNode.parentNode.style.display = 'none';
        // console.log('222222222222222', document.getElementById('userModal_jform_params_user_choise_class'))
      }
      
      document.getElementById('jform_params_user_choise_class-lbl').parentNode.parentNode.style.display = 'none';
      // jQuery('#jform_params_user_choise_class_id').parents('.control-group').hide()
    }
    
    
    if(document.getElementById('jform_params_layout_type').value == 'category'){
        document.getElementById('instance').parentNode.parentNode.style.display = 'none';
        document.getElementById('category').parentNode.parentNode.style.display = '';
    }else{
        document.getElementById('instance').parentNode.parentNode.style.display = '';
        document.getElementById('category').parentNode.parentNode.style.display = 'none';
    }
    
    if(document.getElementById('jform_params_layout_type').value != 'instance'){
      document.getElementById('instance').parentNode.parentNode.style.display = 'none';
    }else{
      
      document.getElementById('instance').parentNode.parentNode.style.display = '';
    }

    if(document.getElementById('jform_params_layout_type').value == 'parent_child'){
      if(document.getElementById('jform_params_entity_type').value == 'joomla_user'){
        document.getElementById('userModal_jform_params_user_choise_class').parentNode.parentNode.style.display = '';
        document.getElementById('jform_params_user_choise_class-lbl').parentNode.parentNode.style.display = '';
      }else{
        document.getElementById('instance').parentNode.parentNode.style.display = '';
      }

    }

    if(document.getElementById('jform_params_show_type').value == '4'){
      document.getElementById('jform_params_hover_position').parentNode.parentNode.style.display = '';
    }else{
      document.getElementById('jform_params_hover_position').parentNode.parentNode.style.display = 'none';
    }
    jQuery('#jform_params_show_type').on('change', function(){
      if(jQuery(this).val() == 4){
        document.getElementById('jform_params_hover_position').parentNode.parentNode.style.display = '';
      }else{
        document.getElementById('jform_params_hover_position').parentNode.parentNode.style.display = 'none';  
      }
    })        


    if(document.getElementById('jform_params_layout_type').value == '1'){

      
    }else{
      var type = document.getElementById('jform_params_layout_type').value
      var fk_eid = document.getElementById("changeLink").getAttribute('fk_eid')
      
      if(type == 'instance' || type == 'parent_child'){
        document.getElementById("changeLink").setAttribute('src', "index.php?option=com_os_cck&task=show_instance_modal&layout_type="+type+"&fk_eid="
                                                      +fk_eid+"&tmpl=component");
      }
    }
    if(jQuery('[role=tablist] [aria-controls=attrib-params]').length > 0){
        if(jQuery('[role=tablist] [aria-controls=attrib-params]').attr('aria-expanded') == 'true'){
        
        jQuery('.layout-button').parent().append('<div class="arrow_box step-2"><div>Step 2</div></div>')
        
      }else{
        var html=  '<div class="arrow_box step-1"><div>Step 1</div></div>'
        jQuery('[role=tablist] [aria-controls=attrib-params]').append(html)
      }
      

      jQuery('[role=tablist] [aria-controls=attrib-params]').on('click', function(){
        jQuery('.arrow_box.step-1').hide()
        // if(document.getElementById('jform_params_layout_type').value == '1'){
          jQuery('.layout-button').parent().append('<div class="arrow_box step-2"><div>Step 2</div></div>')
        // }
      })  
    }else{
      // console.log('33333333333', jQuery('a[href=\#attrib-params]'))
      if(jQuery('a[href=\#attrib-params]').parent().hasClass('active')){
      
        jQuery('.layout-button').parent().append('<div class="arrow_box step-2"><div>Step 2</div></div>')
        
      }else{
        var html=  '<div class="arrow_box step-1"><div>Step 1</div></div>'
        jQuery('a[href=\#attrib-params]').parent().append(html)
      }
      

      jQuery('a[href=\#attrib-params]').on('click', function(){
        jQuery('.arrow_box.step-1').hide()
        // if(document.getElementById('jform_params_layout_type').value == '1'){
          jQuery('.layout-button').parent().append('<div class="arrow_box step-2"><div>Step 2</div></div>')
        // }
      })
    }
    
    

  }, false);
</script>
<?php
if (version_compare(JVERSION, "4.0.0-alpha10", "ge")) {
    JHtml::_('bootstrap.modal');
    JHtml::_('jquery.framework');
}else{
    JHTML::_('behavior.modal', 'a.modal-button');
}
$doc = JFactory::getDocument();
includeJavaScriptCCK('components/com_os_cck/assets/js/functions.js');
includeStylesCCK('components/com_os_cck/assets/css/admin_style.css');

if (version_compare(JVERSION, "4.0.0-alpha10", "ge")) {
    includeStylesCCK("components/com_os_cck/assets/css/admin_style-j4.css");
}else{
    includeStylesCCK("components/com_os_cck/assets/css/admin_style-j3.css");
}

  class JFormFieldLayouttype extends JFormField{
    protected function getInput(){
      $db = JFactory::getDBO();
      $input = JFactory::getApplication()->input;
      $module_id = $input->get('id','');
      $params_string = '';
      if($module_id !=''){//get current module's params; 
        $query = "SELECT params FROM #__modules WHERE id=".$module_id." ";
        $db->setQuery($query);
        $params_string = $db->loadResult();        
      }
      if(version_compare(JVERSION, '3.0', 'ge')) {
        $params = new JRegistry;
        $params->loadString($params_string);
      }
      else {
        $params = new JRegistry();
      }
      $lay_type = ($params->get('layout_type',''))? $params->get('layout_type') : '';
      return '<input id="layout_type" type="hidden" name='.$this->name.' value="'.$lay_type.'">';
    }
}

class JFormFieldLayout extends JFormField{
  protected function getInput(){
    $db = JFactory::getDBO();
    $input = JFactory::getApplication()->input;
    $module_id = $input->get('id','');
    $params_string = '';
    if($module_id !=''){//get current module's params; 
      $query = "SELECT params FROM #__modules WHERE id=".$module_id." ";
      $db->setQuery($query);
      $params_string = $db->loadResult();
    }
    if($input->get('id', '') != '') {
        $db->setQuery("SELECT `params` FROM `#__menu` WHERE `id` = ".$input->get('id', ''));
        $params = json_decode($db->loadResult());
    }
    else {
        $params = new JRegistry();
    }
    $link = JRoute::_('index.php?option=com_os_cck&task=manage_layout_modal&module_id='.$module_id.'&module=0&tmpl=component');
    $rel="{handler: 'iframe', size: {x: 900, y: 550}}";
    
    $lid = (isset($params->layout) && !empty($params->layout)) ? $params->layout : '';
    if (version_compare(JVERSION, "4.0.0-alpha10", "ge")) {
        $html = '<input id="selected_layout" type="text" name="'.$this->name.'" value="'.$lid.'" readonly>';
        $html .= '<div class="fixedform">'.
                    '<a class="btn modal-button btn-primary layout-button" href="'.$link.'" data-bs-toggle="modal" data-bs-target="#layouts">'.
                      'Select layout'.
                    '</a>'.
                  '</div>';
        $html .= '<div class="modal hide" id="layouts">
            <div class="modal-dialog modal-xl modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-body">
                        <iframe src="' . $link .'" width="900" height="550"></iframe>
                    </div>
                    <div class="modal-footer">
                        <a href="#" id="close_layout_modal" class="btn btn-info btn-primary" data-bs-dismiss="modal">
                            Cancel
                        </a>
                    </div>
                </div>
                
            </div>
        </div>';
    }else{
        $html = '<input id="selected_layout" type="text" name="'.$this->name.'" value="'.$lid.'" readonly>';
        $html .= '<div class="fixedform">'.
                    '<a class="btn modal-button btn-primary layout-button" href="'.$link.'" rel="'.$rel.'">'.
                      'Select layout'.
                    '</a>'.
                  '</div>';
    }
    
    return $html;
  }
}

class JFormFieldInstance extends JFormField{
  protected function getInput(){
    $db = JFactory::getDBO();
    $input = JFactory::getApplication()->input;
    $module_id = $input->get('id','');
    $params_string = '';
    if($module_id !=''){//get current module's params; 
      $query = "SELECT params FROM #__modules WHERE id=".$module_id." ";
      $db->setQuery($query);
      $params_string = $db->loadResult();
    }
    if($input->get('id', '') != '') {
        $db->setQuery("SELECT `params` FROM `#__menu` WHERE `id` = ".$input->get('id', ''));
        $params = json_decode($db->loadResult());
    }
    else {
        $params = new JRegistry();
    }
    $selected_entity = $eiid = ''; 
    //     if(isset($params->instance_layout) && !empty($params->instance_layout)){
    if(isset($params->layout) && !empty($params->layout)){
      $selected_entity = $db->loadResult($db->setQuery("SELECT tt.eid FROM `#__os_cck_layout` AS cl "
                                                            . "\n LEFT JOIN #__os_cck_entity AS tt ON cl.fk_eid = tt.eid "
                                                            . "\n WHERE cl.lid=".$params->layout));
      $eiid = $params->instance;
      
    }
    $ceid = ($selected_entity)? '&fk_eid='.$selected_entity : '';
    $link = JRoute::_('index.php?option=com_os_cck&task=show_instance_modal'.$ceid.'&tmpl=component');
    $rel="{handler: 'iframe', size: {x: 900, y: 550}}";
    if (version_compare(JVERSION, "4.0.0-alpha10", "ge")) {
        $html = '<input id="instance" type="text" name="'.$this->name.'" value="'.$eiid.'" readonly>';
        $html .= '<div class="fixedform">'.
                    '<a class="btn modal-button btn-primary instance-button" href="'.$link.'" data-bs-toggle="modal" data-bs-target="#instances" >'.
                      'Select instance'.
                    '</a>'.
                  '</div>';
        $html .= '<div class="modal hide" id="instances">
            <div class="modal-dialog modal-xl modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-body">
                        <iframe id="changeLink" dataSrc="' . $link .'" width="900" height="550" fk_eid="'.$selected_entity.'"></iframe>
                    </div>
                    <div class="modal-footer">
                        <a href="#" id="close_layout_modal" class="btn btn-info btn-primary" data-bs-dismiss="modal">
                            Cancel
                        </a>
                    </div>
                </div>
                
            </div>
        </div>';
    }else{
        $html = '<input id="instance" type="text" name="'.$this->name.'" value="'.$eiid.'" readonly>';
        $html .= '<div class="fixedform">'.
                    '<a id="changeLink" class="btn modal-button btn-primary instance-button" href="'.$link.'" rel="'.$rel.'">'.
                      'Select instance'.
                    '</a>'.
                  '</div>';
    }
    
    return $html;
  }
}

class JFormFieldCategoryCck extends JFormField{   

  protected function getInput(){
    $input = JFactory::getApplication()->input;
    $db = JFactory::getDBO();
    $menuId = 0;
    if($input->get('id', '') != '') {
        $db->setQuery("SELECT `params` FROM `#__menu` WHERE `id` = ".$input->get('id', ''));
        $params = json_decode($db->loadResult());
    }
    $selected_entity = $eiid = '';
    if(isset($params->layout) && !empty($params->layout)){
      $selected_entity = $db->loadResult($db->setQuery("SELECT tt.eid FROM `#__os_cck_layout` AS cl "
                                                            . "\n LEFT JOIN #__os_cck_entity AS tt ON cl.fk_eid = tt.eid "
                                                            . "\n WHERE cl.lid=".$params->layout));
      $eiid = $params->category;
    }
    $ceid = ($selected_entity)? '&fk_eid='.$selected_entity : '';
    $link = JRoute::_('index.php?option=com_os_cck&task=show_categories_modal'.$ceid.'&tmpl=component');
    $rel="{handler: 'iframe', size: {x: 900, y: 550}}";
    if (version_compare(JVERSION, "4.0.0-alpha10", "ge")) {
        $html = '<input id="category" type="text" name="'.$this->name.'" value="'.$eiid.'" readonly>';
        $html .= '<div class="fixedform">'.
                    '<a class="btn modal-button btn-primary category-button" href="'.$link.'" data-bs-toggle="modal" data-bs-target="#categories">'.
                      'Select category'.
                    '</a>'.
                  '</div>';
        $html .= '<div class="modal hide" id="categories">
            <div class="modal-dialog modal-xl modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-body">
                        <iframe id="changeLink2" src="' . $link .'" width="900" height="550"></iframe>
                    </div>
                    <div class="modal-footer">
                        <a href="#" id="close_layout_modal" class="btn btn-info" data-bs-dismiss="modal">
                            ' . cck_lang_text('COM_OS_CCK_CANCEL') .'
                        </a>
                    </div>
                </div>
                
            </div>
        </div>';
    }else{
        $html = '<input id="category" type="text" name="'.$this->name.'" value="'.$eiid.'" readonly>';
        $html .= '<div class="fixedform">'.
                    '<a id="changeLink2" class="btn modal-button category-button" href="'.$link.'" rel="'.$rel.'">'.
                      'Select category'.
                    '</a>'.
                  '</div>';
    }
    return $html;
  }

  
}


