



 $(document).ready(function () {


            
            
            categories();
            products();
            // product();
            clients();
            sellbills();
           

            getDaterange();
            function getDaterange() {$('#todate,#fromdate').datepicker();}
            $(".select2class").select2({});
            $(".select2products").select2({
                ajax: {
                    url: 'chartserp.php?do=select2products',
                    type: "post",
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            searchTerm: params.term,
                        };
                    },
                    processResults: function (response) {
                        return {
                            results: response
                        };
                    },
                    cache: true
                }
            });


            $(".select2clients").select2({
                ajax: {
                    url: 'chartserp.php?do=select2clients',
                    type: "post",
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            searchTerm: params.term,
                        };
                    },
                    processResults: function (response) {
                        return {
                            results: response
                        };
                    },
                    cache: true
                }
            });


            $(".select2categories").select2({
                ajax: {
                    url: 'chartserp.php?do=select2categories',
                    type: "post",
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            searchTerm: params.term,
                        };
                    },
                    processResults: function (response) {
                        return {
                            results: response
                        };
                    },
                    cache: true
                }
            });

             $('body').on('change', '#todate,#fromdate,#kindprice', function () {
              categories();
              products();
              // product();
              clients();
              sellbills(); 
              
            });

             $('body').on('change', '#select2categories', function () {
                  categories();
            });

            $('body').on('change', '#select2product', function () {
                  products();
            });
            $('body').on('change', '#select2client', function () {
                  clients();
            });
 







function categories() {
        var fromdate = $('#fromdate').val();
        var categorieid = $('#select2categories').val();
        var todate = $('#todate').val();
        var kindprice = $('#kindprice').val();
        $.post('chartserp.php?do=categories',{kindprice: kindprice, fromdate: fromdate, todate: todate, categorieid: categorieid},function (data){
        var data = jQuery.parseJSON(data);
        var idhtml = 'categories';
        var labeltext = 'التصنيفات  ';
          if(data.length > 0 && data.length < 20){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
            datalength(data, idhtml, labeltext, 'pie');
        }else if(data.length > 19 && data.length < 40){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
           datalength(data, idhtml, labeltext, 'line');
        }else if(data.length > 39 && data.length < 60){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
           datalength(data, idhtml, labeltext, 'bar');
        }else if(data.length > 59 && data.length < 80){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'radar');
        }else if(data.length > 79 && data.length < 90){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'polarArea');
        }else if(data.length > 89 && data.length < 101){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'pie');
        }else if(data.length > 100 && data.length < 109){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'doughnut');
        }else if(data.length > 108){
            $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'derivedBubble');
        }else{
            $('#t'+idhtml).html('');
            datalength(data = '', idhtml ='', labeltext ='', '');
        }
        
    });
}


function products() {
        var fromdate = $('#fromdate').val();
        var todate = $('#todate').val();
        var kindprice = $('#kindprice').val();
        var productid = $('#select2product').val();
        $.post('chartserp.php?do=products',{kindprice: kindprice, fromdate: fromdate, todate: todate, productid: productid},function (data){
        var data = jQuery.parseJSON(data);
        var idhtml = 'products';
        var labeltext = 'المنتجات  ';
        if(data.length > 0 && data.length < 20){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
            datalength(data, idhtml, labeltext, 'pie');
        }else if(data.length > 19 && data.length < 40){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
           datalength(data, idhtml, labeltext, 'line');
        }else if(data.length > 39 && data.length < 60){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
           datalength(data, idhtml, labeltext, 'bar');
        }else if(data.length > 59 && data.length < 80){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'radar');
        }else if(data.length > 79 && data.length < 90){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'polarArea');
        }else if(data.length > 89 && data.length < 101){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'pie');
        }else if(data.length > 100 && data.length < 109){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'doughnut');
        }else if(data.length > 108){
            $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'derivedBubble');
        }else{
            $('#t'+idhtml).html('');
            datalength(data = '', idhtml ='', labeltext ='', '');
        }

        
    });
}

// function product() {
//          var productid = $('#select2product').val();
//          var fromdate = $('#fromdate').val();
//          var todate = $('#todate').val();
//          var kindprice = $('#kindprice').val();
//          $.post('chartserp.php?do=product',{kindprice: kindprice, fromdate: fromdate, todate: todate, productid: productid},function (data){         
//         var data = jQuery.parseJSON(data);
//         var idhtml = 'product';
//         var labeltext = 'المنتج';
//         if(data.length > 0 && data.length < 20){
//             $('#scharts'+idhtml).addClass('col');
//             $('#table'+idhtml).addClass('col');
//             datalength(data, idhtml, labeltext, 'pie');
//         }else if(data.length > 19 && data.length < 40){
//             $('#scharts'+idhtml).addClass('col');
//             $('#table'+idhtml).addClass('col');
//            datalength(data, idhtml, labeltext, 'line');
//         }else if(data.length > 39 && data.length < 60){
//             $('#scharts'+idhtml).addClass('col');
//             $('#table'+idhtml).addClass('col');
//            datalength(data, idhtml, labeltext, 'bar');
//         }else if(data.length > 59 && data.length < 80){
//               $('#scharts'+idhtml).removeClass('col');
//             $('#table'+idhtml).removeClass('col');
//            datalength(data, idhtml, labeltext, 'radar');
//         }else if(data.length > 79 && data.length < 90){
//               $('#scharts'+idhtml).removeClass('col');
//             $('#table'+idhtml).removeClass('col');
//            datalength(data, idhtml, labeltext, 'polarArea');
//         }else if(data.length > 89 && data.length < 101){
//               $('#scharts'+idhtml).removeClass('col');
//             $('#table'+idhtml).removeClass('col');
//            datalength(data, idhtml, labeltext, 'pie');
//         }else if(data.length > 100 && data.length < 109){
//               $('#scharts'+idhtml).removeClass('col');
//             $('#table'+idhtml).removeClass('col');
//            datalength(data, idhtml, labeltext, 'doughnut');
//         }else if(data.length > 108){
//             $('#scharts'+idhtml).removeClass('col');
//             $('#table'+idhtml).removeClass('col');
//            datalength(data, idhtml, labeltext, 'derivedBubble');
//         }else{
//             $('#t'+idhtml).html('');
//             datalength(data = '', idhtml ='', labeltext ='', '');
//           // derivedBubble
//         }

        
//     });
// }


function clients() {
         var clientid = $('#select2client').val();
         var fromdate = $('#fromdate').val();
         var todate = $('#todate').val();
         var kindprice = $('#kindprice').val();
         $.post('chartserp.php?do=clients',{kindprice: kindprice, fromdate: fromdate, todate: todate, clientid: clientid},function (data){         
        var data = jQuery.parseJSON(data);
        var idhtml = 'clients';
        var labeltext = 'العميل ';
        if(data.length > 0 && data.length < 20){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
            datalength(data, idhtml, labeltext, 'pie');
        }else if(data.length > 19 && data.length < 40){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
           datalength(data, idhtml, labeltext, 'line');
        }else if(data.length > 39 && data.length < 60){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
           datalength(data, idhtml, labeltext, 'bar');
        }else if(data.length > 59 && data.length < 80){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'radar');
        }else if(data.length > 79 && data.length < 90){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'polarArea');
        }else if(data.length > 89 && data.length < 101){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'pie');
        }else if(data.length > 100 && data.length < 109){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'doughnut');
        }else if(data.length > 108){
            $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'derivedBubble');
        }else{
            $('#t'+idhtml).html('');
            datalength(data = '', idhtml ='', labeltext ='', '');
          // derivedBubble
        }

        
    });
}



function sellbills() {
        var fromdate = $('#fromdate').val();
        var todate = $('#todate').val();
        var kindprice = $('#kindprice').val();
        $.post('chartserp.php?do=sellbills',{kindprice: kindprice, fromdate: fromdate, todate: todate},function (data){
        var data = jQuery.parseJSON(data);
        var idhtml = 'sellbills';
        var labeltext = ' فواتير المبيعات ';
          if(data.length > 0 && data.length < 20){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
            datalength(data, idhtml, labeltext, 'pie');
        }else if(data.length > 19 && data.length < 40){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
           datalength(data, idhtml, labeltext, 'line');
        }else if(data.length > 39 && data.length < 60){
            $('#scharts'+idhtml).addClass('col');
            $('#table'+idhtml).addClass('col');
           datalength(data, idhtml, labeltext, 'bar');
        }else if(data.length > 59 && data.length < 80){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'radar');
        }else if(data.length > 79 && data.length < 90){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'polarArea');
        }else if(data.length > 89 && data.length < 101){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'pie');
        }else if(data.length > 100 && data.length < 109){
              $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'doughnut');
        }else if(data.length > 108){
            $('#scharts'+idhtml).removeClass('col');
            $('#table'+idhtml).removeClass('col');
           datalength(data, idhtml, labeltext, 'derivedBubble');
        }else{
            $('#t'+idhtml).html('');
            datalength(data = '', idhtml ='', labeltext ='', '');
        }
        
    });
}



function  datalength(data, idhtml, labeltext, types){

            var name = [];
            var marks = [];
            var random = [];
            var total = 0;
            var totalpro = 0;
            var totalquantity = 0;
            $('#t'+idhtml).html('');
            for (var i in data) {
                 var quantitys = Math.floor(data[i]['quantitys']);
                 var totals = Math.floor(data[i]['totals']);
                 var totalpros = Math.floor(data[i]['totalpro']);
                 var Names = data[i]['Namedata'];
                 name.push(Names);
                 var profit = totals - totalpros; 
                 marks.push(profit);
                 total += totals;
                 totalpro += totalpros;
                 totalquantity += quantitys;
                 $('#t'+idhtml).append('<tr><td>'+Names+'</td><td>'+quantitys+'</td><td>'+totals+'</td><td>'+totalpros+'</td><td>'+profit+'</td></tr>');
                 random.push('rgba('+Math.floor(Math.random() * 300)+', '+Math.floor(Math.random() * 500)+', '+Math.floor(Math.random() * 300)+', 0.2)');

            }
            $('.total'+idhtml).text(total);
            $('.totalpro'+idhtml).text(totalpro);
            $('.totalquantity'+idhtml).text(totalquantity);
            $('.profit'+idhtml).text(total - totalpro);
     var chartdata = {
            labels: name,
            datasets: [
                {
                    label: labeltext,
                    backgroundColor: '#49e2ff',
                    borderColor: '#000000',
                    hoverBackgroundColor: '#000000',
                    hoverBorderColor: '#666666',
                    data: marks,
                    backgroundColor:random,
                    borderColor: random,
                    borderWidth: 3
                }
            ]
        };

        var graphTarget = $('#'+idhtml);
        var barGraph = new Chart(graphTarget, {
            type: types,
            data: chartdata
        });

}    


      
});
       
        
 
