<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->
<style>

#table_labours th, #table_labours td {
  padding-left: 5px !important;
  padding-right: 5px !important;
}
</style>
<?php $this->view("includes/Inc_navigation");?>
<div class="m-grid__item m-grid__item--fluid m-wrapper">
  <!-- BEGIN: Subheader -->
  <div class="m-subheader ">
    <div class="d-flex align-items-center">
      <div class="mr-auto">
        <h3 class="m-subheader__title m-subheader__title--separator">
          Add Labour Wage Slip (Group)
        </h3>
        <ul class="m-subheader__breadcrumbs m-nav m-nav--inline">
          <li class="m-nav__item m-nav__item--home">
            <a href="#" class="m-nav__link m-nav__link--icon">
              <i class="m-nav__link-icon la la-home"></i>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'dashboard'?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Home
              </span>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo base_url().'labour_wages/add_group' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Add Labour Wage Slip (Group)
              </span>
            </a>
          </li>


        </ul>
      </div>
      <div>

      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <!--begin::Portlet-->
    <div class="m-portlet">
      <div class="m-portlet__head">
        <div class="m-portlet__head-caption">
          <div class="m-portlet__head-title">
            <h3 class="m-portlet__head-text">
              Add Labour Wage Slip (Group)
            </h3>
          </div>
        </div>
      </div>
      <!--begin::Form-->
      <form autocomplete="off" id="form_labour_wage_add" method="post" action="<?php echo base_url().'labour_wages/create_group'?>" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right ">
        <div class="m-portlet__body" style="padding-top: 0px">
          <div class="m-form__content">
            <div class="m-alert m-alert--icon alert alert-warning m--hide" role="alert" id="form_work_msg">
              <div class="m-alert__icon">
                <i class="la la-warning"></i>
              </div>
              <div class="m-alert__text">
                Oh snap! Change a few things up and try submitting again.
              </div>
              <div class="m-alert__close">
                <button type="button" class="close" data-close="alert" aria-label="Close"></button>
              </div>
            </div>
          </div>
          <div style="background: white;border: 4px solid #f7f7fa;padding: 30px;margin: 20px;padding-bottom: 0px">
            <div class="m-section" style="margin-bottom: 20px">
              <div class="row">
                <div class="col-lg-7">
                  <div class="form-group m-form__group row">
                    <label for="example-text-input" class="col-4 col-form-label"><h3 class="m-section__heading">Select Team Leader</h3></label>
                    <div class="col-8">
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select required style="width: 100%" class="form-control m-select2  form_labour_wage_add_select" id="labour_head_add" name="labour"  >
                          <option selected disabled>Select Labour</option>
                          <?php
                          foreach($labours as $row):
                            if($row->labour_type==0){
                              $labour_type = "Individual";
                            }else{
                              if($row->group_position==1){
                                $labour_type = "Group Head";
                              }else{
                                $labour_type = "Group Member";
                              }
                            }
                            ?>
                            <option value="<?php echo $row->labour_id?>"><?php echo $row->labour_name." [".$row->type_name."] - (".$labour_type.")"?></option>
                            <?php
                          endforeach;
                          ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div class="col-lg-5">
                  <div class="form-group m-form__group row" >
                    <label for="example-text-input" class="col-6 col-form-label"><h3 class="m-section__heading">Work Date</h3></label>
                    <div class="col-6">
                      <div class="input-group m-input-group--air m-input-group--pill  date">
                        <input required value="<?php echo date('d-m-Y')?>"  data-date-format="dd-mm-yyyy" id="work_date" required type="text" name="work_date" class="form-control  m-input " placeholder="Work Date" >
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="labour_list_div" style="background: white;border: 4px solid #f7f7fa;padding-top: 30px;margin: 20px;padding-bottom: 0px;display: none">
            <div class="m-section">
              <h3 class="m-section__heading">List of Labours</h3>
              <table style="width: 100%;max-width: 100%"  id="table_labours" class="table order-list table-striped table-bordered table-advance table-hover">
                <thead>
                  <tr>
                    <th style="width:6%">Sl No</th>
                    <th style="width:15%">Labour</th>
                    <th style="width:5%">Worked</th>
                    <th style="width:12%">Work</th>
                    <th style="width:5%"><?php echo ucwords($this->config->item('Labour_wage_calculation_type')) ?>s</th>
                    <th style="width:5%">Labours</th>
                    <th style="width:7%">Basic Wage</th>
                    <th style="width:6%">Overtime</th>
                    <th style="width:6%">Extra</th>
                    <th style="width:5%">TA</th>
                    <th style="width:8%">Total</th>
                    <?php
                    $member_payment_details_show='';
                    if($this->config->item('Labour_group_member_payment')==0){
                      $member_payment_details_show='display:none';
                    }?>
                    <th style="width:7%;<?php echo $member_payment_details_show?>">Prev. Balance</th>
                    <th style="width:8%;<?php echo $member_payment_details_show?>">Total to Pay</th>
                    <th style="width:8%">Total Paid</th>
                  </tr>
                </thead>
                <tbody id="table_body">

                </tbody>
                <tfoot>
                  <tr>
                  </tr>
                </tfoot>
              </table>
              <input type="hidden" value="0" id="current_row_count" name="current_row_count" >

            </div>
          </div>
          <div class="labour_list_div" style="background: white;border: 4px solid #f7f7fa;padding: 30px;margin: 20px;padding-bottom: 0px;display: none">
            <div class="m-section">
              <h3 class="m-section__heading">Summary</h3>
              <div class="row">
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Total Labours Worked
                    </label>
                    <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                      <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                          <i class="la la-users"></i>
                        </span>
                      </div>
                      <input value="0" readonly min="1" type="text" name="grand_total_labours_worked" id="grand_total_labours_worked"  class="form-control form_labour_add_fields m-input" aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Total Wage
                    </label>
                    <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                      <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                          <?php echo $this->config->item('Currency_currency_format'); ?>
                        </span>
                      </div>
                      <input value="0" readonly type="text" name="grand_total_wage" id="grand_total_wage"  class="form_labour_add_fields form-control m-input"  aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Total Previous Balance
                    </label>
                    <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                      <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                          <?php echo $this->config->item('Currency_currency_format'); ?>
                        </span>
                      </div>
                      <input value="0" readonly type="text" name="grand_total_previous_balance" id="grand_total_previous_balance"  class="form_labour_add_fields form-control m-input"  aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Total To Pay
                    </label>
                    <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                      <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                          <?php echo $this->config->item('Currency_currency_format'); ?>
                        </span>
                      </div>
                      <input value="0" readonly type="text" name="grand_total_to_pay" id="grand_total_to_pay"  class="form_labour_add_fields form-control m-input"  aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Total Paid
                    </label>
                    <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                      <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                          <?php echo $this->config->item('Currency_currency_format'); ?>
                        </span>
                      </div>
                      <input value="0" readonly type="text" name="grand_total_paid" id="grand_total_paid"  class="form_labour_add_fields form-control m-input"  aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-6">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Wage Remarks
                    </label>
                    <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                      <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                          <i class="la la-edit"></i>
                        </span>
                      </div>
                      <input  type="text" name="remarks" id="remarks"  class="form-control form_labour_add_fields m-input" placeholder="Remarks" aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Current Balance
                    </label>
                    <div class="input-group m-input-group m-input-group--pill m-input-group--air">
                      <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1">
                          <?php echo $this->config->item('Currency_currency_format'); ?>
                        </span>
                      </div>
                      <input value="0" readonly type="text" name="grand_total_current_balance" id="grand_total_current_balance"  class="form_labour_add_fields form-control m-input"  aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="labour_list_div" style="background: white;border: 4px solid #f7f7fa;padding: 30px;margin: 20px;padding-bottom: 0px;display: none">
            <div class="m-section" style="margin: 0px 0 10px 0;">
              <h3 class="m-section__heading" style="padding: 10px;margin: 0px">Transaction Details</h3>
              <div class="row" style="padding-top: 15px" >
                <div class="col-lg-3" >
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Paid Amount
                    </label>
                    <div class="m-input-icon m-input-icon--right">
                      <input required readonly type="text" name="paid_amount" id="labour_wage_paid_amount_add" value="0" class="form-control m-input m-input--pill m-input--air form_labour_wage_add_fields" placeholder="Paid Amount" aria-describedby="basic-addon1">
                      <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Paid Date
                    </label>
                    <div class="m-input-icon m-input-icon--right">
                      <input  data-date-format="dd-mm-yyyy" id="labour_wage_paid_date_add"  type="text" name="paid_date" class="form_labour_wage_add_fields form-control m-input m-input--pill m-input--air labour_wage_paid_amount_req_fields " placeholder="Paid Date" >
                      <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-calendar"></i></span></span>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Paid From Acc
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select id="labour_wage_debit_acc_add" name="debit_acc" style="width: 100%" class="form-control m-input form_labour_wage_add_select labour_wage_paid_amount_req_fields"  aria-describedby="basic-addon1">
                        <?php
                        foreach($this->config->item('account_heads') as $row):
                          $selected="";
                          if($row->acc_head_id==1){
                            $selected="selected";
                          }
                          ?>
                          <option <?php echo $selected?> value="<?php echo $row->acc_head_id?>"><?php echo $row->acc_head_name?></option>
                          <?php
                        endforeach;
                        ?>
                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Payment Mode
                    </label>
                    <div class="m-select2 m-select2--air m-select2--pill">
                      <select id="labour_wage_payment_mode_add" name="payment_mode" style="width: 100%" class="form-control m-input form_labour_wage_add_select labour_wage_paid_amount_req_fields"  aria-describedby="basic-addon1">
                        <option selected disabled>Select Payment Mode</option>
                        <?php
                        foreach($this->config->item('payment_modes') as $row):
                          ?>
                          <option value="<?php echo $row?>"><?php echo $row?></option>
                          <?php
                        endforeach;
                        ?>
                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
              <div class="row" >
                <div class="col-lg-3" >
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      TR Reference No
                    </label>
                    <div class="m-input-icon m-input-icon--right">
                      <input type="text" name="reference_no"  class="form-control m-input m-input--pill m-input--air form_labour_wage_add_fields" placeholder="Reference No" aria-describedby="basic-addon1">
                      <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      TR Reference Date
                    </label>
                    <div class="m-input-icon m-input-icon--right">
                      <input type="text" data-date-format="dd-mm-yyyy" name="reference_date" id="labour_wage_reference_date_add"  class="form-control m-input m-input--pill m-input--air form_labour_wage_add_fields" placeholder="Reference Date" aria-describedby="basic-addon1">
                      <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-tag"></i></span></span>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-6" >
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Transaction Remarks
                    </label>
                    <div class="m-input-icon m-input-icon--right">
                      <input type="text" name="tr_remarks" class="form-control m-input m-input--pill m-input--air form_labour_wage_add_fields" placeholder="Transaction Remarks" aria-describedby="basic-addon1">
                      <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="m-portlet__foot m-portlet__foot--fit">
          <div class="m-form__actions m-form__actions">
            <div class="row">
              <div class="col-lg-9 ml-lg-auto">
                <button type="submit" class="btn btn-brand">
                  Submit
                </button>
                <button type="reset" class="btn btn-secondary">
                  Cancel
                </button>
              </div>
            </div>
          </div>
        </div>
      </form>
      <!--end::Form-->

    </div>
  </div>


</div>
</div>
<!-- end:: Body -->
<!-- begin::Footer -->

<?php $this->view("includes/Inc_footer");?>
<!-- end::Footer -->
</div>
<!-- end:: Page -->

<?php $this->view("includes/Inc_sidebar");?>
<?php $this->view("includes/Inc_scripts");?>
<!--begin::Page Snippets -->

<script>
$("#labour_menu").addClass('m-menu__item--active m-menu__item--open');
$("#labour_menu_2").addClass('m-menu__item--active');
</script>
<script>
//    Toggle Sidebar
$("#m_aside_left_minimize_toggle").addClass("m-brand__toggler--active");
$("#body").addClass("m-brand--minimize m-aside-left--minimize");

$("#labour_head_add").select2({
  placeholder: "Select Team Head",
  dropdownParent: $("#form_labour_wage_add")
})
$("#work_date").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 } ?>
  })

  $("#labour_wage_paid_date_add,#labour_wage_paid_date_edit,#labour_wage_reference_date_add,#labour_wage_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 } ?>
    })
    $("#labour_wage_payment_mode_add").select2({
      placeholder: "Select Debit Account",
      dropdownParent: $("#form_labour_wage_add")
    })
    $("#labour_wage_debit_acc_add").select2({
      placeholder: "Select Debit Account",
      dropdownParent: $("#form_labour_wage_add")
    })
    </script>
    <script>


    //  User Add / Edit Form Validation
    var FormControls=function() {

      add=function() {
        $("#form_labour_wage_add").validate( {
          rules: {
            email: {
              email: !0
            },
            client_contact: {
              number: !0,
              required: !0
            },
            budget_amt: {
              number: !0,
              required: !0
            }
          },
          invalidHandler:function(e, r) {
            var i=$("#form_work_msg");
            i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
          }
          , submitHandler:function(e) {
            mApp.blockPage({
              overlayColor: "#000000",
              type: "loader",
              state: "primary",
              message: "Processing...Please Wait"
            }), setTimeout(function() {}, 3e3);
            this.submit();
          }
        }
      );
      $('.m-select2,.m-select2-multiple',  $("#form_work")).change(function () {
        $("#form_work").validate().element($(this)); //revalidate the chosen dropdown value and show error or success message for the input
        $(this).select2("close");
      });
    };
    return {
      init:function() {
        add();
      }
    }
  }

  ();
  jQuery(document).ready(function() {
    FormControls.init();

    $(".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);
            select_work_type(".work_type");
            $('input[type=radio]').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");
        }
      })
    )
  });
});

</script>

<script>
$(document).ready(function () {
  $('body').on('change', '#labour_head_add', function() {
    var labour_id=$('#labour_head_add :selected').val();
    var work_id = "<?php echo $work_id?>";
    $(".labour_list_div").show();
    $("#table_body").html("");
    $.post("<?php echo base_url() ?>labours/select_details",{labour_id:labour_id},function(data1) {
      var obj1 = $.parseJSON(data1);
      if(obj1.count==1) {
        var labour_group = obj1.labour_group;
        $.post("<?php echo base_url() ?>labours/select",{labour_group:labour_group},function(data) {
          var obj = $.parseJSON(data);
          var i=0;
          var table_body="";
          if(obj.count>0) {
            while (i < obj.id.length) {
              table_body += '<tr>';
              table_body += '<td><div class="form-group m-form__group" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input style="padding: 10px" readonly type="text" class="form-control m-input" name="wage_slip[sl_no]['+(i)+']" value="' + (i+1) + '"/></div></div></td>';
              table_body += '<td><div class="form-group m-form__group" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input style="padding: 10px" readonly type="text" class="form-control m-input" name="wage_slip[labour_name]['+(i)+']" value="' + obj.name[i] + '"/><input type="hidden"  name="wage_slip[labour_id]['+(i)+']" value="' + obj.id[i] + '"/></div></div></td>';
              table_body += '<td><div class="form-group m-form__group" style="padding: 0px;margin: 0px;"><span class="m-switch m-switch--outline m-switch--icon m-switch--success"><label><input type="checkbox" class="work_status " data-labour-no="' + (i+1) + '" checked="checked" name="wage_slip[work_status]['+(i)+']" id="work_status_' + (i+1) + '"><span></span></label></span></div></td>';
              table_body += '<td><div class="form-group m-form__group select_work worked_field_'+(i+1)+' div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="m-select2 m-select2--air"><select required  style="width: 100%" class="form-control m-select2  form_labour_wage_add_select field_labour_'+(i+1)+'" data-labour-no="' + (i+1) + '" id="work_add_' + (i+1) + '" name="wage_slip[work]['+(i)+']"  ><option selected disabled>Select Work</option>'+
              <?php
              foreach($work_contracts as $row):
                $selected = '';
                if($work_id!='' && $work_id==$row->work_id){
                  $selected='selected';
                }
                ?>
                '<option <?php echo $selected?> value="<?php echo $row->work_id?>"><?php echo str_replace("'","",$row->work_name)?></option>'+
                <?php
              endforeach;
              ?>
              '</select></div></div></td>';
              table_body += '<td><div class="form-group m-form__group worked_field_'+(i+1)+' div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' wage_calculate_fields numeric_fields" name="wage_slip[no_of_days]['+(i)+']" id="no_of_days_' + (i+1) + '" value="1"/></div></div></td>';
                table_body += '<td><div class="form-group m-form__group worked_field_'+(i+1)+' div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' wage_calculate_fields numeric_fields" name="wage_slip[no_of_labours]['+(i)+']" id="no_of_labours_' + (i+1) + '" value="1"/></div></div></td>';
              table_body += '<td><div class="form-group m-form__group worked_field_'+(i+1)+' div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' wage_calculate_fields numeric_fields" name="wage_slip[basic_wage]['+(i)+']" id="basic_wage_' + (i+1) + '" value="' + obj.daily_wage[i] + '"/></div></div></td>';
              table_body += '<td><div class="form-group m-form__group worked_field_'+(i+1)+' div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' wage_calculate_fields numeric_fields" name="wage_slip[overtime]['+(i)+']" id="overtime_' + (i+1) + '" value="0"/></div></div></td>';
              table_body += '<td><div class="form-group m-form__group worked_field_'+(i+1)+' div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' wage_calculate_fields numeric_fields" name="wage_slip[extra]['+(i)+']" id="extra_' + (i+1) + '" value="0"/></div></div></td>';
              table_body += '<td><div class="form-group m-form__group worked_field_'+(i+1)+' div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' wage_calculate_fields numeric_fields" name="wage_slip[ta]['+(i)+']" id="ta_' + (i+1) + '" value="' + obj.ta_per_day[i] + '"/></div></div></td>';
              table_body += '<td ><div class="form-group m-form__group worked_field_'+(i+1)+' div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' total_wage_field " readonly name="wage_slip[total_wage]['+(i)+']" id="total_wage_' + (i+1) + '" value="' + (parseFloat(obj.daily_wage[i])+ parseFloat(obj.ta_per_day[i])) + '"/></div></div></td>';

              var member_payment_disabled="";
              var member_payment_details_show="";
              var labour_settings_group_member_payment = "<?php echo $this->config->item('Labour_group_member_payment')?>";
              if(labour_settings_group_member_payment==0){
                member_payment_details_show="style='display:none'";
              }
              if(labour_settings_group_member_payment==0 && obj.labour_type[i]==1 && obj.group_position[i]==0){
                member_payment_disabled="style='display:none'";
              }
              table_body += '<td '+member_payment_details_show+'><div class="form-group m-form__group div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' previous_balance_field " readonly name="wage_slip[prev_balance]['+(i)+']" id="prev_balance_' + (i+1) + '" value="' + obj.current_balance[i] + '"/></div></div></td>';
              table_body += '<td '+member_payment_details_show+'><div class="form-group m-form__group div_labour_'+(i+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input field_labour_'+(i+1)+' total_to_pay_field " readonly name="wage_slip[total_to_pay]['+(i)+']" id="total_to_pay_' + (i+1) + '" value="' + (parseFloat(obj.daily_wage[i])+parseFloat(obj.current_balance[i])) + '"/></div></div></td>';
              table_body += '<td><div class="form-group m-form__group " style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input '+member_payment_disabled+' required style="padding: 10px" data-labour-no="' + (i+1) + '" type="text" class="form-control m-input total_paid_field numeric_fields" name="wage_slip[total_paid]['+(i)+']" id="total_paid_' + (i+1) + '" value="0"/></div></div></td>';
              table_body += '</tr>';

              i++;
            }
            $("#table_body").append(table_body);
            $(".form_labour_wage_add_select").select2({
              placeholder: "Select Work",
              dropdownParent: $("#form_labour_wage_add")
            })
            calculate_summary();
            $(".numeric_fields").each(function () {
              $(this).rules('add', {
                number: !0
              });
            });

            var group_wage_worked_selected_default = "<?php echo $this->config->item('Labour_group_wage_worked_selected_default')?>";
            if(group_wage_worked_selected_default!=1){
              $(".work_status").each(function () {
                $(this).attr('checked',false).trigger('change');
              });
            }


          }
        })
      }
    })
    if(work_id!='' && work_id !=0){
      $(".select_work").val(work_id).trigger('change');
    }
  })
  $('body').on('change', '.work_status', function() {
    var id = $(this).attr('id');
    var labour_no = $(this).attr('data-labour-no');
    if ($('#'+id).is(':checked')){
      $(".worked_field_"+labour_no).show();
      $(".field_labour_"+labour_no).attr('required',true);
    }else {
      $(".worked_field_"+labour_no).hide();
      $(".field_labour_"+labour_no).removeAttr('required',true);
    }
    calculate_wage(labour_no);
  })
  function calculate_wage(labour_no){
    var wage_per_day = parseFloat($("#basic_wage_"+labour_no).val());
    var no_of_days = parseFloat($("#no_of_days_"+labour_no).val());
      var no_of_labours = parseFloat($("#no_of_labours_"+labour_no).val());
    var overtime_wage = parseFloat($("#overtime_"+labour_no).val());
    var extra_wage = parseFloat($("#extra_"+labour_no).val());
    var ta_wage = parseFloat($("#ta_"+labour_no).val());
    var previous_balance = parseFloat($("#prev_balance_"+labour_no).val());
    var total_wage = (no_of_days*no_of_labours*wage_per_day) + overtime_wage+extra_wage+ta_wage;
    if ($('#work_status_'+labour_no).is(':checked')){
      var total_balance = previous_balance + total_wage;
      $("#total_wage_"+labour_no).val(total_wage);
      $("#total_to_pay_"+labour_no).val(total_balance);
    }else{
      $("#total_wage_"+labour_no).val(0);
      $("#total_to_pay_"+labour_no).val(previous_balance);
    }
    calculate_summary();
  }
  function calculate_summary(){
    var total_labours_worked = total_wage = previous_balance = total_to_pay = total_paid = 0;
    $(".work_status").each(function(){
      if ($(this).is(':checked')){
        var labour_no = $(this).attr('data-labour-no');
        var no_of_labours = parseFloat($("#no_of_labours_"+labour_no).val());
        total_labours_worked = total_labours_worked+no_of_labours;
      }
    });
    $(".total_wage_field").each(function(){
      total_wage += +$(this).val();
    });
    $(".previous_balance_field").each(function(){
      previous_balance += +$(this).val();
    });
    $(".total_to_pay_field").each(function(){
      total_to_pay += +$(this).val();
    });
    $(".total_paid_field").each(function(){
      total_paid += +$(this).val();
    });

    $("#grand_total_labours_worked").val(total_labours_worked);
    $("#grand_total_wage").val(total_wage);
    $("#grand_total_previous_balance").val(previous_balance);
    $("#grand_total_to_pay").val(total_to_pay);
    $("#grand_total_paid").val(total_paid);
    $("#labour_wage_paid_amount_add").val(total_paid);
    if(total_paid>0){
      $(".labour_wage_paid_amount_req_fields").attr('required',true);
    }else{
      $(".labour_wage_paid_amount_req_fields").removeAttr('required',true);
    }
    var current_balance = parseFloat(total_to_pay)-parseFloat(total_paid);
    $("#grand_total_current_balance").val(current_balance);
  }
  $('body').on('change keyup', '.wage_calculate_fields', function() {
    var labour_no = $(this).attr('data-labour-no');
    if($(this).val()==''){
      $(this).val(0);
    }
    calculate_wage(labour_no);
  })
  $('body').on('change keyup', '.total_paid_field', function() {
    var labour_no = $(this).attr('data-labour-no');
    if($(this).val()==''){
      $(this).val(0);
    }
    calculate_summary();
  })
})
</script>
