$( document ).ready(function() {
purchase_report();
expense(); 
income_report();
stock_report();
payment_voucher();
});

function expense(){
    var datefrom=$("#datefrom").val();
    var dateto=$("#dateto").val();
    var manageTable = $('#expense-report').DataTable();
    manageTable.destroy();
    manageTable= $("#expense-report").DataTable({
      "ajax": {
                "url": 'fetch-expense-report',
                '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 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));

            }
      });
   }

   function purchase_report(){
   
    var datefrom=$("#datefrom").val();
    var dateto=$("#dateto").val();
    var manageTable = $('#purchase-report').DataTable();
    manageTable.destroy();
    manageTable= $("#purchase-report").DataTable({
      "ajax": {
                "url": 'fetch-purchase-report',
                '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 qty=api
                        .column(6, {page: 'current'})
                        .data()
                        .reduce(function (a, b) {

                            return intVal(a) + intVal(b);
                        }, 0);
                  var price=api
                        .column(7, {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(6).footer()).html(' ' + qty.toFixed(2));
                  $(api.column(7).footer()).html(' ' + price.toFixed(2));
                  $(api.column(9).footer()).html(' ' + total.toFixed(2));

            }
      });
}


function income_report(){
    var company_id=$("#company_id").val();
    var datefrom=$("#datefrom").val();
    var dateto=$("#dateto").val();
    var manageTable = $('#income-report').DataTable();
    manageTable.destroy();
    manageTable= $("#income-report").DataTable({
      "ajax": {
                "url": 'fetch-income-report',
                'type': "GET",
                'data':{company_id:company_id,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(7))'
                    }
                },
                {
                    extend: 'csv',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))'
                    }
                },
                {
                    extend: 'print',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))',
                    }

                },
                {
                    extend: 'excel',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))'
                    }
                },
                {
                    extend: 'pdf',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))'
                    }
                }],
                   "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));
                  // $(api.column(6).footer()).html(' ' + total.toFixed(2));

            }
      });
}

function stock_report(){
  
    var manageTable = $('#stock-table').DataTable();
    manageTable.destroy();
    manageTable= $("#stock-table").DataTable({
      "ajax": {
                "url": 'fetch-stock-report',
                '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(7))'
                    }
                },
                {
                    extend: 'csv',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))'
                    }
                },
                {
                    extend: 'print',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))',
                    }

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

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


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

    e.preventDefault();
    var id = $(this).data('id');
    
      
            $.ajaxSetup({
            headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
            });
            $.ajax({
                type: "POST",
                url: 'update_print_status/' + id,
                success: function (data) {
               return true;
               
                }         
            });
          
        
      
 
});




function payment_voucher(){
    var company_id=$("#company_id").val();
    var datefrom=$("#datefrom").val();
    var dateto=$("#dateto").val();
    var manageTable = $('#payment-voucher').DataTable();
    manageTable.destroy();
    manageTable= $("#payment-voucher").DataTable({
      "ajax": {
                "url": 'fetch-payment-voucher',
                'type': "GET",
                'data':{company_id:company_id,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(7))'
                    }
                },
                {
                    extend: 'csv',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))'
                    }
                },
                {
                    extend: 'print',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))',
                    }

                },
                {
                    extend: 'excel',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))'
                    }
                },
                {
                    extend: 'pdf',
                    exportOptions: {
                        columns: ':visible:not(:eq(7))'
                    }
                }],
                   "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));
                  // $(api.column(6).footer()).html(' ' + total.toFixed(2));

            }
      });
}
