<?php
class PriceChange{

	private $conn;
	private $table_name = "price_change";

	public $id;	
	public $city;	
	public $package;	
    public $mbps;	
    public $days;	
    public $price_change_date;	
    public $package_price;	
    public $fee;	
    public $entry_by;
    public $entry_date_time;

	public function __construct($db){
		$this->conn = $db;
	}

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET city=:city, package=:package, mbps=:mbps, days=:days, price_change_date=:price_change_date, package_price=:package_price, fee=:fee, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":package", $this->package);
        $stmt->bindParam(":mbps", $this->mbps);
		$stmt->bindParam(":days", $this->days);
        $stmt->bindParam(":price_change_date", $this->price_change_date);
		$stmt->bindParam(":package_price", $this->package_price);
        $stmt->bindParam(":fee", $this->fee);
        $stmt->bindParam(":entry_by", $this->entry_by);
        $stmt->bindParam(":entry_date_time", $this->entry_date_time);

		if($stmt->execute()){
			return true;
		}	 
		return false;
	}

	function getAllRows(){
		$condition = "";

		if($this->city){
			if($condition!=""){ $condition .= " AND "; }
			$condition .= " a.city=:city ";
		}
		if($this->price_change_date){
			if($condition!=""){ $condition .= " AND "; }
			$condition .= " package=:package ";
		}

		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT price_change.* from (SELECT *,MAX(price_change_date) AS mpcd FROM price_change GROUP BY package) AS a LEFT JOIN 
		price_change ON a.mpcd=price_change.price_change_date AND a.package=price_change.package" . $condition . " order by price_change.mbps, price_change.days";
		$stmt = $this->conn->prepare( $query );
		if($this->city) $stmt->bindParam(":city", $this->city);
		if($this->price_change_date) $stmt->bindParam(":price_change_date", $this->price_change_date);

		$stmt->execute();
		return $stmt;
	}

	function delete(){
		$query = "DELETE FROM `" . $this->table_name . "` WHERE id = ?";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(1, $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;
    }
	
		function deletePC(){
		$query = "DELETE FROM `" . $this->table_name . "` WHERE city = ? and price_change_date= ?";
		$stmt = $this->conn->prepare($query);
		$stmt->bindParam(1, $this->city);
		$stmt->bindParam(2, $this->price_change_date);
		if($stmt->execute()){
			return true;
		}
		return false;
    }

}
?>
