<script>
  <?php if($page_name=='reports_boq_purchase'){ ?>
  $("#table_filter_work").select2({
    placeholder: "Select  Work",
    templateResult: format_project_dropdown
  })
  $('body').on('click', '#btn_go', function() {
    var project_id = $('#table_filter_work :selected').val();
    if(project_id!='' && project_id!=null && project_id!='all'){
      $("#boq_purchase_report_div").show();
      boq_purchase_report_table();
    }else{
     toastr["warning"]("Please select project to generate report ", "Select Project");
   }
 })
 <?php } ?>

  var boq_purchase_report_table=function() {
    $('#boq_purchase_report_view_table').dataTable().fnDestroy();


    function GetSearchwork(){
      <?php if($page_name=='reports_boq_purchase'){ ?>
      return $('#table_filter_work').val();
      <?php } ?>
      <?php if($page_name=='work_profile'){ ?>
      return "<?php echo $work_data->work_id; ?>";
      <?php } ?>
    }

    $('.table_caption').html("");
    $('#boq_purchase_report_view_table').append('<caption class="table_caption" style="caption-side: top !important;text-align: center"><strong>BOQ Purchase report</strong></caption>');
    $.post("<?php echo base_url() ?>reports/boq_purchase_integration",{project_id:GetSearchwork()},function(data) {
      $('#boq_purchase_report_view_table_body').html("");
      var obj = $.parseJSON(data);
      var i=0;
      if(obj.count>0) {
        $('#boq_purchase_report_view_table_body').append(obj.table_body);
      }
      var table = $('#boq_purchase_report_view_table').DataTable({

        "scrollX" : true,
        buttons: [
        { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'BOQ - Purchase Report' },
        { 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
        ],
      // set the initial value
      "pageLength": 25,
      "columnDefs": [
      {
        "targets": [  ],
        className: "hidden"
      }
      ],
      'ordering': false
    });
      $(".hide_column").hide();
    })
  }

  var project_id = '';
  <?php if($page_name=='reports_boq_purchase'){ ?>
  project_id = <?php if ($project_id!='') {echo $project_id;} else { echo 0; }?>;
  <?php } ?>
  <?php if($page_name=='work_profile'){ ?>
  project_id = "<?php echo $work_data->work_id; ?>";
  <?php } ?>
  if(project_id!='' && project_id!=null && project_id!=0){
    $('#table_filter_work').val(project_id).trigger('change');
    $("#boq_purchase_report_div").show();
    boq_purchase_report_table();
  }

  jQuery(document).ready(function() {
    $(".hidden").hide();
  });
</script>
