<?php
class Member{
	    // database connection and table name
	private $conn;
	private $table_name = "member";

	// object properties

	public $id;
	public $city;
	public $member_type;
	public $member_discount;
	public $member_install_discount;
	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, member_type=:member_type, member_discount=:member_discount, member_install_discount=:member_install_discount, entry_by=:entry_by, entry_date_time=:entry_date_time";

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

		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":member_type", $this->member_type);
		$stmt->bindParam(":member_discount", $this->member_discount);
		$stmt->bindParam(":member_install_discount", $this->member_install_discount);
		$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 * FROM `" . $this->table_name . "` WHERE city=:city and member_type=:member_type ORDER BY city, member_type";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":member_type", $this->member_type);
		$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);
		$stmt->bindParam(1, $this->id);
		if($stmt->execute()){
			return true;
		}
		return false;
    }

	function getAllRows(){
		$condition = "";

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

		if($condition!=""){
			$condition = " WHERE " . $condition;
		}
		$query = "SELECT * FROM " . $this->table_name . $condition . " ORDER BY city,member_discount";
		$stmt = $this->conn->prepare( $query );
		if($this->city) $stmt->bindParam(":city", $this->city);
		if($this->member_type) $stmt->bindParam(":member_type", $this->member_type);

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



}
?>