<script>
function select_material(category=''){
  var category = $('#table_filter_material_category :selected').val();
  $('#table_filter_material').html('');
  $("#table_filter_material").append("<option  selected value='all' >All Materials</option>");
  $.post("<?php echo base_url() ?>material/select",{category:category,material_type:'vehicle'},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.id.length) {
        $("#table_filter_material").append("<option value=" + obj.id[i] + ">" + obj.name[i] + " [" + obj.unit_name[i] + "]</option>");
        i++;
      }
    }
  })
}
select_material('all');
$('body').on('change', '#table_filter_material_category', function() {
  select_material();
});
$('body').on('click', '#btn_delete_consumption', function() {
  var no = $(this).closest('tr').children('td');
  var cons_ids = no.eq(1).text();
  $('#consumption_delete_ids').val(cons_ids);
})
<?php if($page_name=='vehicle_material_consumption'){?>
  $("#table_filter_vehicle").select2({
    placeholder: "Select  Vehicle"
  })
  $("#table_filter_material").select2({
    placeholder: "Select  Material"
  })
  $("#table_filter_material_category").select2({
    placeholder: "Select  Material"
  })

  function GetSearchvehicle(){
    return $('#table_filter_vehicle').val();
  }
  function GetSearchMaterial(){
    return $('#table_filter_material').val();
  }
  function GetSearchMaterialCategory(){
    return $('#table_filter_material_category').val();
  }
  function GetSearchDate(){
    return $('#table_filter_date_range').val();
  }
  <?php } ?>
  <?php if($page_name=='data_entry_approval'){?>
    function GetStartDate(){
      return $('#table_filter_start_date').val();
    }
    function GetEndDate(){
      return $('#table_filter_end_date').val();
    }
    function GetSearchWork(){
      return $('#table_filter_work').val();
    }
    function GetSearchUser(){
      return $('#table_filter_user').val();
    }
    function GetSearchApprovalStatus(){
      return $('#table_filter_approval_status').val();
    }
    <?php } ?>
    <?php
    $page_type='';
    if($page_name=='data_entry_approval'){
      $page_type='approval_entries';
    }
    ?>
    var vehicle_material_consumption_view_table = $('#vehicle_material_consumption_view_table').DataTable({
      "ajax":{
        url :"<?php echo base_url().'stock/select_stock_consumption/'.$page_type ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d){
          d.type = 'vehicle'
          <?php if($page_name=='vehicle_material_consumption'){?>
            d.vehicle_id = GetSearchvehicle();
            d.material_id = GetSearchMaterial();
            d.material_category = GetSearchMaterialCategory();
            d.date = GetSearchDate();
            <?php } ?>
<?php if($page_name=='vehicle_profile'){?>
  d.vehicle_id = '<?php echo $vehicle_data->id?>';
  <?php } ?>
                <?php if($page_name=='data_entry_approval'){?>
                  d.start_date = GetStartDate();
                  d.end_date = GetEndDate();
                  d.approval_status = GetSearchApprovalStatus();
                  d.user_id = GetSearchUser();
                  <?php } ?>
                }
              },
              "bProcessing": true,
              "deferRender": true,
              "bDeferRender": true,
              "scrollX" : true,
              scrollX:!0, scrollCollapse:!0,
              buttons: [
                { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Stock consumptions' },
                { extend: 'csv', className: 'btn purple btn-outline ' },
                { extend: 'colvis', className: 'btn dark btn-outline', text: 'Columns'}
              ],
              dom:"<'row'<'col-sm-6 text-left'B><'col-sm-6 text-right'f>>\n\t\t\t<'row'<'col-sm-12'tr>>\n\t\t\t<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7 dataTables_pager'lp>>", // horizobtal scrollable datatable
              "lengthMenu": [
                [25, 50, 100, -1],
                [25, 50, 100, "All"] // change per page values here
              ],
              "columnDefs": [
                {
                  "targets": [ 1],
                  className: "hidden"
                }
              ],
              'order': false
            });
            $(".table_filter").on("change", function() {
              vehicle_material_consumption_view_table.ajax.reload();
            });
            $(".hide_column").hide();
            <?php if($page_name=='vehicle_material_consumption'){?>
              // Date Range Picker
              var start = moment();
              var end = moment();

              function cb(start, end) {
                $('#table_filter_date_range').val(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                vehicle_material_consumption_view_table.ajax.reload();
              }

              $('#table_filter_date_range').daterangepicker({
                buttonClasses: "m-btn btn",
                applyClass: "btn-primary",
                cancelClass: "btn-secondary",
                startDate: start,
                endDate: end,
                locale: {
                  format: 'MMMM D, YYYY'
                },
                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')],
                  'All time': [moment('1970-01-01'), moment().add(9999, 'days')],
                }
              }, cb);

              cb(start, end);

              <?php } ?>
              </script>
