<?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"] : "");
$bewerber_id = (isset($_REQUEST['bewerber_id']) ?  $_REQUEST['bewerber_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_bewerbung");
          
        ?>
        <div id="closeoverlay" onclick="disableOverlay();"></div>
    </div>

    <ul class="toolbar_menu">
        <?php
 
         

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

        // Speichern und schliessen
        button("save", $translation->get("save_and_close"), $formname, "loadCard('save_bewerbungform', true, '', true)");

        if($bewerber_id != ''){
             button("left", $translation->get("back"), $formname, "loadCard('edit_bewerberform', 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_bewerbungform', true, '{$translation->get('delete_stufeform_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["bunID"] ?>">
          <input name="user_id" type="hidden" value="<?= $input_bewerberform["id"] ?>">
          <input name="bewerber_id" type="hidden" value="<?= $bewerber_id ?>">
          
        <input name="contact_id" type="hidden" value="<?= $input_bewerberform["contact_id"] ?>">
        <input name="main_collection_id" type="hidden" value="<?= $input_bewerberform["main_collection_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["bunId"], "bewerbung");
                        show_changed_by($input_bewerberform["bunID"], "bewerbung");
                    }
                    ?>
                </td>
            </tr> 
            <tr>   
                <td>
                    

                     
                    <?php
                    if ($input_bewerberform["id"] != "") {
                        $query = "SELECT * FROM main_collection WHERE id = ".$input_bewerberform["main_collection_id"];
                        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
                        $collection = @mysqli_fetch_array($result);
                        input($translation->get("job_applied"), "main_collection", "text", $collection["description"], 255, TRUE)  ;
                    }
                    ?>
                </td>
                
                <td>
                    

                     
                    <?php
                    if ($input_bewerberform["id"] != "") {
                        create_process_set($input_bewerberform["id"], $input_bewerberform["main_collection_id"]);
                    }
                    ?>
                </td>
            </tr>
           <?php  if ($input_bewerberform["id"] != "") { ?>
            <tr>   
                <td>
                    <img class="loader" style="display:none;" src="/layout/admin/img/icons/ajax-loader.gif" />

                    <div class="process_stufe">
                        <?php
                        if ($input_bewerberform["id"] != "") {
                            create_stufe_set($input_bewerberform["bunID"]);
                        }
                        ?>
                    </div>
                </td>
            </tr> 
            <?php } ?>
            <tr>
                <td colspan="2">
                <hr>
                    <h2><?php echo $translation->get("top_userdata");;  ?></h2>
                </td>   
            </tr>
            <tr>   
                <?php 
                     $query  = "SELECT cl.* FROM contactform_line as cl INNER JOIN contactform_header as ch on cl.header_id = ch.id WHERE header_id = '" . $input_bewerberform["contact_id"] . "' AND code != 'registration_name' AND code != 'registration_email' AND is_registration = 1 ORDER BY sorting ASC";
                     $result = @mysqli_query($GLOBALS['mysql_con'], $query);
                      
                     if (@mysqli_num_rows($result) > 0) {
                         while ($field = @mysqli_fetch_array($result)) {
                            $queryLink  = "SELECT bucl.* FROM contactform_line as cl INNER JOIN bewerber_user_contact_link as bucl on cl.id = bucl.line_id WHERE header_id = '" . $input_bewerberform["contact_id"] . "' AND line_id = ".$field['id']." AND user_id = ". $input_bewerberform["id"]." ORDER BY sorting ASC";
                            $resultLink = @mysqli_query($GLOBALS['mysql_con'], $queryLink);
                            $row =  @mysqli_fetch_array($resultLink);
                            echo '<td>';
                            input($field['name'], $field["code"], "text", $row["content"], 255, TRUE)  ;
                            echo '</td>';
                         }
                     }    
                ?>
                
            </tr>

        </table>
    </form>
    <br />
<?
 
?>
<script type="text/javascript">
    function loadProcessStufe( process_id ) {
      
 
        $('.process_stufe', $('#<?php echo $formname; ?>')).html(""); 

        if (process_id == "" || process_id == 0) {
            return;
        }

        $('.loader', $('#<?php echo $formname ?>')).show();

        // ajax
        var formData = new FormData(jQuery('#<?php echo $formname; ?>' )[0]);
        formData.append("process_id", process_id);
 
         
        
        $.ajax({
                   url        : "?action=loadProcessStufe",
                   type       : 'POST',
                   data       : formData,
                   cache      : false,
                   processData: false, // Don't process the files
                   contentType: false, // Set content type to false as jQuery will tell the server its a query string request
                //    dataType   : 'html',
                   success    : function ( output, status, xhr ) {
                       console.log('log ist' + xhr.getResponseHeader('REQUIRES_AUTH'));
                       if (xhr.getResponseHeader('REQUIRES_AUTH') == 1) {
                           window.location.replace("/mysyde/");
                       } else {
                           $('.loader').hide();
                           $('.process_stufe' ).html(output); 
                       }
                   },
                   error: function (jqXHR, exception) {
                        var msg = '';
                        if (jqXHR.status === 0) {
                            msg = 'Not connect.\n Verify Network.';
                        } else if (jqXHR.status == 404) {
                            msg = 'Requested page not found. [404]';
                        } else if (jqXHR.status == 500) {
                            msg = 'Internal Server Error [500].';
                        } else if (exception === 'parsererror') {
                            msg = 'Requested JSON parse failed.';
                        } else if (exception === 'timeout') {
                            msg = 'Time out error.';
                        } else if (exception === 'abort') {
                            msg = 'Ajax request aborted.';
                        } else {
                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                        }
                        console.log(msg);
                   }
                  
               });
    }

     

    
    
</script>