<?php
class City{

	private $conn;
	private $table_name = "city";

	public $id;	
	public $profile_picture;
	public $name;	
	public $code;
	public $location;
	public $invoice_remark;
	public $smart_key;
	public $olt_id;


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

	function getAllRows(){
		$query = "SELECT * FROM " . $this->table_name . " ORDER BY name ";
		$stmt = $this->conn->prepare($query);
		$stmt->execute();
		return $stmt;
	}

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET name=:name, code=:code, profile_picture=:profile_picture, invoice_remark=:invoice_remark, location=:location";

		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":name", $this->name);
		$stmt->bindParam(":code", $this->code);
		$stmt->bindParam(":location", $this->location);
		$stmt->bindParam(":profile_picture", $this->profile_picture);
		$stmt->bindParam(":invoice_remark", $this->invoice_remark);	;

		if($stmt->execute()){
			$this->id = $this->conn->lastInsertId();
			return true;
		}
		return false;		
	}

	function update(){
		$condition = "";
		if($this->profile_picture){
			$condition = " , profile_picture=:profile_picture ";
		}
		$query = "UPDATE " . $this->table_name . " SET name=:name, code=:code, location=:location, invoice_remark=:invoice_remark " . $condition . " WHERE id=:id";

		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":id", $this->id);
		$stmt->bindParam(":name", $this->name);
		$stmt->bindParam(":code", $this->code);
		$stmt->bindParam(":location", $this->location);
		$stmt->bindParam(":invoice_remark", $this->invoice_remark);	
		if($this->profile_picture!="")  $stmt->bindParam(":profile_picture", $this->profile_picture);
		// $stmt->bindParam(":entry_by", $this->entry_by);
		// $stmt->bindParam(":entry_date_time", $this->entry_date_time);

		if($stmt->execute()){
			return true;
		}
		return false;		
	}
	
	function exist(){
		$query = "SELECT id FROM " . $this->table_name . " WHERE name = ? LIMIT 0,1";
		$stmt = $this->conn->prepare( $query );
		$this->name=htmlspecialchars(strip_tags($this->name));

		$stmt->bindParam(1, $this->name);

		$stmt->execute();
		if($stmt->rowCount()>0){
			return true;
		}
		return false;
	}
	function delete(){
		$query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
		$stmt = $this->conn->prepare($query);
		$this->id = htmlspecialchars(strip_tags($this->id));
		$stmt->bindParam(1, $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;
	}
	function getOneRow(){	
		$query = "SELECT * FROM " . $this->table_name . " WHERE id=:id";
		$stmt = $this->conn->prepare($query);
			
		$stmt->bindParam(":id", $this->id);

		$stmt->execute();
		if($stmt->rowCount()>0){
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			$this->id = $row['id'];
			$this->name = $row['name'];	
			$this->code = $row['code'];
			$this->location = $row['location'];
			$this->invoice_remark = $row['invoice_remark'];	
			$this->profile_picture = $row['profile_picture'];
			$this->olt_id = $row['olt_id'];
			$this->smart_key = $row['smart_key'];
		}
	}

}
?>
