<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'lro_edit') ) {
    echo '<script>window.location.href="index.php?page=no_permission.php"</script>';die();
  }
    
  $vendor     = "SELECT * FROM `vendor_master` WHERE `status` = '0'";
  $vendor_res = mysqli_query($conn,$vendor);
   
  $vendors      = "SELECT * FROM `vendor_master` WHERE `status` = '0'";
  $vendor_resu  = mysqli_query($conn,$vendors);

  $bill     = "SELECT * FROM `vendor_master` WHERE `status` = '0'";
  $bill_res = mysqli_query($conn,$bill);

  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid'])){
    $select_update     = "SELECT * FROM lr_opening WHERE lr_no='".$_REQUEST['eid']."' AND `company_id` ='".$_SESSION['company_id']."' AND `branch_id` ='".$_SESSION['branch_id']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
  }
  
  $con     = "SELECT * FROM `vendor_master` WHERE `id` = '".$update_row['consignor_name']."'";
  $con_res = mysqli_query($conn,$con);
  $con_row = mysqli_fetch_array($con_res);

  $cons     = "SELECT * FROM `vendor_master` WHERE `id` = '".$update_row['consignee_name']."'";
  $cons_res = mysqli_query($conn,$cons);
  $cons_row = mysqli_fetch_array($cons_res);

  $bills      = "SELECT * FROM `vendor_master` WHERE `id` = '".$update_row['billing_to']."'";
  $bills_res  = mysqli_query($conn,$bills);
  $bill_rows  = mysqli_fetch_array($bills_res);

?>
<style type="text/css">
  .ui-autocomplete {
    border: 1px solid #730215 !important;
    width: 20% !important;
  }
  .jq-icon-warning{
    background-color: #f39c12 !important;
  }
</style>
<section class="content-header">
  <div class="row">
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 bottom20">
        <h3 class="capital font15 padding5 bold">UPDATE LR</h3>
      </div>
      <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 text-right bottom20">
        <h3>
          <?php if( hasPermission($_SESSION['user_detail']['role_id'], 'lro_list') ) { ?>
          <a href="index.php?page=lr_opeing/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW LR DETAILS</a>
          <?php } ?>
        </h3>
      </div>
  </div>
</section>
<hr class="bottom35"> 
<section class="hitz-top-header">
   <div class="row bottom40">
      <div class="col-xs-12">
         <div class="box box-success">
            <div class="box-body">
              <form action="lr_opeing/insert.php" method="post" enctype="multipart/form-data" class="common-frm-validation" id="signupForm">
                
                <div class="row">
                  <div class="col-md-1">
                    <div class="form-group">
                      <label class="capital">LR.NO</label><span style="color: red">*</span>
                      <!-- <input type="text" size="35" id="lr_no" name="lr_no" class="form-control lr_number" autocomplete="off" placeholder="LR.NO" onChange="CheckLr(this.value);" /> -->
                      <input type="text" size="35" id="lr_no" name="lr_no" class="form-control lr_number required" autocomplete="off" placeholder="LR.NO" value="<?php echo $update_row['lr_no'];?>" onChange="CheckCode(this.value);"/>
                      
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">DATE</label><span style="color: red">*</span>
                      <div class="input-group date">
                        <div class="input-group-addon">
                          <i class="fa fa-calendar"></i>
                        </div>
                        <input type="text" size="35" id="l_date" name="l_date" class="form-control date-picker required" placeholder="DATE" autocomplete="off" value="<?php echo date('d-m-Y', strtotime($update_row['l_date']));?>"/>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-1">
                    <div class="form-group">
                      <label class="capital">INVOICE</label>
                      <input type="text" size="35" id="party_invoice" name="party_invoice" class="form-control" autocomplete="off" placeholder="INVOICE" value="<?php echo $update_row['party_invoice'];?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">PARTY AMOUNT</label>
                      <input type="text" size="35" id="party_amount" name="party_amount" class="form-control" autocomplete="off" placeholder="PARTY AMOUNT" value="<?php echo $update_row['party_amount'];?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">truck no</label><span style="color: red">*</span>
                      <input type="text" size="35" id="truck_no" name="truck_no" class="form-control" autocomplete="off" placeholder="TRUCK NO" onchange="get_Truck_weight(this);CheckUplode(this);" value="<?php echo $update_row['truck_no'];?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">FROM PLACE</label><span style="color: red">*</span>
                      <input type="text" size="35" id="from_place" name="from_place" class="form-control required" autocomplete="off" placeholder="FROM PLACE" value="<?php echo $update_row['from_place'];?>" />
                    </div>
                  </div>
                  <div class="col-md-2">
                    <div class="form-group">
                      <label class="capital">TO PLACE</label><span style="color: red">*</span>
                      <input type="text" size="35" id="to_place" name="to_place" class="form-control required" autocomplete="off" placeholder="TO PLACE" value="<?php echo $update_row['to_place'];?>" />
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-4">
                    <legend class="capital hitz-legend">CONSIGNOR</legend>
                    <div class="col-md-12">
                      <div class="form-group">
                        <label class="capital">name</label>
                        <select class="form-control select2" name="consignor_name" id="consignor_name" onchange="get_consignor_data(this);">
                          <option></option>
                          <?php while ($vendor_row = mysqli_fetch_array($vendor_res)) { ?>
                          <option <?php if($vendor_row['id'] == $update_row['consignor_name']) { echo "selected='selected'"; } ?> value="<?php echo $vendor_row['id'];?>"><?php echo $vendor_row['name'];?></option>
                          <?php } ?>
                         </select>
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <textarea class="form-control hitz-text-height" name="consignor_address" id="consignor_address" readonly=""><?php echo $con_row['address'];?></textarea> 
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignor_city" id="consignor_city" class="form-control get_city" placeholder="CITY" readonly="" value="<?php echo $con_row['city'];?>" />  
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignor_contact" id="consignor_contact" class="form-control" placeholder="CONTACT" readonly="" value="<?php echo $con_row['mobile'];?>" />  
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignor_pan" id="consignor_pan" class="form-control" placeholder="PAN" readonly="" value="<?php echo $con_row['pan'];?>"/>  
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignor_gst" id="consignor_gst" class="form-control" placeholder="GST" readonly="" value="<?php echo $con_row['gstin'];?>"/>  
                      </div>
                    </div>
                  </div>
                  <div class="col-md-4">
                    <legend class="capital hitz-legend">CONSIGNEE</legend>
                    <div class="col-md-12">
                      <div class="form-group">
                          <label class="capital">name</label>
                          <select class="form-control select2" name="consignee_name" id="consignee_name" onchange="get_consignee_data(this);">
                             <option></option>
                             <?php while ($vendor_rows = mysqli_fetch_array($vendor_resu)) { ?>
                             <option <?php if($vendor_rows['id'] == $update_row['consignee_name']) { echo "selected='selected'"; } ?> value="<?php echo $vendor_rows['id'];?>"><?php echo $vendor_rows['name'];?></option>
                             <?php } ?>
                          </select>
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <textarea class="form-control hitz-text-height" name="consignee_address" id="consignee_address" readonly=""><?php echo $cons_row['address'];?></textarea> 
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignee_city" id="consignee_city" class="form-control" placeholder="CITY" readonly="" value="<?php echo $cons_row['city'];?>" />  
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignee_contact" id="consignee_contact" class="form-control" placeholder="CONTACT" readonly="" value="<?php echo $cons_row['mobile'];?>"/>  
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignee_pan" id="consignee_pan" class="form-control" placeholder="PAN" readonly="" value="<?php echo $cons_row['pan'];?>"/>
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignee_gst" id="consignee_gst" class="form-control" placeholder="GST" readonly="" value="<?php echo $cons_row['gstin'];?>"/>  
                      </div>
                    </div>
                  </div>
                  <div class="col-md-4">
                    <legend class="capital hitz-legend">BILLING TO</legend>
                    <div class="col-md-12">
                      <div class="form-group">
                        <label class="capital">name</label>
                        <select class="form-control select2 required" name="billing_to" id="billing_to" onchange="get_bill_data(this);">
                          <option></option>
                          <?php while ($bill_row = mysqli_fetch_array($bill_res)) { ?>
                          <option <?php if($bill_row['id'] == $update_row['billing_to']) { echo "selected='selected'"; } ?> value="<?php echo $bill_row['id'];?>"><?php echo $bill_row['name'];?></option>
                          <?php } ?>
                         </select>
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <textarea class="form-control hitz-text-height" name="consignor_address" id="bill_address" readonly=""><?php echo $bill_rows['address'];?></textarea> 
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignor_city" id="bill_city" class="form-control get_city" placeholder="CITY" readonly="" value="<?php echo $bill_rows['city'];?>" />  
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignor_contact" id="bill_contact" class="form-control" placeholder="CONTACT" readonly="" value="<?php echo $bill_rows['mobile'];?>" />  
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignor_pan" id="bill_pan" class="form-control" placeholder="PAN" readonly="" value="<?php echo $bill_rows['pan'];?>" />  
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <input type="text" name="consignor_gst" id="bill_gst" class="form-control" placeholder="GST" readonly="" value="<?php echo $bill_rows['gstin'];?>" />  
                      </div>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-4">
                    <div class="col-md-12">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-2 control-label capital hitz-center">agent</label> 
                          <div class="col-md-10">
                            <input type="text" size="35" id="agent" name="agent" class="form-control" placeholder="AGENT" autocomplete="off" value="<?php echo $update_row['agent'];?>" />
                          </div>
                        </div>
                      </div>
                    </div><br/><br/><br/>
                    <div class="col-md-12">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-2 control-label capital hitz-center">nature of goods</label> 
                          <div class="col-md-10">
                            <input type="text" size="35" id="nature_of_goods" name="nature_of_goods" class="form-control" placeholder="NATURE OF GOODS" autocomplete="off" value="<?php echo $update_row['nature_of_goods'];?>"/>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-4 control-label capital hitz-center">Billing Party</label> 
                          <div class="col-md-8">
                            <select class="form-control select2" name="billing_party" id="billing_party">
                              <option></option>
                              <option value="CONSIGNOR" <?php if($update_row['billing_party'] == 'CONSIGNOR') { ?> selected="selected"<?php } ?>>CONSIGNOR</option>
                              <option value="CONSIGNEE" <?php if($update_row['billing_party'] == 'CONSIGNEE') { ?> selected="selected"<?php } ?>>CONSIGNEE</option>
                              <option value="TRANSPORTER" <?php if($update_row['billing_party'] == 'TRANSPORTER') { ?> selected="selected"<?php } ?>>TRANSPORTER</option>
                              <option value="OTHER" <?php if($update_row['billing_party'] == 'OTHER') { ?> selected="selected"<?php } ?>>OTHER</option>
                              <option value="TO PAY" <?php if($update_row['billing_party'] == 'TO PAY') { ?> selected="selected"<?php } ?>>TO PAY</option>
                            </select>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-4 control-label capital hitz-center">GST PAID BY</label> 
                          <div class="col-md-8">
                            <select class="form-control select2" name="gst_paid_by" id="gst_paid_by">
                              <option></option>
                              <option <?php if($update_row['gst_paid_by'] == 'CONSIGNOR') { ?> selected="selected"<?php } ?> value="CONSIGNOR">CONSIGNOR</option>
                              <option <?php if($update_row['gst_paid_by'] == 'CONSIGNEE') { ?> selected="selected"<?php } ?> value="CONSIGNEE">CONSIGNEE</option>
                              <option <?php if($update_row['gst_paid_by'] == 'TRANSPORTER') { ?> selected="selected"<?php } ?> value="TRANSPORTER">TRANSPORTER</option>
                              <option <?php if($update_row['gst_paid_by'] == 'OTHER') { ?> selected="selected"<?php } ?> value="OTHER">OTHER</option>
                              <option <?php if($update_row['gst_paid_by'] == 'NOT APPLICABLE') { ?> selected="selected"<?php } ?> value="NOT APPLICABLE">NOT APPLICABLE</option>
                            </select>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-4 control-label capital hitz-center">vehicle in</label> 
                          <div class="col-md-8">
                            <div class="input-group date">
                              <div class="input-group-addon">
                                <i class="fa fa-calendar"></i>
                              </div>
                              <input type="text" size="35" id="vehicle_in" name="vehicle_in" class="form-control date-picker" placeholder="VEHICLE IN" autocomplete="off" value="<?php 
                              if($update_row['vehicle_in'] != ''){
                                echo date('d-m-Y', strtotime($update_row['vehicle_in']));
                              }else{
                                echo '';
                              }
                              ?>" />
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-4 control-label capital hitz-center">vehicle OUt</label> 
                          <div class="col-md-8">
                            <div class="input-group date">
                              <div class="input-group-addon">
                                <i class="fa fa-calendar"></i>
                              </div>
                              <input type="text" size="35" id="vehicle_out" name="vehicle_out" class="form-control date-picker" placeholder="VEHICLE OUT" autocomplete="off" value="<?php 
                              if($update_row['vehicle_out'] != ''){
                                echo date('d-m-Y', strtotime($update_row['vehicle_out']));
                              }else{
                                echo '';
                              }
                              ?>" />

                            </div>
                          </div>
                        </div>
                      </div>
                    </div>

                  </div>
                  <div class="col-md-4">
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-3 control-label capital hitz-center">weight (MT)</label> 
                          <div class="col-md-9">
                            <input type="text" size="35" id="weight" name="weight" class="form-control number" placeholder="WEIGHT(MT)" autocomplete="off" value="<?php echo $update_row['weight'];?>" />
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-4 control-label capital hitz-center">Actual weight(MT)</label> 
                          <div class="col-md-8">
                            <input type="text" size="35" id="actual_weight" name="actual_weight" class="form-control number" placeholder="WEIGHT(MT)" autocomplete="off" value="<?php echo $update_row['actual_weight'];?>"/>
                          </div>
                        </div> 
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-3 control-label capital hitz-center">HEIGHT</label> 
                          <div class="col-md-9">
                            <input type="text" size="35" id="height" name="height" class="form-control number" placeholder="HEIGHT" autocomplete="off" value="<?php echo $update_row['height'];?>"/>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-4 control-label capital hitz-center">extra HEIGHT</label> 
                          <div class="col-md-8">
                            <input type="text" size="35" id="extra_height" name="extra_height" class="form-control number" placeholder="EX.HEIGHT" autocomplete="off" value="<?php echo $update_row['extra_height'];?>"/>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-12">
                      <div class="form-group">
                        <label class="capital">REMARK</label>
                        <textarea class="form-control hitz-text-height" id="remark" name="remark" style="height: 64px !important;"><?php echo $update_row['remark'];?></textarea>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-4">
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-2 control-label capital hitz-center">pcs</label> 
                          <div class="col-md-10">
                            <input type="text" size="35" id="pcs" name="pcs" class="form-control number" placeholder="PCS" autocomplete="off" value="<?php echo $update_row['pcs'];?>" />
                          </div>
                        </div>
                      </div><br/>
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-2 control-label capital hitz-center">rate</label> 
                          <div class="col-md-10">
                            <input type="text" size="35" id="rate" name="rate" class="form-control number" placeholder="RATE" autocomplete="off" value="<?php echo $update_row['rate'];?>"/>
                          </div>
                        </div>
                      </div><br/>
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-2 control-label capital hitz-center">RATE ON</label> 
                          <div class="col-md-10">
                            <select class="form-control select2" name="rate_on" id="rate_on">
                              <option></option>
                              <option <?php if($update_row['rate_on'] == 'WEIGHT') { ?> selected="selected"<?php } ?> value="WEIGHT">WEIGHT</option>
                              <option <?php if($update_row['rate_on'] == 'PCS') { ?> selected="selected"<?php } ?> value="PCS">PCS</option>
                              <option <?php if($update_row['rate_on'] == 'KM') { ?> selected="selected"<?php } ?> value="KM">KM</option>
                              <option <?php if($update_row['rate_on'] == 'NONE') { ?> selected="selected"<?php } ?> value="NONE">NONE</option>
                              <option <?php if($update_row['rate_on'] == 'TO BE BILLED') { ?> selected="selected"<?php } ?> value="TO BE BILLED">TO BE BILLED</option>
                            </select>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-md-6">
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-3 control-label capital hitz-center">freight</label> 
                          <div class="col-md-9">
                            <input type="text" size="35" id="freight" name="freight" class="form-control number" placeholder="FREIGHT" autocomplete="off" onchange="calculateFinalAmount()" value="<?php echo $update_row['freight'];?>"/>
                          </div>
                        </div>
                      </div><br/>
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-3 control-label capital hitz-center">advance</label> 
                          <div class="col-md-9">
                            <input type="text" size="35" id="advance" name="advance" class="form-control number" placeholder="ADVANCE" autocomplete="off" onchange="calculateFinalAmount()" value="<?php echo $update_row['advance'];?>"/>
                          </div>
                        </div>
                      </div><br/>
                      <div class="row">
                        <div class="form-group">
                          <label class="col-md-3 control-label capital hitz-center">total amount</label> 
                          <div class="col-md-9">
                            <input type="text" size="35" id="total_amnt" name="total_amnt" class="form-control number" placeholder="TOTAL AMOUNT" autocomplete="off" readonly="" value="<?php echo $update_row['total_amnt'];?>"/>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
                <input type="hidden" value="LR" name="module" id="module">
                <input type="hidden" value="<?php echo $_GET['eid'];?>" name="id" id="id">
                <hr>
                <div class="row" align="center">
                  <button id="btn" class="btn btn-primary ajax-submit capital" type="submit"> UPDATE & SAVE</button>
                  <a href="index.php?page=lr_opeing/list.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
              
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
  $(document).ready(function(){
    $('#rate_on').select2();
    $('#gst_paid_by').select2();
    $('#billing_party').select2();
    $('#consignor_name').select2();
    $('#consignee_name').select2();
    $('#location_id').select2();
    $('#billing_branch').select2();
    $('#billing_to').select2();
    $( "#from_place" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "lr/get_from_place.php",
          type: 'post',
          dataType: "json",
          data: {
              search: request.term
          },
          success: function( data ) {
              response( data );
          }
        });
      },
      select: function (event, ui) {
        $('#from_place').val(ui.item.label);
        return false;
      }
    });
   
    $( "#method_pack" ).autocomplete({
       source: function( request, response ) {
          $.ajax({
              url: "lr/get_method_pack.php",
              type: 'post',
              dataType: "json",
              data: {
                  search: request.term
              },
              success: function( data ) {
                  response( data );
              }
          });
       },
       select: function (event, ui) {
          $('#method_pack').val(ui.item.label);
          return false;
       }
    });
    
    //TRUCK NO
    $( "#truck_no" ).autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "lr/get_truck_data.php",
          type: 'post',
          dataType: "json",
          data: {
            search: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
      select: function (event, ui) {
        if(ui.item.value != 0) {
          $('#truck_no').val(ui.item.label);
          get_Truck_weight();
          return false;
        }
      }
    });

    $( "#to_place" ).autocomplete({
       source: function( request, response ) {
          $.ajax({
              url: "lr/get_to_place.php",
              type: 'post',
              dataType: "json",
              data: {
                  search: request.term
              },
              success: function( data ) {
                  response( data );
              }
          });
       },
       select: function (event, ui) {
          $('#to_place').val(ui.item.label);
          return false;
       }
    });
   
    $( "#agent" ).autocomplete({
       source: function( request, response ) {
          $.ajax({
              url: "lr/get_agent.php",
              type: 'post',
              dataType: "json",
              data: {
                  search: request.term
              },
              success: function( data ) {
                  response( data );
              }
          });
       },
       select: function (event, ui) {
          $('#agent').val(ui.item.label);
          return false;
       }
    });
   
    $( "#nature_of_goods" ).autocomplete({
       source: function( request, response ) {
          $.ajax({
              url: "lr/get_nature_of_goods.php",
              type: 'post',
              dataType: "json",
              data: {
                  search: request.term
              },
              success: function( data ) {
                  response( data );
              }
          });
       },
       select: function (event, ui) {
          $('#nature_of_goods').val(ui.item.label);
          return false;
       }
    });
   
    $('#rate_on').on('change', function() {
      calculateFinalAmount();
    });
    
    $('#billing_party').on('change', function() {
      if ($(this).val()  == 'CONSIGNOR') {
        var $consi = parseFloat(jQuery('#consignor_name').val());
        if($consi == $consi){
          var $consis = parseFloat(jQuery('#billing_to').val($consi).change());
        }
      }else if($(this).val()  == 'CONSIGNEE'){
        var $consignee = parseFloat(jQuery('#consignee_name').val());
        if($consignee == $consignee){
          var $consignee = parseFloat(jQuery('#billing_to').val($consignee).change());
        }
      }else{
         var $consignee = parseFloat(jQuery('#billing_to').val(0).change());
      }
    });

  });
  
  function calculateFinalAmount(ele) {

    var lr_no = $(ele).val();

    var $weight = parseFloat(jQuery('#weight').val());
    if ($weight == '' || $weight == undefined || isNaN($weight)) {
      $weight = 0;
    }

    $rate       = parseFloat(jQuery('#rate').val());
    if ($rate == '' || $rate == undefined || isNaN($rate)) {
      $rate = 0;
    }

    $pcs  = parseFloat(jQuery('#pcs').val());
    if ($pcs == '' || $pcs == undefined || isNaN($pcs)) {
      $pcs = 0;
    }

    var $freight = parseInt(jQuery('#freight').val());
      if ($freight == '' || $freight == undefined || isNaN($freight)) {
      $freight = 0;
    }

    var $advance = parseInt(jQuery('#advance').val());
    if ($advance == '' || $advance == undefined || isNaN($advance)) {
      $advance = 0;
    }
    
    var $extra_height_amnt = parseInt(jQuery('#extra_height_amnt').val());
    if ($extra_height_amnt == '' || $extra_height_amnt == undefined || isNaN($extra_height_amnt)) {
       $extra_height_amnt = 0;
    }
    if(lr_no == 'WEIGHT'){
      if($weight !=''){
        $amnt  = $weight * $rate;
      }
      if($weight !='' && $pcs !=''){
       $amnt  = $weight * $rate;
      }
    }else if(lr_no == 'PCS'){
      if($pcs !=''){
        $amnt  = $pcs * $rate;
      }
      if($weight !='' && $pcs !=''){
       $amnt  = $pcs * $rate;
      }
      
    }else{

    }
    if(lr_no != 'WEIGHT' && lr_no != 'PCS'){
      
      $net   = $freight - $advance;
      jQuery('#total_amnt').val($net.toFixed(2));  
    }else{
      $net   = $amnt - $advance;
      jQuery('#freight').val($amnt.toFixed(2));
      jQuery('#total_amnt').val($net.toFixed(2));  
    }
    
    
    if($extra_height_amnt !=''){
      $final_am = $freight + $extra_height_amnt - $advance;  
      jQuery('#total_amnt').val($final_am.toFixed(2));
    }
    document.getElementById('advance').setAttribute('max', $freight);
  }
   
  function get_Truck_weight(ele) {
    var truck_no = $(ele).val();
    $.ajax({
      'type': 'POST',
      'url': 'lr/get_truck.php',
      'data': 'truck_no=' + truck_no,
      dataType: 'JSON',
      success: function (result) {
        if (result != '') {
          $('#height').val(result.height);
          $('#weight').val(result.weight);
        }
      }
    });
  }
  function get_consignor_data(ele) {
    var consignor_name = $(ele).val();
    $.ajax({
        'type': 'POST',
        'url': 'lr/get_consignor_data.php',
        'data': 'consignor_name=' + consignor_name,
        dataType: 'JSON',
        success: function (result) {
           if (result != '') {
              $('#consignor_address').val(result.address);
              $('#consignor_city').val(result.city);
              $('#consignor_contact').val(result.mobile);
              $('#consignor_pan').val(result.pan);
              $('#consignor_gst').val(result.gstin);
           }
        }
    });
  }

  function get_bill_data(ele) {
    var billing_to = $(ele).val();
    $.ajax({
        'type': 'POST',
        'url': 'lr/get_bill_data.php',
        'data': 'billing_to=' + billing_to,
        dataType: 'JSON',
        success: function (result) {
           if (result != '') {
              $('#bill_address').val(result.address);
              $('#bill_city').val(result.city);
              $('#bill_contact').val(result.mobile);
              $('#bill_pan').val(result.pan);
              $('#bill_gst').val(result.gstin);
           }
        }
    });
  }
   
  function get_consignee_data(ele) {
    var consignee_name = $(ele).val();
    $.ajax({
        'type': 'POST',
        'url': 'lr/get_consignee_data.php',
        'data': 'consignee_name=' + consignee_name,
        dataType: 'JSON',
        success: function (result) {
           if (result != '') {
              $('#consignee_address').val(result.address);
              $('#consignee_city').val(result.city);
              $('#consignee_contact').val(result.mobile);
              $('#consignee_pan').val(result.pan);
              $('#consignee_gst').val(result.gstin);
           }
        }
    });
  }
   
  function CheckLr() {
    var lr_no      = $("#lr_no").val();
    var branch_id  = $("#branch_id").val();
    var module     = $("#module").val();
    $.ajax({
      'type': 'POST',
      'url': 'lr/check-lr-no.php',
      'data': {
        'lr_no': lr_no,
        'branch_id': branch_id,
        'module': module
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          jQuery('.submit').attr('disabled', false);
        }else{
          jQuery('.submit').attr('disabled', true);
          swal("Opps !!", "Not A Valid LR Number", "info")  
        }
      }
    });
  }

  function CheckCode() {
    var lr_no      = $("#lr_no").val();
    $.ajax({
      'type': 'POST',
      'url': 'lr/check-code.php',
      'data': {
        'lr_no': lr_no
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          swal("Opps !!", "ALREADY CODE EXIST...!!!", "info")  
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  }

  function CheckTruck() {
    var truck_no      = $("#truck_no").val();
    $.ajax({
      'type': 'POST',
      'url': 'lr/check-truck-no.php',
      'data': {
        'truck_no': truck_no
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status != 1) {
          var $truck      = $("#truck_no").val();
          
          var url = "index.php?page=truck_mst/add.php" + '&type=' + 'truck' + '&no=' + jQuery('#truck_no').val();

          window.open(url);
        }else{
          get_Truck_weight();
          //swal("Opps !!", "ALREADY EXIST...!!!", "info")
          //setTimeout("location.reload(true);",1000);
        }
      }
    });
  }

  function CheckUplode() {
    var truck_no      = $("#truck_no").val();
    $.ajax({
      'type': 'POST',
      'url': 'lr/check_truck_doc.php',
      'data': {
        'truck_no': truck_no
      },
      dataType: 'JSON',
      success: function (result) {
        if(result.status == 1) {
          $.toast({
            heading: 'OPPS....',
            text: 'RC AND IMAGE NOT UPLODED',
            icon: 'warning',
            position: 'top-right'
          });  
        }else{
          jQuery('.submit').attr('enable', true);
        }
      }
    });
  }
</script>