<?

$messages = array();
$error    = FALSE;
 

if (isset($custom_action)) {
    $action = $custom_action;
   
} else {
    $action = $_REQUEST["action"];
}
 
switch ($action) {
    case 'edit_bewerberform':
        edit_bewerberform();
        break;
       
    case 'delete_bewerberform':
        delete_bewerberform();
        break;
   
     
    case 'save_bewerberform':
        save_bewerberform();
        break;
    case 'save_bewerbungform':
        save_bewerbungform();
        break;    
    case 'bewerbung':
        require_once("edit_bewerbung_listform.inc.php");
        break;       
    case 'edit_bewerbung':
        
        edit_bewerbung();
         break;       
    case 'loadProcessStufe':
            loadProcessStufe();
            break;     
    default:
        
        require_once("edit_bewerber_listform.inc.php");
        
        break;
}
function save_bewerbungform() {

    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $input_id    = "";
    
    if ($_REQUEST["user_id"] <> '') {
        $query    = "UPDATE bewerber_user SET
				name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_name"]) . "',
                email = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_email"]) . "',
				modified_date = " . time() . ",
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . "
               
				 
				WHERE id = '" . $_REQUEST["user_id"] . "'
			LIMIT 1";
        $inserted = FALSE;
        
        $input_id = $_REQUEST["user_id"];
       
    } else {
        
        $query    = "INSERT INTO bewerber_user
			(main_language_id, name, modified_date, modified_user, email)
			VALUES (
				" . (int)$GLOBALS["language"]['id'] . ",
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_name']) . "',
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . ", 
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_email']) . "'
			)";
            
        $inserted = TRUE;
    }
 
    if (($_REQUEST["input_name"] == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_name") . "</div>\n";
        $error      = TRUE;
    }
    if (($_REQUEST["input_email"] == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_email") . "</div>\n";
        $error      = TRUE;
    }

    if (!$error) {
        @mysqli_query($GLOBALS['mysql_con'], $query);
        if ($inserted == TRUE) {
            $input_id   = mysqli_insert_id($GLOBALS['mysql_con']);
            $messages[] = '<div class="successbox">' . $translation->get("user_msg_success1") . '</div>';
        } else {
            $messages[] = '<div class="successbox">' . $translation->get("user_msg_success2") . '</div>';
        }

        $process = '';
        if( isset($_POST['select_process'])){
            
            $process = $_REQUEST['select_process'];
        }
        
        if($process != '')  { 
            $queryDel =  "DELETE FROM bewerber_user_process WHERE user_id='".$input_id."' AND main_collection_id = '".$_REQUEST['main_collection_id']."'";
           
            $deleteOld = @mysqli_query($GLOBALS["mysql_con"], $queryDel );    
            
               
                    $query = "INSERT INTO bewerber_user_process
                    (user_id, process_id, main_collection_id)
                    VALUES (
                        " .  $input_id . ",
                        '" . $process . "',
                        '".$_REQUEST['main_collection_id']."'
                    )";
                    
                    @mysqli_query($GLOBALS['mysql_con'], $query);
                
                    $stufe = array();
                    $stufe_ids = $_POST['select_stufe'];
                    if($stufe_ids != NULL && isset($_POST['select_stufe'])){
                        
                        $stufe = explode(',', $stufe_ids);
                    }
                    $queryDel =  "DELETE FROM bewerbung_process_stufe WHERE   bewerbung_id = '".$_REQUEST['input_id']."' AND user_id = '".$input_id."'";
                    $deleteOldStufe = @mysqli_query($GLOBALS["mysql_con"],$queryDel);    
                       
                    if(!empty($stufe) && gettype($stufe) == 'array')  { 
                        
                            foreach($stufe as $key => $val){
                                $query = "INSERT INTO bewerbung_process_stufe
                                (bewerbung_id, stufe_id, process_id, user_id)
                                VALUES (
                                    " .  $_REQUEST['input_id'] . ",
                                    " .  $val . ",
                                    '" . $process . "',
                                    " .  $input_id . "
                                )";
                                
                                @mysqli_query($GLOBALS['mysql_con'], $query);
                            }
                    
                        
                    }            
            
        }
            $_REQUEST['action'] = 'save_bewerbungform';
            edit_bewerbung($_REQUEST['input_id'], $messages);
        

    } else {
        header('EDIT_ERROR: 1');
        $input_bewerberform["name"]     = $_REQUEST["input_name"];
        $input_bewerberform["email"]     = $_REQUEST["input_email"];
        $input_bewerberform["input_id"]              = $_REQUEST["input_id"];
        require_once("edit_bewerbung_cardform.inc.php");
    }
}
function save_bewerberform() {

    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $input_id    = "";
    
    if ($_REQUEST["input_id"] <> '') {
        $query    = "UPDATE bewerber_user SET
				name = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_name"]) . "',
                email = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["input_email"]) . "',
				modified_date = " . time() . ",
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . "
               
				 
				WHERE id = '" . $_REQUEST["input_id"] . "'
			LIMIT 1";
        $inserted = FALSE;
        
        $input_id = $_REQUEST["input_id"];
       
    } else {
        
        $query    = "INSERT INTO bewerber_user
			(main_language_id, name, modified_date, modified_user, email)
			VALUES (
				" . (int)$GLOBALS["language"]['id'] . ",
				'" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_name']) . "',
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . ", 
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST['input_email']) . "'
			)";
            
        $inserted = TRUE;
    }
 
    if (($_REQUEST["input_name"] == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_name") . "</div>\n";
        $error      = TRUE;
    }
    if (($_REQUEST["input_email"] == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_email") . "</div>\n";
        $error      = TRUE;
    }

    if (!$error) {
        @mysqli_query($GLOBALS['mysql_con'], $query);
        if ($inserted == TRUE) {
            $input_id   = mysqli_insert_id($GLOBALS['mysql_con']);
            $messages[] = '<div class="successbox">' . $translation->get("user_msg_success1") . '</div>';
        } else {
            $messages[] = '<div class="successbox">' . $translation->get("user_msg_success2") . '</div>';
        }

        $process = array();
        $process_ids = $_POST['select_process'];
        if($process_ids != NULL && isset($_POST['select_process'])){
            
            $process = explode(',', $process_ids);
        }
        
        if(!empty($process) && gettype($process) == 'array')  { 
            $deleteOld = @mysqli_query($GLOBALS["mysql_con"], "DELETE FROM bewerber_user_process WHERE user_id='".$input_id."'");    
            
                foreach($process as $key => $val){
                    $query = "INSERT INTO bewerber_user_process
                    (user_id, process_id)
                    VALUES (
                        " .  $input_id . ",
                        '" . $val . "'
                    )";
                    
                    @mysqli_query($GLOBALS['mysql_con'], $query);
                }
        
            
        }
        // notes
        
        $deleteData = @mysqli_query($GLOBALS["mysql_con"], "DELETE FROM bewerber_user_notes WHERE user_id='".$input_id."' ");
        print_r($options);
        if(isset($_REQUEST['totalOption']) && $_REQUEST['totalOption'] > 0){
            $options = $_REQUEST['totalOption'] ;
            for($column = 0; $column <= $options; $column++){
                
                if (array_key_exists("input_user_note_" . $input_id."_".$column,$_POST) && $_POST["input_user_note_" . $input_id."_".$column] != "" && $_POST["input_user_note_" . $input_id."_".$column] != NULL) {
                   
                    $query = "INSERT INTO bewerber_user_notes
                    (user_id, notes)
                    VALUES (
                        " .  $input_id . ",
                        '" . $_POST["input_user_note_" . $input_id."_".$column] . "'
                    )";
                   
                    @mysqli_query($GLOBALS['mysql_con'], $query);
                 }  
             }
        }
        // notea end
            edit_bewerberform($input_id, $messages);
        

    } else {
        header('EDIT_ERROR: 1');
        $input_bewerberform["name"]     = $_REQUEST["input_name"];
        $input_bewerberform["email"]     = $_REQUEST["input_email"];
        $input_bewerberform["id"]              = $_REQUEST["input_id"];
        require_once("edit_bewerber_cardform.inc.php");
    }
}
function edit_bewerbung( $_id = "", $messages = array() ) {
    
    $input_id = '';
   
    if ((int)$_id > 0) {
        $input_id = $_id;
    } if((isset($_REQUEST['input_id']) || isset($_REQUEST['data-inputid']) ) && $_id == ''){
        $input_id = $_REQUEST["input_id"];
    }   
 
    if(isset($_REQUEST['data-inputid'])){
        $input_id = $_REQUEST["data-inputid"];
    }
 
    if ($input_id <> '') {
        $query  = "SELECT bu.*, bun.id as bunID, bun.main_collection_id, bun.contact_id FROM bewerber_user as bu INNER JOIN bewerbung as bun on bu.id = bun.user_id WHERE bun.id = '" . $input_id . "' LIMIT 1";
       
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $input_bewerberform = @mysqli_fetch_array($result);
            
                require_once("edit_bewerbung_cardform.inc.php");
          
        }
    } else {
        $input_bewerberform = array();
         
        require_once("edit_bewerbung_cardform.inc.php");
    }
}
function edit_bewerberform( $_id = "", $messages = array() ) {
    
    $input_id = '';
   
    if ((int)$_id > 0) {
        $input_id = $_id;
    } if(isset($_REQUEST['input_id']) && $_id == ''){
        $input_id = $_REQUEST["input_id"];
    }   
    if(isset($_REQUEST['bewerber_id'])){
        $input_id = $_REQUEST['bewerber_id'];
    }
 
    if ($input_id <> '') {
        $query  = "SELECT * FROM bewerber_user WHERE id = '" . $input_id . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $input_bewerberform = @mysqli_fetch_array($result);
            
                require_once("edit_bewerber_cardform.inc.php");
          
        }
    } else {
        $input_bewerberform = array();
         
        require_once("edit_bewerber_cardform.inc.php");
    }
}
function delete_processform() {
    if ($_REQUEST["input_id"] <> '') {
        $query = "DELETE FROM bewerber_user WHERE id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
         

        
    }
 
    require_once("edit_process_listform.inc.php");
}
function create_process_set($user_id, $collection_id){

    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
 

    $query = "SELECT * FROM bewerber_process WHERE main_language_id = '" . $GLOBALS["language"]['id'] . "'";
    $result = mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) {
        return;
    }

    $preselect = array();
    $query2 = "SELECT * FROM bewerber_user_process WHERE user_id = '" . $user_id . "' AND main_collection_id = ".$collection_id;
 
    $result2 = mysqli_query($GLOBALS['mysql_con'], $query2);
    
    while($row2 = @mysqli_fetch_array($result2)){
        array_push($preselect,$row2['process_id']);
    }
    echo "<div class=\"label\"><label for=\"" ."select_process" . "\">" . $translation->get("select_process") . "</label></div>";
    echo "<div class=\"input\"><select onchange=\"loadProcessStufe(this.value);\" class=\"bigselect\" name=\"" . "select_process" . "\" id=\"" . "select_process" . "\">";


    // neue typen die nur fuer kollektionen vorgesehen sind
    while($row = @mysqli_fetch_array($result)) {
        $selected = '';
        if (in_array($row['id'], $preselect)) {
            $selected = 'selected="selected"';
        }
        echo "<option " . $selected . " value=\"" . $row['id'] . "\">" . $row["description"].' - '.$row['code'] . "</option>";
    }

 

    echo "</select>";
    ?>
    
 
      
    <? }
    function loadProcessStufe(){
        $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
 
       $process_id = $_REQUEST['process_id']; 
        $query = "SELECT st.* FROM stufe as st INNER JOIN  bewerber_process_stufe as bps on st.id = bps.stufe_id WHERE st.main_language_id = '" . $GLOBALS["language"]['id'] . "' AND process_id = ".$process_id ;
        $result = mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 0) {
            return;
        }
    
    ?>
     <div class="label"><?php echo $translation->get("select_stufe"); ?></div>
            <div class="groups bc-select-ui ui fluid multiple search selection dropdown" style="clear:both;display:inline-block;">
                <input type="hidden" name="select_stufe">
                <i class="dropdown icon"></i>
                <div class="default text"><?php echo $translation->get("select_stufe"); ?></div>
                <div class="menu">
                    <?  
                        
                    
                        while($row = @mysqli_fetch_array($result)) {
                            echo "<div class=\"item\" data-value=\"{$row['id']}\">".$row["description"]."</div>";
                        }
                      
                    ?>
                </div>
        </div>
        <script type="text/javascript" src="/plugins/jquery/jquery-3.5.1.min.js"></script>
                <script type="text/javascript" src="/plugins/Semantic-UI-master/semantic.min.js"></script>
                <link rel="stylesheet" type="text/css" href="/plugins/Semantic-UI-master/bc-semantic.css?time=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/plugins/Semantic-UI-master/bc-semantic.css')?>">
                <script>
                    var $y = jQuery.noConflict();

                    $y( document ).ready(function() {
                        $y('.groups.ui.fluid.multiple.dropdown').dropdown(); 
                    });


        </script>
    <? 
    }
    function create_stufe_set($bunID){

        $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
     
        
        $query = "SELECT st.* FROM stufe as st INNER JOIN bewerbung_process_stufe as bps on st.id = bps.stufe_id WHERE main_language_id = '" . $GLOBALS["language"]['id'] . "' AND bewerbung_id = ".$bunID;
        $result = mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 0) {
            return;
        }
    
        $preselect = array();
        $query2 = "SELECT * FROM bewerbung_process_stufe WHERE bewerbung_id = '" . $bunID . "'";
        $result2 = mysqli_query($GLOBALS['mysql_con'], $query2);
        
        while($row2 = @mysqli_fetch_array($result2)){
            array_push($preselect,$row2['stufe_id']);
        }
        
        ?>
         <div class="label"><?php echo $translation->get("select_stufe"); ?></div>
                <div class="groups bc-select-ui ui fluid multiple search selection dropdown" style="clear:both;display:inline-block;">
                    <input type="hidden" name="select_stufe">
                    <i class="dropdown icon"></i>
                    <div class="default text"><?php echo $translation->get("select_stufe"); ?></div>
                    <div class="menu">
                        <?  
                            
                        
                            while($row = @mysqli_fetch_array($result)) {
                                echo "<div class=\"item\" data-value=\"{$row['id']}\">".$row["description"]."</div>";
                            }
                          
                        ?>
                    </div>
            </div>
            <script type="text/javascript" src="/plugins/jquery/jquery-3.5.1.min.js"></script>
                    <script type="text/javascript" src="/plugins/Semantic-UI-master/semantic.min.js"></script>
                    <link rel="stylesheet" type="text/css" href="/plugins/Semantic-UI-master/bc-semantic.css?time=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/plugins/Semantic-UI-master/bc-semantic.css')?>">
                    <script>
                        var $y = jQuery.noConflict();
    
                        $y( document ).ready(function() {
                            $y('.groups.ui.fluid.multiple.dropdown').dropdown('set selected', <?= json_encode($preselect) ?>); 
                        });
    
    
            </script>
        <? }   

function create_notes_set($user_id){
     $query = "SELECT * FROM bewerber_user_notes WHERE user_id = ".$user_id;
     $result = mysqli_query($GLOBALS['mysql_con'], $query);
     $counter = 0;
     while($row = @mysqli_fetch_array($result)){
        echo '<div class="notes_input input_note_'.$counter.'">';
        echo '<textarea name="input_user_note_'.$user_id.'_'.$counter.'"> '.$row['notes'].'</textarea>';
            
          echo '<button type="button"  data-counter="'.$counter.'" class="r-btnRemove btnRemove btn btn-danger">Entfernen -</button>';
        echo '</div>';  
          $counter++;
     }
     echo '<input type="hidden" id="totalOption" name="totalOption" value="'.$counter.'">';
}        
?>