<script>
$("#requested_material_table_filter_material").select2({
  placeholder: "Select  Material"
})
$("#requested_material_table_filter_material_category").select2({
  placeholder: "Select  Category"
})
function select_material(category=''){
  $('#requested_material_table_filter_material').html('');
  $("#requested_material_table_filter_material").append("<option value='all' selected >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) {
        $("#requested_material_table_filter_material").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
        i++;
      }
    }
  })
}
function select_material_category(){

  $('#requested_material_table_filter_material_category').html('');
  $("#requested_material_table_filter_material_category").append("<option selected value='all' >All Categories</option>");

  $.post("<?php echo base_url() ?>material/select_category",{},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.id.length) {
        $("#requested_material_table_filter_material_category").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
        i++;
      }
    }
  })
}
$('#requested_material_table_filter_material_category').on("change", function(e) {
  var category = $('#requested_material_table_filter_material_category :selected').val();
  select_material(category);
});
select_material_category();
select_material();
<?php if($page_name=='requested_materials'){?>
  $("#table_filter_work").select2({
    placeholder: "Select  Work",
    templateResult: format_project_dropdown
  })
  $("#table_filter_location").select2({
    placeholder: "Select  Location",
    templateResult: format_project_dropdown
  })

  function GetSearchDate(){
    return $('#table_filter_date_range').val();
  }
  function GetSearchLocation(){
    return $('#table_filter_location').val();
  }
  <?php } ?>

  function GetSearchmaterial(){
    return $('#requested_material_table_filter_material').val();
  }
  function GetSearchmaterialcategory(){
    return $('#requested_material_table_filter_material_category').val();
  }

  <?php if($page_name=='user_profile'){?>
  function GetStartDate(){
    return $('#table_filter_start_date').val();
  }
  function GetEndDate(){
    return $('#table_filter_end_date').val();
  }
  <?php } ?>
  var materials_requested_view_table = $('#materials_requested_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'purchase_request/select_requested_items_list' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        <?php if($page_name=='requested_materials'){?>
          d.date = GetSearchDate();
          d.material = GetSearchmaterial();
          d.material_category = GetSearchmaterialcategory();
          d.location_id = GetSearchLocation();

          <?php } ?>
          <?php if($page_name=='work_profile'){?>
            d.material = GetSearchmaterial();
            d.material_category = GetSearchmaterialcategory();

            d.work = "<?php echo $work_data->work_id?>";
            <?php } ?>
            <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
              d.material = GetSearchmaterial();
              d.material_category = GetSearchmaterialcategory();
              d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
              <?php } ?>
            <?php if($page_name=='reports_activity'){?>
              d.date = GetSearchDate();
              d.work = GetSearchWork();
              <?php } ?>
              <?php if($page_name=='supplier_profile'){?>
                d.material = GetSearchmaterial();
                d.material_category = GetSearchmaterialcategory();
                <?php } ?>
                <?php if($page_name=='user_profile'){?>
                  d.user_id = "<?php echo $user_data->user_id?>";
                    d.start_date = GetStartDate();
                    d.end_date = GetEndDate();
                    <?php } ?>
                  <?php if($page_name=='stock_location_profile'){?>
                    d.location_id = "<?php echo $location_data->location_id?>";
                    <?php } ?>

                  }
                },
                "bProcessing": true,
                "deferRender": true,
                "bDeferRender": true,
                "scrollX" : true,
                buttons: [
                  { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Materials Requested' },
                  { 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": [],
                    className: "hidden"
                  }
                ],
                'order': false
              });
              $(".table_filter").on("change", function() {
                materials_requested_view_table.ajax.reload();
              });
              $(".hide_column").hide();

              <?php if($page_name=='requested_materials'){?>
                // 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'));
                  materials_requested_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>
