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

  $('#purchased_material_table_filter_material_category').html('');
  $("#purchased_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) {
        $("#purchased_material_table_filter_material_category").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
        i++;
      }
    }
  })
}
$('#purchased_material_table_filter_material_category').on("change", function(e) {
  var category = $('#purchased_material_table_filter_material_category :selected').val();
  select_material(category);
});
select_material_category();
select_material();
<?php if($page_name=='purchased_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 $('#purchased_material_table_filter_material').val();
  }
  function GetSearchmaterialcategory(){
    return $('#purchased_material_table_filter_material_category').val();
  }
  function GetSearchsupplier(){
    return $('#purchased_material_table_filter_supplier').val();
  }
  function GetSearchsuppliercategory(){
    return $('#purchased_material_table_filter_supplier_category').val();
  }
  <?php if ($page_name == 'work_summary') { ?>
    function GetStartDate() {
      return $('#table_filter_start_date').val();
    }
    function GetEndDate() {
      return $('#table_filter_end_date').val();
    }
    <?php } ?>
  <?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_purchased_view_table = $('#materials_purchased_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'purchases/select_purchased_items_list' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        <?php if($page_name=='purchased_materials'){?>
          d.date = GetSearchDate();
          d.material = GetSearchmaterial();
          d.material_category = GetSearchmaterialcategory();
          d.location_id = GetSearchLocation();
          d.supplier = GetSearchsupplier();
          d.supplier_category = GetSearchsuppliercategory();
          <?php } ?>
          <?php if($page_name=='work_profile'){?>
            d.material = GetSearchmaterial();
            d.material_category = GetSearchmaterialcategory();
            d.supplier = GetSearchsupplier();
            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.supplier = GetSearchsupplier();
              d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
              <?php } ?>
            <?php if($page_name=='work_summary'){?>
                d.work = "<?php echo $work_data->work_id?>";
          d.start_date = GetStartDate();
          d.end_date = GetEndDate();
            <?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();
                // d.location_id = GetSearchLocation();
                d.supplier = "<?php echo $supplier_data->supplier_id?>";
                <?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,
                <?php if ($page_name == 'work_summary') { ?> "scrollX": false,
                bFilter: false,
                "bLengthChange": false,
                "bPaginate": false,
                "bInfo": false,
                buttons: [{
                  extend: 'colvis',
                  className: 'btn dark btn-outline',
                  text: 'Columns'
                }],
                "dom": "<'row' <'col-md-12'B>><'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r><'table-scrollable't><'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>", // horizobtal scrollable datatable
                <?php } else { ?>
                "scrollX" : true,
                buttons: [
                  { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Materials Purchased' },
                  { 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
                ],
                <?php } ?>
                "columnDefs": [
                  {
                    "targets": [ <?php if($page_name=='work_summary'){ echo ",23,25"; }?><?php if($this->config->item('Tax_tax_splitting')==0){ echo ",9,10,11,12,13,14,15,16";}?>],
                    className: "hidden"
                  }
                ],
                'order': false
              });
              $(".table_filter").on("change", function() {
                materials_purchased_view_table.ajax.reload();
              });
              $(".hide_column").hide();

              <?php if($page_name=='purchased_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_purchased_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>
