<?php
  if( !hasPermission($_SESSION['user_detail']['role_id'], 'tr_add') ) {
    echo '<script>window.location.href="index.php?page=no_permission.php"</script>';die();
  }
  $tracking_id = Tracking($conn);
?>
<style type="text/css">
  .text-right{
    text-align: right !important;
  }
  .text-left{
    text-align: left !important;
  }
  .hitz-report {
    background: #fff !important;
    padding: 6px 0px 9px 0 !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">ADD NEW 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="content-header bottom35-report bottom40">
  <div class="row">
    <form action="tracking/insert.php" method="post" enctype="multipart/form-data" class="common-frm-validation" id="signupForm" name="signupForm">
      <div class="col-md-12 hitz-report">
        <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 $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 date-picker" placeholder="DATE" autocomplete="off" value="<?php echo date('d-m-Y')?>" />
            </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">
              <option value>ALL</option>
              <?php 
                $truck     = "SELECT * FROM `emp_mst` WHERE `status` = '0'";
                $truck_res = mysqli_query($conn,$truck);
                while ($truck_row=mysqli_fetch_array($truck_res)) { ?>f
                <option value="<?php echo $truck_row['id']?>"><?php echo $truck_row['name']?></option>
                <?php } ?>
            </select>
          </div>
        </div>
        <div class="col-md-2">
          <div class="form-group">
            <label class="capital">TYPE</label>
            <select class="form-control select2" name="type" id="type">
              <option value>ALL</option>
              <option value="CONSIGNEE">CONSIGNEE</option>
              <option value="CONSIGNOR">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">
              <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 value="<?php echo $ven_row['id'];?>"><?php echo $ven_row['name'];?></option>
              <?php } ?>
            </select>
            <input type="hidden" id="branch_id" name="branch_id" value="<?php echo $_SESSION['branch_id'];?>">

            <input type="hidden" id="company_id" name="company_id" value="<?php echo $_SESSION['company_id'];?>">
          </div>
        </div>
        <div class="col-md-1" style="position: relative;top: 10px;">
          <div class="form-group">
            <button type="button" class="btn btn-primary" id="btn-genrate"><i class="fa fa-refresh" aria-hidden="true"></i></button>
          </div>
        </div>
        <div class="col-md-12">
          <div class="box-body table-responsive">
            <table id="list_data" class="table table-bordered table-striped list_data">
              <thead>
                <tr>
                  <th width="40" class="text-center">LR NO.</th>
                  <th width="40" class="text-center">TRUCK NO</th>
                  <th width="180" class="text-center">CONSIGNEE</th>
                  <th width="180" 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>
                <tr>
                  <td colspan="10" class="text-center">No Data found to show !!</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div><hr>
        <input type="hidden" name="id" value="">
        <div class="row" align="center">
          <button id="btn" class="btn btn-primary ajax-submit capital" type="submit"> Save & List</button>
          <a href="index.php?page=tracking/list.php" class="btn btn-danger capital"> Cancel</a>
        </div>
      </div>

    </form>
  </div>
</section>
<script type="text/javascript">
  jQuery(document).ready(function() {
    $('#emp_id').select2();
    $('#vendor_id').select2();
    $('#type').select2();
    jQuery(document).on('click', '#btn-genrate', function(e){
      getData();
    });
  });
  
  function getData() {
    var url = "tracking/get_lr_data.php?>" + '&t_date=' + jQuery('#t_date').val() + '&emp_id=' + jQuery('#emp_id').val() + '&type=' + jQuery('#type').val() + '&vendor_id=' + jQuery('#vendor_id').val() + '&branch_id=' + jQuery('#branch_id').val() + '&company_id=' + jQuery('#company_id').val();
    //jQuery('#list_data .start_date').html(jQuery('#start_date').text());
    //jQuery('#list_data .end_date').html(jQuery('#end_date').text());
    jQuery.ajax({
      type        : 'GET',
      url         :  url,
      dataType    : 'JSON',
      beforeSend:function(){
        jQuery('#list_data tbody').html('<tr><td colspan="12" class="text-center"><i class="fa fa-2x fa-spin fa-spinner"></i> Loading Data....</td></tr>');
      },
      success: function(data) {
        if(data.status == 'success') {
          if(data.tbody != '') {
            jQuery('#list_data tbody').html(data.tbody);
            jQuery('.total-records').html(data.total_count);  
            jQuery('.min_data').html(data.min_data);  
            jQuery('.max_data').html(data.max_data);
            jQuery('.average').html(data.average);
          } else {
            jQuery('#list_data tbody').html('<tr><td colspan="12" class="text-center">No Data found to show !!</td></tr>');
            jQuery('.total-records').html(0);
          }
        }
      }
    });
  }
</script>
