<?php
$lr_table = combine(getAllRecords('lr_mst'), 'lr_no', 'freight'); 
$lhc = mysqli_query($conn, "SELECT * FROM `lhc` WHERE lr_amount is null AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'");
while ($rows = mysqli_fetch_assoc($lhc)) {
   $lr_freight = @$lr_table[$rows['lr_no']];
   $update = mysqli_query($conn, "UPDATE `lhc` SET`lr_amount` = '".$lr_freight."' WHERE `lr_no` = '".$rows['lr_no']."'");
}

$agent = mysqli_query($conn, "SELECT DISTINCT agent FROM `lr_mst` WHERE `agent` != '' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'");
$agent_data = array();
while ($agent_row = mysqli_fetch_assoc($agent)) {
   $agent_data[] = $agent_row;
}

$truck = mysqli_query($conn, "SELECT DISTINCT truck_no FROM `lr_mst` WHERE `truck_no` != '' AND company_id = '".$_SESSION['company_id']."' AND branch_id = '".$_SESSION['branch_id']."'");
$truck_data = array();
while ($truck_row = mysqli_fetch_assoc($truck)) {
   $truck_data[] = $truck_row;
}

$vendor = mysqli_query($conn, "SELECT * FROM `vendor_master` WHERE `status` = '0' AND type = 'V'");
$vender_data = array();
while ($vendor_row = mysqli_fetch_assoc($vendor)) {
   $vender_data[] = $vendor_row;
}

$emp = mysqli_query($conn, "SELECT * FROM `emp_mst` WHERE `status` = '0'");
$emp_data = array();
while ($emp_row = mysqli_fetch_assoc($emp)) {
   $emp_data[] = $emp_row;
}
?>
<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">TRIP WISE PROFIT</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">
               <div class="row">
                  <div class="col-md-2">
                     <div class="form-group">
                        <label class="capital">START DATE</label>
                        <div class="input-group">
                           <div class="input-group-addon">
                              <i class="fa fa-calendar date-icon-color"></i>
                           </div>
                           <input type="text" name="start_date" id="start_date" class="form-control date-picker" autocomplete="off" value="<?php echo risFormatDate($year,'d-m-Y')?>" tabindex="1"/>
                        </div>
                     </div>
                  </div>
                  <div class="col-md-2">
                     <div class="form-group">
                        <label class="capital">END DATE</label>
                        <div class="input-group">
                           <div class="input-group-addon">
                              <i class="fa fa-calendar date-icon-color"></i>
                           </div>
                           <input type="text" name="end_date" id="end_date" class="form-control date-picker" autocomplete="off" value="<?php echo risFormatDate($date);?>" tabindex="2">
                        </div>
                     </div>
                  </div>
                  <div class="col-md-3">
                     <div class="form-group">
                        <label class="capital">AGENT</label>
                        <select class="form-control select2" id="agent" name="agent">
                           <option value></option>
                           <?php foreach ($agent_data as $key => $agent_datas) { ?>
                           <option value="<?php echo $agent_datas['agent']?>"><?php echo $agent_datas['agent']?></option>
                           <?php } ?>
                         </select>
                     </div>
                  </div>
                  <div class="col-md-2">
                     <div class="form-group">
                        <label class="capital">TRUCK</label>
                        <select class="form-control select2" id="truck_no" name="truck_no">
                           <option value></option>
                           <?php foreach ($truck_data as $key => $truck_datas) { ?>
                           <option value="<?php echo $truck_datas['truck_no']?>"><?php echo $truck_datas['truck_no']?></option>
                           <?php } ?>
                         </select>
                     </div>
                  </div>
                  <div class="col-md-3">
                     <div class="form-group">
                        <label class="capital">VENDOR</label>
                        <select class="form-control select2" id="vendor" name="vendor">
                           <option value></option>
                           <?php foreach ($vender_data as $key => $vender_datas) { ?>
                           <option value="<?php echo $vender_datas['id']?>"><?php echo $vender_datas['name']?></option>
                           <?php } ?>
                         </select>
                     </div>
                  </div>
               </div>
               <div class="row">
                  <div class="col-md-3">
                     <div class="form-group">
                        <label class="capital">EMPLOYEE</label>
                        <select class="form-control select2" id="employee_id" name="employee_id">
                           <option value></option>
                           <?php foreach ($emp_data as $key => $emp_datas) { ?>
                           <option value="<?php echo $emp_datas['id']?>"><?php echo $emp_datas['name']?></option>
                           <?php } ?>
                         </select>
                     </div>
                  </div>
                  <div class="col-lg-1">
                     <div class="form-group">
                        <button type="button" class="btn btn-primary top10" id="btn-genrate">GET DATA</button>
                     </div>
                  </div>
               </div>
               <div class="row">
                  <div class="col-lg-12">
                     <table class="table table-bordered list_data">
                        <thead>
                           <tr>
                              <th class="text-center" width="5%">LR NO</th>
                              <th class="text-center" width="5%">DATE</th>
                              <th class="text-left" width="17%">CONSIGNOR</th>
                              <th class="text-left" width="17%">CONSIGNEE</th>
                              <th class="text-left" width="15%">AGENT</th>
                              <th class="text-left" width="10%">FROM</th>
                              <th class="text-left" width="10%">TO</th>
                              <th class="text-center" width="6%">TRUCK</th>
                              <th class="text-right" width="5%">INCOME</th>
                              <th class="text-right" width="5%">EXPENSE</th>
                              <th class="text-right" width="5%">PROFIT</th>
                           </tr>
                        </thead>
                        <tbody>
                           <tr>
                               <td colspan="11" class="text-center">No Data found to show !!</td>
                           </tr>
                        </tbody>
                      </table>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</section>
<script type="text/javascript">
jQuery(document).ready(function() {
   jQuery(document).on('click', '#btn-genrate', function(e){
      getData();
   });
});

function getData() {
   var url = "report/trip_wise_profit/trip_wise_profit_data.php?>" + '&start_date=' + jQuery('#start_date').val() + '&end_date=' + jQuery('#end_date').val() + '&agent=' + jQuery('#agent').val() + '&truck_no=' + jQuery('#truck_no').val() + '&vendor=' + jQuery('#vendor').val() + '&employee_id=' + jQuery('#employee_id').val()
   jQuery.ajax({
      type        : 'GET',
      url         :  url,
      dataType    : 'JSON',
      beforeSend:function(){
         jQuery('#list_data tbody').html('<tr><td colspan="11" 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);
            } else {
               jQuery('.list_data tbody').html('<tr><td colspan="7" class="text-center">No Data found to show !!</td></tr>');
            }
         }
      }
   });
}
</script>
