<?
switch ($_GET["action"]) {
    case 'save':
        save_infoboard();
        break;
    case 'new_infoboard':
        require_once("infoboard_cardform.inc.php");
        break;
    case 'save_infoboard_card':
        save_infoboard_card();
        break;
    default:
        require_once("infoboard_listform.inc.php");
        break;
}

function filter_mandant($filter){ ?>
    <h3><?php echo get_translation('filter_mandant'); ?>:</h3>
    <div class="input">
        <select id="filter_mandant" name="filter_mandant" class="bigselect" onchange="filter_mandant();">
            <option value="0"><?php echo get_translation('all_mandate'); ?></option>
            <?php
                $query = "SELECT * FROM main_mandant";
                $result = @mysqli_query($GLOBALS['mysql_con'], $query);
                while($row = @mysqli_fetch_array($result)){ ?>
                    <option value="<?= $row['id'] ?>" <?= ($filter == $row['id'] ? 'selected="selected"' : ""); ?>><?= $row['description'] ?></option>
            <?php } ?>
        </select>
    </div>
<?php }

function save_infoboard(){
    $date = $_POST['input_date'];
    $category_id = $_POST['input_category'];
    $mandant_id = $_POST['input_mandant'];

    $where_ids = "";
    if($mandant_id != 0){
        $where_ids = " AND main_contact_id IN (SELECT main_contact.id FROM main_contact LEFT JOIN main_contact_department ON main_contact.id = main_contact_department.main_contact_id WHERE main_mandant_id = ".$mandant_id.")";
    }
    $delete_query = "DELETE FROM infoboard WHERE date='".$date."' AND category_id = ".$category_id . $where_ids;
    $deleteInfoboard = @mysqli_query($GLOBALS["mysql_con"], $delete_query);    

    $input_contact = $_POST['input_contact'];
    $contacts = explode(',', $input_contact);
    if(!empty($contacts) && gettype($contacts) == 'array')  { 
        foreach($contacts as $key => $contact){
            $query = "INSERT INTO infoboard
            (category_id, date, main_contact_id)
            VALUES (
                '" .  $category_id . "',
                '" . $date . "',
                '" . $contact . "'
            )";
            @mysqli_query($GLOBALS['mysql_con'], $query);
        }
    }
}

function save_infoboard_card(){
    $start_date = new DateTime($_POST['input_start_date']);
    $end_date = new DateTime($_POST['input_end_date']);
    $category_id = $_POST['input_category']; 

    $input_contact = $_POST['input_contact'];
    $contacts = explode(',', $input_contact);
    if(!empty($contacts) && gettype($contacts) == 'array')  { 
        foreach($contacts as $key => $contact){
            for($i = $start_date; $i <= $end_date; $i->modify('+1 day')){
                $query = "SELECT * FROM infoboard WHERE date = '".$i->format('Y-m-d')."' AND category_id = ".$category_id. " AND main_contact_id = ".$contact;
                $result = @mysqli_query($GLOBALS['mysql_con'], $query);
                if (@mysqli_num_rows($result) == 0) { 
                    $query = "INSERT INTO infoboard
                    (category_id, date, main_contact_id)
                    VALUES (
                        '" .  $category_id . "',
                        '" . $i->format('Y-m-d') . "',
                        '" . $contact . "'
                    )";
                    // var_dump($query);
                    @mysqli_query($GLOBALS['mysql_con'], $query);
                }
            }
        }
    }
    // require_once("infoboard_cardform.inc.php");
}

function load_contact($date, $category_id){
    global $filter_mandant;
    $join_mandant = "";
    $where_mandant = "";
    if($filter_mandant != 0){
        $join_mandant = "LEFT JOIN main_contact_department ON main_contact.id = main_contact_department.main_contact_id";
        $where_mandant = " AND main_mandant_id = ".$filter_mandant; 
    }
    $query = "SELECT main_contact.* FROM infoboard LEFT JOIN main_contact ON infoboard.main_contact_id = main_contact.id ".$join_mandant." WHERE date = '".$date."' AND category_id = ".$category_id. " AND main_contact.id != 0" . $where_mandant;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) { 
        $person = "Keine Auswahl";
    }
    $i = 1;
    while($contact = @mysqli_fetch_array($result)) {
        if($i > 1){
            $person .= ", ";
        }
        $person .= $contact['shortcut'];
        $i++;
    }
    return $person;
}

function load_contact_ids($date, $category_id){
    $preselect = array();
    
    global $filter_mandant;
    $join_mandant = "";
    $where_mandant = "";
    if($filter_mandant != 0){
        $join_mandant = "LEFT JOIN main_contact_department ON main_contact.id = main_contact_department.main_contact_id";
        $where_mandant = " AND main_mandant_id = ".$filter_mandant; 
    }

    $query = "SELECT main_contact.* FROM infoboard LEFT JOIN main_contact ON infoboard.main_contact_id = main_contact.id  ".$join_mandant." WHERE date = '".$date."' AND category_id = ".$category_id . $where_mandant;
    $result = @mysqli_query($GLOBALS['mysql_con'], $query);

    while($row = @mysqli_fetch_array($result)){
        array_push($preselect,$row['id']);
    }

    return $preselect;
}

function load_infoboard(){
    global $filter_mandant;
    $current_date = new DateTime();
    $this_day = new DateTime('Monday this week');
    $next_week = $current_date->format('W')+1;
    $prev_week = $current_date->format('W')-1;
    if(isset($_GET['week'])){
        $this_day->setISODate($current_date->format('Y'), $_GET['week']);
        $next_week = $_GET['week']+1;
        $prev_week = $_GET['week']-1;
    }
    $categories = array("Abwesend", "Home Office", "Urlaub", "Berufsschule", "Anwesend");
    $days = array("Mo", "Di", "Mi", "Do", "Fr");

    echo "<div class='infoboard_categories_wrapper'>";
    echo "KW ".$this_day->format('W');
    foreach($categories as $key_categories => $value_categories){
        echo "<div class='infoboard_categories'>".$value_categories."</div>";
    }
    echo "</div>";


    foreach ($days as $key_days => $value_days) {
        $current = "";
        if($this_day->format('d.m.Y') == $current_date->format('d.m.Y')){
            $current = "current";
        }
        echo "<div class='infoboard_day_person_group ".$current."'>";
        echo "<div class='infoboard_day'>".$value_days.". ".$this_day->format('d.m.Y')."</div>";
        echo "<div class='infoboard_person_wrapper'>";
        foreach($categories as $key_categories => $value_categories){
            $person = load_contact($this_day->format('Y-m-d'), $key_categories);
            $person_ids = load_contact_ids($this_day->format('Y-m-d'), $key_categories);
            echo "<div class='infoboard_person_border'>";
            echo "<div data-contact='".json_encode($person_ids)."' data-date='".$this_day->format('Y-m-d')."' data-category='".$key_categories."' data-mandant='".$filter_mandant."' class='infoboard_person'>".$person."</div>";
            echo "</div>";
        }
        echo "</div>";
        $this_day->modify("+1 day");
        echo "</div>";
    }
    echo "<div class='change_weeks'>";
    echo "<a class='change_week'href='?week=".$prev_week."&filter=".$filter_mandant."'>vorherige Woche</a>";
    echo "<a class='change_week'href='?week=".$next_week."&filter=".$filter_mandant."'>nächste Woche</a>";
    echo "</div>";

 

}


function select_contact(){
    global $filter_mandant;
    $join_mandant = "";
    $where_mandant = "";
    if($filter_mandant != 0){
        $join_mandant = "LEFT JOIN main_contact_department ON main_contact.id = main_contact_department.main_contact_id";
        $where_mandant = "WHERE main_mandant_id = ".$filter_mandant; 
    }

    $query = "SELECT main_contact.* FROM main_contact ". $join_mandant . " " . $where_mandant;
    $result_contact = @mysqli_query($GLOBALS['mysql_con'], $query);
        
    if (@mysqli_num_rows($result_contact) == 0) { 
        return;
    }
    ?>
    <div class="contact bc-select-ui ui fluid multiple search selection dropdown" style="clear:both;display:inline-block;">
        <input type="hidden" name="input_contact">
        <i class="dropdown icon"></i>
        <div class="default text">Personen hinzufügen</div>
        <div class="menu">
            <?  
                while($contact = @mysqli_fetch_array($result_contact)) {
                    echo "<div class=\"item\" data-value=\"{$contact['id']}\">".$contact["name"]."</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')?>">
<?php
}