/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$('.daterange').daterangepicker({
    ranges   : {
      'Today'       : [moment(), moment()],
      'Yesterday'   : [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
      'Last 7 Days' : [moment().subtract(6, 'days'), moment()],
      'Last 30 Days': [moment().subtract(29, 'days'), moment()],
      'This Month'  : [moment().startOf('month'), moment().endOf('month')],
      'Last Month'  : [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
    },
    startDate: moment().startOf('month'),
    endDate  : moment().endOf('month')
  }, function (start, end) {
        _purchasesChart(start.format('DD/MM/YYYY'),end.format('DD/MM/YYYY'),);
  });
  
  function _purchasesChart(startDate,endDate){
    $.post('/Controller/Compra.php', {
            action: 'ajax',
            request: 'getTotalPurchases',
            start: startDate,
            end: endDate
        }, function(data) {
            $("#canvas_chart_purchases").remove();
            $("#chart_purchases").append('<canvas id="canvas_chart_purchases"></canvas>');
            
            if (data.response){                
                $("#tbl_purchases").find('tbody').empty().append(data.purchasesData);
                $("#tbl_purchases").find('caption').empty().append(data.caption);
                var vendor = [];
                var purchases = [];
                for (var name in data.chartData) {
                    vendor.push(name);
                    purchases.push(data.chartData[name]);
                }     
                var barChartData = {
                        labels: vendor,
                        datasets: [{
                                label: 'Compras',
                                backgroundColor: '#3b83bd',
                                borderColor: '#3b83bd',
                                borderWidth: 1,
                                data: purchases
                        }]
                };             
                
                var ctx = document.getElementById('canvas_chart_purchases').getContext('2d');
                window.myBar = new Chart(ctx, {
                        type: 'bar',
                        data: barChartData,
                        options: {
                            responsive: true,
                            legend: {
                                position: 'top'
                            },
                            title: {
                                display: true,
                                text: ['Compras por proveedor',data.caption,'Total compras $'+data.totalPurchases.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")]
                            },
                            scales: {
                                yAxes: [{
                                  ticks: {
                                    beginAtZero: true,
                                    callback: function(value, index, values) {
                                      if(parseInt(value) >= 1000){
                                        return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                                      } else {
                                        return '$' + value;
                                      }
                                    }
                                  }
                                }]
                            },
                            tooltips: {
                                callbacks: {
                                    label: function(tooltipItems, data) {
                                    return "$" + tooltipItems.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
                                    }
                                }
                            }
                        }
                });
            }else{
                $("#tbl_purchases").find('caption').empty().append('');
                $("#tbl_purchases").find('tbody').empty().append("<td colspan='2' class='text-center' style='padding:10px'>No se encontraton resultados.</td>");                
            }
        }, 'json');
  }
 