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

	// object properties

	public $id;
	public $city;
	public $suspend_date;
	public $unsuspend_date;
	public $id_no;
	public $customer_id;
	public $invoice_no;
	public $remark;
	public $days;
	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, suspend_date=:suspend_date, id_no=:id_no, customer_id=:customer_id, invoice_no=:invoice_no, remark=:remark, entry_by=:entry_by, entry_date_time=:entry_date_time";

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

		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":suspend_date", $this->suspend_date);
		$stmt->bindParam(":id_no", $this->id_no);
		$stmt->bindParam(":customer_id", $this->customer_id);
		$stmt->bindParam(":invoice_no", $this->invoice_no);
		$stmt->bindParam(":remark", $this->remark);
		$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 delete(){
		$query = "UPDATE " . $this->table_name . " set is_deleted=1 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 getAllRows(){
		// $condition = "";

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

		// if($condition!=""){
		// 	$condition = " WHERE " . $condition;
		// }
		$query = "SELECT suspend.*,customer.name,phone FROM suspend LEFT JOIN sales ON suspend.id_no=sales.id_no and suspend.invoice_no=sales.invoice_no LEFT JOIN customer ON sales.customer_id=customer.id
        where suspend.city=:city and suspend.is_deleted=0 ";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":city", $this->city);

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

	function update(){
		$query = "UPDATE " . $this->table_name . " SET unsuspend_date=:unsuspend_date,days=:days where id=:id";
		$stmt = $this->conn->prepare($query);


		$stmt->bindParam(":unsuspend_date", $this->unsuspend_date);
		$stmt->bindParam(":days", $this->days);
		$stmt->bindParam(":id", $this->id);

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

}
?>