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

  function GetSearchsupplier(){
    return $('#table_filter_supplier').val();
  }
  function GetSearchDate(){
    return $('#table_filter_date_range').val();
  }
  function GetSearchlocation(){
    return $('#table_filter_location').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 } ?>
    <?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();
      }
      function GetSearchAccount() {
        return $('#table_filter_account').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 purchase_order_transaction_view_table = $('#purchase_order_transaction_view_table').DataTable({
      "ajax":{
        url :"<?php echo base_url().'accounts/select_account_transactions/purchase_order/'.$page_type.'/table/bill_not_created' ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d){
          <?php if($page_name=='purchase_order_payments'){?>
            d.date = GetSearchDate();
            d.supplier = GetSearchsupplier();
            d.location_id = GetSearchlocation();
            <?php } ?>
            <?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=='reports_activity'){?>
                d.date = GetSearchDate();
                d.work = GetSearchWork();
                <?php } ?>
                <?php if($page_name=='supplier_profile'){?>
                  d.supplier = "<?php echo $supplier_data->supplier_id?>";
                  <?php } ?>
                  <?php if($page_name=='user_profile'){?>
                    d.acc_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 } ?>
                      <?php if($page_name=='stage_profile' || $page_name=='stage_summary'){?>
                        d.stage_id = "<?php echo $stage_data->stage_id?>";
                        <?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();
                          d.acc_head_id = GetSearchAccount();
                          <?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: 'Purchase Order Payments'},
                        { 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 } ?>
                      'order': false,
                      "columnDefs": [
                        {
                          "targets": [ 6,7,19,20,21,22,23<?php if($page_name!='data_entry_approval'){ echo ",18"; }?><?php if($page_name=='work_profile'){ echo ",4"; }?><?php if($page_name=='work_summary'){ echo ",1,4,8,9,13,14,15"; }?>],
                          className: "hidden"
                        }
                      ],
                      "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
                        if(aData[23]=='0'){
                          $('td', nRow).css('background-color', '#ffcccc');
                        }else if(aData[23]=='2'){
                          $('td', nRow).css('background-color', '#ffcccc');
                        }
                      }
                    });
                    $(".table_filter").on("change", function() {
                      purchase_order_transaction_view_table.ajax.reload();
                    });
                    $(".hide_column").hide();

                    <?php if($page_name=='purchase_order_payments'){?>
                      // 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'));
                        purchase_order_transaction_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>
