<?php
class Agent {

    private $conn;
    private $table_name = "agent";

    public $id;
    public $agent_name; 
    public $commission; 
    public $phone; 
    public $email; 
    public $address; 
    public $remark; 
    public $inactive; 
    public $entry_by; 
    public $update_by; 
    
    

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET agent_name = :agent_name,
                      commission = :commission, phone = :phone,
                      email = :email, address = :address,
                      remark = :remark, inactive = :inactive,
                      entry_by = :entry_by";

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

        $stmt->bindParam(":agent_name", $this->agent_name);
        $stmt->bindParam(":commission", $this->commission);
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":address", $this->address);
        $stmt->bindParam(":remark", $this->remark);
        $stmt->bindParam(":inactive", $this->inactive);
        $stmt->bindParam(":entry_by", $this->entry_by);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    function update() {
        $query = "UPDATE " . $this->table_name . " 
                  SET agent_name = :agent_name,
                      commission = :commission, phone = :phone,
                      email = :email, address = :address,
                      remark = :remark, inactive = :inactive, update_by=:update_by  
                  WHERE id=:id";
        $stmt = $this->conn->prepare($query);

        $stmt->bindParam(":id", $this->id);
        $stmt->bindParam(":agent_name", $this->agent_name); 
        $stmt->bindParam(":commission", $this->commission);
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":address", $this->address);
        $stmt->bindParam(":remark", $this->remark);
        $stmt->bindParam(":inactive", $this->inactive);
        $stmt->bindParam(":update_by", $this->update_by);

        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    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 getOneRow() {
        $query = "SELECT * FROM " . $this->table_name . "  WHERE id = ?";
        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($row) {
            $this->id = $row['id'];
            $this->agent_name = $row['agent_name'];
            $this->commission = $row['commission'];
            $this->phone = $row['phone'];
            $this->email = $row['email'];
            $this->address = $row['address'];
            $this->remark = $row['remark'];
            $this->inactive = $row['inactive'];
        }
    }

    function getAllRows() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY agent_name";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }

    // public function getRoomByType($roomTypeId) {
    //     $query = "SELECT agent_name, email, commission
    //               FROM " . $this->table_name . " 
    //               LEFT JOIN contact_person ON room.contact_person_id = contact_person.id 
    //               WHERE room.contact_person_id = :contact_person_id 
    //               ORDER BY agent_name";
    
    //     $stmt = $this->conn->prepare($query);
    //     $stmt->bindParam(':contact_person_id', $roomTypeId);
    //     $stmt->execute();
    //     return $stmt;
    // }

    function exist() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE agent_name = ? LIMIT 0,1";
        $stmt = $this->conn->prepare($query);
        $this->agent_name = htmlspecialchars(strip_tags($this->agent_name));

        $stmt->bindParam(1, $this->agent_name);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            return true;
        }
        return false;
    }
}
?>
