<?php

function get_task_groups(){
    $query_group = "SELECT * FROM task_group WHERE main_contact_id = ".$GLOBALS['main_contact']['id'];
    $result_group = @mysqli_query($GLOBALS['mysql_con'], $query_group);
    echo "<select class='change_task_group'>";
    echo "<option value='-2'>Alle Tasks</option>";
    echo "<option value='0'>Aufgaben</option>";
    echo "<option value='-1'>Mir zugewiesen</option>";
    while($row_group = @mysqli_fetch_array($result_group)){
        echo "<option value='".$row_group['id']."'>".$row_group['description']."</option>";
    }
    echo "</select>";
}

if(isset($_POST['submit_task'])){
    $modified_date = new DateTime();
    $insertquery = "INSERT INTO tasks
    (title, description, modified_date, modified_user, due_date, status, task_group_id)
    VALUES (
        '" . $_POST['title'] . "',
        '',
        '" . $modified_date->format('Y-m-d') . "',
        '" . $GLOBALS['main_contact']['id'] . "',
        '',
        '0',
        '" . $_POST['task_group_id_input'] . "'
    )";
    @mysqli_query($GLOBALS['mysql_con'], $insertquery);
    $input_id = mysqli_insert_id($GLOBALS['mysql_con']);
    
    $contactquery = "INSERT INTO task_contact_link
    (task_id, main_contact_id)
    VALUES (
        " . $input_id . ",
        " . $GLOBALS['main_contact']['id'] . "
    )";
    @mysqli_query($GLOBALS['mysql_con'], $contactquery);
    $_POST = array();
}

?>

<div class='tasks'>
    <div class="tasks_container_dashboard">
        <div class="title_with_icon">
            <div class="textcontent">
                <h2>Tasks</h2>
            </div>
            <a href="/intranet/de/tasks/">
                <div class="svg_content">
                    <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
                    <g id="dot-circle-line" transform="translate(-2 -2)">
                        <path id="Pfad_280" data-name="Pfad 280" d="M18,11a7,7,0,1,1-7,7,7,7,0,0,1,7-7"/>
                        <path id="Pfad_281" data-name="Pfad 281" d="M18,34A16,16,0,1,1,34,18,16,16,0,0,1,18,34ZM18,4A14,14,0,1,0,32,18,14,14,0,0,0,18,4Z"/>
                    </g>
                    </svg>
                    
                </div>
            </a>
        </div>
        <div class="select_group_and_toggle_creation_form">
            <?= get_task_groups()?>
            <div class="add_svg_content">
                <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18">
                <path id="add" d="M19.125,16.875V9h-2.25v7.875H9v2.25h7.875V27h2.25V19.125H27v-2.25Z" transform="translate(-9 -9)"/>
                </svg>
            </div>
        </div>
        <div class='tasks_wrapper_dashboard'>
            <div class="tasks_list_overflow">
                <div class="task_item_list">
                </div>
            </div>
            <form method='post' class='create_task_dashboard'>
                <input type='hidden' name='task_group_id_input' class='task_group_id_input' value='0'>
                <input class="add_task" type='text' required name='title' placeholder='Neue Aufgabe erstellen'>
                <input class="send_task" type='submit' name='submit_task' value='Aufgabe hinzufügen'>
            </form>
        </div>
    </div>
</div>
<script>
$('.task_item_list').on('click', '.task_item .complete_task', function() {
    var task_id = $(this).parent().attr('data-id');
    var complete = $(this).parent().attr('data-complete');
    var selectedValue = $(".change_task_group").val();
    ajax_action(task_id, 'complete_task', complete, false);
    $(this).addClass("animated_check");
    setTimeout(function() {
        ajax_action(selectedValue, 'get_tasks', 0, true);
    }, 850);
}); 

function ajax_action(task_id, action, status = 0, show){
    $.ajax({
        type: 'POST',
        url: "/module/tasks/tasks_ajax.inc.php",
        data: { 
            task_id : task_id,
            action : action,
            status : status,
            main_contact_id : <?= $GLOBALS['main_contact']['id'] ?>,
        }, 
        success: function(data){
            if(show == true){
                $('.task_item_list').html(data)
            }
        }
    });     
}

$(".change_task_group").on("change", function() {
    var selectedValue = $(this).val();
    $(".task_group_id_input").val(selectedValue);
    ajax_action(selectedValue, 'get_tasks', 0, true);
    $(".add_svg_content").show();
    if(selectedValue == "-1" || selectedValue == "-2"){
        $(".add_svg_content").hide();
    }
});

$(document).ready(function() {
    ajax_action(-2, 'get_tasks', 0, true);
    $(".add_svg_content").hide();
});

$(".add_svg_content").click(function() {
    $("form.create_task_dashboard").slideToggle();
}); 
</script>