<?php
/**
 * Model for Payment
 *
 * @author Mills ABeiku Godfred
 */
class Payment
{
	
	public $conn;
	

	
	function __construct()
	{
		$this->conn = Database::connect();
    }
    
    public function add_payment(array $data){
		$sql = "INSERT INTO payment ";
		$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 update_payment(array $key_col_vals, array $values) {
        
        $sql = "UPDATE payment 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 get_payment_by_name($payment_name){
		$sql = "SELECT * FROM payment WHERE payment_name LIKE '%".$payment_name."%'";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}
	
	
	public function read_all_payments(){
		$sql = "SELECT a.*,b.*,c.* FROM payment a JOIN student b ON a.payer=b.student_id 
		JOIN payment_type c ON a.payment_type=c.payment_type_id	";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

    public function read_payment($payment_id){
		$sql = "SELECT a.*,b.*,c.* FROM payment a JOIN student b ON a.payer=b.student_id 
		JOIN payment_type c ON a.payment_type=c.payment_type_id WHERE a.payment_id=:payment_id";
		$statement = $this->conn->prepare($sql);
        $statement->bindParam(":payment_id",$payment_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}

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

}