/**

 * SensFRX Fraud Management Center - Filter Fix

 * This file ensures all filters work properly

 */



// Wait for jQuery to be ready

(function($) {

    'use strict';



    // Function to apply risk filters

    window.applyRiskFilter = function() {

        const activeFilters = [];



        $('.fmc-risk-card.active').each(function() {

            activeFilters.push($(this).attr('data-risk'));

        });





        if (activeFilters.length === 0) {

            $('#fraud-list-tbody tr').show();

            return;

        }



        $('#fraud-list-tbody tr').each(function() {

            const scoreElement = $(this).find('td:eq(3) span');

            const score = parseInt(scoreElement.text());

            let riskLevel;



            if (score >= 70) {

                riskLevel = 'high';

            } else if (score >= 40) {

                riskLevel = 'medium';

            } else {

                riskLevel = 'low';

            }



            if (activeFilters.indexOf(riskLevel) > -1) {

                $(this).show();

            } else {

                $(this).hide();

            }

        });

    };



    // Ensure filters work after page loads

    $(document).ready(function() {



        // Give the page time to initialize

        setTimeout(function() {



            // Fix risk card filters

            $('.fmc-risk-card').off('click').on('click', function(e) {

                e.preventDefault();

                e.stopPropagation();



                const card = $(this);

                const isActive = card.hasClass('active');



                if (isActive) {

                    card.removeClass('active').css({

                        'background': 'white',

                        'border-color': '#e2e8f0'

                    });

                } else {

                    card.addClass('active').css({

                        'background': '#f0f4ff',

                        'border-color': '#667eea'

                    });

                }



                applyRiskFilter();

            });



            // Fix search filter

            $('#fmc-search').off('keyup input').on('keyup input', function() {

                const searchTerm = $(this).val().toLowerCase();



                $('#fraud-list-tbody tr').each(function() {

                    const rowText = $(this).text().toLowerCase();

                    $(this).toggle(searchTerm === '' || rowText.indexOf(searchTerm) > -1);

                });

            });



            // Ensure sample data is visible

            if ($('#fraud-list-tbody tr').length > 0) {

                $('#fraud-list-loading').hide();

                $('#fraud-list-content').show();

            }





        }, 1000); // Wait 1 second for everything to load

    });



})(jQuery);