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

	
	function __construct()
	{
		$this->conn = Database::connect();
    }
    
    public function add_item(array $data){
		$sql = "INSERT INTO item ";
		$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_item(array $key_col_vals, array $values) {
        
        $sql = "UPDATE item 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_item_by_name($item_name){
		$sql = "SELECT * FROM item WHERE item_name LIKE '%".$item_name."%'";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
	}
	
	
	public function read_all_items(){
		$sql = "SELECT a.*,b.*,c.*,d.*,e.* FROM item a 
        JOIN item_category b ON a.item_category_id=b.item_category_id 
        JOIN item_level c ON a.item_level_id=c.item_level_id 
        JOIN supplier d ON a.item_supplier_id=d.supplier_id 
		JOIN item_type e ON a.item_type_id=e.item_type_id ";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function read_all_low_inventory_items(){
		$sql = "SELECT a.*,b.*,c.*,d.*,e.* FROM item a 
        JOIN item_category b ON a.item_category_id=b.item_category_id 
        JOIN item_level c ON a.item_level_id=c.item_level_id 
        JOIN supplier d ON a.item_supplier_id=d.supplier_id 
		JOIN item_type e ON a.item_type_id=e.item_type_id 
		WHERE a.item_reorder_level>=a.item_quantity";
		$statement = $this->conn->prepare($sql);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function read_all_items_for_request($item_category_id,$item_level_id,$item_type_id){
		$sql = "SELECT a.*,b.*,c.*,d.*,e.* FROM item a 
        JOIN item_category b ON a.item_category_id=b.item_category_id 
        JOIN item_level c ON a.item_level_id=c.item_level_id 
        JOIN supplier d ON a.item_supplier_id=d.supplier_id 
		JOIN item_type e ON a.item_type_id=e.item_type_id 
		WHERE a.item_category_id=:item_category_id 
		AND a.item_level_id=:item_level_id 
		AND a.item_type_id=:item_type_id";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":item_category_id",$item_category_id);
		$statement->bindParam(":item_level_id",$item_level_id);
		$statement->bindParam(":item_type_id",$item_type_id);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

	public function read_all_items_from_invoice($invoice_no){
		$sql = "SELECT z.*,a.*,b.*,c.*,d.*,e.* FROM item_request_order z 
		JOIN item a ON z.item_id=a.item_id 
        JOIN item_category b ON a.item_category_id=b.item_category_id 
        JOIN item_level c ON a.item_level_id=c.item_level_id 
        JOIN supplier d ON a.item_supplier_id=d.supplier_id 
		JOIN item_type e ON a.item_type_id=e.item_type_id 
		WHERE z.item_request_id=:invoice_no ";
		$statement = $this->conn->prepare($sql);
		$statement->bindParam(":invoice_no",$invoice_no);
		$statement->execute();
		if ($statement->rowCount() > 0) {
			return $statement->fetchAll(PDO::FETCH_ASSOC);
		}
		return false;
    }

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

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

}