<script>

$('body').on('click', '#btn_consumption_details', function() {
  var no = $(this).closest('tr').children('td');
  var id = no.eq(14).text();
  var type = no.eq(5).text();
  if(type=='Rent'){
    type='rent';
  }else{
    type='transfer';
  }
  $('#consumption_list_tool_name').text(no.eq(4).text()+" For Project : "+no.eq(16).text()+"");
  $('#consumption_view_table').dataTable().fnDestroy();
  consumption_view_table(id,type);
})
var consumption_view_table=function(id,type) {

  var consumption_view_table = $('#consumption_view_table').DataTable({
    "ajax": {
      url: "<?php echo base_url().'tools/select_consumption_details' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function (d) {
        d.id = id;
        d.type = type;
      }
    },
    "bProcessing": true,
    "deferRender": true,
    "bDeferRender": true,
    "scrollX" : true,
    buttons: [
      { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Tools Consumptions' },
      { 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
    ],
    'order': [[0, 'asc']],
    scrollCollapse: true,
    fixedColumns:   true,
    fixedColumns:   {
      leftColumns: 4
    }
  });
  $(".hidden").hide();
  consumption_view_table.ajax.reload();
}

<?php if($page_name=='payments'){?>
  function GetSearchDate(){
    return $('#date_range').val();
  }
  function GetSearchwork(){
    return $('#table_filter_work').val();
  }
  function GetSearchtype(){
    return $('#table_filter_type').val();
  }
  <?php } ?>

  var tool_payments_view_table = $('#tool_payments_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'tools/select_tools_payments' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        <?php if($page_name=='work_profile'){?>
          d.work_id = "<?php echo $work_data->work_id ?>";
          <?php } ?>
          <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
            d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
            <?php } ?>
          <?php if($page_name=='payments'){?>
            d.work_id = GetSearchwork();
            d.type = GetSearchtype();
            <?php } ?>

          }
        },
        "bProcessing": true,
        "deferRender": true,
        "bDeferRender": true,
        "scrollX" : true,
        buttons: [
          { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Tool Payments Summary'},
          { 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": [14,15,16],
            className: "hidden"
          }
        ],
        'ordering': false
      });
      <?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
        $page_type='';
        if($page_name=='data_entry_approval'){
          $page_type='approval_entries';
        }
        ?>
        var tool_payment_transanctions_view_table = $('#tool_payment_transanctions_view_table').DataTable({
          "ajax":{
            url :"<?php echo base_url().'accounts/select_account_transactions/tool/'.$page_type ?>", // json datasource
            type: "post", // type of method  , by default would be get
            data: function(d){
              <?php if($page_name=='work_profile' || $page_name=='work_summary'){?>
                d.work = "<?php echo $work_data->work_id ?>";
                <?php } ?>
                <?php if($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
                  d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
                  <?php } ?>
                <?php if($page_name=='work_summary'){?>
                  d.start_date = GetStartDate();
                  d.end_date = GetEndDate();
                  <?php } ?>
                  <?php if($page_name=='payments'){?>
                    d.date = GetSearchDate();
                    d.work = GetSearchwork();
                    d.type = GetSearchtype();
                    <?php } ?>
                    <?php if($page_name=='data_entry_approval'){?>
                      d.start_date = GetStartDate();
                      d.end_date = GetEndDate();
                      d.work = GetSearchWork();
                      d.approval_status = GetSearchApprovalStatus();
                      d.created_by = GetSearchUser();
                      <?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: 'Tool Payment Transactions' },
                        { 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": [3,7,8,9,19<?php if($page_name=='reports_activity' || $page_name=='work_summary'){ echo ",18,"; }?><?php if($this->session->userdata('user_role')=='auditor'){ echo ",18,"; } ?> ],
                          className: "hidden"
                        }
                      ],
                      'order': false
                    });

                    $(".table_filter").on("change", function() {
                      tool_payments_view_table.ajax.reload();
                      tool_payment_transanctions_view_table.ajax.reload();
                    });
                  </script>
