
'use strict';

var intVal = function(i) {
    return typeof i === 'string' ? i.replace(/[\$,]/g, '') * 1 : typeof i === 'number' ? i : 0;
};

setInterval(function(){
    var last_id = $('#last_id').val();
    let srcaudio = './audio/dingdong.wav';
    // console.info('Last ID : ' + last_id);
    $.get('notif.php', {last_id: last_id})
    .done(function(n){
        // var r = JSON.(n);
        // console.log(n);

        if( n.result == true ) {
            
            
            var i,
                data = n.data,
                countrycodes = n.countrycodes,
                last = n.last_id,
                list = '',
                num=0,
                total = 0;
                // lastData = n.lastData;
                // console.log(lastData);
            // for (i = 0; i < lastData.length; i++) {
            //     var img = 'image/icon.png',
            //         text = (_.invert(countrycodes))[lastData[i].country]+' : '+lastData[i].currency_symbol+lastData[i].payout;
            //     if (!("Notification" in window)) {
            //         alert("This browser does not support desktop notification");
            //     }
            
            //     // Let's check whether notification permissions have already been granted
            //     else if (Notification.permission === "granted") {
            //     // If it's okay let's create a notification
            //     var notification = new Notification(lastData[i].subid, {
            //         body: text,
            //         icon: img
            //         });
            //     }
            
            //     // Otherwise, we need to ask the user for permission
            //     else if (Notification.permission !== "denied") {
            //     Notification.requestPermission().then(function (permission) {
            //         // If the user accepts, let's create a notification
            //         if (permission === "granted") {
            //         var notification = new Notification(lastData[i].subid, {
            //             body: text,
            //             icon: img
            //             });
            //         }
            //     });
            //     }
            // }
            for (i = 0; i < data.length; i++) {
                // $('#toast').append('<div class="toast" role="alert" aria-live="assertive" aria-atomic="true" data-delay="3000">\
                //     <div class="toast-header">\
                //     <strong class="mr-auto">New Lead</strong>\
                //     <small>just now</small>\
                //     <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">\
                //         <span aria-hidden="true">&times;</span>\
                //     </button>\
                //     </div>\
                //     <div class="toast-body">\
                //     '+data[i].subid+'\
                //     </div>\
                // </div>');

                
                total = intVal(data[i].payout)+total;
                num = i+1;
                var ctr = data[i].country,
                    usericon = "",
                    os = '',
                    network = '',
                    payout = Math.round(data[i].payout * 100) / 100;
                console.log(ctr);
                if(ctr) {
                    ctr = ctr.toLowerCase();
                } else {
                    ctr = ctr;
                }
                if(data[i].topEarning == data[i].subid) {
                    usericon = "<i class='fa fa-crown text-warning'></i>";
                } else {
                    usericon = "<i class='fa fa-user'></i>";
                }
                if(data[i].os == "web" || data[i].os == "desktop" || data[i].os == "Windows" || data[i].os == "Linux"  || data[i].os == "MacOs" ) {
                    os = "<img src='image/icon-laptop.png' style='height: 20px;'>";
                }
                if(data[i].os == "WAP" || data[i].os == "android" || data[i].os == "ios" || data[i].os == "Android" || data[i].os == "iOS") {
                    os = "<img src='image/icon-cellphone.png' style='height: 20px;'>";
                }
                if(data[i].os == "tablet") {
                    os = "<img src='image/icon-tablet.png' style='height: 20px;'>";
                } 
                if(data[i].network == "imonetizeit") {
                    network = "<div class='bg-success' style='font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>"+data[i].network+"</div>";
                } else if(data[i].network == "adverten") {
                    network = "<div class='bg-danger' style='font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>"+data[i].network+"</div>";
                } else if(data[i].network == "lospollos") {
                    network = "<div class='bg-warning' style='font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>"+data[i].network+"</div>";
                } else if(data[i].network == "clickdealer") {
                    network = "<div class='bg-primary' style='font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>"+data[i].network+"</div>";
                } else if(data[i].network == "datify") {
                    network = "<div class='' style='background-color: magenta; color: white; font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>"+data[i].network+"</div>";
                } else {
                    network = "<div class='' style='background-color: #FFE400; color: white; font-size: .9em; color: white; padding: 2px 10px; border-radius: 4px'>"+data[i].network+"</div>";
                }

                list += '<tr>\
                    <td width="40">'+num+'</td>\
                <td><a href="statsID.php?id='+data[i].subid+'">'+usericon+' '+data[i].subid+'</a></td>\
                <td class=\'text-center\'>'+network+'</td>\
                <td class=\'text-center\'>'+os+'</td>\
                <td class=\'text-center\'><span class=\"flag-icon flag-icon-'+ctr+'\"></span> '+data[i].country+'</td>\
                <td class=\'text-center\'>'+payout+'</td>\
                </tr>';
                
                // var id = data[i].id;
                // console.log(countrycodes);
            }
            
        $('tbody').html(list);
        var api = $('#myTable').dataTable().api();
        $(api.column(5).footer()).html('<b>$'+total.toFixed(2)+'</b>');
        // total = api.column(4).data().reduce(function(a, b) {
        //     return intVal(a) + intVal(b);
        // }, 0);
        // $('.toast').toast('show');
        $('#last_id').val(last).trigger('change');
        $('#totalearning').text('$ ' + total.toFixed(2));
        // var $val = $.fn.val;
        // $.fn.val = function(newVal) {
        //     if (this.attr('id') === 'last_id') this.trigger('change');
        //     $val.call(this, newVal);
        // }
        $('#last_id').on('change', function(){
            // if ( true ) {
            //     $('#ding').get(0).play();
            // } else {
            //     $('#ding').get(0).play();
            // }
            // let srcaudio = './audio/dingdong.wav';
            let audio = new Audio(srcaudio);
            audio.muted = false;
            audio.play();
            // alert('Play');
        });
        
        } 
    });

}, 5000);

// $('#toast').on('hide.bs.toast', function () {
//     $('.toast').toast('dispose');
// })
