<?php $this->view("includes/Inc_pagehead");?>
<!-- Page Styles Goes Here-->
<style>
.m-form .m-form__group {
  padding-top: 0px;
  padding-bottom: 0px;
}
</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">
          Add New Subcontract Bill / Advance Payment
        </h3>
        <ul class="m-subheader__breadcrumbs m-nav m-nav--inline">
          <li class="m-nav__item m-nav__item--home">
            <a href="#" class="m-nav__link m-nav__link--icon">
              <i class="m-nav__link-icon la la-home"></i>
            </a>
          </li>
          <li class="m-nav__separator">
            -
          </li>
          <li class="m-nav__item">
            <a href="<?php echo $this->session->userdata("dashboard_link")?>" 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().'subcontracts' ?>" class="m-nav__link">
              <span class="m-nav__link-text">
                Subcontract Bill
              </span>
            </a>
          </li>
        </ul>
      </div>
      <div>
      </div>
    </div>
  </div>
  <!-- END: Subheader -->
  <div class="m-content">
    <!--begin::Portlet-->
    <div class="row">
      <?php if($this->config->item('Subcontract_bill_template')!=2){ ?>
        <div class="col-lg-1"></div>
        <div class="col-lg-10">
        <?php }else{?>
          <div class="col-lg-12">
          <?php } ?>
          <div class="m-portlet">
            <div class="m-portlet__head">
              <div class="m-portlet__head-caption">
                <div class="m-portlet__head-title">
                  <h3 class="m-portlet__head-text">
                    Add Subcontract Bill / Advance Payment
                  </h3>
                </div>
              </div>
            </div>
            <!--begin::Form-->
            <form autocomplete="off" data-form-type="subcontract" id="form_subcontract_bill_add" data-modal-id="subcontract_bill_add_modal" method="post" action="<?php echo base_url().'bills/create'?>" enctype="multipart/form-data" class="m-form m-form--fit m-form--state m-form--label-align-right " >
              <div class="m-portlet__body" >
                <div class="m-form__content">
                  <div class="m-alert m-alert--icon alert alert-warning m--hide" role="alert" id="form_subcontract_bill_add_msg">
                    <div class="m-alert__icon">
                      <i class="la la-warning"></i>
                    </div>
                    <div class="m-alert__text">
                      Oh snap! Change a few things up and try submitting again.
                    </div>
                    <div class="m-alert__close">
                      <button type="button" class="close" data-close="alert" aria-label="Close"></button>
                    </div>
                  </div>
                  <div class="row" style="padding-bottom: 15px">
                    <div class="col-lg-2" >
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Contract ID
                        </label>
                        <input readonly value="<?php echo $subcontract_data->contract_id ?>" type="text" id="contract_id_bill_add" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields"  aria-describedby="basic-addon1">
                      </div>
                    </div>
                    <div class="col-lg-10" >
                      <div class="row">
                        <div class="col-lg-4" >
                          <div class="form-group m-form__group" >
                            <label for="user_name">
                              Project
                            </label>
                            <input readonly value="<?php echo $subcontract_data->work_name ?>" type="text" id="subcontract_bill_project_add" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields"  aria-describedby="basic-addon1">
                          </div>
                        </div>
                        <div class="col-lg-4" >
                          <div class="form-group m-form__group" >
                            <label for="user_name">
                              Sub Work Name
                            </label>
                            <input readonly value="<?php echo $subcontract_data->sub_work_name ?>" type="text" id="subwork_name_bill_add" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields"  aria-describedby="basic-addon1">
                          </div>
                        </div>
                        <?php
                        if (is_numeric($subcontract_data->contractor)) {
                          $contractor = $subcontract_data->contractor_name;
                        } else {
                          $contractor = $subcontract_data->contractor;
                        } ?>
                        <div class="col-lg-4" >
                          <div class="form-group m-form__group" >
                            <label for="user_name">
                              Contractor
                            </label>
                            <input readonly value="<?php echo $contractor ?>" type="text" id="subwork_name_bill_add" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields"  aria-describedby="basic-addon1">
                          </div>
                        </div>
                        <!-- <div class="col-lg-4" >
                        <div class="form-group m-form__group" >
                        <label for="user_name">
                        Sub Work Type
                      </label>
                      <?php
                      // $work_types = "<ul style='padding-left: 25px;margin-bottom: 0'>";
                      // $work_types_array = explode(",", $subcontract_data->work_type_names);
                      // foreach ($work_types_array as $key => $value) {
                      //   $work_types .= "<li>" . $value . "</li>";
                      // }
                      // $work_types .= "</ul>"; ?>
                      <div id="subwork_type_bill_add" style="border: 1px dashed #ebedf2 !important;"><?php // echo $work_types ?></div>
                    </div>
                  </div> -->
                </div>
              </div>
            </div>
            <div style="background: white;border: 4px solid #f7f7fa;padding: 0px;margin: 0px;padding-bottom: 0px">
              <div class="m-section" style="margin: 0px 0 0px 0;">
                <div class="m-form__group form-group row">
                  <label class="col-3 col-form-label" style="padding-top: 15px">Data Type : </label>
                  <div class="col-9">
                    <div class="m-radio-inline" style="padding-top: 10px">
                      <label class="m-radio m-radio--solid m-radio--state-brand">
                        <input required type="radio" checked name="subcontract_data_type" value="bill"> Subcontract Bill
                        <span></span>
                      </label>
                      <label class="m-radio m-radio--solid m-radio--state-success">
                        <input type="radio"  name="subcontract_data_type" value="advance_payment"> Advance Payment
                        <span></span>
                      </label>
                    </div>
                    <span class="m-form__help"></span>
                  </div>
                </div>
              </div>
            </div>
            <br/>
            <?php
            $bill_gross_amount_readonly = '';
            if($this->config->item('Subcontract_bill_type')=='work_items_based'){
              $bill_gross_amount_readonly = 'readonly';
              ?>
              <div class="subcontract_bill_fields_div" style="background: white;border: 4px solid #f7f7fa;padding: 0px;margin: 0px;padding-bottom: 0px">
                <div class="m-section" style="margin: 0px 0 10px 0;">
                  <h3 class="m-section__heading" style="padding: 10px;margin: 0px">Work Details</h3>
                  <div class="col-lg-12">
                    <table class="table table-striped m-table m-table--head-bg-brand">
                      <thead>
                        <tr>
                          <th>Select</th>
                          <th>Work Type</th>
                          <th>Unit Rate</th>
                          <th>Acheived Qty</th>
                          <th>Total Amount</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        $item_sl_no = 1;
                        $unit_rate_readonly='';
                        if($subcontract_data->contract_type == 'unit_rate') {
                          $unit_rate_readonly='readonly';
                        }

                        foreach ($work_items->result() as $key => $value) { ?>
                          <tr>
                            <td><div class="m-form__group form-group" style="margin-bottom: 2rem;margin-top: 1rem;">
                              <div class="m-checkbox-list">
                                <label class="m-checkbox m-checkbox--bold m-checkbox--state-success">
                                  <input type="checkbox" data-item-id="<?php echo $value->item_id?>" class="checkbox_display">
                                  <span></span>
                                </label>
                              </div>
                            </div></td>
                            <td><label style="margin-top: 15px;"><strong><?php echo $value->type_name ?></strong><br/><?php echo $value->work_details ?></label></td>
                            <td>
                              <div class="form-group m-form__group input_item_<?php echo $value->item_id?>" style="display:none;margin-bottom: 5px;margin-top: 5px;">
                                <div class="input-group m-input-group m-input-group--pill  m-input-group--air">
                                  <input <?php echo $unit_rate_readonly?> value="<?php echo $value->unit_rate?>" style="display:none;" data-item-id="<?php echo $value->item_id?>" type="text" id="unit_rate_<?php echo $value->item_id?>" name="unit_rate_<?php echo $value->item_id?>" class="numeric_fields bill_item_fields input_item_<?php echo $value->item_id?> form-control m-input m-input--air m-input--pill"   placeholder="Unit Rate">
                                  <div class="input-group-append"><span class="input-group-text" id="basic-addon1">/<?php echo $value->unit_name?></span></div>
                                </div>
                              </div>
                            </td>
                            <td>
                              <div class="form-group m-form__group input_item_<?php echo $value->item_id?>" style="display:none;margin-bottom: 5px;margin-top: 5px;">
                                <div class="input-group m-input-group m-input-group--pill  m-input-group--air">
                                  <input  style="display:none;" data-item-id="<?php echo $value->item_id?>" type="text" id="qty_<?php echo $value->item_id?>" name="qty_<?php echo $value->item_id?>" class="numeric_fields input_item_<?php echo $value->item_id?> bill_item_fields form-control m-input m-input--air m-input--pill"   placeholder="Qty">
                                  <div class="input-group-append"><span class="input-group-text" id="basic-addon1"><?php echo $value->unit_name?></span></div>
                                </div>
                              </div>
                            </td>
                            <td>
                              <div class="form-group m-form__group input_item_<?php echo $value->item_id?>" style="display:none;margin-bottom: 5px;margin-top: 5px;">
                                <div class="input-group m-input-group m-input-group--pill  m-input-group--air">
                                  <input readonly style="display:none;" type="text" id="item_total_<?php echo $value->item_id?>" name="item_total_<?php echo $value->item_id?>" class="bill_item_total_fields numeric_fields input_item_<?php echo $value->item_id?> form-control m-input m-input--air m-input--pill"   placeholder="Total">
                                </div>
                              </div>
                            </td>
                          </tr>
                          <?php
                          $item_sl_no++;
                        } ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
              <br/>
            <?php } ?>
            <?php
            if($this->config->item('Subcontract_labours')==1){
              ?>
              <div class="subcontract_bill_fields_div" style="background: white;border: 4px solid #f7f7fa;padding: 0px;margin: 0px;padding-bottom: 0px">
                <div class="m-section" style="margin: 0px 0 10px 0;">
                  <h3 class="m-section__heading" style="padding: 10px;margin: 0px">Labour Details</h3>
                  <div class="col-lg-12">
                    <table class="table table-striped m-table m-table--head-bg-brand">
                      <thead>
                        <tr>
                          <th>Select</th>
                          <th>Labour Name</th>
                          <th>No. of Days</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php
                        $item_sl_no = 1;
                        foreach ($subcontract_labours as $key => $value) { ?>
                          <tr>
                            <td><div class="m-form__group form-group" style="margin-bottom: 2rem;margin-top: 1rem;">
                              <div class="m-checkbox-list">
                                <label class="m-checkbox m-checkbox--bold m-checkbox--state-success">
                                  <input type="checkbox" value="<?php echo $value->labour_id?>" name="labours[]" data-item-id="<?php echo $value->labour_id?>" class="checkbox_display">
                                  <span></span>
                                </label>
                              </div>
                            </div></td>
                            <td><label style="margin-top: 15px;"><strong><?php echo $value->labour_name." [".$value->type_name."]" ?></strong></label></td>
                            <td>
                              <div class="form-group m-form__group input_item_<?php echo $value->labour_id?>" style="display:none;margin-bottom: 5px;margin-top: 5px;">
                                <div class="input-group m-input-group m-input-group--pill  m-input-group--air">
                                  <input value="1" style="display:none;" data-item-id="<?php echo $value->labour_id?>" type="text" id="days_<?php echo $value->labour_id?>" name="days_<?php echo $value->labour_id?>" class="numeric_fields bill_item_fields input_item_<?php echo $value->labour_id?> form-control m-input m-input--air m-input--pill"   placeholder="No of Days">
                                </div>
                              </div>
                            </td>
                          </tr>
                          <?php
                          $item_sl_no++;
                        } ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
              <br/>
            <?php } ?>
            <?php if($this->config->item('Subcontract_bill_template')==2){ ?>
              <div class="subcontract_bill_fields_div" style="background: white;border: 4px solid #f7f7fa;padding: 0px;margin: 0px;padding-bottom: 0px">
                <div class="m-section" style="margin: 0px 0 10px 0;">
                  <h3 class="m-section__heading" style="padding: 10px;margin: 0px">Bill Items</h3>
                  <div class="row" >
                    <div class="col-lg-6" >
                      <div class="form-group m-form__group" >
                        <label for="user_name">
                          Bill Type
                        </label>
                        <div class="m-select2 m-select2--air m-select2--pill ">
                          <select required id="subcontract_bill_type_add" name="subcontract_bill_type" style="width: 100%" class="form-control m-input form_subcontract_bill_add_select form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" aria-describedby="basic-addon1" data-placeholder="Select Type">
                            <option selected disabled>Select Type</option>
                            <?php foreach ($bill_types as $key => $value) {?>
                              <option value="<?= $value->type_id?>"><?= $value->type_name?></option>
                            <?php } ?>
                          </select>
                        </div>
                        <span class="m-form__help"></span>
                      </div>
                    </div>
                  </div>
                  <div class="row" style="padding-left: 10px;padding-right: 10px;" >
                    <div class="col-lg-12">
                      <table style="width: 100%;max-width: 100%"  id="table_subcontract_bill_items" class="table m-table m-table--head-bg-success order-list table-striped table-bordered table-advance table-hover">
                        <thead>
                          <tr>
                            <th style="width:5%">Sl No</th>
                            <th style="width:30%" id="table_subcontract_bill_item_head_2">Description of Work</th>
                            <th style="width:12%" id="table_subcontract_bill_item_head_3">3</th>
                            <th style="width:10%" id="table_subcontract_bill_item_head_4">Unit</th>
                            <th style="width:12%">Rate</th>
                            <th style="width:15%">Amount</th>
                            <th style="width:20%">Remarks</th>
                            <th style="width:6%">&nbsp;</th>
                          </tr>
                        </thead>
                        <tbody id="table_body_subcontract_bill_items">

                        </tbody>
                        <tfoot>
                          <tr>
                            <td colspan="8"  style="text-align: left;border-bottom: 1px solid #e7ecf1;">
                              <button type="button"  class="btn btn-block " id="btn_add_subcontract_bill_items"><i class="fa fa-plus-square"></i> Add Item</button>
                            </td>
                          </tr>
                        </tfoot>
                      </table>
                      <input type="hidden" value="0" id="current_row_count_subcontract_bill_items" name="bill_items_count" >
                    </div>
                  </div>
                </div>
              </div>
            <?php } ?>
            <div class="subcontract_bill_fields_div" style="background: white;border: 4px solid #f7f7fa;padding: 0px;margin: 0px;padding-bottom: 0px">
              <div class="m-section" style="margin: 0px 0 10px 0;">
                <h3 class="m-section__heading" style="padding: 10px;margin: 0px">Bill Details</h3>
                <div class="row" >
                  <div class="col-lg-4" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Bill No
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input type="text" name="bill_no" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="Bill No" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-tag"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-3">
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Bill Date
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input required  data-date-format="dd-mm-yyyy" id="subcontract_bill_date_add"  type="text" name="bill_date" class="form_subcontract_bill_add_fields form-control m-input m-input--pill m-input--air subcontract_bill_add_required_fields " placeholder="Bill Date" >
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-calendar"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-5" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Bill Gross Amount
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input <?php echo $bill_gross_amount_readonly ?> required type="text" name="bill_gross_amount"  id="subcontract_bill_gross_amount_add" value="0" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" placeholder="Bill Gross Amount" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-5" >
                    <div class="row">
                      <div class="col-lg-6" >
                        <div class="form-group m-form__group" >
                          <label for="user_name">
                            <?php echo $this->config->item('Tax_tax_name')?> %
                          </label>
                          <div class="m-select2 m-select2--air m-select2--pill ">
                            <select required id="subcontract_bill_tax_percent_add" name="bill_tax_percent" style="width: 100%" class="form-control m-input form_subcontract_bill_add_select form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" 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>
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                      <div class="col-lg-6" >
                        <div class="form-group m-form__group" >
                          <label for="user_name">
                            <?php echo $this->config->item('Tax_tax_name')?> Amount
                          </label>
                          <div class="m-input-icon m-input-icon--right">
                            <input readonly type="text" name="bill_tax_amount"  id="subcontract_bill_tax_amount_add" value="0" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" placeholder="Tax Amount" aria-describedby="basic-addon1">
                            <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-3" >
                    <div class="row" >
                      <div class="col-lg-5" style="padding-right: 0px;">
                        <div class="form-group m-form__group" >
                          <label for="user_name">
                            Retention%
                          </label>
                          <div class="m-input-icon m-input-icon--right">
                            <input required value="0" type="text" name="bill_retention_percent"  id="subcontract_bill_retention_percent_add" value="0" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" placeholder="Retention %" aria-describedby="basic-addon1">
                            <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="fa fa-percent"></i></span></span>
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                      <div class="col-lg-7" >
                        <div class="form-group m-form__group" >
                          <label for="user_name">
                            Retention Amt
                          </label>
                          <div class="m-input-icon m-input-icon--right">
                            <input readonly type="text" name="bill_retention_amount"  id="subcontract_bill_retention_amount_add" value="0" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" placeholder="Retention Amount" aria-describedby="basic-addon1">
                            <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                          </div>
                          <span class="m-form__help"></span>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="col-lg-2" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        TDS Amount
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input required type="text" name="bill_tds_amount"  id="subcontract_bill_tds_amount_add" value="0" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" placeholder="TDS Amount" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-2" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Deductions
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input required type="text" name="bill_deduction_amount"  id="subcontract_bill_deduction_amount_add" value="0" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" placeholder="TDS Amount" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-4" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Consignee
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input type="text" name="consignee" id="subcontract_bill_consignee_add" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="Consignee Name" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-tag"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Bill Description
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input type="text" name="bill_description" id="subcontract_bill_description_add" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="Bill Description" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Bill Payable Amount
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input readonly required type="text" name="bill_payable_amount"  id="subcontract_bill_payable_amount_add" value="0" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields form_subcontract_bill_amount_fields_add subcontract_bill_add_required_fields" placeholder="Bill Amount" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-6" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Bill Remarks
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input type="text" name="bill_remarks" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="Bill Remarks" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-6" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Upload Bill Copy
                      </label>
                      <div class="custom-file">
                        <input type="file" class="custom-file-input form_subcontract_bill_add_fields" name="uploaded_file" id="subcontract_upload_bill_add">
                        <label class="custom-file-label" for="subcontract_upload_bill_add">Choose file</label>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-6" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        No. of Labours Worked
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input type="text" name="subcontract_labours_no" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="No. of Labours Worked" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <br/>
            <div style="background: white;border: 4px solid #f7f7fa;padding: 0px;margin: 0px;padding-bottom: 0px">
              <div class="m-section" style="margin: 0px 0 10px 0;">
                <h3 class="m-section__heading" style="padding: 10px;margin: 0px">Transaction Details</h3>
                <div class="row" style="padding-top: 15px" >
                  <div class="col-lg-4" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Paid Amount
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input required type="text" name="paid_amount" id="subcontract_bill_paid_amount_add" value="" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="Paid Amount" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4">
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Paid Date
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input  data-date-format="dd-mm-yyyy" id="subcontract_bill_paid_date_add"  type="text" name="paid_date" class="form_subcontract_bill_add_fields form-control m-input m-input--pill m-input--air subcontract_bill_paid_amount_req_fields " placeholder="Paid Date" >
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-calendar"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4">
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Paid From Acc
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select id="subcontract_bill_debit_acc_add" name="debit_acc" style="width: 100%" class="form-control m-input form_subcontract_bill_add_select subcontract_bill_paid_amount_req_fields"  aria-describedby="basic-addon1">
                          <?php
                          foreach($this->config->item('account_heads') as $row):
                            $selected="";
                            if($row->acc_head_id==1){
                              $selected="selected";
                            }
                            ?>
                            <option <?php echo $selected?> value="<?php echo $row->acc_head_id?>"><?php echo $row->acc_head_name?></option>
                            <?php
                          endforeach;
                          ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div class="row" >
                  <div class="col-lg-4">
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Payment Mode
                      </label>
                      <div class="m-select2 m-select2--air m-select2--pill">
                        <select id="subcontract_bill_payment_mode_add" name="payment_mode" style="width: 100%" class="form-control m-input form_subcontract_bill_add_select subcontract_bill_paid_amount_req_fields"  aria-describedby="basic-addon1">
                          <option selected disabled>Select Payment Mode</option>
                          <?php
                          foreach($this->config->item('payment_modes') as $row):
                            ?>
                            <option value="<?php echo $row?>"><?php echo $row?></option>
                            <?php
                          endforeach;
                          ?>
                        </select>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        TR Reference No
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input type="text" name="reference_no"  class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="Reference No" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4">
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        TR Reference Date
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input type="text" data-date-format="dd-mm-yyyy" name="reference_date" id="subcontract_bill_reference_date_add"  class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="Reference Date" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-tag"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-lg-8" >
                    <div class="form-group m-form__group" >
                      <label for="user_name">
                        Transaction Remarks
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input type="text" name="tr_remarks" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields" placeholder="Transaction Remarks" aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><i class="la la-edit"></i></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                  <div class="col-lg-4" >
                    <div class="form-group m-form__group subcontract_bill_fields_div" >
                      <label for="user_name">
                        Bill Balance Amt
                      </label>
                      <div class="m-input-icon m-input-icon--right">
                        <input readonly type="text" name="balance_amount" id="subcontract_bill_balance_amount_add" value="0" class="form-control m-input m-input--pill m-input--air form_subcontract_bill_add_fields"  aria-describedby="basic-addon1">
                        <span class="m-input-icon__icon m-input-icon__icon--right"><span><?php echo $this->config->item('Currency_currency_format'); ?></span></span>
                      </div>
                      <span class="m-form__help"></span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <br/>
            <input type="hidden" name="bill_type" value="subcontract" >
            <input type="hidden" name="subcontract_id" value="<?php echo $subcontract_data->contract_id?>" id="subcontract_id_bill_add">
            <input type="hidden" name="redirect_url" value="<?php echo base_url(uri_string())?>" >
          </div>

        </div>
        <div class="m-portlet__foot m-portlet__foot--fit">
          <div class="m-form__actions m-form__actions">
            <div class="row">
              <div class="col-lg-9 ml-lg-auto">
                <button type="submit" class="btn btn-brand form_submit_btn">
                  Submit
                </button>
                <button type="reset" class="btn btn-secondary">
                  Cancel
                </button>
              </div>
            </div>
          </div>
        </div>
      </form>
      <!--end::Form-->
    </div>
  </div>
  <div class="col-lg-1"></div>
</div>
</div>
</div>
</div>
<!-- end:: Body -->
<!-- begin::Footer -->

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

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

<script>
$("#subcontract_menu").addClass('m-menu__item--active m-menu__item--open');
$("#subcontract_menu_1").addClass('m-menu__item--active');

</script>
<script>
<?php if($this->config->item('Subcontract_bill_template')==2){ ?>
  var i = parseInt($('#current_row_count_subcontract_bill_items').val());
  var item_no = i + 1;
  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="'+item_no+'" name="bill_items[sl_no]['+i+']" value="'+item_no+'"/></div></div></td>';
  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 required type="text" placeholder="Description" class="form-control m-input table_subcontract_bill_item_field_2 subcontract_bill_add_required_fields" name="bill_items[description]['+i+']" id="bill_item_description_'+item_no+'"/></div></div></td>';
  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 required type="text" placeholder="Qty" class="form-control m-input numeric_fields subcontract_bill_item_add_fields table_subcontract_bill_item_field_3 subcontract_bill_add_required_fields" data-item-no="'+item_no+'" name="bill_items[item_qty]['+i+']" id="bill_item_qty_'+item_no+'"/></div></div></td>';
  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 required type="text" placeholder="Unit" class="form-control m-input table_subcontract_bill_item_field_4 subcontract_bill_add_required_fields" name="bill_items[unit]['+i+']" id="bill_item_unit_'+item_no+'"/></div></div></td>';
  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 required type="text" placeholder="Rate" class="form-control m-input numeric_fields subcontract_bill_item_add_fields subcontract_bill_add_required_fields" data-item-no="'+item_no+'" name="bill_items[unit_price]['+i+']" id="bill_item_unit_price_'+item_no+'"/></div></div></td>';
  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 required type="text" readonly placeholder="Total Amt" class="form-control m-input numeric_fields subcontract_bill_item_add_fields subcontract_bill_item_total_add subcontract_bill_add_required_fields"  data-item-no="'+item_no+'" name="bill_items[item_total]['+i+']" id="bill_item_total_'+item_no+'"/></div></div></td>';
  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 type="text" placeholder="Remarks" class="form-control m-input" name="bill_items[remarks]['+i+']" id="bill_item_remarks_'+item_no+'"/></div></div></td>';
  new_row += '<td></td>';
  new_row += '</tr>';
  $('#table_body_subcontract_bill_items').append(new_row);
  $('#current_row_count_subcontract_bill_items').val(item_no);
  calculate_bill_item_total('add');

  $("#subcontract_bill_gross_amount_add").attr('readonly',true);
  $("#subcontract_bill_type_add").select2({
    placeholder: "Select Type",
    dropdownParent: $("#form_subcontract_bill_add")
  })
  function check_bill_type(){
    var bill_type=$('#subcontract_bill_type_add :selected').val();
    // if(bill_type==2){
    //   $("#table_subcontract_bill_item_head_2").text('Description of Work');
    //   $("#table_subcontract_bill_item_head_3").text('Total Len');
    //   $("#table_subcontract_bill_item_head_4").text('Road Cutting');
    //   $(".table_subcontract_bill_item_field_2").attr('placeholder','Description of Work');
    //   $(".table_subcontract_bill_item_field_3").attr('placeholder','Total Len');
    //   $(".table_subcontract_bill_item_field_4").attr('placeholder','Road Cutting');
    //   $(".table_subcontract_bill_item_field_3").removeClass('numeric_fields');
    //   $(".subcontract_bill_item_total_add").addClass('subcontract_bill_item_add_fields');
    //   $(".subcontract_bill_item_total_add").removeAttr('readonly',true);
    // }else{
      $("#table_subcontract_bill_item_head_2").text('Description of Work');
      $("#table_subcontract_bill_item_head_3").text('Total Qty');
      $("#table_subcontract_bill_item_head_4").text('Unit');
      $(".table_subcontract_bill_item_field_2").attr('placeholder','Description of Work');
      $(".table_subcontract_bill_item_field_3").attr('placeholder','Total Qty');
      $(".table_subcontract_bill_item_field_4").attr('placeholder','Unit');
      $(".table_subcontract_bill_item_field_3").addClass('numeric_fields');
      $(".subcontract_bill_item_total_add").removeClass('subcontract_bill_item_add_fields');
      $(".subcontract_bill_item_total_add").attr('readonly',true);
    // }

  }
  $('body').on('change', '#subcontract_bill_type_add', function() {
    check_bill_type();
    var bill_type=$('#subcontract_bill_type_add :selected').val();
    if(bill_type==1){
      var rows = parseInt($('#current_row_count_subcontract_bill_items').val());
      for(var i=rows+1;i<=4;i++){
        add_bill_row();
      }
      $("#bill_item_description_1").val("SPAM Connections");
      $("#bill_item_description_2").val("Ordinary Connections");
      $("#bill_item_description_3").val("Total Add Length");
      $("#bill_item_description_4").val("Concrete");
    }
  })
  function add_bill_row(){
    var i = parseInt($('#current_row_count_subcontract_bill_items').val());
    var item_no = i + 1;
    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="'+item_no+'" name="bill_items[sl_no]['+i+']" value="'+item_no+'"/></div></div></td>';
    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 required type="text" placeholder="Description" class="form-control m-input table_subcontract_bill_item_field_2 subcontract_bill_add_required_fields" name="bill_items[description]['+i+']" id="bill_item_description_'+item_no+'"/></div></div></td>';
    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 required type="text" placeholder="Qty" class="form-control m-input numeric_fields subcontract_bill_item_add_fields table_subcontract_bill_item_field_3 subcontract_bill_add_required_fields" data-item-no="'+item_no+'" name="bill_items[item_qty]['+i+']" id="bill_item_qty_'+item_no+'"/></div></div></td>';
    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 required type="text" placeholder="Unit" class="form-control m-input table_subcontract_bill_item_field_4 subcontract_bill_add_required_fields" name="bill_items[unit]['+i+']" id="bill_item_unit_'+item_no+'"/></div></div></td>';
    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 required type="text" placeholder="Rate" class="form-control m-input numeric_fields subcontract_bill_item_add_fields subcontract_bill_add_required_fields" data-item-no="'+item_no+'" name="bill_items[unit_price]['+i+']" id="bill_item_unit_price_'+item_no+'"/></div></div></td>';
    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 required type="text" readonly placeholder="Total Amt" class="form-control m-input numeric_fields subcontract_bill_item_add_fields subcontract_bill_item_total_add subcontract_bill_add_required_fields" data-item-no="'+item_no+'" name="bill_items[item_total]['+i+']" id="bill_item_total_'+item_no+'"/></div></div></td>';
    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 type="text" placeholder="Remarks" class="form-control m-input" name="bill_items[remarks]['+i+']" id="bill_item_remarks_'+item_no+'"/></div></div></td>';
    new_row += '<td><a href="javascript:" data-row-sl-no="'+i+'" class="ibtnDel_subcontract_bill_items btn-sm btn btn-danger btn-icon-only" ><i class="fa fa-trash"></i></a></td>';
    new_row += '</tr>';
    $('#table_body_subcontract_bill_items').append(new_row);
    $('#current_row_count_subcontract_bill_items').val(item_no);
    check_bill_type();
    calculate_bill_item_total('add');
  }
  $('body').on('click', '#btn_add_subcontract_bill_items', function() {
   add_bill_row();
  })
  $("#table_body_subcontract_bill_items").on("click", ".ibtnDel_subcontract_bill_items", function (event) {
    var sl_no = $(this).attr("data-sl-no");

    var counter = parseInt($("#current_row_count_subcontract_bill_items").val());
    $(this).closest("tr").remove();
    counter -= 1;
    // $("#current_row_count_subcontract_bill_items").val(counter);
    calculate_bill_item_total();
  });
  $("body").on("change keyup", ".subcontract_bill_item_add_fields,#subcontract_bill_type_add", function (event) {
    var item_no = $(this).data("item-no");
    var bill_type=$('#subcontract_bill_type_add :selected').val();
    // if(bill_type==3 || bill_type==1){
      var qty = parseFloat($("#bill_item_qty_"+item_no).val());
      var unit_price = parseFloat($("#bill_item_unit_price_"+item_no).val());
      var total = qty*unit_price;
      $("#bill_item_total_"+item_no).val(total.toFixed(2));
    // }
    calculate_bill_item_total();
  });
  function calculate_bill_item_total(){
    var grand_total_amount = 0;
    $(".subcontract_bill_item_total_add").each(function(){
      if($(this).val()!=''){
        grand_total_amount += parseFloat($(this).val());
      }
    });
    $("#subcontract_bill_gross_amount_add").val(grand_total_amount);
  }
  <?php } ?>
  $("body").on("change", ".checkbox_display", function (event) {
    var id = $(this).data("item-id");
    if($(this).prop("checked") == true){
      $(".input_item_"+id).show();
      $(".input_item_"+id).attr('required',true).trigger('change');
    }else{
      $(".input_item_"+id).hide();
      $(".input_item_"+id).removeAttr('required',true).trigger('change');
    }
    calculate_item_total(id);
  });
  $("body").on("change keyup", ".bill_item_fields", function (event) {
    var id = $(this).data("item-id");
    calculate_item_total(id);
  });
  function calculate_item_total(id){
    var unit_rate = parseFloat($("#unit_rate_"+id).val());
    var qty = parseFloat($("#qty_"+id).val());
    if(qty!='' && qty!=null){
      if(isNaN(qty)) {
        qty = 0;
      }
      var total = (unit_rate*qty).toFixed(2);
      $("#item_total_"+id).val(total);
    }else{
      $("#item_total_"+id).val('');
    }
    calculate_bill_gross_amount();
  }
  function calculate_bill_gross_amount(){
    var bill_gross_amount = 0;
    $(".bill_item_total_fields").each(function(){
      if($(this).val()!=''){
        bill_gross_amount+=parseFloat($(this).val());
      }
    });
    $("#subcontract_bill_gross_amount_add").val(bill_gross_amount);
    calculate_bill_total();
  }

  $('input[type=radio][name=subcontract_data_type]').change(function() {
    if (this.value == 'bill') {
      $(".subcontract_bill_fields_div").show();
      $(".subcontract_bill_add_required_fields").attr('required',true);
      $("#subcontract_bill_gross_amount_add").attr('min',0);
      $("#subcontract_bill_paid_amount_add").attr('min',0);
    }else{
      $(".subcontract_bill_fields_div").hide();
      $(".subcontract_bill_add_required_fields").removeAttr('required',true);
      $("#subcontract_bill_gross_amount_add").attr('min',0);
      $("#subcontract_bill_paid_amount_add").attr('min',0);
    }
  });

  $("#subcontract_bill_tax_percent_add").select2({
    placeholder: "Select Tax %",
    dropdownParent: $("#form_subcontract_bill_add")
  })
  $("#subcontract_bill_debit_acc_add").select2({
    placeholder: "Select Debit Account",
    dropdownParent: $("#form_subcontract_bill_add")
  })
  $("#subcontract_bill_payment_mode_add").select2({
    placeholder: "Select Debit Account",
    dropdownParent: $("#form_subcontract_bill_add")
  })
  $("#subcontract_bill_paid_date_add,#subcontract_bill_paid_date_edit,#subcontract_bill_reference_date_add,subcontract_bill_reference_date_edit,#subcontract_bill_date_add").datepicker( {
    todayHighlight:!0, orientation:"bottom left", templates: {
      leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
    },
    endDate: '+0d',
    <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
      startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
      <?php } ?>
    })

    $('body').on('change', '#subcontract_bill_payment_mode_add', function() {
      var payment_mode=$('#subcontract_bill_payment_mode_add :selected').val();
      if(payment_mode=='POST DATED CHEQUE'){
        $('#subcontract_bill_paid_date_add').datepicker('destroy');
        $("#subcontract_bill_paid_date_add").datepicker( {
          todayHighlight:!0, orientation:"bottom left", templates: {
            leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
          },
          <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
            startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
            <?php } ?>
          })
        }else{
          $('#subcontract_bill_paid_date_add').datepicker('destroy');
          $("#subcontract_bill_paid_date_add").datepicker( {
            todayHighlight:!0, orientation:"bottom left", templates: {
              leftArrow: '<i class="la la-angle-left"></i>', rightArrow: '<i class="la la-angle-right"></i>'
            },
            endDate: '+0d',
            <?php if($this->session->userdata('user_role')!='admin' && $this->config->item('User_calendar_date_restriction_enabled')==1){ ?>
              startDate: '<?php echo $this->config->item('User_calendar_date_restriction_start_days');?>d'
              <?php } ?>
            })
          }
        })

        $("#subcontract_bill_debit_acc_edit").select2({
          placeholder: "Select Debit Account",
          dropdownParent: $("#form_subcontract_bill_edit")
        })
        $("#subcontract_bill_payment_mode_edit").select2({
          placeholder: "Select Debit Account",
          dropdownParent: $("#form_subcontract_bill_edit")
        })
        $("#subcontract_bill_tax_percent_edit").select2({
          placeholder: "Select Tax %",
          dropdownParent: $("#form_subcontract_bill_edit")
        })

        function calculate_subcontract_bill_balance(form_type=''){
          if(form_type=='edit'){
            var payable_amount = ($("#subcontract_bill_payable_amount_edit").val()=='') ? 0 : parseFloat($("#subcontract_bill_payable_amount_edit").val());
            var paid_amount = ($("#subcontract_bill_total_paid_edit").val()=='') ? 0 : parseFloat($("#subcontract_bill_total_paid_edit").val());
            $("#subcontract_bill_total_balance_edit").val(payable_amount-paid_amount);
          }else{
            var payable_amount = ($("#subcontract_bill_payable_amount_add").val()=='') ? 0 : parseFloat($("#subcontract_bill_payable_amount_add").val());
            var paid_amount = ($("#subcontract_bill_paid_amount_add").val()=='') ? 0 : parseFloat($("#subcontract_bill_paid_amount_add").val());
            $("#subcontract_bill_balance_amount_add").val(payable_amount-paid_amount);
          }
        }
        $('body').on('change keyup', '#subcontract_bill_paid_amount_add', function() {
          var value = $(this).val();
          if(value=="" || value==null){
            // $(this).val(0)
          }
          if($(this).val()>0){
            $(".subcontract_bill_paid_amount_req_fields").attr('required',true);
          }else{
            $(".subcontract_bill_paid_amount_req_fields").removeAttr('required',true);
          }
          calculate_subcontract_bill_balance('add');
        })
        function calculate_bill_total(){
          var bill_gross_amount = parseFloat($('#subcontract_bill_gross_amount_add').val());
          var tax_percent = parseFloat($('#subcontract_bill_tax_percent_add :selected').data('tax-percent'));
          var bill_tax_amount = parseFloat(bill_gross_amount*tax_percent/100);
          $('#subcontract_bill_tax_amount_add').val(bill_tax_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
          var bill_tds_amount = parseFloat($('#subcontract_bill_tds_amount_add').val());
          var bill_deduction_amount = parseFloat($('#subcontract_bill_deduction_amount_add').val());
          var bill_retention_percent = parseFloat($('#subcontract_bill_retention_percent_add').val());
          var payable_amount = bill_gross_amount + bill_tax_amount;
          var retention_amount = payable_amount*bill_retention_percent/100;
          payable_amount = payable_amount-retention_amount-bill_tds_amount-bill_deduction_amount;
          $('#subcontract_bill_retention_amount_add').val(retention_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
          $('#subcontract_bill_payable_amount_add').val(payable_amount.toFixed(<?php echo $this->config->item('Currency_amount_decimals_2')?>));
          $("#subcontract_bill_paid_amount_add").val($('#subcontract_bill_payable_amount_add').val());
          $("#subcontract_bill_paid_amount_add").attr('max',$('#subcontract_bill_payable_amount_add').val());
          if($("#subcontract_bill_paid_amount_add").val()>0){
            $(".subcontract_bill_paid_amount_req_fields").attr('required',true);
          }else{
            $(".subcontract_bill_paid_amount_req_fields").removeAttr('required',true);
          }
          calculate_subcontract_bill_balance('add');
        }
        $('body').on('change keyup', '.form_subcontract_bill_amount_fields_add', function() {
          var value = $(this).val();
          if(value=="" || value==null){
            $(this).val(0);
          }
          calculate_bill_total();
        })
        $('body').on('change keyup', '#subcontract_bill_date_add', function() {
          $("#subcontract_bill_paid_date_add").datepicker("setDate",$(this).val());
        })
        </script>
        <script>

        jQuery(document).ready(function() {

          $(".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: {
                "paid_amount": {
                  required: !0,
                  // min: 1,
                  number: !0
                },
                "budget": {
                  number: !0,
                  required: !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);
                  swal({
                    width: 500,
                    title: obj.flashdata_title,
                    text: obj.flashdata_msg,
                    type: obj.flashdata_type,
                    showCancelButton: !0,
                    confirmButtonText: "View Subcontract Profile",
                    cancelButtonText: "Add New Bill",
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    reverseButtons: !0,
                    closeOnClickOutside: false,
                    allowOutsideClick: false,
                  }).then(function(e) {
                    e.value ? window.location.replace("<?php echo base_url()?>subcontracts/profile/<?php echo $subcontract_data->contract_id?>") : "cancel" === e.dismiss && window.location.reload();
                  })
                } 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>
