<?
$formname                    = "form_line_card";
$translation                 = \DynCom\mysyde\common\classes\Registry::get("translation");
$input_page_id               = (isset($_REQUEST["input_page_id"]) ? $_REQUEST["input_page_id"] : "");
$input_collection_id         = (isset($_REQUEST["input_collection_id"]) ? $_REQUEST["input_collection_id"] : "");
$collection_setup_content_id = (isset($_REQUEST["collection_setup_content_id"]) ? $_REQUEST["collection_setup_content_id"] : "");
$input_component_id          = (isset($_REQUEST["input_component_id"]) ? $_REQUEST["input_component_id"] : "");
$header_id                   = (isset($input_line["header_id"]) ? $input_line["header_id"] : "");
 
$queryform  = "SELECT * FROM contactform_header WHERE id = '" . $header_id . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $queryform);
        $contactform  = @mysqli_fetch_array($result);
        $is_multiform = $contactform['is_multistep'];
?>

<div id="overlaycrumb">
    <?php if ($input_line["id"] == "") {
        echo $translation->get("new_contactform_field");
    } else {
        echo $translation->get("edit_contactform_field");
    }
    ?>
    <div id="closeoverlay" onclick="disableOverlay();"></div>
</div>

<ul class="toolbar_menu">
    <?
    if (is_collection_edit()) {
        button("left", $translation->get("overview"), $formname, "loadCard('edit_collection', true)");
    } else {
        button("left", $translation->get("overview"), $formname, "loadCard('edit_contactform', true)");
    }
    ?>
    <?= button("save", $translation->get("save"), $formname, "loadCard('save_line_contactform', true)"); ?>
    <?= button("save", $translation->get("save_and_close"), $formname, "jQuery('#save_and_close', jQuery('#" . $formname . "')).val(1);loadCard('save_line_contactform', true)"); ?>
    <?php
    if ($input_line["id"] != "") {
        echo button("delete", $translation->get("delete"), $formname, "loadCard('delete_line_contactform', true, '{$translation->get('delete_contactform_field_confirm')}')");
    }
    ?>
    <?= button("reset", $translation->get("restore"), $formname, "?action=edit"); ?>
</ul>

<?php

if ($messages !== null) {
    if (count($messages)) {
            echo '<div id="overlayMessages">' . join("\r\n", $messages) . '</div>';
    } 
 }
?>

<form id="<?= $formname ?>" name="<?= $formname ?>" method="post" enctype="multipart/form-data">
    <input name="input_line_id" type="hidden" value="<?= $input_line["id"] ?>" />
    <input name="input_id" type="hidden" value="<?= $header_id; ?>" />
    <input type="hidden" name="input_page_id" value="<?php echo $input_page_id; ?>" />
    <input type="hidden" name="input_collection_id" value="<?php echo $input_collection_id; ?>" />
    <input type="hidden" name="collection_setup_content_id" value="<?php echo $collection_setup_content_id; ?>" />
    <input type="hidden" name="input_component_id" value="<?php echo $input_component_id; ?>" />
    <input name="save_and_close" id="save_and_close" type="hidden" value="0" />
    <input type="hidden" name="linklist_form" value="form_field_contactform_list" />


    <table class="cardform" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>
                <?php 
                  
                if($is_multiform){ 
                    $section_id = 0;
                    $query = " SELECT * FROM contactform_section WHERE contactform_id =  ".$header_id;
                    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
                   
                ?>
                <?php
                    $queryLine  = "SELECT * FROM multi_contactform_link WHERE contactform_line_id = '" . $input_line["id"] . "' LIMIT 1";
                    $resultLine = @mysqli_query($GLOBALS['mysql_con'], $queryLine);
                    $input_liner = array();
                    $input_liner['layout_area_id'] = '';
                    if (@mysqli_num_rows($resultLine) == 1) {
                        $input_liner = @mysqli_fetch_array($resultLine);
                    }  
                ?>
                <? layout_class_select($translation->get("layout_area"), "input_layout_area_id", $input_liner['layout_area_id']); ?>
                <div class="label"><label for="abschnitt">Abschnitt Auswählen</label></div>
                <div class="input">
               
                    <select class="select" name="section_id" id="section_id">
                        <?php while( $row    = mysqli_fetch_array($result)){ ?>
                            <option
                            value="<?= $row['id']?>" <?php if($row['id'] == $input_line['section_id']){ echo 'selected';} ?>><?php echo $row['name'] ?></option>
                        <?php } ?>
                    </select>
                </div>
                <div class="label"><label for="abschnitt">Nächste Abschnitt</label></div>
                <div class="input">
               
                    <select class="select" name="next_section_id" id="next_section_id">
                        <option value="NA" <?php if($input_line['next_section_id'] == 'NA'){ echo 'selected'; } ?>>---</option>
                        <?php $result = @mysqli_query($GLOBALS['mysql_con'], $query); while( $row    = mysqli_fetch_array($result)){ ?>
                            <option
                            value="<?= $row['id']?>" <?php if($row['id'] == $input_line['next_section_id']){ echo 'selected';} ?>><?php echo $row['name'] ?></option>
                        <?php } ?>
                    </select>
                </div>
                <div class="label"><label for="abschnitt">Bild</label></div>
                <div class="input">
                            <input type="file" name="field_img">
                            <?php 
                              $valFile = '';
                              if($input_line["field_img"] != '' && $input_line["field_img"] != NULL){
                                  $valFile = $input_line["field_img"];
                              }
                           ?>
                            <input type="hidden" name="field_img_old" value="<?=$valFile;?>">
                            <?php
                        if($input_line["field_img"] != NULL && $input_line["field_img"] != ''){
                            echo "<div class='img_container intranet_user_image'><img class='img-fluid' src='/userdata/collection/resize/".$input_line["field_img"]."'></div>";
                        }
                     ?>
                </div> 
                <div class="label"><label for="abschnitt" >Icon</label></div>
                <div class="input">
                            <input type="text" name="icon" value="<?= $input_line["icon"];?>">
                </div> 
                <? input('Link -Textfeld?', "linked_field", "checkbox", $input_line["linked_field"]); ?>
                <? input('Linklplatz Platzhalter', "linked_placeholder", "text", $input_line["linked_placeholder"], 50) ?>
                
                <? } ?>
                <div class="label"><label for="input_typ">Typ</label></div>
                <div class="input">
                    <select class="select" name="input_typ" id="input_typ" <?= ($input_line["code"] != 'registration_name' && $input_line["code"] != 'registration_email') ? '' : 'disabled'; ?>>
                         
                        <option<?= ($input_line["typ"] == 1) ? " selected=\"selected\"" : " "; ?>
                            value="1"><?php echo $translation->get("textfield"); ?></option>
                        <option<?= ($input_line["typ"] == 2) ? " selected=\"selected\"" : " "; ?>
                            value="2"><?php echo $translation->get("textarea"); ?></option>
                        <option<?= ($input_line["typ"] == 3) ? " selected=\"selected\"" : " "; ?>
                            value="3"><?php echo $translation->get("yesno"); ?></option>
                        <option<?= ($input_line["typ"] == 4) ? " selected=\"selected\"" : " "; ?>
                            value="4"><?php echo $translation->get("optionfield"); ?></option>
                        <option<?= ($input_line["typ"] == 5) ? " selected=\"selected\"" : " "; ?>
                            value="5"><?php echo $translation->get("headline"); ?></option>
                        <!-- <option<?= ($input_line["typ"] == 6) ? " selected=\"selected\"" : " "; ?>
                            value="6"><?php echo $translation->get("spacerow"); ?></option> -->
                        <!-- <option<?= ($input_line["typ"] == 7) ? " selected=\"selected\"" : " "; ?>
                            value="7"><?php echo $translation->get("title_news"); ?></option> -->
                        <option<?= ($input_line["typ"] == 8) ? " selected=\"selected\"" : " "; ?>
                            value="8"><?php echo $translation->get("file"); ?></option>
                        <option<?= ($input_line["typ"] == 9) ? " selected=\"selected\"" : " "; ?>
                            value="9"><?php echo $translation->get("checkbox"); ?></option>
                        <option<?= ($input_line["typ"] == 10) ? " selected=\"selected\"" : " "; ?>
                            value="10"><?php echo 'Radio'; ?></option>
                        <option<?= ($input_line["typ"] == 11) ? " selected=\"selected\"" : " "; ?>
                            value="11"><?php echo $translation->get("date"); ?></option>
                    </select>
                </div>
                
                <? input($translation->get("textkey"), "input_code", "text", $input_line["code"], 50, ($input_line["code"] != 'registration_name' && $input_line["code"] != 'registration_email') ? FALSE : TRUE) ?>
                <?php if($input_line["code"] == 'registration_email' || $input_line["code"] == 'registration_name' ){ ?>
                    <input type="hidden" name="input_code" value="<?= $input_line["code"] ?>">
                    <input type="hidden" name="input_typ" value="<?= $input_line["typ"] ?>">
                <?php } ?>
                <? input($translation->get("caption"), "input_name", "text", $input_line["name"], 250) ?>
                <? input($translation->get("caption_in_field"), "input_infield", "checkbox", $input_line["infield"]) ?>
                <? input($translation->get("mandatory_field"), "input_mandatory", "checkbox", $input_line["mandatory"]) ?>
                <br />
                <?php if(!$contactform['is_survey']){ ?>
                <? input($translation->get("optionvalues"), "input_option_string", "text", $input_line["option_string"], 200) ?>
                <?php }else{ ?>
                    <input type="hidden" value="" name="input_option_string">
                <?php }?>
            </td>
            <?php if($contactform['is_survey']){ ?>
                <td>
            <div class="main_input_option_string" >
                    <div id="example1">
                        <button type="button" class="r-btnAdd btnAdd btn btn-primary">Hinzufügen +</button>

                        <?php $totalOptions = 0; $queryOpt = 'SELECT * FROM contactform_line_option WHERE line_id = '.$input_line["id"]; 
                                   
                                    $result = @mysqli_query($GLOBALS['mysql_con'], $queryOpt); 
                                    $dataArray = array();
                                    $i = 0;
                                    if(@mysqli_num_rows($result) > 0){
                                        $totalOptions = @mysqli_num_rows($result);
                                        while( $rowData    = mysqli_fetch_array($result)){
                                        
                            ?>  
                            <div class="r-group moduleAppended rGroup<?=$i;?>">
                                <p>
                                    <label for="<?php if($i == 0){ echo 'input_option_0_0' ;} else { echo 'input_option_'.$i.'_name'; } ?>" data-pattern-text="<?=$translation->get("optionvalues")?> +=1:"><?=$translation->get("optionvalues")?>:</label><br>
                                    <input type="text" value="<?=$rowData['option_string']?>"  name="input_option[<?=$i;?>][name]" id="input_option_<?=$i;?>_name" class="form-control text" data-pattern-name="input_option[++][name]" data-pattern-id="input_option_++_name" />
                                </p>
                                
                                 
                                <div class="">
                                        <label for="<?php if($i == 0){ echo 'input_option_0_0' ;} else { echo 'input_option_'.$i.'_linked_field'; } ?>" data-pattern-text="Link -Textfeld? +=1:">Link -Textfeld?:</label><br>
                                        <input <?php if($rowData['linked_field'] == 1){ echo 'checked';} ?> type="checkbox" name="input_option[<?=$i;?>][linked_field]" id="input_option_<?=$i;?>_linked_field" class="form-control checkbox" data-pattern-name="input_option[++][linked_field]" data-pattern-id="input_option_++_linked_field" />
                                </div>
                                <div class="">
                                    <label for="<?php if($i == 0){ echo 'input_option_0_0' ;} else { echo 'input_option_'.$i.'_linked_placeholde'; } ?>" data-pattern-text="Linklplatz Platzhalter +=1:">Linklplatz Platzhalter:</label> <br>
                                    <input type="text" name="input_option[<?=$i;?>][linked_placeholder]" id="input_option_<?=$i;?>_linked_placeholder" class="form-control text" data-pattern-name="input_option[++][linked_placeholder]" data-pattern-id="input_option_++_linked_placeholder" value="<?= $rowData['linked_placeholder'];?>" />
                                </div>     
                                
                                <p>
                                    <!-- Add a remove button for the item. If one didn't exist, it would be added to overall group -->
                                    <?php if($i != 0){ ?>
                                        <button type="button" data-counter="<?= $i;?>" class="r-btnRemove btnRemove btn btn-danger">Entfernen -</button>
                                    <?php } ?>
                                    <hr>
                                </p>
                                
                            </div>
                        <?php $i ++; }
                                    }else{
                        ?>             
                        <div class="r-group">
                            <p>
                                <label for="input_option_0_0" data-pattern-text="<?=$translation->get("optionvalues")?> +=1:"><?=$translation->get("optionvalues")?>:</label><br>
                                <input type="text" name="input_option[0][name]" id="input_option_0_name" class="form-control text" data-pattern-name="input_option[++][name]" data-pattern-id="input_option_++_name" />
                            </p>
                           
                             
                            <!-- <div class="">
                                <label for="input_option_0_0" data-pattern-text="Icon +=1:">Icon:</label> <br>
                                <input type="text" name="input_option[0][icon]" id="input_option_0_icon" class="form-control text" data-pattern-name="input_option[++][icon]" data-pattern-id="input_option_++_icon" />
                            </div> -->
                         
                            <div class="">
                                    <label for="input_option_0_0" data-pattern-text="Link -Textfeld? +=1:">Link -Textfeld?:</label><br>
                                    <input type="checkbox" name="input_option[0][linked_field]" id="input_option_0_linked_field" class="form-control checkbox" data-pattern-name="input_option[++][linked_field]" data-pattern-id="input_option_++_linked_field" />
                            </div>
                            <div class="">
                                <label for="input_option_0_0" data-pattern-text="Linklplatz Platzhalter +=1:">Linklplatz Platzhalter:</label> <br>
                                <input type="text" name="input_option[0][linked_placeholder]" id="input_option_0_linked_placeholder" class="form-control text" data-pattern-name="input_option[++][linked_placeholder]" data-pattern-id="input_option_++_linked_placeholder" />
                            </div>             
                             <p>
                                <!-- Add a remove button for the item. If one didn't exist, it would be added to overall group -->
                                 
                                <hr>
                            </p>
                        </div>   
                        <?php } ?>
                    </div>
                </div>
            </td>
             <?php } ?>   
            <td>&nbsp;</td>
        </tr>
    </table>
    <input type="hidden" value="<?= $totalOptions;?>" id="totalOption">
</form>

<script  src="https://code.jquery.com/jquery-3.6.0.min.js"
  integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
  crossorigin="anonymous"></script>
 
<script type="text/javascript">
    
    $(document).ready(function(){
        $('.btnAdd').click(function(){
            var counter = $('#totalOption').val();
             counter++;
             console.log('counter ist' + counter);
             $('#totalOption').val(counter);
            $('#example1').append(loadNextModule(counter));
        });
        $('.btnRemove').click(function(){
            counter = $(this).attr('data-counter');
            console.log('counter ist' + counter);
            $('.rGroup'+ counter).remove();
            
        });
         

        
        $('.dropbtn').click(function(){
            console.log('jazba');
            var counter = $(this).attr('data-counter');
         
         $("#myDropdown"+counter+".dropdown-content").show();
        });
        $('.iconSelect').click(function(){
            console.log('select');
            var id = $(this).attr('data-counter');
            var icon = $(this).attr('data-icon');
            var htmlIcon = "<span class='material-icons'>" + icon + '</span>';
            $("#input_option_" + id+"_icon").val(icon);
            $('.btnNumber' + id).html(htmlIcon);
        });
    });
  
    
    
    
   function loadNextModule(count = 0){
   
            

    var html = '<p></p>'+'<div class="r-group moduleAppended rGroup'+count+'">' +
                            '<p>' +
                               ' <label for="input_option_0_'+count+'" ><?=$translation->get("optionvalues")?> </label><br>' + 
                                '<input type="text" name="input_option['+count+'][name]" id="input_option_'+count+'_name" class="form-control text"  /> ' +
                            '</p> '+
                            '<p>'+
                            
                             
                           
                            '<div class="">'+
                                   ' <label >Link -Textfeld?</label><br>'+
                                    '<input type="checkbox" name="input_option['+count+'][linked_field]" id="input_option_'+count+'_linked_field" class="form-control checkbox"  />'+
                            '</div>'+
                            '<div class="">'+
                                '<label >Linklplatz Platzhalter:</label> <br>'+
                                '<input type="text" name="input_option['+count+'][linked_placeholder]" id="input_option_'+count+'_linked_placeholder" class="form-control text"  />'+
                            '</div>   '   +       
                             '<p>'+
                                
                                '<button type="button" onclick="myFunctionDelete('+count+')" data-counter="'+count+'" class="r-btnRemove btnRemove btn btn-danger">Entfernen -</button>'+
                                
                           ' </p>'+
                        '</div> ';
                     
                    return html;    

   }
   function   myFunctionDelete(counter){
       console.log('counter');
                jQuery('.rGroup'+ counter).remove();
            }
   
 
  </script>