<?php
class Investor {

    private $conn;
    private $table_name = "investor";

    public $id;
    public $name; 
    public $position; 
    public $phone; 
    public $email; 
    public $nrc; 
    public $address; 
    public $entry_by; 
    public $update_by; 
    
    public $start_date; 
    public $end_date; 

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

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET name = :name, position = :position, 
                      phone = :phone, email = :email,
                      nrc = :nrc, address = :address,
                      entry_by = :entry_by";

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

        $stmt->bindParam(":name", $this->name);
        $stmt->bindParam(":position", $this->position); 
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":nrc", $this->nrc);
        $stmt->bindParam(":address", $this->address);
        $stmt->bindParam(":entry_by", $this->entry_by);

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

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

        $stmt->bindParam(":id", $this->id);
        $stmt->bindParam(":position", $this->position); 
        $stmt->bindParam(":phone", $this->phone);
        $stmt->bindParam(":email", $this->email);
        $stmt->bindParam(":nrc", $this->nrc);
        $stmt->bindParam(":address", $this->address);
        $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 getAllRows() {
        $query = "SELECT investor.*,IFNULL(total,0) as investment FROM investor
LEFT JOIN (SELECT investor_id,SUM(amount) AS total FROM investment GROUP BY investor_id) AS a ON
investor.id=a.investor_id order by name";
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        return $stmt;
    }


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

        $stmt->bindParam(1, $this->name);
        $stmt->execute();
        if ($stmt->rowCount() > 0) {
            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->name = $row['name'];
            $this->position = $row['position'];
            $this->phone = $row['phone'];
            $this->email = $row['email'];
            $this->nrc = $row['nrc'];
            $this->address = $row['address'];
        }
    }

}
?>
