<script>
<?php if($page_name=='production_view'){?>
  $("#table_filter_location").select2({
    placeholder: "Select  Location",
    templateResult: format_project_dropdown
  })
  $("#table_filter_material").select2({
    placeholder: "Select  Material"
  })
  $("#table_filter_material_category").select2({
    placeholder: "Select Category"
  })

  function select_material(category=''){
    $('#table_filter_material').html('');
    $("#table_filter_material").append("<option value='all' selected >All Materials</option>");

    $.post("<?php echo base_url() ?>material/select",{category:category,section:'product'},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] + "</option>");
          i++;
        }
      }
    })
  }
  function select_material_category(){

    $('#table_filter_material_category').html('');
    $("#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) {
          $("#table_filter_material_category").append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
          i++;
        }
      }
    })
  }
  $('#table_filter_material_category').on("change", function(e) {
    var category = $('#table_filter_material_category :selected').val();
    select_material(category);
  });
  select_material_category();
  select_material();
  <?php } ?>

  function GetSearchLocation(){
    return $('#table_filter_location').val();
  }
  function GetSearchMaterial(){
    return $('#table_filter_material').val();
  }
  function GetSearchMaterialCategory(){
    return $('#table_filter_material_category').val();
  }
  <?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 if ($page_name == 'work_summary') { ?>
      function GetStartDate() {
        return $('#table_filter_start_date').val();
      }
      function GetEndDate() {
        return $('#table_filter_end_date').val();
      }
      <?php } ?>
      <?php
      $page_type='';
      if($page_name=='data_entry_approval'){
        $page_type='approval_entries';
      }
      ?>
      var production_view_table = $('#production_view_table').DataTable({
        "ajax":{
          url :"<?php echo base_url().'production/select_production/'.$page_type ?>", // json datasource
          type: "post", // type of method  , by default would be get
          data: function(d){
            <?php if($page_name=='production_view'){?>
              d.location = GetSearchLocation();
              d.material_id = GetSearchMaterial();
              d.material_category = GetSearchMaterialCategory();
              <?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 } ?>
                <?php if ($page_name == 'work_summary') { ?>
                  d.work_id = "<?php echo $work_data->work_id ?>";
                  d.start_date = GetStartDate();
                  d.end_date = GetEndDate();
                  <?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,
                scrollX:!0, scrollCollapse:!0,
                buttons: [
                  { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Stock Transfer History' },
                  { 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": [ 1 ],
                    className: "hidden"
                  }
                ],
                'order': false
              });

              $(".table_filter").on("change", function() {
                production_view_table.ajax.reload();
              });
              $(".hide_column").hide();


            </script>
