
const dataMap = {
    all: "All COVID-19 related Incidents",
    covid: "COVID-19",
    lock: "Lockdown Enforcement",
    qua: "Quarantine Management",
    rel: "COVID-19 Relief Distribution",
    irr: "Irregularities in Commodity Market",
    pre: "Prejudice and Descrimination",
    hea: 'Healthcare Management'
}
function changeData(e) {
    const value = e.value;
    // clearExistingSource()
    if(value === "covid") {
        $("#barChartData").load('showBarData.php?code=all', {});
        loadCovidData()
    } else {
        removeProvCovidSources()
        //alert(value);
        var pro = $('#province-select').val();
        $("#provinceChartHere").load('showProvinceIncidents.php?data=' + value, {});
        if(pro === ""){
        $("#barChartData").load('showBarData.php?code='+value, {});
        //showHorizontal(value);
        } else {
            $("#barChartData").load('showBarDataProvinceWise.php?code=' + value + '&value=' + pro, {});
            //showHorizontal(value);
            
        }
        dataSelectChange()
    }
}


function clearExistingSource() {
    let latlng = [84.1240, 28.3949]
    var zoom = 6
    map.flyTo({
        center: {
            lat: latlng[1],
            lng: latlng[0]
        },
        speed: 0.4,
        zoom: zoom
    });
    $('#district-select').selectpicker('deselectAll');
    $('#district-select').selectpicker('refresh');
    $('#province-select').selectpicker('deselectAll');
    $('#province-select').selectpicker('refresh');

    let sources = map.getStyle().sources
    let layers = map.getStyle().layers
    for(var i in layers) {
        let layer = layers[i]
        var key = Object.keys(layer)[0];
        var ly = layer[key]
        var provLayer = ly.startsWith("prov");
        var distLayer = ly.startsWith("district");
        if(provLayer || distLayer) {
            map.removeLayer(ly)
        }
    }
    Object.keys(sources).map(function (k) {

        var provLayer = k.startsWith("prov");
        var distLayer = k.startsWith("district");
        if(provLayer || distLayer) {
            map.removeSource(k)
        }
    })

}
