<?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">
          Add Labour 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().'labours/add_group' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Add Labour 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 New Labour Group
            </h3>
          </div>
        </div>
      </div>
      <!--begin::Form-->
      <form autocomplete="off" id="form_labour_add" method="post" action="<?php echo base_url().'labours/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">
              <h3 class="m-section__heading">Team Leader Details</h3>
              <div class="row">
                <input type="hidden" name="labour_type" value="1" />
                <div class="col-lg-4">
                  <div class="form-group m-form__group" >
                    <label for="user_name">
                      Labour Name
                    </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-user"></i>
                        </span>
                      </div>
                      <input required type="text" name="name"  class="form-control form_labour_add_fields m-input" placeholder="Labour Name" 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">
                      Contact No
                    </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-tag"></i>
                        </span>
                      </div>
                      <input required type="text" name="contact_no"  class="form_labour_add_fields form-control m-input" placeholder="Phone No" 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">
                      Work Type
                    </label>
                    <div class="input-group m-input-group m-input-group--pill">
                      <div class="input-group-prepend">
                        <a href="#work_type_add_modal"    data-toggle="modal" data-target="#work_type_add_modal" class="btn btn-focus btn_add_types">
                          <i class="fa fa-plus-circle"></i>
                        </a>
                      </div>
                      <select style="width: 80%" class="form-control m-select2 m-select2-general form_labour_add_select work_type" required id="work_type_add" name="work_type">

                      </select>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="col-lg-3">
                  <div class="m-form__group form-group" >
                    <label class="col-form-label">Fixed Daily Basic Wage</label>
                    <div class="col-12">
                      <div class="m-radio-inline">
                        <label class="m-radio m-radio--solid m-radio--state-brand">
                          <input required type="radio" name="fixed_wage" class="form_labour_add_fields" checked value="1"> Yes
                          <span></span>
                        </label>
                        <label class="m-radio m-radio--solid m-radio--state-success">
                          <input type="radio" name="fixed_wage" class="form_labour_add_fields" value="0"> No
                          <span></span>
                        </label>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div class="col-lg-3">
                  <div class="form-group m-form__group wage_per_day_div" >
                    <label for="user_name">
                      Wage Per <strong><?php echo ucwords($this->config->item('Labour_wage_calculation_type')) ?></strong>
                    </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 id="daily_wage_add" type="text" name="daily_wage"  class="form_labour_add_fields form-control m-input daily_wage" placeholder="Basic Wage Per Day" aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3" style="padding-right:5px;padding-left::5px">
                  <div class="form-group m-form__group">
                    <label for="user_name">
                      Alternate Contact No
                    </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-tag"></i>
                        </span>
                      </div>
                      <input type="text" name="contact_no_2"  class="form_labour_add_fields form-control m-input" placeholder="Alternate Phone No" aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-3" style="padding-left:5px">
                  <div class="form-group m-form__group">
                    <label for="user_name">
                      Aadhar No
                    </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-tag"></i>
                        </span>
                      </div>
                      <input type="text" name="aadhar_no"  class="form_labour_add_fields form-control m-input" placeholder="Aadhar No" aria-describedby="basic-addon1">
                    </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">
                      Address
                    </label>
                    <textarea class="form-control form_labour_add_fields m-input m-input--air m-input--pill" name="address" rows="3" placeholder="Address"></textarea>
                    <span class="m-form__help"></span>
                  </div>
                </div>
                <div class="col-lg-4">
                  <div class="form-group m-form__group">
                    <label for="user_name">
                      Photo
                    </label>
                    <div class="custom-file">
                      <input type="file" class="custom-file-input" id="customFile" name="uploaded_file">
                      <label class="custom-file-label"  for="customFile">Choose file</label>
                    </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">
                      Opening Balance Payable
                    </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" required type="text" name="op_balance"  class=" form-control m-input" placeholder="Opening Balance" aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help">Payable Balance At the Time of Profile Creation<br />Add "-" Sign For Receivable Balance</span>
                  </div>
                </div>
                <div class="col-lg-4" style="padding-left:5px;padding-right:5px">
                  <div class="form-group m-form__group">
                    <label for="user_name">
                      Document File
                    </label>
                    <div class="custom-file">
                      <input type="file" class="custom-file-input" id="customFile" name="uploaded_file_2">
                      <label class="custom-file-label"  for="customFile">Choose file</label>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>

            </div>
          </div>
          <div style="background: white;border: 4px solid #f7f7fa;padding: 30px;margin: 20px;padding-bottom: 0px">
            <div class="m-section">
              <h3 class="m-section__heading">Team Members Details</h3>
              <table style="width: 100%;max-width: 100%"  id="table_phone" class="table order-list table-striped table-bordered table-advance table-hover">
                <thead>
                  <tr>
                    <th style="width:8%">Sl No</th>
                    <th style="width:20%">Labour Name</th>
                    <th style="width:20%">Contact No</th>
                    <th style="width:20%">Work Type</th>
                    <th style="width:18%">Daily Wage/<?php echo ucwords($this->config->item('Labour_wage_calculation_type')) ?></th>
                    <th style="width:12%">Op. Balance</th>
                    <th style="width:6%"></th>
                  </tr>
                </thead>
                <tbody id="table_body">

                </tbody>
                <tfoot>
                  <tr>
                    <td colspan="7"  style="text-align: left;border-bottom: 1px solid #e7ecf1;">
                      <button type="button"  class="btn btn-block " id="addrow"><i class="fa fa-plus-square"></i> Add New Member</a>
                      </td>
                    </tr>
                    <tr>
                    </tr>
                  </tfoot>
                </table>
                <input type="hidden" value="0" id="current_row_count" name="current_row_count" >

              </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 class="modal fade" tabindex="-1" id="work_type_add_modal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog " role="document" style="max-width: 800px">
        <div class="modal-content">
          <div class="modal-header" style="background-color: #36a3f7;border-radius: 4px">
            <h5 class="modal-title" id="exampleModalLabel" style="color: #ffffff" >
              Create New Work Type
            </h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">
                &times;
              </span>
            </button>
          </div>
          <form autocomplete="off" id="form_work_type_add" data-modal-id="work_type_add_modal" method="post" action="<?php echo base_url().'work_types/create'?>" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
            <div class="modal-body">
              <div class="m-form__content">
                <div class="m-alert m-alert--icon alert alert-warning m--hide" role="alert" id="form_work_type_add_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="row">
                <div class="col-lg-12">
                  <div class="form-group m-form__group">
                    <label for="user_name">
                      Work Type Name
                    </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-tag"></i>
                        </span>
                      </div>
                      <input required type="text" name="name"  class="form-control m-input form_work_type_add_fields" placeholder="Work Type Name" aria-describedby="basic-addon1">
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-dismiss="modal">
                Close
              </button>
              <button type="submit" class="btn btn-primary form_submit_btn">
                Submit
              </button>
            </div>
          </form>
        </div>
      </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_1").addClass('m-menu__item--active');
</script>
<script>
function select_work_type(select_id){
  $(select_id).each(function (index, value) {
    var id = $(this).attr('id');
    var selected_option = $('#'+id+' :selected').val();
    $("#"+id).html('');
    $("#"+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) {
          $("#"+id).append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
          i++;
        }
      }
      if(selected_option!=''){
        $("#"+id).val(selected_option).trigger('change');
      }
    })
  });
  $(".work_type").select2({
    placeholder: "Select Work Type",
    dropdownParent: $("#form_labour_add")
  })
}
select_work_type(".work_type");

$('input[type=radio][name=fixed_wage]').change(function() {
  if (this.value == 1) {
    $(".wage_per_day_div").show();
    $(".daily_wage").attr('required',true);
  }else{
    $(".wage_per_day_div").hide();
    $(".daily_wage").removeAttr('required',true);
  }
});
</script>
<script>


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

  add=function() {
    $("#form_labour_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 () {
  $("#addrow").on("click", function () {
    var counter = parseInt($("#current_row_count").val());
    var newRow = $("<tr>");
    var cols = "";
    cols += '<td><div class="form-group m-form__group" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input style="padding: 10px" readonly type="text" class="form-control m-input" name="labour[sl_no]['+(counter)+']" value="' + (counter+1) + '"/></div></div></td>';
    cols += '<td><div class="form-group m-form__group" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text" required class="form-control m-input" name="labour[name]['+(counter)+']" id="labour_name_' + (counter+1) + '"/></div></div></td>';
    cols += '<td><div class="form-group m-form__group" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text"  class="form-control m-input" name="labour[phone]['+(counter)+']" id="labour_phone_' + (counter+1) + '"/></div></div></td>';
    cols += '<td><div class="form-group m-form__group" style="padding: 0px;margin: 0px;"><div class="m-select2 m-select2--air m-select2--pill">'+
    '<select required style="width: 100%" class="form-control m-select2  work_type" name="labour[work_type]['+(counter)+']" id="labour_work_type_' + (counter+1) + '"></select></div><span class="m-form__help"></span></div></td>';
    cols += '<td><div class="form-group m-form__group wage_per_day_div" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text"  class="form-control m-input daily_wage" name="labour[daily_wage]['+(counter)+']" id="labour_wage_' + (counter+1) + '"/></div></div></td>';
    cols += '<td><div class="form-group m-form__group" style="padding: 0px;margin: 0px;"><div class="input-group m-input-group m-input-group--pill m-input-group--air"><input type="text"  class="form-control m-input" name="labour[op_balance]['+(counter)+']" id="labour_op_balance_' + (counter+1) + '"/></div></div></td>';
    cols += '<td><a href="javascript:" class="ibtnDel btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td>';
    cols += '</tr>';
    newRow.append(cols);
    $("#table_body").append(newRow);
    counter++;
    $("#current_row_count").val(counter);
    select_work_type(".work_type");
    var fixed_wage = $("input[name='fixed_wage']:checked").val();
    if (fixed_wage == 1) {
      $(".wage_per_day_div").show();
      $(".daily_wage").attr('required',true);
    }else{
      $(".wage_per_day_div").hide();
      $(".daily_wage").removeAttr('required',true);
    }
  });
  $("#table_body").on("click", ".ibtnDel", function (event) {
    var counter = parseInt($("#current_row_count").val());
    $(this).closest("tr").remove();
    counter -= 1;
    $("#current_row_count").val(counter);
  });
})
</script>
