<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->
<?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">
          Quick Add Labour Payment
        </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_quick_payment' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Quick Add Labour Payment
              </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">
              Quick Add Labour Payment
            </h3>
          </div>
        </div>
      </div>
      <!--begin::Form-->
      <form autocomplete="off" id="form_labour_wage_add" method="post" action="<?php echo base_url().'labour_wages/create_quick_payment'?>" 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 class="labour_list_div" style="background: white;border: 4px solid #f7f7fa;padding: 30px;margin: 20px;padding-bottom: 0px;">
            <div class="m-section">
              <div class="row">
                <div class="col-lg-6"><h3 class="m-section__heading" style="margin-top: 15px;">List of <?php if($this->config->item('Labour_projet_assigning')==1 ){ echo "Assigned "; } ?>Labours</h3></div>
                <div class="col-lg-3"></div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group row" style="margin-bottom: 15px;">
                    <select style="width: 100%" class="form-control m-select2 " id="work_type"  >

                    </select>
                  </div>
                </div>
              </div>

              <table style="width: 100%;max-width: 100%"  id="table_labour_quick_payment" 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%">Pay/Not</th>
                    <th style="width:8%">Current Balance</th>
                    <th style="width:8%">Paid Amount</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;">
            <div class="m-section">
              <h3 class="m-section__heading">Summary</h3>
              <div class="row">
                <div class="col-lg-4">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Total Paid Amount
                    </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 required value="0" readonly type="text" name="total_paid_amount" id="total_paid_amount"  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-4">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Paid Date
                    </label>
                    <div class="m-input-icon m-input-icon--right">
                      <input required 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-4">
                  <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 required 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>
              <div class="row" >
                <div class="col-lg-4">
                  <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 required 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 class="col-lg-4" >
                  <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-4">
                  <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-12">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      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="tr_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>
            </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>


$("#work_add").select2({
  placeholder: "Select Project",
  dropdownParent: $("#form_labour_wage_add"),
  templateResult: format_project_dropdown
})
$("#work_type").select2({
  placeholder: "Select Work Type",
  dropdownParent: $("#form_labour_wage_add")
})
function select_work_type(select_id){
  $('#'+select_id).html('');
  $("#"+select_id).append("<option selected value='all' >All Work Types</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");

$("#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 } ?>
    })
    $('body').on('change', '#labour_wage_payment_mode_add', function() {
      var payment_mode=$('#labour_wage_payment_mode_add :selected').val();
      if(payment_mode=='POST DATED CHEQUE'){
        $('#labour_wage_paid_date_add').datepicker('destroy');
        $("#labour_wage_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{
          $('#labour_wage_paid_date_add').datepicker('destroy');
          $("#labour_wage_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 } ?>
            })
          }
        })
        $("#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) {
                var total_checked = $('input.work_status:checked').length;
                if(total_checked!=0){
                  mApp.blockPage({
                    overlayColor: "#000000",
                    type: "loader",
                    state: "primary",
                    message: "Processing...Please Wait"
                  }), setTimeout(function() {}, 3e3);
                  this.submit();
                }else{
                  toastr["warning"]("Please Select Labours", "No Labours Selected");
                }

              }
            }
          );
          $('.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);
                $('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', '#work_type', function() {
        get_labours();
      })
      <?php if($this->config->item('Labour_projet_assigning')==1 ){ ?>
        $('body').on('change', '#work_add', function() {
          get_labours();
          $("#btn_show_not_assigned_labours").show();
        })
        <?php } ?>
        $('body').on('click', '#btn_show_not_assigned_labours', function() {
          get_labours('not_assigned');
          $("#btn_show_not_assigned_labours").hide();
        })
        var selected_work = '<?php echo $work_id ?>';
        $('#work_add').val(selected_work).trigger('change');

        function get_labours(assign_type=''){
          var work_type=$('#work_type').val();
          var work_id=$('#work_add').val();
          var current_row_count = parseFloat($('#current_row_count').val());
          $.post("<?php echo base_url() ?>labours/select/member_payment",{work_type:work_type,work_id:work_id,assign_type:assign_type},function(data) {
            // $("#table_body").html("");
            var obj = $.parseJSON(data);
            var i=0;
            var table_body="";
            $('#table_labour_quick_payment').dataTable().fnDestroy();
            if(obj.count>0) {
              while (i < obj.id.length) {
                var sl_no = current_row_count+i;
                if(i>1 && obj.labour_group[i]!=obj.labour_group[i-1]){
                  table_body += '<tr style="border-top: 3px solid black">';
                }else{
                  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="payment_data[sl_no]['+(sl_no)+']" value="' + (sl_no+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="payment_data[labour_name]['+(sl_no)+']" value="' + obj.name[i] + '"/><input type="hidden"  name="payment_data[labour_id]['+(sl_no)+']" 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="' + (sl_no+1) + '"  name="payment_data[work_status]['+(sl_no)+']" id="work_status_' + (sl_no+1) + '"><span></span></label></span></div></td>';
                table_body += '<td><div style="display:none" class="form-group m-form__group payment_field_'+(sl_no+1)+' div_labour_'+(sl_no+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input readonly style="padding: 10px" data-labour-no="' + (sl_no+1) + '" type="text" class="form-control m-input field_labour_'+(sl_no+1)+'" name="payment_data[current_balance]['+(sl_no)+']" id="current_balance_' + (sl_no+1) + '" value="' + obj.current_balance[i] + '"/></div></div></td>';
                table_body += '<td><div style="display:none" class="form-group m-form__group payment_field_'+(sl_no+1)+' div_labour_'+(sl_no+1)+'" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group  m-input-group--air"><input  style="padding: 10px" data-labour-no="' + (sl_no+1) + '" type="text" class="form-control m-input field_labour_'+(sl_no+1)+' payment_calculate_fields numeric_fields labour_paid_amount" name="payment_data[paid_amount]['+(sl_no)+']" id="paid_amount_' + (sl_no+1) + '" /></div></div></td>';

                table_body += '</tr>';

                i++;
              }
              $("#table_body").append(table_body);
              $('#current_row_count').val(current_row_count+parseFloat(obj.count));
              calculate_summary();
              $(".numeric_fields").each(function () {
                $(this).rules('add', {
                  number: !0
                });
              });
            }else{
              $("#table_body").html("");
              $('#current_row_count').val(current_row_count+parseFloat(obj.count));
              calculate_summary();
            }

            var table = $('#table_labour_quick_payment').DataTable({
              "scrollX" : true,
              dom:"<'row'<'col-sm-6 text-left'><'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
              "pageLength": false,
              'ordering': false,
              "bLengthChange": false,
              "bPaginate": false,
              "bInfo": false,
            });
          })

        }

        get_labours();
        $('body').on('change', '.work_status', function() {
          var id = $(this).attr('id');
          var labour_no = $(this).attr('data-labour-no');
          if ($('#'+id).is(':checked')){
            $(".payment_field_"+labour_no).show();
            $("#paid_amount_"+labour_no).attr('required',true);
          }else {
            $(".payment_field_"+labour_no).hide();
            $("#paid_amount_"+labour_no).removeAttr('required',true);
          }
          calculate_summary();
        })

        function calculate_summary(){
          var total_paid = 0;
          $(".labour_paid_amount").each(function(){
            var labour_no = $(this).attr('data-labour-no');
            if ($('#work_status_'+labour_no).is(':checked')){
              total_paid += +$(this).val();
            }
          });
          $("#total_paid_amount").val(total_paid);
        }
        $('body').on('change keyup', '.payment_calculate_fields', function() {
          var labour_no = $(this).attr('data-labour-no');
          if($(this).val()==''){
            $(this).val(0);
          }
          calculate_summary();
        })

      })
      </script>
