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


   function report(){
    var manageTable = $('#expense-category-table').DataTable();
    manageTable.destroy();
    manageTable= $("#expense-category-table").DataTable({
      "ajax": {
                "url": 'fetch-expense-category-data',
                'type': "GET",
                
               },
      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))'
                    }
                }],

               
      });
   }




 $(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-expense-category-data/' + id,
                success: function (data) {
                report();   
                swal("Successfully deleted!", {
                icon: "success",
                });
                }         
            });
          
        } 
        });
 
});

function expense(){
    var datefrom=$("#datefrom").val();
    var dateto=$("#dateto").val();
    var expense_category_id=$("#expense_category_id").val();
    var manageTable = $('#expense-table').DataTable();
    manageTable.destroy();
    manageTable= $("#expense-table").DataTable({
      "ajax": {
                "url": 'fetch-expense-data',
                'type': "GET",
                'data':{datefrom:datefrom,dateto:dateto,expense_category_id:expense_category_id},
               },
      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 total=api
                        .column(5, {page: 'current'})
                        .data()
                        .reduce(function (a, b) {

                            return intVal(a) + intVal(b);
                        }, 0);
                  $(api.column(0).footer()).html('TOTAL');
                  $(api.column(5).footer()).html(' ' + total.toFixed(2));

            }
      });
   }


    $(document).on('click', '#delete-expense', 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-expense/' + id,
                success: function (data) {
                expense();   
                swal("Successfully deleted!", {
                icon: "success",
                });
                }         
            });
          
        } 
        });
 
});