<?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: 90% !important;
}

.m-invoice-2 .m-invoice__wrapper .m-invoice__body.m-invoice__body--centered {
  width: 90% !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">
          Sales Quotation
        </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() . 'sales_quotation' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Sales Quotation
              </span>
            </a>
          </li>
        </ul>
      </div>
      <div></div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <form autocomplete="off" id="form_quotation_add" data-form-type="quotation_create" method="post" action="<?php echo base_url() ?>sales_quotation/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__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-12">
                    <div class="row">
                      <div class="col-lg-6">
                        <div class="form-group m-form__group">
                          <label>Customer Name</label>
                          <input type="text" required name="customer_name" class="form-control m-input m-input--air m-input--pill" placeholder="Customer Name">
                          <!-- <span class="m-form__help"></span> -->
                        </div>
                        <div class="form-group m-form__group" style="padding-top:0px">
                          <label>Customer Phone</label>
                          <input type="text"  name="customer_phone" class="form-control m-input m-input--air m-input--pill" placeholder="Customer Phone No">
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                      <div class="col-lg-6">
                        <div class="form-group m-form__group">
                          <label>Address</label>
                          <textarea required name="address" rows="6" class="form-control m-input m-input--air m-input--pill" placeholder="Address"></textarea>
                          <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>State</label>
                          <input type="text" name="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>Quotation Date</label>
                          <input data-date-format="dd-mm-yyyy" id="quotation_date" required type="text" name="quotation_date" class="form-control m-input m-input--air m-input--pill" placeholder="Quotation Date">
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                      <div class="col-lg-2">
                        <div class="form-group m-form__group">
                          <label>Expiry Date</label>
                          <input data-date-format="dd-mm-yyyy" id="expiry_date" type="text" name="expiry_date" class="form-control m-input m-input--air m-input--pill" placeholder="Expiry Date">
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                      <div class="col-lg-4">
                        <div class="form-group m-form__group m-select2--pill">
                          <label for="user_name">
                            Prepared 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">
                              <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-12">
                        <div class="form-group m-form__group">
                          <label for="user_name">
                            Subject
                          </label>
                          <input type="text" id="subject" required name="subject" class="form-control m-input m-input--air m-input--pill" placeholder="Subject">
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                      <div class="col-lg-12">
                        <div class="form-group m-form__group">
                          <label for="user_name">
                            Quotation Description
                          </label>
                          <textarea required rows="12" class="form-control m-input--air m-input--pill" placeholder="Quotation Description" name="quotation_description" id="quotation_description" aria-required="true"></textarea>
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                      <div class="col-lg-12">
                        <div class="form-group m-form__group ">
                          <label for="user_name">
                            Notes
                          </label>
                          <textarea rows="4" class="form-control m-input--air m-input--pill" placeholder="Quotation Foot Notes" name="notes" id="notes" aria-required="true"><?php if(isset($clone_quotation_data) && !empty($clone_quotation_data)){ echo $clone_quotation_data->quotation_notes;} ?></textarea>
                          <span class="m-form__help"></span>
                        </div>
                      </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">QUOTED PRICE</h3>
                    <div class="">
                      <table class="table m-table table-bordered item_table">
                        <thead>
                          <tr>
                            <th width="5%">Sl No</th>
                            <th width="45%" style="text-align: left">Description</th>
                            <th width="10%" style="text-align: left">Qty</th>
                            <th width="12%" style="text-align: left">Unit</th>
                            <th width="12%" style="text-align: left">Unit Rate</th>
                            <th width="15%" style="text-align: left">Total</th>
                            <th width="7%">&nbsp;</th>
                          </tr>
                        </thead>
                        <tbody id="item_body">

                        </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: 16%;text-align:left">Total Amount</th>
                            <th style="width: 16%;text-align:left">Discount Amount</th>
                            <th style="width: 16%;text-align:left">Taxable Amount</th>
                            <th style="width: 16%;text-align:left"><?php echo $this->config->item('Tax_tax_name')?> %</th>
                            <th style="width: 16%;text-align:left">Tax Amount</th>
                            <th style="width: 16%;text-align: left">
                              NET Total Amount
                            </th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr>
                            <td>
                              <input required type="text" required readonly name="grand_total_amount" 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>
                              <input required type="text" required readonly name="total_taxable_amount" id="total_taxable_amount" class="form-control m-input m-input--air m-input--pill" value="0" placeholder="" >
                            </td>
                            <td><select required id="tax_percent" name="tax_percent" style="width: 100%" class="form-control m-bootstrap-select m-bootstrap-select--air m-bootstrap-select--pill m_selectpicker item_tax_percent">
                              <?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
                              }
                              ?>
                            </select>
                            </td>
                            <td>
                              <input required type="text" required readonly name="total_tax_amount" id="total_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="net_total_amount" id="net_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="0">
                  </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">
                          <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 & 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 -->
<?php //$this->view("modules/include_scripts/bill_scripts");
?>
<script>
//    Toggle Sidebar
$("#m_aside_left_minimize_toggle").addClass("m-brand__toggler--active");
$("#body").addClass("m-brand--minimize m-aside-left--minimize");

$("#sales_quotation_menu").addClass('m-menu__item--active m-menu__item--open');
$("#sales_quotation_menu_1").addClass('m-menu__item--active');


$("#quotation_template").select2({
  placeholder: "Select Quotation Template"
})

$("select[name=prepared_by]").select2({
  placeholder: "Select Prepared By"
})
$("#quotation_description").summernote({
  height: 300,
  width: '100%'
});

$("#quotation_date,#expiry_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 select_units(select_id,selected_value=''){
  $('#'+select_id).html('');
  $("#"+select_id).append("<option disabled selected >Select Unit</option>");
  $.post("<?php echo base_url() ?>units/select",{},function(data) {
    var obj = $.parseJSON(data);
    var i=0;
    if(obj.count>0) {
      while (i < obj.id.length) {
        $('#'+select_id).append("<option value=" + obj.id[i] + ">" + obj.name[i] + "</option>");
        i++;
      }
      if(selected_value!=''){
        $('#'+select_id).val(selected_value).trigger('change');
      }
    }
  })
}
$("#item_description_1").summernote({
  height: 100,
  width: '100%',
  toolbar: [
    // [groupName, [list of button]]
    ['style', ['bold', 'italic','underline']],
    ['fontsize', ['fontsize']],
    ['color', ['color']],
    ['para', ['ul', 'ol', 'paragraph']],
    ['height', ['height']]
  ]
});
$("#item_unit_1").select2({
  placeholder: "Select Unit",
  dropdownParent: $("#form_quotation_add")
})
select_units('item_unit_1');
$(".m_selectpicker").selectpicker();
$('.m_selectpicker').selectpicker('refresh');

<?php if(isset($clone_quotation_data) && !empty($clone_quotation_data)){ ?>
  $("#subject").val("<?php echo $clone_quotation_data->quotation_subject ?>");
  $("#prepared_by").val("<?php echo $clone_quotation_data->prepared_by ?>").trigger('change');
  $("#tax_percent").val("<?php echo $clone_quotation_data->quotation_tax_percent ?>").trigger('change');
  $('#quotation_description').summernote('code', '<?php echo str_replace(array("\r", "\n"), "'+'", str_replace("'","",$clone_quotation_data->quotation_description))?>');
  $("#discount_amount").val("<?php echo $clone_quotation_data->quotation_discount_amount ?>");
  <?php
  $i=0;
  foreach ($clone_items_data as $key => $value) { ?>

    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" required name="item_description['+counter+']" id="item_description_'+(counter+1)+'" class="form-control m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Description" ></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" required name="item_qty['+counter+']" class="form-control item_rate_fields m-input m-input--air m-input--pill numeric_fields" 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-select2 m-select2--air m-select2--pill"><select required style="width: 100%" class="form-control m-select2 m-select2-general" name="item_unit['+counter+']" id="item_unit_'+(counter+1)+'" data-sl-no="'+(counter+1)+'"></select></div></div></td>';
    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" required name="item_unit_price['+counter+']" class="form-control item_rate_fields m-input m-input--air m-input--pill numeric_fields" id="item_unit_price_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Unit Price" ></div></div></td>';
    newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" required name="item_total['+counter+']" 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);

    $("#item_description_"+(counter+1)).summernote({
      height: 100,
      width: '100%',
      toolbar: [
        // [groupName, [list of button]]
        ['style', ['bold', 'italic','underline']],
        ['fontsize', ['fontsize']],
        ['color', ['color']],
        ['para', ['ul', 'ol', 'paragraph']],
        ['height', ['height']]
      ]
    });
    $("#item_unit_"+(counter+1)).select2({
      placeholder: "Select Unit",
      dropdownParent: $("#form_quotation_add")
    })
    select_units('item_unit_'+(counter+1));

    select_units('item_unit_'+(counter+1),'<?php echo $value->unit ?>');
    $("#item_description_"+(counter+1)).summernote('code', '<?php echo str_replace(array("\r", "\n"), "'+'", str_replace("'","",$value->description))?>');
    $("#item_qty_"+(counter+1)).val('<?php echo $value->quantity ?>');
    $("#item_unit_price_"+(counter+1)).val('<?php echo $value->unit_price ?>');
    $("#item_total_"+(counter+1)).val('<?php echo $value->total_amount ?>');

    counter++;
    $("#current_row_count").val(counter);
    calculate_total_amount();

    <?php
    $i++;
  } ?>
<?php }else{ ?>
  // Add Default 1 Row
  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" required name="item_description['+counter+']" id="item_description_'+(counter+1)+'" class="form-control m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Description" ></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" required name="item_qty['+counter+']" class="form-control item_rate_fields m-input m-input--air m-input--pill numeric_fields" 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-select2 m-select2--air m-select2--pill"><select required style="width: 100%" class="form-control m-select2 m-select2-general" name="item_unit['+counter+']" id="item_unit_'+(counter+1)+'" data-sl-no="'+(counter+1)+'"></select></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" required name="item_unit_price['+counter+']" class="form-control item_rate_fields m-input m-input--air m-input--pill numeric_fields" id="item_unit_price_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Unit Price" ></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" required name="item_total['+counter+']" 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);
  $("#item_description_"+(counter+1)).summernote({
    height: 100,
    width: '100%',
    toolbar: [
      // [groupName, [list of button]]
      ['style', ['bold', 'italic','underline']],
      ['fontsize', ['fontsize']],
      ['color', ['color']],
      ['para', ['ul', 'ol', 'paragraph']],
      ['height', ['height']]
    ]
  });
  $("#item_unit_"+(counter+1)).select2({
    placeholder: "Select Unit",
    dropdownParent: $("#form_quotation_add")
  })
  select_units('item_unit_'+(counter+1));
  counter++;
  $("#current_row_count").val(counter);
  calculate_total_amount();
  <?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;
  }
  var taxable_amount = total_amount-discount_amount;
  var tax_percent = parseFloat($("#tax_percent :selected").data('tax-percent'));
  var tax_amount = taxable_amount*tax_percent/100;
  var net_total = taxable_amount+tax_amount;
  $("#grand_total_amount").val((total_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
  $("#total_taxable_amount").val((taxable_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
  $("#total_tax_amount").val((tax_amount).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
  $("#net_total_amount").val((net_total).toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));

}
$('body').on('change keyup', '#discount_amount,#tax_percent', 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_unit_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();
}


</script>
<script>

$(".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) {}
  }), 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 Quotation Details",
          cancelButtonText: "Add New Quotation",
          confirmButtonColor: '#3085d6',
          cancelButtonColor: '#d33',
          reverseButtons: !0,
          closeOnClickOutside: false,
          allowOutsideClick: false,
        }).then(function(e) {
          e.value ? window.location.replace("<?php echo base_url() ?>sales_quotation/profile/view/" + obj.quotation_id) : "cancel" === e.dismiss && window.location.replace("<?php echo base_url() ?>sales_quotation/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" required name="item_description['+counter+']" id="item_description_'+(counter+1)+'" class="form-control m-input m-input--air m-input--pill" data-sl-no="'+(counter+1)+'" placeholder="Description" ></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" required name="item_qty['+counter+']" class="form-control item_rate_fields m-input m-input--air m-input--pill numeric_fields" 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-select2 m-select2--air m-select2--pill"><select required style="width: 100%" class="form-control m-select2 m-select2-general" name="item_unit['+counter+']" id="item_unit_'+(counter+1)+'" data-sl-no="'+(counter+1)+'"></select></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" required name="item_unit_price['+counter+']" class="form-control item_rate_fields m-input m-input--air m-input--pill numeric_fields" id="item_unit_price_'+(counter+1)+'" data-sl-no="'+(counter+1)+'" placeholder="Unit Price" ></div></div></td>';
  newRow += '<td style="text-align: left"><div class="form-group m-form__group"><div class="m-input-icon "><input  type="text" required name="item_total['+counter+']" 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);
  $("#item_description_"+(counter+1)).summernote({
    height: 100,
    width: '100%',
    toolbar: [
      // [groupName, [list of button]]
      ['style', ['bold', 'italic','underline']],
      ['fontsize', ['fontsize']],
      ['color', ['color']],
      ['para', ['ul', 'ol', 'paragraph']],
      ['height', ['height']]
    ]
  });
  $("#item_unit_"+(counter+1)).select2({
    placeholder: "Select Unit",
    dropdownParent: $("#form_quotation_add")
  })
  select_units('item_unit_'+(counter+1));
  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>
$("#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);

});
</script>
