$(document).ready(function () {
    $('.check-filter').click(function(){
        if($(this).is(':checked'))
            $('.'+$(this).attr('id')).show();
        else
            $('.'+$(this).attr('id')).hide();
    });

    $('.direct_patient').on('change', function(){
        $('#direct_modal').modal('toggle');
        $('#send_direction').data('routing_id', $(this).data('routing_id'));
        $('#send_direction').data('clinic_id', $(this).val());

        $('#dm_patinet').html($(this).data('patient_name'));
        $('#dm_clinic').html($(this).children(':selected').text());
    });

    $('#send_direction').on('click', function(){
        var params = {
            routing_id : $(this).data('routing_id'),
            clinic_id : $(this).data('clinic_id')
        };
        $.ajax({
            type:'POST',
            url: '/route_patient',
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            data: JSON.stringify(params),
            success: (data) => {
                console.log('data')
                if(data['success'] == true)
                {
                    $('#direct_modal').modal('hide');
                } 
            },
            error:function (jqXHR, exception) {
                after_error();
            }
        });
    });



    $('.patient-sort').click(function(){
        $('.check-filter').prop('checked', true);
        if($(this).attr('selected')){
            $(this).removeAttr('selected');
            $('.routing-card').show();
            return;
        }
        else{
            $(this).attr('selected', '1');
            var patient_id = $(this).data('patient_id');
            $.each($('.routing-card'), function(e) {
                if($(this).data('patient_id') == patient_id)
                {
                    $(this).show();
                }
                else
                {
                    $(this).hide();
                }
            })
        }
    });
    $('.excl_dwnld').on('click', function() {
        $.ajax({
            type: 'GET',
            url: '/dwnld_excl',
            processData: false,  // tell jQuery not to process the data
            contentType: false,  // tell jQuery not to set contentType
            success: (data) => {
                var a = document.createElement('a');
                a.href = '/excel/'+ data['file'];
                filename = $('#clinic_name').val()+'_report'
                a.download = filename + '.xlsx';
                document.body.append(a);
                a.click();
                a.remove();
                window.URL.revokeObjectURL('/excel/'+ data['file']);
            },
        })
    })

});
