$(document).ready(function() {
    $('#division').select2();
    $('#block').select2();
    $('#damageType').select2();
    $('#division-update').select2();
    $('#block-update').select2();
    $('#damageType-update').select2();

    $.ajax({
        url: 'get_locations.php',
        method: 'GET',
        success: function(data) {
            var divisions = JSON.parse(data);
            divisions.forEach(function(division) {
                $('#division').append(new Option(division, division));
            });
            $('#division').select2();
        }
    });

    $('#division').change(function() {
        var selectedDivision = $(this).val();
        $('#block').empty().append(new Option('Pilih Blok', '')); 
        $('#block').select2(); 
    
        $.ajax({
            url: 'get_blocks.php',
            method: 'GET',
            data: { division: selectedDivision },
            success: function(data) {
                var blocks = JSON.parse(data);
                blocks.forEach(function(block) {
                    $('#block').append(new Option(block, block));
                });
                
                $('#block').select2();
            }
        });
    });

    $.ajax({
        url: 'fetch_report.php',
        method: 'GET',
        success: function(data) {
            $('#report-data').html(data);
        },
        error: function() {
            $('#report-data').html('<tr><td colspan="10">Gagal memuat data</td></tr>');
        }
    });

    $.ajax({
        url: 'fetch_photo.php',
        method: 'GET',
        success: function(data) {
            $('#photo-view-data').html(data); 
        },
        error: function() {
            $('#photo-view-data').html('<td>Gagal memuat data</td></tr>');
        }
    });

    function loadData(searchKeyword = '') {
        $.ajax({
            url: 'fetch_report.php',
            type: 'GET',
            data: { search: searchKeyword },
            success: function(data) {
                $('#report-data').html(data);
            },
            error: function(xhr, status, error) {
                console.error('Error fetching data:', error);
            }
        });
    }

    function getQueryParameter(name) {
        const urlParams = new URLSearchParams(window.location.search);
        return urlParams.get(name);
    }

    const initialSearch = getQueryParameter('search');
    if (initialSearch) {
        $('#searchInput').val(initialSearch);
        loadData(initialSearch);
    } else {
        loadData();
    }

    $('#searchInput').on('input', function() {
        const searchValue = $(this).val();
        loadData(searchValue);
    });

    $('#searchButton').on('click', function() {
        const searchValue = $('#searchInput').val();
        if (searchValue) {
            const newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?search=' + encodeURIComponent(searchValue);
            window.history.pushState({ path: newUrl }, '', newUrl);
            loadData(searchValue);
        }
    });
});
