<script type="text/javascript">

<?php if($page_name=='payments_view'){?>
$('input[type=radio][name=employee_payment_type]').change(function() {
  select_details();
});
$('#salary_slip_employee_add').change(function() {
  select_details();
});
$('#salary_slip_employee_payment_add').change(function() {
  var employee_id = $('#salary_slip_employee_add :selected').val();
  $.post("<?php echo base_url() ?>salary_slips/select_salary_slips/view/employee_payment",{employee:employee_id},function(data) {
    var obj = $.parseJSON(data);
    var slip_id = $('#salary_slip_employee_payment_add :selected').val();
    if(slip_id!='' && slip_id!=null && slip_id!=0 && slip_id!='Select Salary Slip'){
      for (var i = obj.length - 1; i >= 0; i--) {
        if(obj[i][2]==slip_id){
          var obj=obj[i];
        }
      }
      var slip_balance = obj[1];
      if(slip_balance>0) {
        $('#salary_slip_paid_amount_add').removeAttr('disabled');
        $('#payment_payable_amount').val(parseFloat(slip_balance));
        $('#salary_slip_paid_amount_add').attr('max',parseFloat(slip_balance));
      }
      else{
        $('#payment_payable_amount').val(0);
        $('#salary_slip_paid_amount_add').attr('disabled','disabled');
      }
    }

  });
});
function select_details(){
  $('#salary_slip_paid_amount_add').removeAttr('disabled');
  var type = $("input[name='employee_payment_type']:checked"). val();
  $('.payment_fields').hide();
  $('.payment_fields_general').show();
  $('#payment_payable_amount').val(0);
  $("."+type+'_fields').show();


  if(type=='salary_slip'){
    $('#salary_slip_employee_payment_add').attr('required',true);
    $('#salary_slip_paid_amount_add').removeAttr('disabled');
    $('#salary_slip_work_add').removeAttr('required',true);
    var employee_id = $('#salary_slip_employee_add :selected').val();
    if(employee_id!='' && employee_id!=null && employee_id!=0 && employee_id!='Select Employee'){
      $.post("<?php echo base_url() ?>salary_slips/select_salary_slips/view/employee_payment",{employee:employee_id},function(data) {
        var obj = $.parseJSON(data);
        var count = obj.length;
        $('#salary_slip_employee_payment_add option').remove();
        $('#salary_slip_employee_payment_add').append("<option selected disabled >Select Salary Slip</option>");
        for (var i = obj.length - 1; i >= 0; i--)
        {
          $('#salary_slip_employee_payment_add').append("<option value="+obj[i][2]+" >" + obj[i][0] + "</option>");
        }
      });
    }
  }else if(type=='op_balance'){
    $('#salary_slip_employee_payment_add').removeAttr('required',true);
    $('#salary_slip_paid_amount_add').removeAttr('disabled');
    $('#salary_slip_work_add').removeAttr('required',true);
    var employee_id = $('#salary_slip_employee_add :selected').val();
    if(employee_id!='' && employee_id!=null && employee_id!=0 && employee_id!='Select Employee'){
      $.post("<?php echo base_url() ?>employees/get_employee_opbalance",{employee_id:employee_id},function(data) {
        var obj = $.parseJSON(data);
        if(obj.op_balance>0) {
          $('#salary_slip_paid_amount_add').removeAttr('disabled');
          $('#payment_payable_amount').val(parseFloat(obj.op_balance));
          $('#salary_slip_paid_amount_add').attr('max',parseFloat(obj.op_balance));
        }
        else{
          $('#payment_payable_amount').val(0);
          $('#salary_slip_paid_amount_add').attr('disabled','disabled');
        }
      });
    }
  }else if(type=='advance_salary'){
    $('#salary_slip_work_add').attr('required',true);
    $('#salary_slip_employee_payment_add').removeAttr('required',true);
    $('body').on('change', '#salary_slip_employee_add', function() {
      $('#salary_slip_paid_amount_add').removeAttr('disabled');
    })
  }else if(type=='bata'){
    $('#salary_slip_work_add').attr('required',true);
    $('#salary_slip_employee_payment_add').removeAttr('required',true);
    $('body').on('change', '#salary_slip_employee_add', function() {
      $('#salary_slip_paid_amount_add').removeAttr('disabled');
    })
  }
}

$('body').on('change keyup', '#salary_slip_paid_amount_add', function() {
  var paid = $(this).val();
  var payable = $('#payment_payable_amount').val();
  var balance =  payable - paid;
  $('#salary_slip_balance_amount_add').val(balance);
});
<?php } ?>
</script>



<script>
<?php if($page_name=='payments_view'){ ?>
$("#salary_slip_month_add,#salary_slip_month_edit,#table_filter_start_month,#table_filter_end_month").datepicker( {
  todayHighlight:!0, orientation:"bottom left",format: "M yyyy",viewMode: "months", minViewMode: "months", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  }
})
$("#salary_slip_paid_date_add,#salary_slip_paid_date_edit,#salary_slip_reference_date_add,#salary_slip_date_add").datepicker( {
  todayHighlight:!0, orientation:"bottom left", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  },
  endDate: '+0d',
  <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
    startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
    <?php } ?>
  })
  $('body').on('change', '#salary_slip_payment_mode_add', function() {
    var payment_mode=$('#salary_slip_payment_mode_add :selected').val();
    if(payment_mode=='POST DATED CHEQUE'){
      $('#salary_slip_paid_date_add').datepicker('destroy');
      $("#salary_slip_paid_date_add").datepicker( {
        todayHighlight:!0, orientation:"bottom left", templates: {
          leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
        },
        <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
          startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
          <?php } ?>
        })
    }else{
      $('#salary_slip_paid_date_add').datepicker('destroy');
      $("#salary_slip_paid_date_add").datepicker( {
        todayHighlight:!0, orientation:"bottom left", templates: {
          leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
        },
        endDate: '+0d',
        <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
          startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
          <?php } ?>
        })
    }
  })
  $("#salary_slip_payment_mode_add").select2({
    placeholder: "Select Debit Account",
    dropdownParent: $("#form_salary_slip_add")
  })
  $("#salary_slip_debit_acc_add").select2({
    placeholder: "Select Debit Account",
    dropdownParent: $("#form_salary_slip_add")
  })
  $("#salary_slip_work_add").select2({
    placeholder: "Select Work",
    dropdownParent: $("#form_salary_slip_add"),
    templateResult: format_project_dropdown
  })
  $("#salary_slip_employee_add,#salary_slip_employee_payment_add").select2({
    placeholder: "Select Slip",
    dropdownParent: $("#form_salary_slip_add")
  })
  $("#salary_slip_basic_wage_type_add").select2({
    placeholder: "Select Type",
    dropdownParent: $("#form_salary_slip_add")
  })
  $("#salary_slip_overtime_type_add").selectpicker();
  // $("#salary_slip_overtime_type_add").select2({
  //   placeholder: "Select Type",
  //   dropdownParent: $("#form_salary_slip_add")
  // })

  $("#salary_slip_work_edit").select2({
    placeholder: "Select Work",
    dropdownParent: $("#form_salary_slip_edit"),
    templateResult: format_project_dropdown
  })
  $("#salary_slip_employee_edit").select2({
    placeholder: "Select Slip",
    dropdownParent: $("#form_salary_slip_edit")
  })
  $("#salary_slip_basic_wage_type_edit").select2({
    placeholder: "Select Type",
    dropdownParent: $("#form_salary_slip_edit")
  })
  $("#salary_slip_overtime_type_edit").select2({
    placeholder: "Select Type",
    dropdownParent: $("#form_salary_slip_edit")
  })

  $("#salary_slip_employee_add_2").select2({
    placeholder: "Select Slip",
    dropdownParent: $("#form_employee_payment_add")
  })

  $("#salary_slip_employee_edit_2").select2({
    placeholder: "Select Slip",
    dropdownParent: $("#form_employee_payment_edit")
  })

  function GetSearchDate(){
    return $('#table_filter_date_range').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
  $page_type='';
  if($page_name=='data_entry_approval'){
    $page_type='approval_entries';
  }
  ?>
  var employee_payments_view_table = $('#employee_payments_view_table').DataTable({
    "ajax":{
      url :"<?php echo base_url().'accounts/select_account_transactions/employee/'.$page_type ?>", // json datasource
      type: "post", // type of method  , by default would be get
      data: function(d){
        <?php if($page_name=='payments_view'){?>
        d.date = GetSearchDate();
        <?php }elseif($page_name=='work_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=='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: 'Employee 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
    ],
    // set the initial value
    "pageLength": 25,
    "columnDefs": [
      {
        "targets": [8,10,20,21,22,23,24<?php if($this->session->userdata('user_role')=='auditor'){ echo ",19"; } ?>],
        className: "hidden"
      }
    ],
    'ordering': false,
    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
      if(aData[24]=='0'){
        $('td', nRow).css('background-color', '#ffcccc');
      }else if(aData[24]=='2'){
        $('td', nRow).css('background-color', '#ffcccc');
      }
    }
  });
  $(".table_filter").on("change", function() {
    employee_payments_view_table.ajax.reload();
  });
  $(".hide_column").hide();

<?php if($page_name=='payments_view'){?>
  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'));
    employee_payments_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);


  $(".form_submit_btn").click(function (e) {
    e.preventDefault();
    var a = $(this),
    l = $(this).closest("form"),
    action = l.attr('action');
    var form_id = l.attr('id');
    l.validate({
      rules: {
        op_balance: {
          number: !0
        },
        basic_wage: {
          number: !0
        },
        contact_no: {
          number: !0
        }
      }, invalidHandler: function (e, r) {
        var i = $("#" + form_id + "_msg");
        i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
      }, submitHandler: function (e) {
      }
    }), l.valid() && (a.addClass("m-loader m-loader--right m-loader--light").attr("disabled", !0),
    l.ajaxSubmit({
      url: action,
      success: function (e, t, r, s) {
        var modal_id = l.attr('data-modal-id');
        $("#"+modal_id).modal('toggle');
        var form_type = l.attr('data-form-type');
        $("#" + form_id + "_msg").addClass("m--hide");
        // Block UI
        mApp.blockPage({
          overlayColor: "#000000",
          type: "loader",
          state: "success",
          message: "Please wait..."
        }), setTimeout(function () {
          mApp.unblockPage()
        }, 2e3);
        var obj = $.parseJSON(e);
        if (obj.status == 1) {
          a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
          $("."+form_id+"_fields").val('');
          $("."+form_id+"_select").val('').trigger("change");
          toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
          employee_payments_view_table.ajax.reload();
          $('input[name=basic_wage_type]').prop('checked', false);
        } else {
          a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
          toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
        }
        if (obj == null) {
          setTimeout(function () {
            a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
            toastr["warning"]("Your Attempt is Timed Out. Please Try Again.", "Timed Out");
          }, 2e4)
        }
      },error: function() {
        a.removeClass("m-loader m-loader--right m-loader--light").attr("disabled", !1);
        toastr["error"]("Sorry. There Has Been Some Error Occurred. Please Try Again.", "Error");
      }
    })
  )
});
<?php } ?>
</script>

</script>
