<?php

/**
 * CodeShareData 
 * Original Author: @strider (Danial C)
 * 
 * 
 * This file was modded by Leonard.
 * Donot replace it with newer versions, or it may break.
 */
class CodeShareData extends CodonData
{


  public static function getCodeShare_airlines() {
    	return DB::get_results("SELECT * FROM ".TABLE_PREFIX."codeshare_airlines");
  }

  public static function edit_airline($id, $icao, $logo, $airlinename, $status) {

    $sql = "UPDATE `phpvms_codeshare_airlines` SET `icao` = '$icao', `image` = '$logo', `name` = '$airlinename' WHERE `id` = '$id'";
    $ret = DB::query($sql);
    return $ret;
  }


  public static function get_airline($id) {
      $sql = "SELECT * FROM ".TABLE_PREFIX."codeshare_airlines WHERE `id` = '$id'";
      $ret = DB::get_row($sql);
      return $ret;
  }

  public static function CodeShareFlightNum($flightnum) {
    $codeshare_airline = CODESHARE_MAIN_AIRLINE;
    $codeshare_flightnum = $flightnum.'C';
    $gen = array();

    $gen['code'] = $codeshare_airline;
    $gen['flightnum'] = $codeshare_flightnum;
    $gen['notes'] = "CodeShare partner, schedules operated on behalf of {$codeshare_airline}";

    return $gen;
  }

  public function add_newairline($name, $icao, $logo, $status) {
    $sql = "INSERT INTO ".TABLE_PREFIX."codeshare_airlines (`icao`, `name`, `image`, `active`) VALUES
    ('$icao', '$name', '$logo', '$status')";
    $ret = DB::query($sql);

    return $ret;
  }

  public function check_airline($icao) {
    $icao = strtoupper($icao);
    $sql = "SELECT * FROM ".TABLE_PREFIX."codeshare_airlines WHERE `icao` LIKE '$icao'";
    $ret = DB::get_row($sql);
    return $ret;
  }



  public static function updateFlightCount($airline) {

    $icao = strtoupper($icao);
    $sql = "SELECT * FROM ".TABLE_PREFIX."codeshare_airlines WHERE `icao` LIKE '$icao'";
    $ret = DB::get_row($sql);


    $id = $ret->id;
    $flight_count = $check->flights;
    $flight_count = $flight_count++;
    $sql = "UPDATE `phpvms_codeshare_airlines` SET `flights` = '$flight_count' WHERE `id` = $id";
    DB::query($sql);
  }


    public static function get_codeshare()
    {
		return DB::get_results("SELECT * FROM ".TABLE_PREFIX."codeshares ORDER BY airline ASC");

    }


 	public static function get_upcoming_codeshares()
    {
        $query = "SELECT * FROM phpvms_codeshares
                ORDER BY schedid ASC";

        return DB::get_results($query);
    }
    public static function get_codeshares($id)
    {
        $query = "SELECT * FROM phpvms_codeshares WHERE `schedid` = '$id'";
        $ret = DB::get_row($query);
        return $ret;
    }

    public static function get_codeshares_icao($icao)
    {
        $query = "SELECT * FROM `phpvms_codeshares` WHERE `operator` LIKE '$icao'";
        $ret = DB::get_results($query);
        return $ret;
    }


   public static function get_past_codeshare()
    {
        $query = "SELECT * FROM phpvms_codeshares
                ORDER BY schedid DESC";

        return DB::get_results($query);
    }
    public static function save_new_codeshare($schedid, $airline, $operator)
    {
        $airline_details = self::check_airline($airline);
        $query = "INSERT INTO phpvms_codeshares (schedid, airline, operator)
                VALUES ('$schedid', '$airline', '$operator')";
        DB::query($query);
        self::updateFlightCount($airline);
    }

    public static function updateCodeShareflight($id, $schedID) {
      $sql = "UPDATE `phpvms_codeshares` SET `schedid` = '$schedID' WHERE `phpvms_codeshares`.`id` = 28";
      DB::query($sql);
    }


     public static function save_edit_codeshare($schedid, $airline, $id)
    {
        $query = "UPDATE phpvms_codeshares SET
         schedid='$schedid',
         airline='$airline'
         WHERE id='$id'";

        DB::query($query);
    }

    public static function delete_codeshare($id)
    {
        $query = "DELETE FROM phpvms_codeshares
                    WHERE id='$id'";

        DB::query($query);
    }

}
