<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->
<style>
.item_table td{
  padding: .75rem !important;
}
.item_table th{
  white-space: nowrap;
  padding: .75rem !important;
}
.table thead th {
  border-top: 2px solid #f4f5f8 !important;
}
.payment-table tbody td{
  border-top: 2px solid #f4f5f8 !important;
}
.m-invoice-2 .m-invoice__wrapper .m-invoice__head .m-invoice__container.m-invoice__container--centered {
  width: 85% !important;
}
.m-invoice-2 .m-invoice__wrapper .m-invoice__body.m-invoice__body--centered {
  width: 85% !important;
}
.holder {
  height: 100px;
  width: 100px;
  border: 2px solid black;
}
.holder img {
  max-width: 100px;
  max-height: 100px;
  min-width: 100px;
  min-height: 100px;
}
</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">
          Work Order
        </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().'work_order' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Work Order
              </span>
            </a>
          </li>
        </ul>
      </div>
      <div></div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <form autocomplete="off" id="form_work_order_add" data-form-type="work_order_create" method="post" action="<?php echo base_url()?>work_order/create" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
      <div class="m-portlet m-portlet--full-height">
        <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 Work Order - Step 1/2 <?php if(!empty($quotation_data)){ echo " [ Create From Quotation #".$quotation_data->quotation_id." ]";}?>
              </h3>
            </div>
          </div>
          <div class="m-portlet__head-tools" >

          </div>
        </div>
        <div class="m-portlet__body m-portlet__body--no-padding">
          <div class="m-wizard m-wizard--1 m-wizard--success" id="m_wizard">
            <div class="m-wizard__head m-portlet__padding-x">

              <!--begin: Form Wizard Progress -->
              <div class="m-wizard__progress">
                <div class="progress">
                  <div class="progress-bar" style="width:50%" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100"></div>
                </div>
              </div>

              <!--end: Form Wizard Progress -->

              <!--begin: Form Wizard Nav -->
              <div class="m-wizard__nav">
                <div class="m-wizard__steps">
                  <div class="m-wizard__step m-wizard__step--current" m-wizard-target="m_wizard_form_step_1">
                    <div class="m-wizard__step-info">
                      <a href="#" class="m-wizard__step-number">
                        <span><span>1</span></span>
                      </a>
                      <div class="m-wizard__step-line">
                        <span></span>
                      </div>
                      <div class="m-wizard__step-label">
                        Customer & Work Order Information
                      </div>
                    </div>
                  </div>
                  <div class="m-wizard__step" m-wizard-target="m_wizard_form_step_2">
                    <div class="m-wizard__step-info">
                      <a href="#" class="m-wizard__step-number">
                        <span><span>2</span></span>
                      </a>
                      <div class="m-wizard__step-line">
                        <span></span>
                      </div>
                      <div class="m-wizard__step-label">
                        Items & Order Value
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="m-invoice-2" style="padding-bottom: 20px">
            <div  class="m-invoice__wrapper">
              <div class="m-invoice__head" >
                <div class="m-invoice__container m-invoice__container--centered">
                  <div class="m-invoice__logo" style="padding-top: 3rem;">
                    <a href="#">
                    </a>
                  </div>
                </div>
              </div>
              <div class="m-invoice__body m-invoice__body--centered" style="min-height: 200px">
                <?php if(!empty($quotation_data)){
                  if($this->config->item('Quotation_sales_quotation_no_manual')==1){
                    $quotation_no =  $quotation_data->quotation_no_suffix;
                  }else{
                    if($quotation_data->quotation_prefix!=''){
                      $quotation_data->quotation_prefix = $quotation_data->quotation_prefix."-";
                    }
                    if($quotation_data->quotation_no_prefix!=''){
                      $quotation_data->quotation_prefix = $quotation_data->quotation_no_prefix;
                    }
                    $quotation_data->quotation_no = sprintf("%04d", $quotation_data->quotation_no);
                    $quotation_no = $quotation_data->quotation_prefix.$quotation_data->quotation_no.$quotation_data->quotation_no_suffix;
                  }
                  ?>
                  <div class="row">
                    <input type="hidden" name="customer" value="<?php echo $quotation_data->customer_id?>">
                    <input type="hidden" name="quotation_id" value="<?php echo $quotation_data->quotation_id?>">
                    <div class="col-lg-3">
                      <div class="form-group m-form__group">
                        <label>Quotation No</label>
                        <input disabled type="text" value="<?php echo $quotation_no?>" class="form-control m-input m-input--air m-input--pill">
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-3">
                      <div class="form-group m-form__group">
                        <label>Quotation Date</label>
                        <input disabled type="text" value="<?php echo date('d-m-Y',strtotime($quotation_data->date))?>" class="form-control m-input m-input--air m-input--pill">
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                  </div>
                  <div class="row">
                    <div class="col-lg-2">
                      <div class="form-group m-form__group">
                        <label>Customer ID</label>
                        <input disabled type="text" value="<?php echo $quotation_data->customer_id?>" class="form-control m-input m-input--air m-input--pill">
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-6">
                      <div class="form-group m-form__group">
                        <label>Customer/Company Name</label>
                        <input disabled type="text" value="<?php echo $quotation_data->c_customer_name?>" class="form-control m-input m-input--air m-input--pill">
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-4">
                      <div class="form-group m-form__group">
                        <label>Contact Person</label>
                        <input disabled type="text" value="<?php echo $quotation_data->contact_person?>" class="form-control m-input m-input--air m-input--pill">
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-4">
                      <div class="form-group m-form__group">
                        <label>Location</label>
                        <input disabled type="text" value="<?php echo $quotation_data->customer_location?>" class="form-control m-input m-input--air m-input--pill">
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-4">
                      <div class="form-group m-form__group">
                        <label>Phone</label>
                        <input disabled type="text" value="<?php echo $quotation_data->c_customer_phone?>" class="form-control m-input m-input--air m-input--pill">
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                    <div class="col-lg-4">
                      <div class="form-group m-form__group">
                        <label>Email</label>
                        <input disabled type="text" value="<?php echo $quotation_data->c_customer_email?>" class="form-control m-input m-input--air m-input--pill">
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                  </div>
                <?php }else{ ?>
                  <div class="row">
                    <div class="col-lg-12">
                      <div class="form-group m-form__group">
                        <label>
                          Customer
                        </label>
                        <div class="input-group m-input-group  m-input-group--pill  m-input-group--air">
                          <div class="input-group-prepend">
                            <a href="#customer_add_modal" data-toggle="modal" data-target="#customer_add_modal" class="btn btn-focus">
                              <i class="fa fa-plus-circle"></i>
                            </a>
                          </div>
                          <select class="form-control m-select2 m-select2-general" required id="customer_add" name="customer" style="width: 95%">
                          </select>
                        </div>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                  </div>
                <?php } ?>
                <div class="row">
                  <div class="col-lg-3">
                    <div class="form-group m-form__group" style="padding-top: 0px !important;padding-bottom: 0px !important">
                      <label >Work Order Date</label>
                      <input  data-date-format="dd-mm-yyyy" id="work_order_date" required type="text" name="order_date" class="form-control m-input m-input--air m-input--pill" placeholder="Work Order Date" >
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group" style="padding-top: 0px !important;padding-bottom: 0px !important">
                      <label >Work Order Reference No</label>
                      <input  type="text"  name="order_reference_no" value="<?php if(!empty($quotation_data)){ echo $quotation_data->reference_no;}?>" class="form-control m-input m-input--air m-input--pill" placeholder="Work Order Reference No" >
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group" style="padding-top: 0px !important;padding-bottom: 0px !important">
                      <label >Customer Order/PO Ref No</label>
                      <input  type="text"  name="customer_order_ref_no" value="" class="form-control m-input m-input--air m-input--pill" placeholder="Customer Order Ref No" >
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group" style="padding-top: 0px !important;padding-bottom: 0px !important">
                      <label >Customer Order Date</label>
                      <input  data-date-format="dd-mm-yyyy" id="customer_order_date"  type="text" name="customer_order_date" class="form-control m-input m-input--air m-input--pill" placeholder="Customer Order Date" >
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-5">
                    <div class="form-group m-form__group m-select2--pill">
                      <label for="user_name">
                        Prepared/Handled By
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select style="width: 100%" class="form-control m-select2 m-select2-general" name="prepared_by" id="prepared_by">
                          <option selected disabled> Select Prepared By</option>
                          <?php foreach ($sales_staffs as $key => $value) { ?>
                            <option value="<?php echo $value->user_id ?>"><?php echo $value->user_alias ?></option>
                          <?php } ?>
                        </select>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-7">
                    <div class="form-group m-form__group" style="padding-top: 0px !important;padding-bottom: 0px !important">
                      <label >Project Name</label>
                      <input  type="text"  name="project_name" id="project_name" class="form-control m-input m-input--air m-input--pill" placeholder="Project Name" >
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-12">
                    <div class="form-group m-form__group">
                      <label for="user_name">
                        Work Order Description
                      </label>
                      <textarea  rows="12" class="form-control m-input--air m-input--pill" placeholder="Work Order Description" name="order_description" id="order_description" aria-required="true"></textarea>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div style="background: white;border: 4px solid #f7f7fa;padding: 15px;margin: 5px;padding-bottom: 0px">
                  <div class="m-section" style="margin-bottom: 10px">
                    <h3 class="m-section__heading">Terms & Conditions</h3>
                    <div class="row">
                      <div class="col-lg-12">
                        <table style="width: 100%;max-width: 100%" id="table_terms" class="table m-table table-bordered item_table m-table--head-bg-success">
                          <thead>
                            <tr>
                              <th style="width:5%;text-align:left;color:white">Sl No</th>
                              <th style="width:30%;text-align:left;color:white">Title</th>
                              <th style="width:70%;text-align:left;color:white">Description</th>
                              <th style="width:6%;text-align:left;color:white">&nbsp;</th>
                            </tr>
                          </thead>
                          <tbody id="table_body_terms">
                            <?php
                            $i = 0;
                            if(!empty($quotation_data) && $quotation_data->terms_and_conditions!=''){
                              $terms_and_conditions = unserialize($quotation_data->terms_and_conditions);
                              foreach ($terms_and_conditions as $key => $value) {
                              ?>
                                <tr class="row_<?php echo $i ?>">
                                  <td>
                                    <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                      <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 item_slno" data-sl-no="<?php echo $i + 1 ?>" name="terms[sl_no][<?php echo $i ?>]" value="<?php echo $i + 1 ?>" /></div>
                                    </div>
                                  </td>
                                  <td>
                                    <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                      <textarea rows="2" class="form-control m-input m-input--pill m-input--air" name="terms[title][<?php echo $i ?>]" id="title_<?php echo $i + 1 ?>" /><?php echo $value['title'] ?></textarea>
                                    </div>
                                  </td>
                                  <td>
                                    <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                      <textarea rows="2" class="form-control m-input m-input--pill m-input--air" name="terms[description][<?php echo $i ?>]" id="description_<?php echo $i + 1 ?>" /><?php echo $value['description'] ?></textarea>
                                    </div>
                                  </td>
                                  <td>
                                    <a href="javascript:" data-row-sl-no="<?php echo $i ?>" class="ibtnDel_terms btn btn-danger btn-icon-only">
                                      <i class="fa fa-trash"></i>
                                    </td>
                                  </tr>
                                  <?php $i++;
                                }
                            }else{
                              foreach ($terms_and_condition_templates as $key => $value) {?>
                                <tr class="row_<?php echo $i ?>">
                                  <td>
                                    <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                      <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 item_slno" data-sl-no="<?php echo $i + 1 ?>" name="terms[sl_no][<?php echo $i ?>]" value="<?php echo $i + 1 ?>" /></div>
                                    </div>
                                  </td>
                                  <td>
                                    <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                      <textarea rows="2" class="form-control m-input m-input--pill m-input--air" name="terms[title][<?php echo $i ?>]" id="title_<?php echo $i + 1 ?>" /><?php echo $value->title ?></textarea>
                                    </div>
                                  </td>
                                  <td>
                                    <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                      <textarea rows="2" class="form-control m-input m-input--pill m-input--air" name="terms[description][<?php echo $i ?>]" id="description_<?php echo $i + 1 ?>" /><?php echo $value->description ?></textarea>
                                    </div>
                                  </td>
                                  <td>
                                    <a href="javascript:" data-row-sl-no="<?php echo $i ?>" class="ibtnDel_terms btn btn-danger btn-icon-only">
                                      <i class="fa fa-trash"></i>
                                    </td>
                                  </tr>
                                  <?php $i++;
                                }
                              }?>
                            </tbody>
                            <tfoot>
                              <tr>
                                <td colspan="10" style="text-align: left;border-bottom: 1px solid #e7ecf1;">
                                  <button type="button" class="btn btn-block " id="addrow_terms"><i class="fa fa-plus-square"></i> Add new Terms and Condition</button>
                                </td>
                              </tr>
                            </tfoot>
                          </table>
                          <input type="hidden" value="<?php echo $i ?>" id="current_row_count_terms_and_conditions" name="current_row_count_terms">
                          <input type="hidden" value="<?php echo $i ?>" id="actual_row_count_terms_and_conditions" name="actual_row_count_terms">
                        </div>
                      </div>
                    </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-3 ml-lg-auto" style="border-right: 1px solid #ebedf2;">
                  <button type="submit" class="btn btn-primary form_submit_btn_3">
                    Save & Proceed to Add Items
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>
</div>
<?php $this->view("modules/include_modals/modal_customers");?>

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

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

<!-- end:: Page -->

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

//    Toggle Sidebar
$("#m_aside_left_minimize_toggle").addClass("m-brand__toggler--active");
$("#body").addClass("m-brand--minimize m-aside-left--minimize");

$("#work_order_menu").addClass('m-menu__item--active m-menu__item--open');
$("#work_order_menu_1").addClass('m-menu__item--active');

$("#work_order_date,#customer_order_date").datepicker( {
  todayHighlight:!0, orientation:"bottom left", templates: {
    leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
  }
})
$("#order_description").summernote({
  height: 300,
  width: '100%'
});

$("select[name=prepared_by]").select2({
  placeholder: "Select Prepared By"
})
$("#customer_add").select2({
  placeholder: "Select Customer"
})
select_customers('customer_add');
<?php if(isset($quotation_data) && !empty($quotation_data)){ ?>
  $("#project_name").val("<?php echo $quotation_data->project_name ?>");
  $("#prepared_by").val("<?php echo $quotation_data->prepared_by ?>").trigger('change');
  $('#order_description').summernote('code', '<?php echo str_replace(array("\r", "\n"), "'+'", str_replace("'","",$quotation_data->quotation_description))?>');
  <?php } ?>

  function calculate_total_amount() {
    var total_amount = 0;
    $(".item_amount_field").each(function(){
      if($(this).val()!='' && $(this).val()!=null){
        total_amount += parseFloat($(this).val());
      }
    });
    var discount_amount = parseFloat($("#discount_amount").val());
    if(isNaN(discount_amount)){
      discount_amount=0;
    }
    $("#grand_total_amount").val((total_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
    $("#total_amount").val((total_amount-discount_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));

  }
  $('body').on('change keyup', '#discount_amount', function() {
    calculate_total_amount();
  })
  $('body').on('change keyup', '.item_rate_fields', function() {
    var sl_no = $(this).attr('data-sl-no');
    calculate_item_total(sl_no);
  })
  function calculate_item_total(sl_no){
    var qty = parseFloat($("#item_qty_"+sl_no).val());
    var price = parseFloat($("#item_price_"+sl_no).val());
    var total_amount = 0;
    if(isNaN(qty)){
      qty=0;
    }
    if(isNaN(price)){
      price=0;
    }
    total_amount = (qty*price).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>);
    $("#item_total_"+sl_no).val(total_amount);
    calculate_total_amount();
  }
  $('body').on('change keyup', '.schedule_percent', function() {
    var sl_no = $(this).attr('data-sl-no');
    var percent = parseFloat($(this).val());
    var total_amount = parseFloat($("#total_amount").val());
    var percent_amount = total_amount*percent/100;
    $("#schedule_amount_"+sl_no).val(percent_amount.toFixed());
  })
</script>
<script>

$("#form_template_select").validate( {
  rules: {
    template_id: {
      required: !0
    }
  }
  , invalidHandler:function(e, r) {
    var i=$("#form_template_select_msg");
    i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
  }
  , submitHandler:function(e) {
    this.submit();
  }
});
$(".form_submit_btn_3").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: {
      "quantity": {
        required: !0,
        number: !0
      },
      "amount": {
        required: !0,
        number: !0
      }
    }, invalidHandler: function (e, r) {
      var i = $("#" + form_id + "_msg");
      i.removeClass("m--hide").show(), mUtil.scrollTo(i, -200)
    }, submitHandler: function (e) {
    }
  }),
  $(".numeric_fields").each(function () {
    $("#"+$(this).attr("id")).rules('add', {
      number: !0
    });
  }),
  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);
        // toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
        // $("#"+modal_id).modal('toggle');
        // $("."+form_id+"_fields").val("");
        // location.reload();
        <?php if($step==1){ ?>
          swal({
            width: 500,
            title: obj.flashdata_title,
            text: obj.flashdata_msg,
            type: obj.flashdata_type,
            showCancelButton: 0,
            confirmButtonText: "OK",
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            reverseButtons: !0,
            closeOnClickOutside: false,
            allowOutsideClick: false,
          }).then(function(e) {
            window.location.replace("<?php echo base_url() ?>work_order/add/2/" + obj.order_id)
          })
          <?php }else{ ?>
            swal({
              width: 500,
              title: obj.flashdata_title,
              text: obj.flashdata_msg,
              type: obj.flashdata_type,
              showCancelButton: !0,
              confirmButtonText: "Go to Work Order Details",
              cancelButtonText: "Add New Work Order",
              confirmButtonColor: '#3085d6',
              cancelButtonColor: '#d33',
              reverseButtons: !0,
              closeOnClickOutside: false,
              allowOutsideClick: false,
            }).then(function(e) {
              e.value ? window.location.replace("<?php echo base_url()?>work_order/profile/view/"+obj.order_id) : "cancel" === e.dismiss && window.location.replace("<?php echo base_url()?>work_order/add");
            })
            <?php } ?>
          } 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>
$("#btn_add_row").on("click", function () {
  var counter = parseInt($("#current_row_count").val());
  var newRow = '<tr class="row_'+(counter)+'"><td>'+(counter+1)+'</td>';

  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_code['+counter+']" required class="form-control m-input m-input--air m-input--pill" id="item_code_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Item Code" ></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_size['+counter+']" required class="form-control m-input m-input--air m-input--pill" id="item_size_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Size" ></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_type['+counter+']" required class="form-control m-input m-input--air m-input--pill" id="item_type_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Item Type" ></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_qty['+counter+']" required class="form-control item_rate_fields m-input m-input--air m-input--pill" id="item_qty_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Qty" ></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" name="item_price['+counter+']" required id="item_price_'+(counter+1)+'" class="form-control item_rate_fields m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Rate" ></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" name="item_total['+counter+']" required readonly id="item_total_'+(counter+1)+'" class="form-control item_amount_field m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Total Amount" ></div></div></td>';

  newRow += '<td><a href="javascript:" data-row-sl-no='+(counter)+' class="ibtnDel btn btn-danger m-btn m-btn--icon m-btn--icon-only m-btn--pill m-btn--air" ><i class="fa fa-trash"></i></td>';
  newRow += '</tr>';
  $("#item_body").append(newRow);
  counter++;
  $("#current_row_count").val(counter);
  calculate_total_amount();
});
$("#item_body").on("click", ".ibtnDel", function (event) {
  var row_sl_no = $(this).data('row-sl-no');
  var counter = parseInt($("#current_row_count").val());
  $('.row_'+row_sl_no).remove();
  counter -= 1;
  $("#current_row_count").val(counter);
  calculate_total_amount();
});
</script>
<script>
//  Terms & Conditions
$("#addrow_terms").on("click", function() {
  var i = parseInt($('#current_row_count_terms_and_conditions').val());
  var new_i = i + 1;
  console.log(new_i);

  var new_row = '<tr class="row_' + i + '">';
  new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><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 item_slno" data-sl-no="' + new_i + '" name="terms[sl_no][' + i + ']" value="' + new_i + '"/></div></div></td>';
  new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><textarea rows="2" class="form-control m-input m-input--pill m-input--air" name="terms[title][' + i + ']" id="title_' + new_i + '"/></textarea></div></td>';
  new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><textarea rows="2" class="form-control m-input m-input--pill m-input--air" name="terms[description][' + i + ']" id="description_' + new_i + '"/></textarea></div></td>';
  new_row += '<td><a href="javascript:" data-row-sl-no="' + i + '" class="ibtnDel_terms btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td></tr>';

  $('#table_body_terms').append(new_row);
  $('#current_row_count_terms_and_conditions').val(new_i);
});
$("#table_body_terms").on("click", ".ibtnDel_terms", function (event) {
  var sl_no = $(this).attr("data-sl-no");

  var counter = parseInt($("#current_row_count_terms_and_conditions").val());
  $(this).closest("tr").remove();
  counter -= 1;
  $("#current_row_count_terms_and_conditions").val(counter);
});

// Payment Schedule
$("#addrow_schedule").on("click", function () {
  var i = parseInt($('#current_row_count_schedule').val());
  var new_i = i + 1;
  console.log(new_i);

  var new_row = '<tr class="row_'+i+'">';
  new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><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 item_slno" data-sl-no="'+new_i+'" name="schedule[sl_no]['+i+']" value="'+new_i+'"/></div></div></td>';
  new_row += '<td>  <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><input type="text" class="form-control m-input m-input--pill m-input--air" name="schedule[particulars]['+i+']" id="schedule_particulars_'+new_i+'"/></div></td>';
  new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><input type="text" value="0" class="form-control m-input m-input--pill m-input--air numeric_fields schedule_percent" max="100" data-sl-no="'+new_i+'"  name="schedule[percent]['+i+']" id="schedule_percent_'+new_i+'"/></div></td>';
  new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><input type="text" value="0" class="form-control m-input m-input--pill m-input--air numeric_fields" data-sl-no="'+new_i+'"  name="schedule[amount]['+i+']" id="schedule_amount_'+new_i+'"/></div></td>';
  new_row += '<td><div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;"><textarea rows="2" class="form-control m-input m-input--pill m-input--air"  name="schedule[remarks]['+i+']" id="remarks_'+new_i+'"/></textarea></div></td>';
  new_row += '<a href="javascript:" data-row-sl-no="'+i+'" class="ibtnDel_schedule btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></td></tr>';

  $('#table_body_schedule').append(new_row);
  $('#current_row_count_schedule').val(new_i);
});
$("#table_body_schedule").on("click", ".ibtnDel_schedule", function (event) {
  var sl_no = $(this).attr("data-sl-no");

  var counter = parseInt($("#current_row_count_schedule").val());
  $(this).closest("tr").remove();
  counter -= 1;
  $("#current_row_count_schedule").val(counter);
});
$(".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: {
      "mobile_number": {
        required: !0,
        number: !0,
        minlength: 8,
        maxlength: 12
      }
    }, 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);
        toastr[obj.flashdata_type](obj.flashdata_msg, obj.flashdata_title);
        $("#"+modal_id).modal('toggle');
        $("."+form_id+"_fields").val('');
        $("."+form_id+"_select").val('').trigger("change");
        select_customers('customer_add');
        select_customers('customer_edit');
      } 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>
