<?php
class Staff{ 
	private $conn;
	private $table_name = "staff";

    public $id;	
	public $branch_id;	
	public $name;
	public $join_date;
	public $position;
	public $dob;
	public $nrc_no;
	public $gender;
	public $phone;
	public $email;
	public $address;
	public $entry_by;
	public $entry_date_time;
	
	public function __construct($db){
		$this->conn = $db;
	}

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET branch_id=:branch_id, name=:name, join_date=:join_date, position=:position, dob=:dob, nrc_no=:nrc_no, gender=:gender, phone=:phone, email=:email, address=:address, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":branch_id", $this->branch_id);
		$stmt->bindParam(":name", $this->name);
		$stmt->bindParam(":join_date", $this->join_date);
		$stmt->bindParam(":position", $this->position);
		$stmt->bindParam(":dob", $this->dob);
		$stmt->bindParam(":nrc_no", $this->nrc_no);
		$stmt->bindParam(":gender", $this->gender);
		$stmt->bindParam(":phone", $this->phone);
		$stmt->bindParam(":email", $this->email);
		$stmt->bindParam(":address", $this->address);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);
		
		if($stmt->execute()){
			$this->id = $this->conn->lastInsertId();
			return true;
		}
		return false;
	}

	function update(){
		$query = "UPDATE " . $this->table_name . " SET join_date=:join_date, position=:position, dob=:dob, nrc_no=:nrc_no, gender=:gender, phone=:phone, email=:email, address=:address, resign=:resign, resign_date=:resign_date, entry_by=:entry_by, entry_date_time=:entry_date_time where id=:id";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":id", $this->id);
		$stmt->bindParam(":join_date", $this->join_date);
		$stmt->bindParam(":position", $this->position);
		$stmt->bindParam(":dob", $this->dob);
		$stmt->bindParam(":nrc_no", $this->nrc_no);
		$stmt->bindParam(":gender", $this->gender);
		$stmt->bindParam(":phone", $this->phone);
		$stmt->bindParam(":email", $this->email);
		$stmt->bindParam(":address", $this->address);
		$stmt->bindParam(":resign", $this->resign);
		$stmt->bindParam(":resign_date", $this->resign_date);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);
		
		if($stmt->execute()){
			$this->id = $this->conn->lastInsertId();
			return true;
		}
		return false;
	}

	function exist(){
		$query = "SELECT * 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 getAllRows(){
		$query = "SELECT staff.*,branch FROM " . $this->table_name . " left join branch on staff.branch_id=branch.id where branch_id=:branch_id ORDER BY `name`";
		$stmt = $this->conn->prepare($query); 
		$stmt->bindParam(":branch_id", $this->branch_id);
		$stmt->execute();
		return $stmt;
	}	
}
?>
