<?php
class SendMsg{
    private $conn;
    private $table_name = "send_msg";
 
	public $sales_id;
	public $id_no;
	public $cid;
	public $sms_type;
	public $phone;
	public $msg;
	public $entry_by;
	public $entry_date_time;
	
    public function __construct($db){
        $this->conn = $db;
    }
	


	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET sales_id=:sales_id, id_no=:id_no, cid=:cid, sms_type=:sms_type, phone=:phone, msg=:msg, entry_by=:entry_by, entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":sales_id", $this->sales_id);
		$stmt->bindParam(":id_no", $this->id_no);
		$stmt->bindParam(":cid", $this->cid);
		$stmt->bindParam(":sms_type", $this->sms_type);
		$stmt->bindParam(":phone", $this->phone);
		$stmt->bindParam(":msg", $this->msg);
		$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 . " SETexpired=:expired, reminder=:reminder, noti1=:noti1, noti2=:noti2, entry_by=:entry_by, entry_date_time=:entry_date_time WHERE id=:id";
		
	// 	$stmt = $this->conn->prepare($query);
		
	// 	$stmt->bindParam(":expired", $this->expired);
	// 	$stmt->bindParam(":reminder", $this->reminder);
	// 	$stmt->bindParam(":noti1", $this->noti1);
	// 	$stmt->bindParam(":noti2", $this->noti2);
	// 	$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 getAll(){
		// $condition = "";

		// if($this->statuss){
		// 	if($condition!=""){ $condition .= " AND "; }
		// 	$condition .= " sms_type=:statuss";
		// }
		$query = "SELECT send_msg.*,substring(send_msg.entry_date_time,12,8) as et,name as customer_name FROM " . $this->table_name . " 
		left join (SELECT * from (SELECT MAX(id) AS mxid FROM sales GROUP BY id_no) AS a
		LEFT JOIN sales ON a.mxid=sales.id) as b on send_msg.id_no=b.id_no
		where (send_msg.id_no LIKE  :city '%' or send_msg.id_no LIKE '%' :city '%' or send_msg.id_no Like '%' :city ) and substring(send_msg.entry_date_time,1,10)=:date " . (($this->statuss=="")?"":" and sms_type='$this->statuss'") . " ORDER BY send_msg.id";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":date", $this->date);
		// if($this->statuss)  $stmt->bindParam(":statuss", $this->statuss);
		$stmt->execute();
		return $stmt;
	}

	// function getMsg(){
	// 	$query = "SELECT * FROM " . $this->table_name . " where `default`='Yes'";
	// 	$stmt = $this->conn->prepare( $query );
	// 	$stmt->execute();
	// 	return $stmt;
	// }

	// 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;
	// }
}
?>
