/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


function filterDropdown(elem){
    var value ;
    if(elem>0)
    {
        value = elem;
    }else {
         value = $(elem).val();
    }

    $('#client-list-div').hide();
    $('#team-list-div').hide();
    
    var formData = {};
    if(value > 0){
        if(value == 4){
            $('#client-list-div').show();
        }else if(value == 3){
            $('#team-list-div').show();
        }
        
        if(value == 1){
            formData['action'] = 'GET_EVERYONE_DATA'
        }else if(value == 2){
            formData['action'] = 'GET_ME_DATA'
        }else if(value == 3){
            formData['action'] = 'GET_TEAM_DATA'
        }else if(value == 4){
            formData['action'] = 'GET_CLIENT_DATA'
        }
        // ajaxRequest(formData);
    }
}

function ajaxRequest(formData){
    $('.loading').show();
    $.ajax({
        type: "POST",
        data: formData,
        url: EVERYTHING_URL,
        success: function (data) {
            $('.loading').hide();
            var jsonData = JSON.parse(data);
            if(jsonData){
                $('#timeline-content-area').html(jsonData)
            }
        }
    });
}



$('select[name=client_id]').select2({
    placeholder: 'Select a Client',
    ajax: {
        url: EVERYTHING_URL_CLIENT_LIST,
        dataType: 'json',
        delay: 250,
        data: function (data) {
            return {
                searchTerm: data.term // search term
            };
        },
        processResults: function (response) {
            return {
                results:response
            };
        },
        cache: true
    },

});

$('select[name=team_id]').select2({
    placeholder: 'Select a Team Member',
    ajax: {
        url: EVERYTHING_URL_TEAM_LIST,
        dataType: 'json',
        delay: 250,
        data: function (data) {
            return {
                searchTerm: data.term // search term
            };
        },
        processResults: function (response) {
            return {
                results:response
            };
        },
        cache: true
    }
});


function clientSelect(elem){
    var value = $(elem).val();
    $('select[name=client_list]').val(value).select2('refresh');
    var formData = {};
    if(value > 0){
        formData['action'] = 'GET_CLIENT_INDIVIDUAL_DATA'
        formData['client_id'] = value;
    }
    ajaxRequest(formData);
}

function teamMemberSelect(elem){
    var value = $(elem).val();
    $('select[name=team_list]').select2("val", value).trigger('change');
        var formData = {};
    if(value > 0){
        formData['action'] = 'GET_TEAM_INDIVIDUAL_DATA'
        formData['employee_id'] = value;
    }
    ajaxRequest(formData);
}

// Fetch the preselected item, and add to the control
var team_list = $('select[name=team_id]');
$.ajax({
    url: EVERYTHING_URL_TEAM_LIST,
    dataType: 'json',
    delay: 250,
    data: function (data) {
        return {
            searchTerm: data.term // search term
        };
    },
}).then(function (data) {
    if($('#filter_id').val() == 3) {
        const params = (new URL(location)).searchParams;
        var emp_id = parseInt(params.get('team_id'));
        var name = data.find(x => x.id === emp_id).text;
        // create the option and append to Select2
        var option = new Option(name, emp_id, true, true);
        team_list.append(option).trigger('change');

        // manually trigger the `select2:select` event
        team_list.trigger({
            type: 'select2:select',
            params: {
                data: data
            }
        });
    }
    else {
        var option = new Option('Select a Team Member', null, false, false);
        team_list.append(option).trigger('change');
        team_list.trigger({
            type: 'select2:select',
            params: {
                data: data
            }
        });
    }
});

var client_list = $('select[name=client_id]');
$.ajax({
    url: EVERYTHING_URL_CLIENT_LIST,
    dataType: 'json',
    delay: 250,
    data: function (data) {
        return {
            searchTerm: data.term // search term
        };
    },
}).then(function (data) {
    if($('#filter_id').val() == 4) {
        const params = (new URL(location)).searchParams;
        var client_id = parseInt(params.get('client_id'));
        var name = data.find(x => x.id === client_id).text;
        // create the option and append to Select2
        var option = new Option(name, client_id, true, true);
        client_list.append(option).trigger('change');

        // manually trigger the `select2:select` event
        client_list.trigger({
            type: 'select2:select',
            params: {
                data: data
            }
        });
    } else {
        var option = new Option('Select a Client', null, false, false);
        client_list.append(option).trigger('change');
        client_list.trigger({
            type: 'select2:select',
            params: {
                data: data
            }
        });
    }
});