<?php
$translation        = \DynCom\mysyde\common\classes\Registry::get("translation");
$formname           = "bewerber_forms";
$input_page_id      = (isset($_REQUEST["input_page_id"]) ? $_REQUEST["input_page_id"] : "");
$input_component_id = (isset($_REQUEST["input_component_id"]) ? $_REQUEST["input_component_id"] : "");
$is_survey = 0;
 
$query  = 'SELECT * FROM google_recaptcha';
$result = @mysqli_query($GLOBALS['mysql_con'], $query);
$grecaptcha = @mysqli_fetch_array($result);
$reCaptchaDisabled = false;
 
if ($grecaptcha["secretkey"] == "" && $grecaptcha["websitekey"] == "") {
    $reCaptchaDisabled = true;
}
?>

    <div id="overlaycrumb">
        <?php 
             
                echo $translation->get("edit_user");
          
        ?>
        <div id="closeoverlay" onclick="disableOverlay();"></div>
    </div>

    <ul class="toolbar_menu">
        <?php
 
         

        // Speichern button
        button("save", $translation->get("save"), $formname, "loadCard('save_bewerberform', true)");

        // Speichern und schliessen
        button("save", $translation->get("save_and_close"), $formname, "loadCard('save_bewerberform', true, '', true)");
        ?>
        <li>
            <ul>
                <?php
                // Loeschen  button
                if ($input_bewerberform["id"] != "" && is_page_edit() === FALSE && is_component_edit() === FALSE) {
                    echo button("delete", $translation->get("delete"), $formname, "loadCard('delete_bewerberform', true, '{$translation->get('delete_user_confirm')}', true)", "", FALSE);
                }
                    
               
                ?>
            </ul>
        </li>
    </ul>
    <div class="clearfix"></div>

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

    <form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
        <input name="input_id" type="hidden" value="<?= $input_bewerberform["id"] ?>">
        <input type="hidden" name="get_real_page_link_id" value="1" />
       
        
        
        <table class="cardform" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td>
                    <? input($translation->get("name"), "input_name", "text", $input_bewerberform["name"], 255) ?>
                    <? input($translation->get("email"), "input_email", "text", $input_bewerberform["email"], 255) ?>
                      
                </td>
                <td>
                    

                     
                    <?php
                    if ($input_bewerberform["id"] != "") {
                        show_changed_on($input_bewerberform["id"], "bewerber_user");
                        show_changed_by($input_bewerberform["id"], "bewerber_user");
                    }
                    ?>
                </td>
            </tr> 
            <tr>   
                <td>
                    
                    <div class="notes_section">
                    <button style="position:inherit;" type="button" class="r-btnAdd btnAdd btn btn-primary" data-user="<?=$input_bewerberform["id"];?>">Hinzufügen +</button>
                    <?php
                    if ($input_bewerberform["id"] != "") {
                        create_notes_set($input_bewerberform["id"]);
                    }
                    ?>
                    </div>
                </td>
            </tr>

        </table>
    </form>
    <br />
    <?php  $formname .= "_listing";
    ?>
    <form id="<?= $formname ?>" name="<?= $formname ?>" method="post">
    <input name="bewerber_id" type="hidden" value="<?= $input_bewerberform['id']; ?>">
       <?php 
        $query = "SELECT bun.id, description as '". $translation->get("description")."', from_unixtime(bun.modified_date, '%d.%m.%Y %H:%i:%s') AS '" . $translation->get("changed_on") . "', main_admin_user.name AS '" . $translation->get("changed_by") . "' from bewerber_user AS bu left join main_admin_user ON bu.modified_user = main_admin_user.id INNER JOIN bewerbung as bun on bu.id = bun.user_id INNER JOIN main_collection as mc on bun.main_collection_id = mc.id  where   (bu.id = " . (int)$input_bewerberform['id'] . ")  ORDER BY id DESC";
          
        $format = array('option', 'text', 'text');
        if ($result = @mysqli_query($GLOBALS['mysql_con'], $query)) {
            linklist($result, $formname, $format, "input_id", "edit_bewerbung", FALSE, 'update_sortorder_bewerber', "sendRequest('delete_bewerbung', false, '{$translation->get('delete_stufeform_confirm')}')"); 
        }
       ?>             
    </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">
        $('.btnAdd').click(function(){
            var counter = $('#totalOption').val();
           var user   = $(this).attr('data-user');
             counter++;
             
             $('#totalOption').val(counter);

            $('.notes_section').append(loadNextModule(counter, user));
        });
        $('.btnRemove').click(function(){
            
            counter = $(this).attr('data-counter');
            $('.input_note_'+ counter).remove();
            console.log('counter ist' + counter);
            counter--;
            $('#totalOption').val(counter);
             
            
        });
        function   myFunctionDelete(counter){
      
       $('.input_note_'+ counter).remove();
        
             
            counter--;
            $('#totalOption').val(counter);
            }
        function loadNextModule(counter, user_id){
            var html =  '<div class="notes_input input_note_'+counter+'">' + 
                     '<textarea name="input_user_note_'+user_id+'_'+counter+'"> </textarea>'+
            '<button type="button" onclick="myFunctionDelete('+counter+')"   data-counter="'+counter+'" class="r-btnRemove btnRemove btn btn-danger">Entfernen -</button>'+
          '</div>';  

          return html
        }
    </script>