<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'tr_edit') ) {
    echo '<script>window.location.href="index.php?page=no_permission.php"</script>';die();
  }

  if (isset($_REQUEST['eid']) && !empty($_REQUEST['eid'])){
    $select_update     = "SELECT * FROM tracking WHERE id='".$_REQUEST['eid']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
    $select_update_res = mysqli_query($conn,$select_update);
    $update_row        = mysqli_fetch_array($select_update_res,MYSQLI_BOTH);
  }
  $tracker        = "SELECT * FROM tracking WHERE id='".$_REQUEST['eid']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
  $tracker_res    = mysqli_query($conn,$tracker);
  $total_records  = mysqli_num_rows($tracker_res);
  $master_vendor  = combine(getAllRecords('vendor_master'), 'id', 'name');
?>
<style type="text/css">
 .height29 {
    height: 60px !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 TRACKING</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'], 'tr_list') ) { ?>
          <a href="index.php?page=tracking/list.php" class="btn btn-danger capital bottom10 custome-btn">VIEW TRACKING 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="tracking/insert.php" method="post" enctype="multipart/form-data" class="common-frm-validation" id="signupForm" name="signupForm">
                <div class="row">
                  <div class="col-md-1">
                    <div class="form-group">
                      <label class="capital">TRACK ID</label><span style="color: red">*</span>
                      <div class="input-group date">
                        <input type="text" size="35" id="tracking_id" name="tracking_id" class="form-control date-picker" placeholder="TRACK ID" autocomplete="off" value="<?php echo $update_row['tracking_id'];?>" readonly=""/>
                      </div>
                    </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="t_date" name="t_date" class="form-control" placeholder="DATE" autocomplete="off" value="<?php echo $update_row['t_date']?>" readonly=""/>
                      </div>
                    </div>
                  </div>
                  <!-- <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">employee</label>
                      <select class="form-control select2" name="emp_id" id="emp_id" onChange="lr_data(this.value);">
                        <option value>ALL</option>
                        <?php 
                          $truck     = "SELECT * FROM `emp_mst` WHERE `status` = '0'";
                          $truck_res = mysqli_query($conn,$truck);
                          while ($truck_row=mysqli_fetch_assoc($truck_res)) { ?>f
                          <option <?php if($truck_row['id'] == $update_row['emp_id']) { echo "selected='selected'"; } ?> value="<?php echo $truck_row['id']?>"><?php echo $truck_row['name']?></option>
                          <?php } ?>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <div class="form-group">
                      <label class="capital">TYPE</label><span style="color: red">*</span>
                      <select class="form-control select2 required" name="type" id="type" onChange="lr_data(this.value);">
                        <option value>ALL</option>
                        <option value="CONSIGNEE" <?php if($update_row['type'] == 'CONSIGNEE') { ?> selected="selected"<?php } ?> >CONSIGNEE</option>
                        <option value="CONSIGNOR" <?php if($update_row['type'] == 'CONSIGNOR') { ?> selected="selected"<?php } ?>>CONSIGNOR</option>
                      </select>
                    </div>
                  </div>
                  <div class="col-md-3">
                    <label class="capital">VENDOR</label>
                    <div class="form-group">
                      <select class="form-control select2" style="width: 100%;" name="vendor_id" id="vendor_id" onChange="lr_data(this.value);">
                        <option value>ALL</option>
                        <?php 
                        $ven      = "SELECT * FROM `vendor_master` WHERE `status` = '0'";
                        $ven_res  = mysqli_query($conn,$ven);
                        while ($ven_row = mysqli_fetch_assoc($ven_res)) { ?>
                        <option <?php if($ven_row['id'] == $update_row['vendor_id']) { echo "selected='selected'"; } ?> value="<?php echo $ven_row['id'];?>"><?php echo $ven_row['name'];?></option>
                        <?php } ?>
                      </select>
                    </div>
                  </div> -->
                </div>
                <div class="col-md-12">
                  <hr> 
                  <section>
                    <div class="card">
                      <div class="card-body table-responsive">
                        <table class="table table-bordered table-striped list_data width100">
                          <thead>
                            <tr>
                              <th width="40" class="text-center">LR NO.</th>
                              <th width="70" class="text-center">TRUCK NO</th>
                              <th width="150" class="text-center">CONSIGNEE</th>
                              <th width="150" class="text-center">CONSIGNOR</th>
                              <th width="40" class="text-center">TIME</th>
                              <th width="70" class="text-center">FROM</th>
                              <th width="70" class="text-center">TO</th>
                              <th width="70" class="text-center">DRIVER NO</th>
                              <th width="100" class="text-center">LOCATION</th>
                              <th width="100" class="text-center">STAUS</th>
                            </tr>
                          </thead>
                          <tbody>
                            <?php 
                            $count=$total_records;  if($count>0){
                            $i = 0;
                              while ($rows = mysqli_fetch_array($tracker_res,MYSQLI_BOTH)) { 
                              $lr     = "SELECT * FROM lr_mst WHERE lr_no = '".$rows['lr_no']."'";
                              $lr_res = mysqli_query($conn,$lr);
                              $lr_row = mysqli_fetch_assoc($lr_res);
                              ?>
                              <tr class="text-center">
                                <td>
                                  <?php echo $rows['lr_no'];?>
                                  <input type="hidden" name="lrdata[<?php echo $i; ?>][lr_no]" value="<?php echo $rows['lr_no'];?>">  
                                </td>

                                <td>
                                  <?php echo $rows['truck_no'];?>
                                  <input type="hidden" name="lrdata[<?php echo $i; ?>][truck_no]" value="<?php echo $rows['truck_no'];?>">  
                                </td>

                                <td><?php echo @$master_vendor[$lr_row['consignee_name']];?></td>
                                <td><?php echo @$master_vendor[$lr_row['consignor_name']];?></td>

                                <td>
                                <input type="time" name="lrdata[<?php echo $i; ?>][time]" class="form-control add-more-table-tracking" autocomplete="off" value="<?php echo $rows['follow_up_time'];?>">
                                </td>

                                <td><?php echo $lr_row['from_place'];?></td>
                                <td><?php echo $lr_row['to_place'];?></td>
                                <td><?php echo $lr_row['truck_driver_no'];?></td>
                                <td><input type="text" name="lrdata[<?php echo $i; ?>][location]" class="form-control add-more-table-tracking" value="<?php echo $rows['location']?>"></td>
                                <td>
                                  <select class="form-control select2 add-more-table-tracking chosen-select" name="lrdata[<?php echo $i; ?>][tracking_status]">
                                    <option value></option>
                                    <option value="P" <?php if($rows['tracking_status'] == 'P') { ?> selected="selected"<?php } ?>>PENDING</option>
                                    <option value="C" <?php if($rows['tracking_status'] == 'C') { ?> selected="selected"<?php } ?>>CLOSING</option>
                                  </select>
                                </td>
                              </tr>
                              <?php $i++; ?>
                              <?php } } else{ ?>
                              <tr>
                                <td colspan="8" class="text-center"><?php echo "NO RECORD FOUND"; ?></td>
                              </tr>
                              <?php } ?>
                          </tbody>
                        </table>
                      </div>
                    </div>
                  </section>
                  <input type="hidden" name="id" value="<?php echo $_GET['eid']?>">
                </div>
                <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=tracking/list.php" class="btn btn-danger capital"> Cancel</a>
                </div>
              </form>
            </div>
         </div>
      </div>
   </div>
</section>

<script type="text/javascript">
  jQuery(document).ready(function() {
    $('#emp_id').select2();
    $('#vendor_id').select2();
    $('#type').select2();
  });
  function lr_data(str) {
    if (str=="") {
      document.getElementById("txtHitz").innerHTML="";
      return;
    } 
    if (window.XMLHttpRequest) {
      xmlhttp=new XMLHttpRequest();
    } else { 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
      if (this.readyState==4 && this.status==200) {
        document.getElementById("txtHitz").innerHTML=this.responseText;
      }
    }
    var emp_id    = document.signupForm.emp_id.value;
    var vendor_id = document.signupForm.vendor_id.value;
    xmlhttp.open("GET","tracking/get_lr_data.php?q="+str+"&emp_id="+emp_id+"&vendor_id="+vendor_id,true);
    xmlhttp.send();
  }
</script>