<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},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_damages', function() {
  var no = $(this).closest('tr').children('td');
  var cons_ids = no.eq(1).text();
  $('#damages_delete_ids').val(cons_ids);
})
<?php if($page_name=='stock_damages'){?>
  $("#table_filter_location").select2({
    placeholder: "Select Location"
  })
  $("#table_filter_material").select2({
    placeholder: "Select  Material"
  })
  $("#table_filter_material_category").select2({
    placeholder: "Select  Material"
  })

  function GetSearchLocation(){
    return $('#table_filter_location').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 stock_damages_view_table = $('#stock_damages_view_table').DataTable({
      "ajax":{
        url :"<?php echo base_url().'production/select_stock_damages/'.$page_type ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d){
          <?php if($page_name=='stock_damages'){?>
            d.location_id = GetSearchLocation();
            d.material_id = GetSearchMaterial();
            d.material_category = GetSearchMaterialCategory();
            d.date = GetSearchDate();
            <?php } ?>
            <?php if($page_name=='work_profile'){?>
              d.work_id = "<?php echo $work_data->work_id?>";
              <?php } ?>
              <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
                d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
                <?php } ?>
              <?php if($page_name=='stage_profile' || $page_name=='stage_summary'){?>
                d.stage_id = "<?php echo $stage_data->stage_id?>";
                <?php } ?>
                <?php if($page_name=='data_entry_approval'){?>
                  d.start_date = GetStartDate();
                  d.end_date = GetEndDate();
                  d.work_id = GetSearchWork();
                  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 damagess' },
                { 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() {
              stock_damages_view_table.ajax.reload();
            });
            $(".hide_column").hide();
            <?php if($page_name=='stock_damages'){?>
              // 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'));
                stock_damages_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>
