<?php
class Complaint{
    private $conn;
    private $table_name = "`complaint`";
 
	public $id;
	public $city;
	public $complaint_date;
	public $complaint;
	public $particular;
	public $customer_id;
	public $id_no;
	public $name;
	public $entry_by;
	public $entry_date_time;
	public $answer_date;
	public $answer_by;
	public $remark;
	public $answer_entry_by;
	public $answer_date_time;

    public function __construct($db){
        $this->conn = $db;
    }

	function create(){
		$query = "INSERT INTO " . $this->table_name . " SET city=:city, complaint_date=:complaint_date, complaint=:complaint, particular=:particular,customer_id=:customer_id, id_no=:id_no, name=:name, entry_by=:entry_by,  entry_date_time=:entry_date_time";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":city", $this->city);
		$stmt->bindParam(":complaint_date", $this->complaint_date);
		$stmt->bindParam(":complaint", $this->complaint);
		$stmt->bindParam(":particular", $this->particular);
		$stmt->bindParam(":customer_id", $this->customer_id);
		$stmt->bindParam(":id_no", $this->id_no);
		$stmt->bindParam(":name", $this->name);
		$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 getAllRows(){
		$condition = "";

		// if($this->df){

		// 	$condition .= " and complaint_date>=:df ";
		// }
		// if($this->dt){

		// 	$condition .= " and complaint_date<=:dt ";
		// }
		if($this->status){
			if($this->status=='Answer'){

				$condition .= " and answer_date<>'' ";
			}else if($this->status=='No Answer'){

				$condition .= " and answer_date is null";
			}
		}
		// if($this->id_no){

		// 	$condition .= " and id_no=:id_no ";
		// }
		// if($condition!=""){
		// 	$condition = " AND " . $condition;
		// }

		$query = "SELECT complaint.*,phone,answer_date,answer_by,remark,answer_entry_by,answer_date_time FROM complaint LEFT JOIN sales ON complaint.id_no=sales.id_no LEFT JOIN customer ON sales.customer_id=customer.id
		LEFT JOIN (SELECT * from complaint_detail right JOIN (SELECT MAX(id) AS mxid FROM complaint_detail GROUP BY complaint_id) AS a ON complaint_detail.id=a.mxid) AS cd ON complaint.id=cd.complaint_id
        GROUP BY complaint.id HAVING complaint.city=:city and complaint.is_deleted=0 " . $condition . " order by complaint_date desc";
		$stmt = $this->conn->prepare( $query );
		$stmt->bindParam(":city", $this->city);
		// if($this->df) $stmt->bindParam(":df", $this->df);
		// if($this->dt) $stmt->bindParam(":dt", $this->dt);
		// if($this->id_no) $stmt->bindParam(":id_no", $this->id_no);
		$stmt->execute();
		return $stmt;
	}

	function update(){
		$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 updateR(){
		$query = "UPDATE  " . $this->table_name . " SET city=:city, complaint_date=:complaint_date, complaint=:complaint, particular=:particular,customer_id=:customer_id, id_no=:id_no, name=:name, 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(":city", $this->city);
		$stmt->bindParam(":complaint_date", $this->complaint_date);
		$stmt->bindParam(":complaint", $this->complaint);
		$stmt->bindParam(":particular", $this->particular);
		$stmt->bindParam(":customer_id", $this->customer_id);
		$stmt->bindParam(":id_no", $this->id_no);
		$stmt->bindParam(":name", $this->name);
		$stmt->bindParam(":entry_by", $this->entry_by);
		$stmt->bindParam(":entry_date_time", $this->entry_date_time);
		
		if($stmt->execute()){
			return true;
		}
		return false;
	}
	
	// function updateAnswer(){
	// 	$query = "UPDATE " . $this->table_name . " set answer_date=:answer_date,answer_by=:answer_by,remark=:remark,answer_entry_by=:answer_entry_by,answer_date_time=:answer_date_time WHERE id =:id";
	// 	$stmt = $this->conn->prepare($query);
	// 	$this->id = htmlspecialchars(strip_tags($this->id));
	// 	$stmt->bindParam(":id", $this->id);
	// 	$stmt->bindParam(":answer_date", $this->answer_date);
	// 	$stmt->bindParam(":answer_by", $this->answer_by);
	// 	$stmt->bindParam(":remark", $this->remark);
	// 	$stmt->bindParam(":answer_entry_by", $this->answer_entry_by);
	// 	$stmt->bindParam(":answer_date_time", $this->answer_date_time);
	// 	if($stmt->execute()){
	// 		return true;
	// 	}
	// 	return false;
	// }
	
}
?>
