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

/**
* @version 1.0
* @package OS CCK
* @copyright 2015 OrdaSoft
* @author 2015 Andrey Kvasnevsliy(akbet@mail.ru)
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @description OrdaSoft Content Construction Kit 
* @license GNU General Public license version 2 or later; 
*/
?>
<script type="text/javascript">
  document.addEventListener('DOMContentLoaded', function(){ 
    if(!document.getElementById('instance').value){
      document.getElementById('instance').parentNode.parentNode.style.display = 'none';
      document.getElementById('jform_params_cart_type').parentNode.parentNode.style.display = 'none';
    }
    document.getElementById('layout_type').parentNode.parentNode.style.display = 'none';
    document.getElementById('jform_params_entity_type').parentNode.parentNode.style.display = 'none';
    if(document.getElementById('layout_type').value == 'add_instance' 
        || document.getElementById('layout_type').value == 'request_instance'){
      document.getElementById('jform_params_show_type').parentNode.parentNode.style.display = '';
    }else{
      document.getElementById('jform_params_show_type').parentNode.parentNode.style.display = 'none';
    }

    if(document.getElementById('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 = '';
    }else{
//        console.log('1111111111111', document.getElementById('userModal_jform_params_user_choise_class'))
//        console.log('2222222222222', document.getElementById('jform_params_user_choise_class-lbl'))
        if(document.getElementById('userModal_jform_params_user_choise_class') != null){
            document.getElementById('userModal_jform_params_user_choise_class').parentNode.parentNode.style.display = 'none';
        }
        if(document.getElementById('jform_params_user_choise_class-lbl') != null){
            document.getElementById('jform_params_user_choise_class-lbl').parentNode.parentNode.style.display = 'none';
        }
    }
    
    if(document.getElementById('layout_type').value == 'cart'){
        document.getElementById('jform_params_cart_type').parentNode.parentNode.style.display = '';
    }else{
        document.getElementById('jform_params_cart_type').parentNode.parentNode.style.display = 'none';
    }

    if(document.getElementById('layout_type').value == 'category'){
        document.getElementById('category').parentNode.parentNode.style.display = '';
    }else{
        document.getElementById('category').parentNode.parentNode.style.display = 'none';
    }

    if(document.getElementById('layout_type').value == 'instance'){
      document.getElementById('instance').parentNode.parentNode.style.display = '';
    }else{
      document.getElementById('instance').parentNode.parentNode.style.display = 'none';
    }
    if(document.getElementById('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 = '';
      }

    }
    // console.log('111111111', document.getElementById('layout_type').value == '')  
    if(document.getElementById('layout_type').value == ''){
      var html=  '<div class="arrow_box step-1"><div>Step 1</div></div>'
      jQuery('.layout-button').parent().append(html)
      
    }else{
        if(document.getElementById('jform_params_layout_type') != null){
            var type = document.getElementById('jform_params_layout_type').value
        }else if(document.getElementById('layout_type') != null){
            var type = document.getElementById('layout_type').value
        }else{
            var type = ''
        }
      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");
      }
    }

    document.getElementById('toolbar-apply').addEventListener('click', function(){
      
      var lay_type = document.getElementById('layout_type').value;
      var instance = document.getElementById('instance').value;
      var category = document.getElementById('category').value;
      if((lay_type == 'instance' || lay_type == 'parent_child') && instance == ''){
        alert('Please. Select an instance to display.');
        return false;
      }

      if(lay_type == 'category' && category == ''){
        alert('Please. Select a category to display.');
        return false;
      }
        
    })

  }, 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();
includeStylesCCK("components/com_os_cck/assets/css/admin_style.css");
includeJavaScriptCCK("components/com_os_cck/assets/js/functions.js");


  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 JParameter($params_string);
      }
      $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(version_compare(JVERSION, '3.0', 'ge')) {
      $params = new JRegistry;
      $params->loadString($params_string);
    }
    else {
      $params = new JParameter($params_string);
    }
    $link = JRoute::_('index.php?option=com_os_cck&task=manage_layout_modal&module_id='.$module_id.'&tmpl=component');
    $rel="{handler: 'iframe', size: {x: 900, y: 550}}";
    $lid = ($params->get('layout',''))? $params->get('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(version_compare(JVERSION, '3.0', 'ge')) {
      $params = new JRegistry;
      $params->loadString($params_string);
    }
    else {
      $params = new JParameter($params_string);
    }
    $selected_entity = $eiid = ''; 
    if($params->get('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->get('layout')));
      $eiid = $params->get('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"></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(){
    
    $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 JParameter($params_string);
    }
    
    // if($input->get('id', '') != '') {
    //     $db->setQuery("SELECT `params` FROM `#__menu` WHERE `id` = ".$input->get('id', ''));
    //     $params = json_decode($db->loadResult());
    // }
    $selected_entity = $eiid = '';
    if($params->get('layout') && !empty($params->get('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->get('layout')));
      $eiid = $params->get('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">
                            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;
  }
}