<script>
<?php if($page_name=='labours_view'){?>
$("#table_filter_work").select2({
  placeholder: "Select Work",
  templateResult: format_project_dropdown
})
$("#table_filter_work_type").select2({
  placeholder: "Select Work Type"
})
$("#table_filter_labour").select2({
  placeholder: "Select Labour"
})
$("#table_filter_labour_group_head").select2({
  placeholder: "Select Team Head"
})
$("#table_filter_start_date,#table_filter_end_date,#labour_wage_edit_from_date").datepicker( {
  todayHighlight:!0, orientation:"bottom left", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  }
})
$('body').on('change keyup', '#daily_wage_add', function() {
  var daily_wage = parseFloat($('#daily_wage_add').val());
  if(isNaN(daily_wage)) {
    var daily_wage = 0;
  }
  if(daily_wage!='' && daily_wage!=0){
    var total_duty_hours = parseFloat("<?php echo $this->config->item('Labour_total_duty_hours'); ?>");
    var overtime_rate = daily_wage/total_duty_hours;
    $('#overtime_rate_add').val(overtime_rate.toFixed(2));
  }else{
    $('#overtime_rate_add').val('');
  }
})
$('body').on('change keyup', '#daily_wage_edit', function() {
  var daily_wage = parseFloat($('#daily_wage_edit').val());
  if(isNaN(daily_wage)) {
    var daily_wage = 0;
  }
  if(daily_wage!='' && daily_wage!=0){
    var total_duty_hours = parseFloat("<?php echo $this->config->item('Labour_total_duty_hours'); ?>");
    var overtime_rate = daily_wage/total_duty_hours;
    $('#overtime_rate_edit').val(overtime_rate.toFixed(2));
  }else{
    $('#overtime_rate_edit').val('');
  }
})
$('body').on('click', '#btn_add_bill_payment', function() {
  var no = $(this).closest('tr').children('td');
  $('#labour_add_bill_payment').val(no.eq(0).text()).trigger('change');
})
$('body').on('click', '#btn_qr_code', function() {
  var no = $(this).closest('tr').children('td');
  var labour_id = no.eq(0).text();
  window.open('<?php echo base_url().'labours/print_qrcode/'?>'+labour_id,'newwindow','width=1200,height=700'); return false
})
function select_work_type(select_id){
  $('#'+select_id).html('');
  $("#"+select_id).append("<option disabled selected >Select Work Type</option>");
  $.post("<?php echo base_url() ?>work_types/select",{},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.id.length) {
        $("#"+select_id).append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
        i++;
      }
    }
  })
}
select_work_type("work_type_add");
$("#work_type_add").select2({
  placeholder: "Select Work Type",
  dropdownParent: $("#form_labour_add")
})
select_work_type("work_type_edit");
$("#work_type_edit").select2({
  placeholder: "Select Work Type",
  dropdownParent: $("#form_labour_edit")
})
$('input[type=radio][name=fixed_wage]').change(function() {
  if (this.value == 1) {
    $(".wage_per_day_div").show();
    $("#daily_wage_add").attr('required',true);
  }else{
    $(".wage_per_day_div").hide();
    $("#daily_wage_add").removeAttr('required',true);
    $("#daily_wage_add").val(0);
    $("#overtime_rate_add").val(0);
    $("#ta_amount_add").val(0);
  }
});
$('input[type=radio][name=wage_fixed_edit]').change(function() {
  if (this.value == 1) {
    $(".wage_per_day_div").show();
    $("#daily_wage_edit").attr('required',true);
  }else{
    $(".wage_per_day_div").hide();
    $("#daily_wage_edit").removeAttr('required',true);
  }
});

function GetSearchWork(){
  return $('#table_filter_work').val();
}
function GetSearchWorkType(){
  return $('#table_filter_work_type').val();
}
function GetSearchLabour(){
  return $('#table_filter_labour').val();
}
function GetSearchLabourGroupHead(){
  return $('#table_filter_labour_group_head').val();
}
function GetSearchStartDate(){
  return $('#table_filter_start_date').val();
}
function GetSearchEndDate(){
  return $('#table_filter_end_date').val();
}
function GetSearchWorkedOnly(){
  if($("#table_worked_only").prop('checked') == true){
    return 1;
  }else{
    return 0;
  }
}
<?php } ?>
<?php if($page_name=='labours_view' || ($page_name=='labour_profile' && $labour_data->labour_type==1 && $labour_data->group_position==1)){?>
var labour_view_table = $('#labour_view_table').DataTable({
  "ajax":{
    url :"<?php echo base_url().'labours/select_labour' ?>", // json datasource
    type: "post", // type of method  , by default would be get
    data: function(d){
      <?php if($page_name=='labour_profile'){ ?>
        d.labour_group_head = "<?php echo $labour_data->labour_id?>";;
        <?php }else{ ?>
      d.labour_type = 0;
      <?php } ?>
    }
  },
  "bProcessing": true,
  "deferRender": true,
  "bDeferRender": true,
  "scrollX" : true,
  buttons: [
    { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Individual Labours List' },
    { 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": [ 1,13,17,18,19,20,21,22,23,24,25<?php if($this->config->item('Labour_projet_assigning')==0){ echo ",14";}?><?php if($page_name=='labour_profile'){ echo ",10,15,16";}?> ],
      className: "hidden"
    }
  ],
  'order': [[0, 'desc']]
});
<?php } ?>
<?php if($page_name=='labours_view'){?>
var labour_group_view_table = $('#labour_group_view_table').DataTable({
  "ajax":{
    url :"<?php echo base_url().'labours/select_labour' ?>", // json datasource
    type: "post", // type of method  , by default would be get
    data: function(d){
      d.labour_type = 1;
    }
  },
  "bProcessing": true,
  "deferRender": true,
  "bDeferRender": true,
  "scrollX" : true,
  buttons: [
    { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Group Labours List' },
    { 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": [ 1,17,18,19,20,21,22,23,24,25<?php if($this->config->item('Labour_projet_assigning')==0){ echo ",14";}?> ],
      className: "hidden"
    }
  ],
  'ordering': false,
  "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    if ( aData[2] == "" )
    {
      $('td', nRow).css('background-color', '#868686');
    }
  }
});
var labour_payment_view_table = $('#labour_payment_view_table').DataTable({
  "ajax":{
    url :"<?php echo base_url().'labours/select_labour_payment_summary' ?>", // json datasource
    type: "post", // type of method  , by default would be get
    data: function(d){
      d.work = GetSearchWork();
      d.work_type = GetSearchWorkType();
      d.labour = GetSearchLabour();
      d.labour_group_head = GetSearchLabourGroupHead();
      d.start_date = GetSearchStartDate();
      d.end_date = GetSearchEndDate();
      d.worked_only = GetSearchWorkedOnly()
    }
  },
  "bProcessing": true,
  "deferRender": true,
  "bDeferRender": true,
  "scrollX" : true,
  buttons: [
    { extend: 'print', className: 'btn dark btn-outline',exportOptions: {columns: ':visible'},title: 'Labour Payment 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
  ],
  // set the initial value
  "pageLength": 25,
  "columnDefs": [
    {
      "targets": [ 15 ],
      className: "hidden"
    }
  ],
  'order': [[0, 'desc']],
  "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    if(aData[15]=='1'){
      $('td', nRow).css('background-color', '#ffcccc');
    }else if(aData[15]=='2'){
      $('td', nRow).css('background-color', '#ccfaff');
    }
  }
});
$(".table_filter").on("change", function() {
  labour_view_table.ajax.reload();
  labour_payment_view_table.ajax.reload();
  labour_group_view_table.ajax.reload();
  if(GetSearchWork()!=0 && GetSearchWork()!='all' && GetSearchWork()!=''){
    labour_group_view_table.columns([5,11,13]).visible(false);
  }else{
    labour_group_view_table.columns([5,11,13]).visible(true);
  }
});
$(".hide_column").hide();

$(".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
      },
      daily_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');
      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);
        $("#"+modal_id).modal('toggle');
        $("."+form_id+"_fields").val('');
        $("."+form_id+"_select").val('').trigger("change");
        toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
        labour_view_table.ajax.reload();
        labour_payment_view_table.ajax.reload();
        labour_group_view_table.ajax.reload();
        select_work_type("work_type_add");
        select_work_type("work_type_edit");
        $('input[name=fixed_wage]').prop('checked', false);

        $(".custom-file-input").val(null);
        $(".custom-file-label").text('');
        $("#overtime_rate_add").val(0);
        $("#ta_amount_add").val(0);
      } 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");
    }
  })
)
});
jQuery(document).ready(function() {

});
</script>
<script>
$('body').on('click', '#labour_edit_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#labour_id').val(no.eq(0).text());
  $('#name').val(no.eq(20).text());
  var contact_no = no.eq(3).text();
  if (contact_no.indexOf(',') > -1) {
    var contact_nos = contact_no.split(',');
    $('#contact_no').val(contact_nos[0]);
    $('#contact_no_2').val(contact_nos[1]);
  }else{
    $('#contact_no').val(no.eq(3).text());
    $('#contact_no_2').val('');
  }
  $('#aadhar_no').val(no.eq(10).text());
  $('#address').val(no.eq(11).text());

  $('#daily_wage_edit').val(no.eq(6).text());
  $('#overtime_rate_edit').val(no.eq(7).text());
  $('#ta_amount_edit').val(no.eq(8).text());

  if(no.eq(19).text()==1){
    // $(".wage_per_day_div_edit").show();
    $("#daily_wage_edit").attr('required',true);
  }else{
    // $(".wage_per_day_div_edit").hide();
    $("#daily_wage_edit").removeAttr('required',true);
  }
  $("input[name=wage_fixed_edit][value='" + no.eq(19).text() + "']").attr('checked', true);
  $("input[name=active_status_edit][value='" + no.eq(24).text() + "']").attr('checked', true);
  $('#op_balance').val(no.eq(9).text());
  if(no.eq(25).text()!=''){
  $('#file_preview_edit').html(no.eq(12).html());
}else{
  $('#file_preview_edit').html("");
}
  $('#work_type_edit').val(no.eq(17).text()).trigger('change');
  // $("input[name=labour_type_edit][value=" + no.eq(12).text() + "]").prop('checked', true);
  if(no.eq(24).text()!=''){
    $("#photo_preview_edit").attr('src',"<?php echo base_url()?>uploads/labours/"+no.eq(24).text());
  }else{
    $("#photo_preview_edit").attr('src',"");
  }
  $('input[type=radio][name=change_basic_wage_previous][value=0]').prop('checked', true);
  $('#change_basic_wage_previous_div').hide();
  $("#labour_wage_edit_from_date").removeAttr('required',true);
})
$('input[type=radio][name=change_basic_wage_previous]').change(function() {
  var change_basic_wage_previous = $("input[type=radio][name=change_basic_wage_previous]:checked").val();
  if (change_basic_wage_previous == 1) {
    $("#change_basic_wage_previous_div").show();
    $("#labour_wage_edit_from_date").attr('required',true);
  }else{
    $("#change_basic_wage_previous_div").hide();
    $("#labour_wage_edit_from_date").removeAttr('required',true);
  }
})
$('body').on('click', '#labour_delete_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#labour_id_delete').val(no.eq(0).text());
})
$('body').on('click', '#member_add_btn', function() {
  var no = $(this).closest('tr').children('td');
  $('#team_head').val(no.eq(20).text());
  $('#labour_type_add').val(1);
  $('#team_head_div').show();
  $("#labour_group_add").val(no.eq(21).text());
})
<?php } ?>
</script>
