jQuery(document).ready(function($) {
    $('#submit_button').on('click', function() {
        var $btn = $(this);
        var pdfname = $('.pdf-history').val();
        var customer_id = $('.customer-list').val();

        if ( customer_id != null && customer_id.length > 0 ) {
            $btn.val(kgdc_admin_history.sending).css('opacity', '0.7').prop('disabled', true);
            
            var data = {
                action: 'kgdc_email_user',
                customer_id: customer_id,
                pdfname: pdfname
            };
            
            if ( typeof kgdc_admin !== 'undefined' && kgdc_admin.email_nonce ) {
                data.security = kgdc_admin.email_nonce;
            }

            var ajaxUrl = ( typeof kgdc_admin !== 'undefined' && kgdc_admin.ajax_url ) ? kgdc_admin.ajax_url : ajaxurl;
            
            $.post( ajaxUrl, data, function( response ) {
                $btn.val(kgdc_admin_history.send_now).css('opacity', '1').prop('disabled', false);
                if ( response && response.success ) {
                    alert( response.data );
                    tb_remove();
                } else {
                    alert( response.data || kgdc_admin_history.unexpected_error );
                }
            } ).fail( function() {
                $btn.val(kgdc_admin_history.send_now).css('opacity', '1').prop('disabled', false);
                alert( kgdc_admin_history.request_failed );
            } );
        } else {
            alert( kgdc_admin_history.select_customer );
        }
    } );
} );
