  $(document).on('change', 'input#qty', function (e) {
  var quantity = 0, unit_price = 0;var tax=0;var total=0;
  quantity=$('#qty').val();
  unit_price=$('#price').val();
  tax=$('#tax').val();
  tax_amount=((quantity*unit_price)*tax/100);
  $('#final_amount').val((quantity*unit_price)+tax_amount);
  });
  $(document).on('change', 'input#price', function (e) {
  var quantity = 0, unit_price = 0;var tax=0;var total=0;
  quantity=$('#qty').val();
  unit_price=$('#price').val();
  tax=$('#tax').val();
  tax_amount=((quantity*unit_price)*tax/100);
  $('#final_amount').val((quantity*unit_price)+tax_amount);
  });
  $(document).on('change', 'input#tax_new', function (e) {
  var quantity = 0, unit_price = 0;var tax=0;var total=0;
  quantity=$('#qty').val();
  unit_price=$('#price').val();
  tax=$('#tax_new').val();
  tax_amount=((quantity*unit_price)*tax/100);
  $('#final_amount').val((quantity*unit_price)+tax_amount);
  });

  $( document ).ready(function() {
    report();
   
});

function report(){
   
    var datefrom=$("#datefrom").val();
    var dateto=$("#dateto").val();
    var manageTable = $('#purchase-table').DataTable();
    manageTable.destroy();
    manageTable= $("#purchase-table").DataTable({
      "ajax": {
                "url": 'fetch-purchase-data',
                'type': "GET",
                'data':{datefrom:datefrom,dateto:dateto},
               },
    //   lengthMenu: [
    //             [10, 25, 50, 100, -1],
    //             ['10', '25', '50', '100', 'All']],
    //             "responsive": false, "lengthChange": false, "autoWidth": false,
     "order": [[0, 'asc']],
       dom: "<'row'<'col-sm-4'l><'col-sm-4 text-center'B><'col-sm-4'f>>" +
                    "<'row'<'col-sm-12'tr>>" +
                    "<'row'<'col-sm-6'i><'col-sm-6'p>>",
            buttons: [{
                    extend: 'copy',
                    exportOptions: {
                        columns: ':visible:not(:eq(9))'
                    }
                },
                {
                    extend: 'csv',
                    exportOptions: {
                        columns: ':visible:not(:eq(9))'
                    }
                },
                {
                    extend: 'print',
                    exportOptions: {
                        columns: ':visible:not(:eq(9))',
                    }

                },
                {
                    extend: 'excel',
                    exportOptions: {
                        columns: ':visible:not(:eq(9))'
                    }
                },
                {
                    extend: 'pdf',
                    exportOptions: {
                        columns: ':visible:not(:eq(9))'
                    }
                }],

                 "footerCallback": function (row, data, start, end, display) {
                var api = this.api(), data;

                // converting to interger to find total

                var intVal = function (i) {


                    if (typeof i === 'string') {

                        i = i.replace(/[\a-zA-Z₹]/g, '') * 1;

                    }
                    // check if you got a valid number.
                    if (Number.isNaN(i)) {
                        return 0;
                    }
                    return i;
                };
                  var price=api
                        .column(7, {page: 'current'})
                        .data()
                        .reduce(function (a, b) {
                        return intVal(a) + intVal(b);
                        }, 0);
                          var qty=api
                        .column(6, {page: 'current'})
                        .data()
                        .reduce(function (a, b) {
                        return intVal(a) + intVal(b);
                        }, 0);

                var total=api
                        .column(9, {page: 'current'})
                        .data()
                        .reduce(function (a, b) {
                        return intVal(a) + intVal(b);
                        }, 0);
                  $(api.column(0).footer()).html('TOTAL');
                   $(api.column(7).footer()).html(' ' + price.toFixed(2));
                    $(api.column(6).footer()).html(' ' + qty.toFixed(2));
                  $(api.column(9).footer()).html(' ' + total.toFixed(2));

            }
      });
}




 $(document).on('click', '#delete', function (e) {

    e.preventDefault();
    var id = $(this).data('id');
    
        swal({
          title: "Are you sure?",
          icon: "warning",
          buttons: true,
          dangerMode: true,
        })
        .then((willDelete) => {
          if (willDelete) {
            $.ajaxSetup({
            headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
            });
            $.ajax({
                type: "POST",
                url: 'delete-purchase-data/' + id,
                success: function (data) {
                report();   
                swal("Successfully deleted!", {
                icon: "success",
                });
                }         
            });
          
        } 
        });
 
});