<script>
<?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
  $page_type='all';
  if($page_name=='data_entry_approval'){
    $page_type='approval_entries';
  }
  ?>
var supplier_advance_view_table = $('#supplier_advance_view_table').DataTable({
  "ajax":{
    url :"<?php echo base_url().'accounts/select_account_transactions/supplier/'.$page_type.'/all' ?>", // json datasource
    type: "post", // type of method  , by default would be get
    data: function(d){
      <?php if($page_name == 'supplier_profile') { ?>
        d.supplier_id =  "<?php echo $supplier_data->supplier_id?>";
        <?php } ?>
        <?php if($page_name == 'user_profile') { ?>
          d.acc_user_id =  "<?php echo $user_data->user_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,
      "scrollX" : true,
      buttons: [
        { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Supplier Advance 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
      ],
      'order': false,
      "columnDefs": [
        {
          "targets": [4,6,7,8,9,19,20,21,22,23],
          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() {
      supplier_advance_view_table.ajax.reload();
    });

    var supplier_advance_return_view_table = $('#supplier_advance_return_view_table').DataTable({
      "ajax":{
        url :"<?php echo base_url().'supplier/select_advance_return_payments' ?>", // json datasource
        type: "post", // type of method  , by default would be get
        data: function(d){
          d.type = 'advance_return';
          <?php if($page_name == 'supplier_profile') { ?>
            d.supplier_id =  "<?php echo $supplier_data->supplier_id?>";
            <?php } ?>
            <?php if($page_name == 'user_profile') { ?>
              d.user_id =  "<?php echo $user_data->user_id?>";
              <?php } ?>
            }
          },
          "bProcessing": true,
          "deferRender": true,
          "bDeferRender": true,
          "scrollX" : true,
          buttons: [
            { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Supplier Advance Redeems' },
            { 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': false,
          "columnDefs": [
            {
              "targets": [-1<?php if($page_name == 'user_profile') { echo ",6"; }?>],
              className: "hidden"
            }
          ]
        });

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


        $("#advance_payment_acc_edit").select2({
          placeholder: "Select TR Mode",
          dropdownParent: $("#form_advance_payment_edit")
        })
        $("#advance_payment_acc_edit_return").select2({
          placeholder: "Select TR Mode",
          dropdownParent: $("#form_advance_payment_returned_edit")
        })


        $('body').on('click', '#advance_payment_edit_btn', function() {
          var no = $(this).closest('tr').children('td');
          $('#advance_payment_tr_id_edit').val(no.eq(1).text());
          $('#advance_payment_paid_amount_edit').val(no.eq(10).text());
          $("#advance_payment_paid_date_edit").datepicker("setDate", no.eq(2).text());
          $('#advance_payment_remarks_edit').val(no.eq(15).text());
          $("#advance_payment_acc_edit").val(no.eq(19).text()).trigger('change');
        })

        $('body').on('click', '#advance_payment_edit_btn_return', function() {
          var no = $(this).closest('tr').children('td');
          $('#advance_payment_tr_id_edit_return').val(no.eq(1).text());
          $('#advance_payment_paid_amount_edit_return').val(no.eq(4).text());
          $("#advance_payment_paid_date_edit_return").datepicker("setDate", no.eq(2).text());
        })

        $('body').on('click', '#btn_delete_advance_transaction', function() {
          var no = $(this).closest('tr').children('td');
          $('#advance_payment_tr_id_delete').val(no.eq(1).text());
        })
        $('body').on('click', '#btn_delete_advance_transaction_return', function() {
          var no = $(this).closest('tr').children('td');
          $('#advance_payment_tr_id_delete_return').val(no.eq(1).text());
        })
        // on model close
        //   $('#supplier_payment_add_modal,#advance_transaction_delete_modal').on('hidden.bs.modal', function () {
        //     supplier_advance_view_table.ajax.reload();
        //     supplier_advance_return_view_table.ajax.reload();
        // });


      </script>
