<script type="text/javascript">

$("#client_payment_invoice_add").select2({
  placeholder: "Select  Invoice"
});

$("#client_payment_work_edit").select2({
  placeholder: "Select Work",
  dropdownParent: $("#form_client_payment_edit"),
  templateResult: format_project_dropdown
});

$("#client_payment_acc_add").select2({
  placeholder: "Select Credit Account"
})

$("#client_payment_acc_edit").select2({
  placeholder: "Select Credit Account"
})

$('body').on('click', '#invoice_payment_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#client_payment_invoice_add').val(no.eq(23).text()).trigger('change');
  $('#client_payment_client_name_add').val(no.eq(4).text()).trigger('change');
  $('input[type=radio][value=advance]').attr('disabled', true);
  var invoice_type = no.eq(25).text();
  if(invoice_type=='other_income'){
    $("#work_id_add").hide();
    $("#tr_type_add_client_payment").val('income_receipt_transaction');
    $("#reference_id_add_client_payment").val(no.eq(26).text());
    $("#reference_type_add_client_payment").val('client');
  }else{
    $("#work_id_add").show();
    $("#tr_type_add_client_payment").val('client_payment_transaction');
    $("#reference_id_add_client_payment").val(no.eq(26).text());
    $("#reference_type_add_client_payment").val('project');
  }
});


<?php if($page_name=='invoices_profile' || $page_name=='invoices_view' || $page_name=='invoices_payments' || $page_name=='invoices_payable'){?>

  $('#client_payment_invoice_add').on("change", function(e) {
    var invoice_id = $('#client_payment_invoice_add :selected').val();
    $.post("<?php echo base_url() ?>invoices/select_invoices/payment",{invoice_id:invoice_id},function(data) {
      var obj = $.parseJSON(data);
      var balance = obj[0].balance;
      var work_name = obj[0].work_name;
      var client_name = obj[0].client_name;
      var work_id = obj[0].work_id;
      $('#client_payment_taxable_amount_add').attr('max',balance);
      $('#client_payment_received_amount_add').attr('max',balance);
      $('#invoice_work_name').val(work_name);
      $('#invoice_work_name_add').val(work_name);
      $('#client_payment_client_name_add').val(client_name);
      $('#reference_id_add_client_payment').val(work_id);
    })
  });


  $('#client_payment_invoice_edit').on("change", function(e) {
    var invoice_id = $('#client_payment_invoice_edit :selected').val();
    $.post("<?php echo base_url() ?>invoices/select_invoices/payment",{invoice_id:invoice_id},function(data) {
      var obj = $.parseJSON(data);
      var balance = obj[0].balance;
      var work_name = obj[0].work_name;
      var client_name = obj[0].client_name;
      var work_id = obj[0].work_id;
      $('#client_payment_taxable_amount_edit').attr('max',balance);
      $('#invoice_work_name_edit').val(work_name);
      $('#client_payment_client_name_edit').val(client_name);
      $('#reference_id_edit_client_payment').val(work_id);

    })
  });

  //     $('body').on('click', '#client_payment_edit_btn', function() {
  //       var no = $(this).closest('tr').children('td');
  //       $("#client_payment_invoice_edit").val(no.eq(24).text()).trigger('change');
  //       $("#client_payment_received_amount_edit_invoice").val(no.eq(10).text());
  //       $("#client_payment_received_date_edit_invoice").val(no.eq(4).text());
  //       $("#client_payment_acc_edit").val(no.eq(21).text()).trigger('change');
  //       $("#client_payment_remarks_edit_invoice").val(no.eq(13).text());
  //       // $("#client_payment_receipt_no_edit_invoice_invoice").val(no.eq(4).text());

  //       // $('#client_payment_tr_id_edit_invoice').val(no.eq(1).text());
  //       // $('#client_payment_received_amount_edit_invoice').val(no.eq(7).text());
  //       // $("#client_payment_received_date_edit_invoice").datepicker("setDate", no.eq(4).text());
  //       // $('#client_payment_remarks_edit_invoice').val(no.eq(10).text());
  //       // $('#client_payment_receipt_no_edit_invoice').val(no.eq(9).text());
  //       // $("#client_payment_work_edit_invoice").val(no.eq(16).text()).trigger('change');
  //       // $('#client_payment_client_id_edit_invoice').val(no.eq(17).text());
  //       // $('#client_payment_client_name_edit_invoice').val(no.eq(5).text());
  //       // $("#client_payment_acc_edit_invoice").val(no.eq(18).text()).trigger('change');
  //       // $("#reference_id_edit_client_payment_invoice").val(no.eq(16).text());
  //       // $("#reference_type_edit_client_payment_invoice").val('project');
  //       // var invoice_id = no.eq(3).text();
  //       // invoice_id = invoice_id.substring(1,);
  //       // $('#invoice_id_update').val(invoice_id);
  //     })

  $("#table_filter_user2").select2({
    placeholder: "Select  User"
  })
  $("#table_filter_work2").select2({
    placeholder: "Select  Work"
  })

  function GetSearchwork(){
    return $('#table_filter_work2').val();
  }
  function GetSearchDate(){
    return $('#table_filter_date_range2').val();
  }
  <?php } ?>

  var invoice_payments_view_table = $('#invoice_payments_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'client_payments/select_client_payments' ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        d.payment_type = 'invoice';
        <?php if($page_name=='invoices_payments'){?>
          d.date = GetSearchDate();
          d.work = GetSearchwork();
          d.invoice = 'invoice';

          <?php }elseif($page_name=='work_profile' || $page_name=='projects_profile' || $page_name=='work_summary'){ ?>
            d.work = "<?php echo $work_data->work_id?>";
            <?php }elseif($page_name=='work_profile_master' || $page_name=='work_summary_master'){?>
                d.work_ids = "<?php echo implode(',',$sub_project_ids)?>";
                <?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=='reports_activity'){?>
                d.date = GetSearchDate();
                d.work = GetSearchwork();
                <?php } ?>
                <?php if($page_name=='user_profile'){?>
                  d.created_by = "<?php echo $user_data->user_id?>";
                  <?php } ?>
                }
              },
              "bProcessing": true,
              "deferRender": true,
              "bDeferRender": true,
              <?php if($page_name=='work_summary'){?>
                "scrollX": false,
                bFilter: false,
                "bLengthChange": false,
                "bPaginate": false,
                "bInfo": false,
                "dom": "<'row' <'col-md-12'>><'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',title: 'Client Invoice Receipts' },
                    { 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": [ 1,<?php if($page_name=='invoices_payments'){ echo "7,8,9,"; }?>17,18,19,20,21,22,23,24<?php if($page_name=='work_profile' || $page_name=='projects_profile'){ echo ",5,6"; }?><?php if($page_name=='work_summary'){ echo ",2,5,6,9,10"; }?><?php if($page_name=='reports_activity' || $page_name=='user_profile'){ echo ",10"; }?> ],
                      className: "hidden"
                    }
                  ]
                });


                // Date Range Picker
                var start = moment();
                var end = moment();

                function cb(start, end) {
                  $('#table_filter_date_range2').val(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
                  // invoices_view_table.ajax.reload();
                  invoice_payments_view_table.ajax.reload();
                }

                $('#table_filter_date_range2').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);

                $(".table_filter").on("change", function() {
                  // invoices_view_table.ajax.reload();
                  invoice_payments_view_table.ajax.reload();

                });
                $(".hide_column").hide();




                </script>
