$(document).ready(function () {
    initEvents();
    loadIndicators();
});

function initEvents() {
    $('#selected_year').change(loadIndicators);
}

function loadIndicators() {
    let url = BASE_URL + '/getIndicatorEntriesData';
    let _data = {
        year: $('#selected_year').val()
    };
    showLoading();
    $.ajax({
        type: 'post',
        url: url,
        data: _data,
        success: function (data) {
            $('#indicatorEntries').html(data);
            $('.txt_indicator_entry').change(function(){ saveIndicatorValue(this);});
        }
    });
}

function addIndicatorEntry() {
    let url = BASE_URL + '/addIndicatorEntry';
    let _data = {
        indicator_id: $('#indicator_id').val(),
        year: $('#selected_year').val()
    };
    showLoading();
    $.ajax({
        type: 'post',
        url: url,
        data: _data,
        success: function (data) {
            loadIndicators();
        }
    });
    return false;
}

function showLoading() {
    $('#indicatorEntries').html('Loading...');
}

function saveIndicatorValue(input) {
    let _input = $(input);
    
    let _data = {
        value: _input.val(),
        month: _input.data('month'),
        indicator_id: _input.data('indicator'),
        year: $('#selected_year').val()
    };
    
    let url = BASE_URL + '/saveIndicatorEntryValue';
    $.ajax({
        type: 'post',
        url: url,
        data: _data,
        success: function (data) {            
        }
    });
}