<?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
              </h3>
            </div>
          </div>
          <div class="m-portlet__head-tools" >

          </div>
        </div>
        <div class="m-portlet__body m-portlet__body--no-padding">
          <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">
                <div class="row">
                  <div class="col-lg-4">
                    <div class="form-group m-form__group">
                      <label>
                        Select Quotation
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select width="100%" required  class="form-control m-select2" id="quotation_add" name="quotation_id"  >
                          <option selected disabled> Select Quotation</option>
                          <?php foreach ($sales_quotations as $key => $value) {?>
                            <option value="<?php echo $value->quotation_id?>"><?php echo "#".$value->quotation_prefix."-".$value->quotation_no?></option>
                          <?php } ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4">
                    <div class="form-group m-form__group">
                      <label >Customer Name</label>
                      <input  type="text" required  name="customer_name" id="customer_name" class="form-control m-input m-input--air m-input--pill" placeholder="Customer Name" >
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4">
                    <div class="form-group m-form__group">
                      <label >Address</label>
                      <textarea required  name="address" rows="2" id="address" class="form-control m-input m-input--air m-input--pill" placeholder="Address" ></textarea>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-2">
                    <div class="form-group m-form__group">
                      <label >State</label>
                      <input  type="text" required  name="state" id="state" class="form-control m-input m-input--air m-input--pill" placeholder="State" >
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-2">
                    <div class="form-group m-form__group">
                      <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-2">
                    <div class="form-group m-form__group">
                      <label >Project Start Date</label>
                      <input  data-date-format="dd-mm-yyyy" id="project_start_date" required type="text" name="project_start_date" class="form-control m-input m-input--air m-input--pill" placeholder="Project Start Date" >
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group">
                      <label>
                        Bank Account
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select width="100%"   class="form-control m-select2" id="bank_account_add" name="bank_account"  >
                          <option selected disabled> Select Bank Account</option>
                          <?php foreach ($bank_accounts as $key => $value) {?>
                            <option value="<?php echo $value->account_id?>"><?php echo $value->company_name?> [ <?php echo $value->bank_name."-".$value->bank_branch?> ]</option>
                          <?php } ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group">
                      <label >Site Coordinator</label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select style="width: 100%" class="form-control m-select2 m-select2-general" id="site_coordinator" name="site_coordinator">
                          <option selected disabled> Select Site Coordinator</option>
                          <?php foreach ($supervisors 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>
                      <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">QUOTED PRICE</h3>
                    <div class="table-responsive">
                      <table class="table m-table table-bordered item_table">
                        <thead>
                          <tr>
                            <th width="7%">Sl No</th>
                            <th width="20%" style="text-align: left">Area</th>
                            <th width="15%" style="text-align: left">Approx<br/>Square Feet</th>
                            <th width="15%" style="text-align: left">Amount<br/>Per Square Feet</th>
                            <th width="15%" style="text-align: left">Total Amount<br/>(Excluding GST)</th>
                            <th width="18%" style="text-align: left">Warranty</th>
                            <th width="7%">&nbsp;</th>
                            <!-- <th>Item Type</th> -->
                          </tr>
                        </thead>
                        <tbody id="item_body">
                          <tr class="row_0">
                            <td>1</td>
                            <td style="text-align: left">
                              <div class="form-group m-form__group">
                                <div class="m-input-icon ">
                                  <textarea rows="2" required name="item_area_type[0]" id="item_area_type_1" class="form-control m-input m-input--air m-input--pill" data-sl-no="1" placeholder="Area" ></textarea>
                                </div>
                              </div>
                            </td>
                            <td style="text-align: left">
                              <div class="form-group m-form__group">
                                <div class="m-input-icon ">
                                  <input  type="text" required name="item_area[0]" id="item_area_1" class="form-control item_rate_fields m-input m-input--air m-input--pill numeric_fields" data-sl-no="1" placeholder="Approx SQFT" >
                                </div>
                              </div>
                            </td>
                            <td style="text-align: left">
                              <div class="form-group m-form__group">
                                <div class="m-input-icon ">
                                  <input  type="text" required name="item_price[0]" class="form-control item_rate_fields m-input m-input--air m-input--pill numeric_fields" id="item_price_1" data-sl-no="1" placeholder="Amount Per SQFT" >
                                </div>
                              </div>
                            </td>
                            <td style="text-align: left">
                              <div class="form-group m-form__group">
                                <div class="m-input-icon ">
                                  <input  type="text" required name="item_total[0]" readonly id="item_total_1" class="form-control item_amount_field m-input m-input--air m-input--pill" data-sl-no="1" placeholder="Total Amount" >
                                </div>
                              </div>
                            </td>
                            <td style="text-align: left">
                              <div class="form-group m-form__group">
                                <div class="m-input-icon ">
                                  <textarea rows="2" required name="item_warranty[0]" id="item_warranty_1" class="form-control m-input m-input--air m-input--pill" data-sl-no="1" placeholder="Warranty" ></textarea>
                                </div>
                              </div>
                            </td>
                            <td>
                              <a href="javascript:" data-row-sl-no='0' 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></a>
                            </td>
                          </tr>
                        </tbody>
                      </table>
                      <table class="table m-table table-bordered item_table">
                        <thead>
                          <th>
                            <button type="button" style=" width: 100%; " id="btn_add_row" class="btn m-btn--pill btn-brand"><i class="fa fa-plus-circle"></i> Add New Item</button>
                          </th>
                        </thead>
                      </table>
                      <table class="table m-table table-bordered item_table">
                        <thead>
                          <tr>
                            <th style="width: 20%;text-align:left">Total Amount</th>
                            <th style="width: 20%;text-align:left">Discount Amount</th>
                            <th style="width: 20%;text-align:left"><?php echo $this->config->item('Tax_tax_name')?> %</th>
                            <th style="width: 20%;text-align:left"><?php echo $this->config->item('Tax_tax_name')?> Amount</th>
                            <th style="width: 20%;text-align: left">
                              NET Total Amount
                            </th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr>
                            <td>
                              <input required type="text" required readonly name="grand_total" id="grand_total_amount" class="form-control m-input m-input--air m-input--pill" value="0" placeholder="" >
                            </td>
                            <td>
                              <input required type="text" required name="discount_amount" id="discount_amount" class="form-control m-input m-input--air m-input--pill" value="0" placeholder="" >
                            </td>
                            <td style="text-align: left">
                              <div class="m-select2 m-select2--air m-select2--pill ">
                                <select required id="tax_percent_add" name="tax_percent" style="width: 100%" class="form-control m-input" aria-describedby="basic-addon1" data-placeholder="Select Tax Percentage">
                                  <?php
                                  $tax_percent = $this->config->item('Tax_tax_percent');
                                  foreach($tax_percent as $row):
                                    $selected_tax='';
                                    if($row->tax_percentage==0){
                                      $selected_tax='selected';
                                    }
                                    ?>
                                    <option <?php echo $selected_tax?> data-tax-percent="<?php echo $row->tax_percentage?>" value="<?php echo $row->tax_id?>"><?php echo $row->tax_name?></option>
                                    <?php
                                  endforeach;
                                  ?>
                                </select>
                              </div>
                            </td>
                            <td>
                              <input required type="text" required readonly name="tax_amount" id="tax_amount" class="form-control m-input m-input--air m-input--pill" value="0" placeholder="" >
                            </td>
                            <td>
                              <input required type="text" required readonly name="total_order_amount" id="total_amount" class="form-control m-input m-input--air m-input--pill" value="0" placeholder="" >
                            </td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                    <input type="hidden" name="current_row_count" id="current_row_count" value="1">
                  </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">Payment Schedule</h3>
                    <div class="row">
                      <div class="col-lg-12">
                        <table style="width: 100%;max-width: 100%"  id="table_schedule" class="table m-table table-bordered item_table m-table--head-bg-focus ">
                          <thead>
                            <tr>
                              <th style="width:5%;text-align:left;color:white">Sl No</th>
                              <th style="width:35%;text-align:left;color:white">Particulars</th>
                              <th style="width:10%;text-align:left;color:white">Percent %</th>
                              <th style="width:20%;text-align:left;color:white">Amount</th>
                              <th style="width:35%;text-align:left;color:white">Detailed Remark</th>
                              <th style="width:6%;text-align:left;color:white">&nbsp;</th>
                            </tr>
                          </thead>
                          <tbody id="table_body_schedule">
                            <?php
                            $k=0;
                            ?>
                            <tr class="row_<?php echo $k ?>">
                              <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 $k+1 ?>" name="schedule[sl_no][<?php echo $k ?>]" value="<?php echo $k+1 ?>"/></div></div>
                              </td>
                              <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" value="First Part Payment" name="schedule[particulars][<?php echo $k ?>]" id="schedule_particulars_<?php echo $k+1 ?>"/>
                                </div>
                              </td>
                              <td>
                                <div class="form-group m-form__group" style="padding: 0px !important;margin: 0px !important;">
                                  <input type="text" value="0" max="100" class="form-control m-input m-input--pill m-input--air numeric_fields schedule_percent" data-sl-no="<?php echo $k+1 ?>"  name="schedule[percent][<?php echo $k ?>]" id="schedule_percent_<?php echo $k+1 ?>"/>
                                </div>
                              </td>
                              <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="<?php echo $k+1 ?>"  name="schedule[amount][<?php echo $k ?>]" id="schedule_amount_<?php echo $k+1 ?>"/>
                                </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="schedule[remarks][<?php echo $k ?>]" id="remarks_<?php echo $k+1 ?>"/></textarea>
                                </div>
                              </td>
                              <td>
                                <a href="javascript:" data-row-sl-no="<?php echo $k ?>" class="ibtnDel_schedule btn btn-danger m-btn m-btn--icon m-btn--icon-only m-btn--pill m-btn--air" >
                                  <i class="fa fa-trash"></i>
                                </a>
                              </td>
                            </tr>
                          </tbody>
                          <tfoot>
                            <tr>
                              <td colspan="10"  style="text-align: left;border-bottom: 1px solid #e7ecf1;">
                                <button type="button"  class="btn btn-block " id="addrow_schedule"><i class="fa fa-plus-square"></i> Add New Payment Schedule</button>
                              </td>
                            </tr>
                          </tfoot>
                        </table>
                        <input type="hidden" value="<?php echo $k+1 ?>" id="current_row_count_schedule" name="current_row_count_schedule" >
                        <input type="hidden" value="<?php echo $k+1 ?>" id="actual_row_count_schedule" name="actual_row_count_schedule" >
                      </div>
                    </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: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;
                            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[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_and_conditions" >
                          <input type="hidden" value="<?php echo $i ?>" id="actual_row_count_terms_and_conditions" name="actual_row_count_terms_and_conditions" >
                        </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 & Submit
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>


<!-- 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");?>

<!--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');

$("#bank_account_add").select2({
  placeholder: "Select Bak Account"
})
$("#site_coordinator").select2({
  placeholder: "Select Site Coordinator"
})
$("#quotation_add").select2({
  placeholder: "Select Quotation"
})
$("#tax_percent_add").select2({
  placeholder: "Select Tax %"
})
$('body').on('change', '#quotation_add', function() {
  var quotation_id = $('#quotation_add :selected').val();
  $.post("<?php echo base_url() ?>sales_quotation/select_quotation_details",{quotation_id:quotation_id},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count==1) {
      $("#customer_name").val(obj.data['customer_name']);
      $("#address").val(obj.data['customer_address']);
      $("#state").val(obj.data['state']);
      var items = obj.data['items'];
      $("#item_body").html('');
      $("#current_row_count").val(0);
      for (i=0;i<items.length;i++){
        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 "><textarea rows="2" name="item_area_type['+counter+']" required id="item_area_type_'+counter+'" class="form-control m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Area" ></textarea></div></div></td>';
        newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" value="'+items[i]['quantity']+'" name="item_area['+counter+']" required class="form-control numeric_fields item_rate_fields m-input m-input--air m-input--pill" id="item_area_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Approx SQFT" ></div></div></td>';
        newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon"><input  type="text" value="'+items[i]['unit_price']+'" name="item_price['+counter+']" required id="item_price_'+(counter+1)+'" class="form-control numeric_fields item_rate_fields m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Amount Per SQFT" ></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 style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><textarea rows="2" name="item_warranty['+counter+']" required id="item_warranty_'+counter+'" class="form-control m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Warranty" >'+items[i]['warranty']+'</textarea></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_item_total(counter);
      }
    }
  })
})
<?php if($selected_quotation_id!=''){?>
  $("#quotation_add").val(<?php echo $selected_quotation_id ?>).trigger('change');
  <?php } ?>
  $("#work_order_date,#project_start_date").datepicker( {
    todayHighlight:!0, orientation:"bottom left", templates: {
      leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
    }
  })


  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());
    var tax_percent = parseFloat($('#tax_percent_add :selected').data('tax-percent'));
    if(isNaN(discount_amount)){
      discount_amount=0;
    }
    var taxable_amount = total_amount-discount_amount;
    var tax_amount = taxable_amount*tax_percent/100;
    $("#grand_total_amount").val((total_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
    $("#grand_total_amount").val((total_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
    $("#tax_amount").val((tax_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
    $("#total_amount").val((taxable_amount+tax_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));

  }
  $('body').on('change keyup', '#discount_amount,#tax_percent_add', 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_area_"+sl_no).val());
    var price = parseFloat($("#item_price_"+sl_no).val());
    var 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();
          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");
          })
        } 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 "><textarea rows="2" name="item_area_type['+counter+']" required id="item_area_type_'+counter+'" class="form-control m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Area" ></textarea></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_area['+counter+']" required class="form-control item_rate_fields m-input m-input--air m-input--pill" id="item_area_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Approx SQFT" ></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="Amount Per SQFT" ></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 style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><textarea rows="2" name="item_warranty['+counter+']" required id="item_warranty_'+counter+'" class="form-control m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Warranty" ></textarea></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[description]['+i+']" id="description_'+new_i+'"/></textarea></div></td><td>';
  new_row += '<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);
});

</script>
