<?php 
/**
 * Model for Employees
 *
 * @author Mills ABeiku Godfred
 */
class Employee
{
	
	public $conn;
	
	function __construct()
	{
		$this->conn = Database::connect();
	}

    public function update_employee(array $key_col_vals, array $values) {
        $sql = "UPDATE employee SET ";
        $fields = array_keys($values);
        $vals = array_values($values);
        $key_cols = array_keys($key_col_vals);
        $key_vals = array_values($key_col_vals);
        foreach ($fields as $i=>$f) {
            $fields[$i] .= " = ? ";
        }

        $sql .= implode(",", $fields);
        $sql .= " WHERE " ;
        
        foreach ($key_cols as $i=>$f) {
            $key_cols[$i] .= " = ? ";
        }
        
        $sql .= implode(" AND ", $key_cols);

        $statement = $this->conn->prepare($sql);
        foreach ($vals as $i=>$v) {
            $statement->bindValue($i+1, $v);
        }
        
        foreach ($key_vals as $i=>$v) {
           if($i==0){
               $statement->bindValue((count($fields)+$i+1), $v);
            }else{
               $statement->bindValue((count($fields)+$i), $v);
            }
        }
        
       return  $statement->execute();

	}


	public function add_employee(array $data){
		$sql = "INSERT INTO employee ";
		$fields = array_keys($data);
		$vals = array_values($data);

		$sql .= "(".implode(",", $fields).")";
		$arr = array();
		foreach ($fields as $f) {
			$arr[] = "?";
		}
		$sql .= " VALUES(".implode(",", $arr).")";

		$statement = $this->conn->prepare($sql);

		foreach ($vals as $i => $v) {
			$statement->bindValue($i + 1, $v);
		}

		return $statement->execute();

	}
  
	
	public function read_all_employees(){
		$sql = "SELECT a.*,b.*,c.* FROM employee a JOIN login b 
        ON a.employee_phone=b.phone || a.employee_email=b.email 
        JOIN role c ON a.employee_role_id=c.role_id";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function read_user($employee_id){
		$sql = "SELECT * FROM employee WHERE employee_id=:employee_id";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":employee_id",$employee_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function read_user_reward($employee_id,$current_month){
		$sql = "SELECT a.*,b.*,c.* FROM employee a 
		JOIN item_request b ON a.employee_id=b.item_requested_by 
		JOIN achievement c ON b.amount_payed>=c.achievement_point 
		WHERE a.employee_id=:employee_id AND b.item_request_approved='Yes' 
		AND MONTH(item_approval_datetime)=:current_month ";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":employee_id",$employee_id);
		$statement->bindParam(":current_month",$current_month);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function get_employee_by_name($employee_name){
		$sql = "SELECT * FROM employee WHERE employee_name LIKE '%".$employee_name."%'";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

	public function read_employee_by_phone($phone){
		$sql = "SELECT * FROM employee WHERE employee_phone=:phone";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":phone",$phone);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function read_employee_by_role_id($role_id){
		$sql = "SELECT * FROM employee WHERE employee_role_id=:role_id";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":role_id",$role_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

    // public function check_username($username){
	// 	$sql = "SELECT a.*,b.* FROM employee a JOIN employee b 
    //     ON a.phone=b.employee_phone || a.email=b.employee_email 
    //     JOIN role c ON b.employee_role_id=c.role_id
    //      WHERE a.email=:username || a.phone=:username";
	// 	$statement = $this->conn->prepare($sql);
	// 	$statement->bindParam(":username",$username);
	// 	$statement->execute();
	// 	if ($statement->rowCount() > 0) {
	// 		return $statement->fetchAll(PDO::FETCH_ASSOC);
	// 	}
	// 	return false;
    // }

    public function remove_employee($employee_id){
		$sql = "DELETE FROM employee WHERE employee_id=:employee_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":employee_id",$employee_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
            $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

}