<?php
class Message{
    private $conn;
    private $table_name = "message";
 
	public $id;
	public $expired;
	public $reminder;
	public $noti1;
	public $noti2;
	public $noti3;
	public $default;
	public $entry_by;
	public $entry_date_time;
	
    public function __construct($db){
        $this->conn = $db;
    }
	
	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET expired=:expired, reminder=:reminder, noti1=:noti1, noti2=:noti2, noti3=:noti3, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		// $this->expired = htmlspecialchars(strip_tags($this->expired));

		$stmt->bindParam(":expired", $this->expired);
		$stmt->bindParam(":reminder", $this->reminder);
		$stmt->bindParam(":noti1", $this->noti1);
		$stmt->bindParam(":noti2", $this->noti2);
		$stmt->bindParam(":noti3", $this->noti3);
		$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 expired=:expired, reminder=:reminder, noti1=:noti1, noti2=:noti2, noti3=:noti3, 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(":expired", $this->expired);
		$stmt->bindParam(":reminder", $this->reminder);
		$stmt->bindParam(":noti1", $this->noti1);
		$stmt->bindParam(":noti2", $this->noti2);
		$stmt->bindParam(":noti3", $this->noti3);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);
		
		if($stmt->execute()){
			return true;
		}
		return false;
	}

	// function update(){
	// 	$query = "UPDATE  " . $this->table_name . " SET expired=:expired, reminder=:reminder, 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(":expired", $this->expired);
	// 	$stmt->bindParam(":reminder", $this->reminder);
	// 	$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(){
		$query = "SELECT * FROM " . $this->table_name . " ORDER BY id";
		$stmt = $this->conn->prepare( $query );
		$stmt->execute();
		return $stmt;
	}

	function getMsg($m){
		$query = "SELECT $m FROM " . $this->table_name . " where `default`='Yes'";
		$stmt = $this->conn->prepare( $query );
		if($stmt->execute()){
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			return $row[$m];
		}
	}

	function getMsgForPromo(){
		$query = "SELECT promo_days_mbps FROM " . $this->table_name . " where `default`='Yes'";
		$stmt = $this->conn->prepare( $query );
		if($stmt->execute()){
			$row = $stmt->fetch(PDO::FETCH_ASSOC);
			return $row["promo_days_mbps"];
		}
	}

	// function getMsg(){
	// 	$query = "SELECT expired FROM " . $this->table_name . " where `default`='Yes'";
	// 	$stmt = $this->conn->prepare( $query );
	// 	if($stmt->execute()){
	// 		$row = $stmt->fetch(PDO::FETCH_ASSOC);
	// 		return $row['expired'];
	// 	}
	// }

	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 exist(){
	// 	$query = "SELECT * FROM " . $this->table_name . " WHERE name = ? and city = ? LIMIT 0,1";
	// 	$stmt = $this->conn->prepare( $query );
	// 	$this->name=htmlspecialchars(strip_tags($this->name));
	 
	// 	$stmt->bindParam(1, $this->name);
	// 	$stmt->bindParam(2, $this->city);
		
	// 	$stmt->execute();
	// 	if($stmt->rowCount()>0){
	// 		return true;
	// 	}
	// 	return false;
	// }
}
?>
