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

  $('body').on('click', '#purchase_history_btn', function() {
    var no = $(this).closest('tr').children('td');
    var material_id = no.eq(2).text();
    var unit_id = no.eq(3).text();
    var location_id = no.eq(4).text();
    var unit_price = $(this).data('unit-price');
    var supplier_id = $(this).data('supplier-id');

    $('#purchase_history_view_table').dataTable().fnDestroy();
    purchase_history_table(material_id,unit_id,location_id,unit_price,supplier_id);
  })

  var purchase_history_table = function(material_id,unit_id,location_id,unit_price,supplier_id){
    var purchase_history_view_table = $('#purchase_history_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){
          d.material = material_id;
          d.unit_id = unit_id;
          d.location_id = location_id;
          d.supplier = supplier_id;
          d.unit_price = unit_price;
        }
      },
      "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 Purchase 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
      ],
      "columnDefs": [
        {
          "targets": [8],
          className: "hidden"
        }
      ],
      'order': false
    });
    purchase_history_view_table.ajax.reload();
  }
  <?php if($page_name=='stock_register'){?>
    var stock_register_datatable=function() {
      var t=function() {

        $('#stock_register_view_table').dataTable().fnDestroy();
        $('#stock_transfer_in_view_table').dataTable().fnDestroy();
        $('#stock_transfer_out_view_table').dataTable().fnDestroy();
        <?php } ?>
        <?php if($page_name=='stock_register'){?>
          function GetSearchLocation(){
            return $('#table_filter_location').val();
          }
          <?php } ?>
          function GetSearchMaterial(){
            return $('#table_filter_material').val();
          }
          <?php
          $hide_column=25;
          if ($this->config->item('app_module_sales_invoice') == 1 ){
            $hide_column = $hide_column+2;
          }
          if ($this->config->item('app_module_production') == 1 ){
            $hide_column = $hide_column+4;
          }
          ?>
          var stock_register_view_table = $('#stock_register_view_table').DataTable({
            "ajax":{
              url :"<?php echo base_url().'stock/select_stock_register' ?>", // json datasource
              type: "post", // type of method  , by default would be get
              data: function(d){
                <?php if($page_name=='stock_register'){?>
                  d.location_id = GetSearchLocation();
                  d.material_id = GetSearchMaterial();
                  <?php } ?>
                  <?php if($page_name=='work_profile'){?>
                    d.work_id = "<?php echo $work_data->work_id?>";
                    d.material_id = GetSearchMaterial();
                    <?php } ?>
                    <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
                      d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
                      d.material_id = GetSearchMaterial();
                      <?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,
                  scrollX:!0, scrollCollapse:!0,
                  buttons: [
                    { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Stock Register' },
                    { 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,2,3,4,<?php echo $hide_column ?>,<?php echo $hide_column+1 ?><?php if($page_name=='work_profile' || $page_name=='stock_location_profile'){ echo ",11,12"; }?> ],
                      className: "hidden"
                    }
                  ],
                  'order': false
                });
                var stock_transfer_in_view_table = $('#stock_transfer_in_view_table').DataTable({
                  "ajax":{
                    url :"<?php echo base_url().'stock/select_stock_transfer' ?>", // json datasource
                    type: "post", // type of method  , by default would be get
                    data: function(d){
                      <?php if($page_name=='work_profile'){?>
                        d.work_id_in = work_id;
                        <?php } ?>
                        <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
                          d.work_ids_in = "<?php echo implode(',',$sub_project_ids)?>";
                          <?php } ?>
                        <?php if($page_name=='stock_location_profile'){?>
                          d.location_to = "<?php echo $location_data->location_id?>";
                          <?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 Transfer In' },
                        { 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,7,10 ],
                          className: "hidden"
                        }
                      ],
                      'order': false
                    });
                    var stock_transfer_out_view_table = $('#stock_transfer_out_view_table').DataTable({
                      "ajax":{
                        url :"<?php echo base_url().'stock/select_stock_transfer' ?>", // json datasource
                        type: "post", // type of method  , by default would be get
                        data: function(d){
                          <?php if($page_name=='work_profile'){?>
                            d.work_id_out = work_id;
                            <?php } ?>
                            <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
                              d.work_ids_out = "<?php echo implode(',',$sub_project_ids)?>";
                              <?php } ?>
                            <?php if($page_name=='stock_location_profile'){?>
                              d.location_from = "<?php echo $location_data->location_id?>";
                              <?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 Transfer Out' },
                            { 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,6,10 ],
                              className: "hidden"
                            }
                          ],
                          'order': false
                        });
                        $(".table_filter").on("change", function() {
                          stock_register_view_table.ajax.reload();
                        });
                        $(".hide_column").hide();
                        <?php if($page_name=='stock_register'){?>
                        };
                        return {
                          init:function() {
                            t()
                          }
                        }
                      }();
                      <?php } ?>
                      <?php if($page_name=='stock_register'){?>
                        $('body').on('click', '#btn_get_stock_register', function() {
                          stock_register_datatable.init();
                          $("#stock_register_result_div").show();
                        })
                        <?php }else{?>
                          $("#stock_register_result_div").show();
                          <?php } ?>
                          </script>
