<?php
/**
* @package OrdaSoft Joomla 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 Joomla Content Construction Kit
*/

defined('_JEXEC') or die;

includeStylesCCK("components/com_os_cck/assets/css/front_end_style.css");

//timepicker
$rand = rand();
$redirect = '';
// var_dump($params);exit;
  
if( !isset( $show_type ) && isset($menu_params) && $menu_params && $menu_params->get('show_type','')){
  $show_type = $menu_params->get('show_type');
}elseif(isset($layout_params['show_type'])){
  $show_type = $layout_params['show_type'];
}else{
  $show_type = 1;
}
$count = 0;
?>
<div class="cck-body">

  <script type="text/javascript" >
    var checkExpr = [];
  </script>

  <?php
  $hidden = '';

  if(!$layout) {
    echo "</div>"; //close <div class="cck-body">
    return;
  }

  if(isset($layout_params['views']['show_layout_title']) 
    && $layout_params['views']['show_layout_title']){
    echo "<h3>";
      echo $layout_params['views']['layout_title'];
    echo "</h3>";
  }

  $fields_list = $layout->field_list;
  $layout_type = $layout->type;
  $unique_form_id = $layout_type.'_Form_'.$layout->parent_eiid.$moduleId.rand();
  $field_from_params = $layout_params['fields'];
  $from = false;
  $to = false;
  //var_dump($show_type); exit;
  if($show_type == 3){
    $Itemid  = protectInjectionWithoutQuote('Itemid');
    if(isset($layout->fk_eid)){
      $link_eiid = "&eiid=$layout->parent_eiid";
    }
    $link = JRoute::_(JURI::root()."index.php?option=com_os_cck&task=show_request_layout$link_eiid&lid=".$layout->lid."&Itemid=$Itemid");
    $div_styling = isset($button_style['div_styling']) ? $button_style['div_styling'] : ''; 
     $field_styling = isset($button_style['field_styling']) ? $button_style['field_styling'] : '';
     $custom_class = isset($button_style['custom_class']) ? $button_style['custom_class'] : '';
     $offset_animation = isset($button_style['offset_animation']) ? $button_style['offset_animation'] : '';
    ?>
  <div <?php echo $div_styling; ?>>
      <a <?php echo $field_styling;?>  class="button btn-info <?php echo $custom_class;?>" <?php echo $offset_animation; ?> href="<?php echo $link ?>">
        <?php echo (isset($layout_params['button_name']) && $layout_params['button_name'])?$layout_params['button_name'] : cck_lang_text('COM_OS_CCK_BUTTON_SHOW_FORM_SAVE'); ?>
      </a>
  </div>
    <?php
    echo "</div>"; //close <div class="cck-body">
    return;
  }



  if($show_type == 2){
     $div_styling = isset($button_style['div_styling']) ? $button_style['div_styling'] : ''; 
     $field_styling = isset($button_style['field_styling']) ? $button_style['field_styling'] : '';
     $custom_class = isset($button_style['custom_class']) ? $button_style['custom_class'] : '';
     $offset_animation = isset($button_style['offset_animation']) ? $button_style['offset_animation'] : '';?>
  <div <?php echo $div_styling; ?>>
    <input <?php echo $field_styling;?>  type="button" class="button btn btn-info <?php echo $custom_class;?>" <?php echo $offset_animation; ?> value="<?php echo (isset($layout_params['button_name']) && $layout_params['button_name']) ? $layout_params['button_name'] : cck_lang_text('COM_OS_CCK_BUTTON_SHOW_FORM_SAVE'); ?>"
          onclick="jQuerOs('#request_wrapper_<?php echo $layout_type?><?php echo $unique_form_id; ?><?php echo ($moduleId) ? '_cckmod_'.$moduleId : '' ;?>').stop().fadeToggle();"/>
  </div>
    <div class="rent_request_wrapper" id="request_wrapper_<?php echo $layout_type?><?php echo $unique_form_id; ?><?php echo ($moduleId) ? '_cckmod_'.$moduleId : '' ;?>" style="display: none;">
    <?php
  }


  if($show_type == 4){
     $div_styling = isset($button_style['div_styling']) ? $button_style['div_styling'] : ''; 
     $field_styling = isset($button_style['field_styling']) ? $button_style['field_styling'] : '';
     $custom_class = isset($button_style['custom_class']) ? $button_style['custom_class'] : '';
     $offset_animation = isset($button_style['offset_animation']) ? $button_style['offset_animation'] : '';?>
  <div <?php echo $div_styling; ?> class="col_box cck_hover_layout">
    <input <?php echo $field_styling;?> type="button" class="button btn btn-info <?php echo $custom_class;?>" <?php echo $offset_animation; ?> value="<?php echo (isset($layout_params['button_name']) && $layout_params['button_name'])?$layout_params['button_name'] : cck_lang_text('COM_OS_CCK_BUTTON_SHOW_FORM_SAVE'); ?>"/>
  
  <ul class="os_cck_menu_child os_cck_hover_child">
    
    <?php
  }
  
  if($show_type == 5){
     $div_styling = isset($button_style['div_styling']) ? $button_style['div_styling'] : ''; 
     $field_styling = isset($button_style['field_styling']) ? $button_style['field_styling'] : '';
     $custom_class = isset($button_style['custom_class']) ? $button_style['custom_class'] : '';
     $offset_animation = isset($button_style['offset_animation']) ? $button_style['offset_animation'] : '';?>
  <div <?php echo $div_styling; ?> class="col_box">
    <button <?php echo $field_styling;?> type="button" class="button btn btn-info <?php echo $custom_class;?>" <?php echo $offset_animation; ?>  data-toggle="os_cck_modal" data-target="#show_add_layout_<?php echo $rand; ?>">
        <?php echo (isset($layout_params['button_name']) && $layout_params['button_name'])?$layout_params['button_name'] : cck_lang_text('COM_OS_CCK_BUTTON_SHOW_FORM_SAVE'); ?>
    </button>
  
    <div class="fade os_cck_modal" id="show_add_layout_<?php echo $rand; ?>" tabindex="-1" role="dialog" aria-hidden="true">
      <div class="os_cck_modal-dialog os_cck_modal-dialog-centered" role="document">
        <div class="os_cck_modal-content">
          <div class="os_cck_modal-header">
            <button type="button" class="close" data-dismiss="os_cck_modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="os_cck_modal-body">
            
          
    
    <?php
  }
  
  $field = new stdClass();
  $field->db_field_name = 'form-'.$layout->lid;
  $form_styling = get_field_styles($field, $layout);
  $form_custom_class = get_field_custom_class($field, $layout);
  $form_offset_animation = get_field_offset_animation($field, $layout);
  $form_hover_animation = get_field_hover_animation($field, $layout);
  $layout_html = urldecode($layout->layout_html);

  
  //add child selects fields to layout (for dependences fields)
  $addChildSelectToLayout = addChildSelectToLayout($fields_list, $entityInstance, $layout_params, $layout_html, $layout);
  $layout_html = $addChildSelectToLayout['layout_html'];
  
  //remove only after addChildSelectToLayout !!!!!!
  $layout_html = cut_params($layout_html);
  $layout_html = str_replace('data-label-styling', 'style',  $layout_html);
  $layout_html = remove_admin_classes($layout_html);

  $layout_params = $addChildSelectToLayout['layout_params'];
  $parent = $addChildSelectToLayout['select_parent'];
  $layout->params = serialize($layout_params);
  $child_entities = (isset($layout_params['child_entities'])) ? $layout_params['child_entities'] : '';


  $robots_option[]  = JHTML::_('select.option','0','Use Global');
  $robots_option[]  = JHTML::_('select.option','1','Index, Follow');
  $robots_option[]  = JHTML::_('select.option','2','No index, follow');
  $robots_option[]  = JHTML::_('select.option','3','Index, No follow');
  $robots_option[]  = JHTML::_('select.option','4','No index, no follow');
  
  $layout_html = $layout->getEditInstanceLayoutViews($layout, $layout_params, $entityInstance, $option, $robots_option, $unique_form_id, $Itemid);
  //end: add child selects to layout





  if($show_type == 2) echo "</div>";
  if($show_type == 4) echo "</ul></div>";
  if($show_type == 5){ ?>
      </div>
          
        </div>
      </div>
    </div>
  </div>
  <?php } ?>

  <script type="text/javascript" >
    loadAddLayout();
    var send_request = null;
    var send_buy_request = null;
    var review_captcha_request = null;
    var check_captcha_request_<?php echo $layout->type?> = null;
    
    const captchaKey<?php echo $unique_form_id; ?>  = Joomla.getOptions('plg_captcha_recaptcha_os.siteKey', '');
    const actionSelector<?php echo $unique_form_id; ?> = 'input.plg-captcha-recaptcha-os-action';
    const answerSelector<?php echo $unique_form_id; ?> = 'input.plg-captcha-recaptcha-os-hidden';
    const findAction<?php echo $unique_form_id; ?> = function (form) {
            if (form.hasAttribute('class') && form.getAttribute('class') !== '') {
                    let matchClass;
                    form.getAttribute('class').split(/\s+/).forEach((className) => {
                            if (className.match(/^(com|mod|plg)\-/)) {
                                    matchClass = className;
                            }
                    });
                    if (matchClass) {
                            return matchClass;
                    }
            }
            if (form.hasAttribute('id') && form.getAttribute('id') !== '') {
                    return form.getAttribute('id');
            }
            if (form.hasAttribute('name') && form.getAttribute('name') !== '') {
                    return form.getAttribute('name');
            }
            return 'submit';
    }
    const getAction<?php echo $unique_form_id; ?> = form => findAction<?php echo $unique_form_id; ?>(form).replace(/[^a-z0-9]+/gi, '_');
    (function ($) {
      check_captcha_request_<?php echo $layout->type?> = function (moduleId,form,type) {
        var status = "";
        if(moduleId){
          form=form+'_'+moduleId;
        }
        $.post("index.php?option=com_os_cck&captcha_type="+type+"&task=checkCaptcha&format=raw", $(form.keyguest).serialize(),
        function (data) {
          if (data.status == '<?php echo cck_lang_text("COM_OS_CCK_SUCCESS"); ?>') {
            form.submit();
          }else{
            reload_captcha_<?php echo $layout->type?>(moduleId,type);
            jQuerOs("[name='keyguest']").val('');
            alert("<?php echo cck_lang_text("COM_OS_CCK_INFOTEXT_JS_WRONG_CAPTCHA");?>");
          }
        } , 'json' );
      }
    })(jQuerOs);

    // (function ($) { 10_12_2025
    //   check_recaptcha_request_<?php echo $layout->type?> = function (moduleId,form,type) {
    //     var status = "";
    //     if(moduleId){
    //       form=form+'_'+moduleId;
    //     }
    //     $.post("index.php?option=com_os_cck&captcha_type="+type+"&task=checkCaptcha&format=raw", $(form.keyguest).serialize(),
    //     function (data) {
    //       if (data.status == '<?php echo cck_lang_text("COM_OS_CCK_SUCCESS"); ?>') {
    //         form.submit();
    //       }else{
    //         reload_captcha_<?php echo $layout->type?>(moduleId,type);
    //         jQuerOs("[name='keyguest']").val('');
    //         alert("<?php echo cck_lang_text("COM_OS_CCK_INFOTEXT_JS_WRONG_CAPTCHA");?>");
    //       }
    //     } , 'json' );
    //   }
    // })(jQuerOs);

    

  //*****************   begin add for show/hiden button "Add review" ********************
    

    function sendRequest<?php echo $unique_form_id; ?> (task,form_name, button_name){
        var submitEvent = event
        //var form = jQuerOs("#"+form_name );
        var form = document.forms[form_name]; 

//      console.log('66666666666666', submitEvent)
//      return
      if(task == 'cancel_instance'){
        
        document.location.href = redirect_arr[button_name]
        return
      
      }
      
    

      jQuerOs('textarea[id*=fi_text_textarea_].joomla-editor-tinymce').each(function(index, el) {
        if(jQuerOs(el).css('display') != 'none'){
          jQuerOs(el).parent().find('.js-tiny-toggler-button').trigger('click')    
        }
        
      })
      checkUploadedFiles(form, <?php echo return_bytes(ini_get('post_max_size')) ?>, <?php echo return_bytes(ini_get('upload_max_filesize')) ?>, <?php echo ini_get('file_uploads') ?>,
        "<?php echo cck_lang_text('COM_OS_CCK_SETTINGS_VIDEO_ERROR_UPLOAD_OFF'); ?>", "<?php echo cck_lang_text('COM_OS_CCK_SETTINGS_VIDEO_ERROR_POST_MAX_SIZE'); ?>", "<?php echo cck_lang_text('COM_OS_CCK_SETTINGS_VIDEO_ERROR_UPLOAD_MAX_SIZE'); ?>",
        "<?php echo cck_lang_text('COM_OS_CCK_ADMIN_INFOTEXT_JS_TRACK_KIND'); ?>", "<?php echo cck_lang_text('COM_OS_CCK_ADMIN_INFOTEXT_JS_TRACK_LANGUAGE'); ?>", "<?php echo cck_lang_text('COM_OS_CCK_ADMIN_INFOTEXT_JS_TRACK_TITLE'); ?>")
      
        jQuerOs('#recaptcha iframe').css('border', 'none')
        jQuerOs('#recaptcha iframe').css('border-color', 'none')
//        console.log('111111111111111111', text_area_arr)
        
        if(typeof sceditor != 'undefined'){
//            console.log("44444444444444", text_area_arr)
          for(var i=0; i < text_area_arr.length; i++){
            var editor = window[text_area_arr[i]]
//            console.log('1111111111111111', text_area_arr)
//            var value = editor.instance(textarea).val()
            var value = sceditor.instance(editor).val()
//            console.log('1111111111111111', value)
            var textarea_field = jQuerOs('[name=fi_'+text_area_arr[i]+']')
            if(textarea_field.length >0){
              jQuerOs(textarea_field[0]).val(value)
            }

            
          }

        }
        
        ret = false;
        if(!ret) checkRequireFields(form_name);
        if(!ret) checkStepNumberFields();
        if(!ret) check_reg_expr(form_name);
             
        if(ret){
          if(ret_scroll != -1){
            window.scrollTo(0,ret_scroll);
          }
          return;
        }

        

        type = 'add_instance';
        jQuerOs(form).find('#task').val(task);
        jQuerOs(form).find('#redirect').val(redirect_arr[button_name]);
        // console.log('1111111111111111', redirect_arr[button_name]);
        // return;
        if(form.querySelector('div[id^="captcha-block"]')){
          check_captcha_request_<?php echo $layout->type?>(0,form,type);
        }else if(form.querySelector('#recaptcha')){
          var actionElement = jQuerOs(form).find(actionSelector<?php echo $unique_form_id; ?>);
          if(actionElement.length > 0){  
                grecaptcha.ready(function () {
      
                      var actionElement = jQuerOs(form).find(actionSelector<?php echo $unique_form_id; ?>);
                      if(actionElement.length > 0){
                          actionElement = actionElement[0]
                      }
      
                      actionElement.value = getAction<?php echo $unique_form_id; ?>(submitEvent.target);

                      grecaptcha.execute(captchaKey<?php echo $unique_form_id; ?>, {action: actionElement.value}).then(function (token) {
                          jQuerOs(form).find(actionSelector<?php echo $unique_form_id; ?>).val(token)
//                          console.log('11111111111111', jQuerOs(form).find(actionSelector<?php echo $unique_form_id; ?>))
//                          return
//                              submitEvent.target.querySelector(answerSelector<?php echo $unique_form_id; ?>).value = token;
//                              submitEvent.target.submit();
                              form.submit();    
                      });
              });
          }else{
          
                var recaptcha_count = jQuerOs(form).find('.recaptcha_field').attr('recaptcha_count')
                var recaptcha_name = jQuerOs(form).find('[name=recaptcha_namespace]').attr('value')
                var response = grecaptcha.getResponse(recaptcha_count);

                  if(response.length > 0){
                    form.submit();    
                  }else{
                    var target_top= jQuerOs('#recaptcha').offset().top;
                    jQuerOs('html, body').animate({scrollTop:target_top}, 'slow');
                    jQuerOs('#recaptcha iframe').css('border', '1px solid')
                    jQuerOs('#recaptcha iframe').css('border-color', 'red')
                  }

          }
        }else{
          form.submit();
        }
    }

    
    //jQuerOs("*[class*='hide-field-name-'] , .delete-field, .delete-row").remove();
    jQuerOs(".delete-field, .delete-row").remove();
    //jQuerOs("span.cck-help-string").remove();
    jQuerOs(function () {
      jQuerOs('[data-toggle="tooltip"]').tooltip();
      jQuerOs(['id^=cck_col']).removeClass('resizable');
      jQuerOs('.ui-resizable-handle').remove();
    })


    function lay_type_select() {
      // console.log('22222222222', document.forms["<?php echo $unique_form_id; ?>"])
      // document.forms["<?php echo $unique_form_id; ?>"].lay_type = 
      var layout_select_url = '<?php echo JUri::root(); ?>index.php?option=com_os_cck&task=add_instance&eiid=<?php echo $entityInstance->eiid;?>&old_lid=<?php echo protectInjectionWithoutQuote('old_lid', 0, 'INT'); ?>&button=<?php echo protectInjectionWithoutQuote('button', 0, 'INT'); ?>&Itemid=<?php echo $Itemid; ?>'
      jQuerOs('form#<?php echo $unique_form_id; ?>').attr('action', layout_select_url)

      document.forms["<?php echo $unique_form_id; ?>"].submit();
      return;
    }

    function swich_task(task){
      if(task == 'save_instance' || task == 'send_request'){
        document.getElementById('inst_task').value = 'save_instance';
      }
      if(task == 'edit_instance'){
        document.getElementById('inst_task').value = 'add_instance';
      }
    }
  </script>
</div>

