<?php 
$airac = '1802';
?>
<div class="row">
  <div class="col-md-12">
    <div class="block">
      <div class="block-title">
        <div class="block-options pull-right">
          <span class="label label-success">
            <?php echo $airac; ?>
          </span>
        </div>
        <h3><Strong>iCrew </Strong>Maintenance</h3>
      </div>
      <?php 
    	/* Automatic Price Calculation 
    			The system auto calculates the price for each schedule based on the 
    			distance. It's a simple algorithm, which is explained below
    			
    			PRICE PER NM: $5
    			
    			If the Distance is GREATER than 500, The PRICE 
    			increases to $10
    			
    			If the Distance is LESSER than 250, the PRICE 
    			increases to $7
    	*/
    	function all_sch(){
   	    $sql = "SELECT * FROM `phpvms_schedules`;";
				return DB::get_results($sql);
        }
        
        function update_price($price, $id){
          
        $sql = "UPDATE `phpvms_schedules` SET `price` = '$price' WHERE `id` = '$id'";
         return DB::get_results($sql);
        }
        
       function get_sch_wo_rte(){
   	    $sql = "SELECT * FROM `phpvms_schedules` LIMIT 5;";
   	    //WHERE route = ''
				return DB::get_results($sql);
        }
        
        function total(){
   	    $sql = "SELECT * FROM `phpvms_schedules` ;";
				return DB::get_results($sql);
        }
        
        function wo(){
   	     $sql = "SELECT * FROM `phpvms_schedules` WHERE route = '';";
				return DB::get_results($sql);
        }
        
         function update_rte($route, $id){
          
        $sql = "UPDATE `phpvms_schedules` SET `route` = '$route' WHERE `id` = '$id'";
         return DB::get_results($sql);
        }
        
        /*First we are getting all the routes which doesn't have routes*/
        $sch_wo_rte = get_sch_wo_rte(); 
        /*Total sch without routes*/
        
        $wo_rte = count(wo()); 
        $total = count(total());
        ?>
      <div class="alert alert-warning">
        Welcome to the iCrew Maintenance, Run this page when you update schedules, the system will now automatically look for Schedules without Route and auto update it.
      </div>
        <code>Out of <?php echo $total; ?>, <?php echo $wo_rte; ?> routes were found without route, fixing them...</code>
        <div style="overflow: auto; height: 450px; border: 0px solid #666; margin-bottom: 20px; padding: 5px; padding-top: 0px; padding-bottom: 20px;">         
     
<div class="table-responsive">
  <table class="table table-hover table-condensed">
      <thead>
        <th>ID</th>
        <th>Flight #</th>
        <th>DEP & ARR</th>
        <th>Route</th>
        <th>AIRAC Cycle</th>
        <th>Distance</th>
        <th>Price</th>
      </thead>
      <tbody>
      
      <?php 
      
        foreach ($sch_wo_rte as $s) {
          ?>
          <tr>
            <td><?php echo $s->id;?></td>
            <td><?php echo $s->code;?><?php echo $s->flightnum;?></td>
            <td><?php echo $s->depicao;?> <i class="fa fa-plane"></i> <?php echo $s->arricao;?></td>
            <td><?php
            
            $context = stream_context_create(
    array(
        "https" => array(
            "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
        )
    )
);



                  if($route) {
                    echo $s->route;
                  } else {
                    $routeGet = file_get_contents('https://vau.aero/route/getRoute.php?dep='.$s->depicao.'&nats=R&airac='.$airac.'&dest='.$s->arricao.'&rt=OFF&level=H', false, $context);
                    $routeData = explode('#', $routeGet);
                    $arrayKeys = array_keys($routeData);
                    if($routeData[$arrayKeys[2]]) {
                      echo $routeData[$arrayKeys[2]];
                    update_rte($routeData[$arrayKeys[2]], $s->id);
                    } else {
                      echo '<code>No Route Found</code>';
                      update_rte('OWN NAVIGATION (ROUTE NOT FOUND)', $s->id);
                    }
                    
                  }
                ?>
            </td>
            <td><?php echo $airac; ?></td>
            <td><?php echo $s->distance; ?></td>
            <td>
            <?php 
            	$dist = $s->distance;
            	$price_per_nm = 1.5;
            	$price = round($dist * $price_per_nm);
            	update_price($price, $id);
            	echo $price;
            ?></td>
          </tr>
          <?
          
        }
        
      ?>
      
       </tbody>
  </table>
  <code class="text-success">Success, all routes added to schedules database.</code>
  </div>
</div>
     
<div class="table-responsive">
  <table class="table table-hover table-condensed">
      <thead>
        <th>ID</th>
        <th>Flight #</th>
        <th>DEP & ARR</th>
        <th>Route</th>
        <th>AIRAC Cycle</th>
      </thead>
      <tbody>
      
      <?php 
      
        foreach ($sch_wo_rte as $s) {
          ?>
          <tr>
            <td><?php echo $s->id;?></td>
            <td><?php echo $s->code;?><?php echo $s->flightnum;?></td>
            <td><?php echo $s->depicao;?> <i class="fa fa-plane"></i> <?php echo $s->arricao;?></td>
            <td><?php
                  if($route) {
                    echo $s->route;
                  } else {
                    $routeGet = file_get_contents('https://vau.aero/route/getRoute.php?dep='.$s->depicao.'&nats=R&airac='.$airac.'&dest='.$s->arricao.'&rt=OFF&level=H');
                    $routeData = explode('#', $routeGet);
                    $arrayKeys = array_keys($routeData);
                    if($routeData[$arrayKeys[2]]) {
                      echo $routeData[$arrayKeys[2]];
                    update_rte($routeData[$arrayKeys[2]], $s->id);
                    } else {
                      echo '<code>No Route Found</code>';
                      update_rte('OWN NAVIGATION (ROUTE NOT FOUND)', $s->id);
                    }
                    
                  }
                ?>
            </td>
            <td><?php echo $airac; ?></td>
          </tr>
          <?
          
        }
        
      ?>
      
       </tbody>
  </table>
  <code class="text-success">Success, all routes added to schedules database.</code>
  </div>
</div>
    <span class="text-muted pull-right"><i> iCrewSystems &copy; <?php echo date("Y"); ?></i></span>
    <br>
    <br>
    </div>
  </div>
</div>