<?php 
class Product{
	public $tbName;
	
	public function __construct($tbName) {
		require_once("class/connectdb.php");
		$con = new DbConnect;
		$this->tbName = $tbName;
		$this->con = $con->connect();
		
		$this->tbName = $tbName;
	}
	
	public function uploadProduct($product_id,$barcode,$main_image,$other_images,$product_name,$cost,$price,$quantity,$formulation,$category,$sub_category_id,$company,$package,$tag,$product_description,$user_id){
		$other_images = implode(',', $other_images);
		$sql  = mysqli_query($this->con, "INSERT INTO $this->tbName (
		product_id, barcode, main_image_url, other_images_url, product_name, cost, price, current_stock_level, formulation_id, category_id, sub_category_id, company_id, package_id, tag, description, availability_status, user_id) 
					 VALUES (
		'$product_id', '$barcode', '$main_image', '$other_images', '$product_name', $cost, $price, $quantity, $formulation, $category, $sub_category_id, $company, $package, '$tag', '$product_description', 1, $user_id)
				");
		return $sql;
	}
	
	public function isUploadedProduct($product_name){
		$query = mysqli_query($this->con, "SELECT * FROM $this->tbName WHERE product_name = '$product_name' ");
	    $count = mysqli_num_rows($query);
		if($count == 0){
			return false;
		}else{
			return true;
		}
	}
	
	public function updateProduct($main_image,$other_images,$product_name,$barcode,$formulation,$category,$sub_category_id,$company,$package,$tag,$product_info,$user_id,$product_id){
		$other_images = implode(',', $other_images);
		if($main_image == '' && $other_images == ''){
			$sql  = mysqli_query($this->con, "UPDATE $this->tbName SET product_name = '$product_name', barcode = '$barcode', formulation_id = '$formulation', 
                    category_id = '$category', sub_category_id = '$sub_category_id', company_id = '$company', package_id = '$package', tag = '$tag',
				    product_info = '$product_info', user_id = $user_id WHERE product_id = '$product_id' "); 
             
			        return $sql;
		}elseif($main_image != '' && $other_images == ''){
			$sql  = mysqli_query($this->con, "UPDATE $this->tbName SET main_image_url = '$main_image', product_name = '$product_name', barcode = '$barcode', formulation_id = '$formulation',
                   category_id = '$category', sub_category_id = '$sub_category_id', company_id = '$company', package_id = '$package', tag = '$tag',
				   product_info = '$product_info', user_id = $user_id WHERE product_id = '$product_id' "); 
             
			    return $sql;
		}elseif($main_image == '' && $other_images != ''){
			$sql  = mysqli_query($this->con, "UPDATE $this->tbName SET other_images_url = '$other_images', product_name = '$product_name', barcode = '$barcode', formulation_id = '$formulation',
                   category_id = '$category', sub_category_id = '$sub_category_id', company_id = '$company', package_id = '$package', tag = '$tag',
				   product_info = '$product_info', user_id = $user_id WHERE product_id = '$product_id' "); 
             
			    return $sql;
		}else{
		    $query  = mysqli_query($this->con, "UPDATE $this->tbName SET main_image_url = '$main_image', other_images_url = '$other_images', product_name = '$product_name', barcode = '$barcode', formulation_id = '$formulation',
		           	   category_id = '$category', sub_category_id = '$sub_category_id', company_id = '$company', package_id = '$package', tag = '$tag',
					   product_info = '$product_info', user_id = $user_id WHERE product_id = '$product_id' "); 
				 
					return $query;
		}
	}
	
    public function addToStock($qty_to_add, $product_id){
		$query = mysqli_query($this->con, "SELECT * FROM $this->tbName WHERE product_id = '$product_id' ");
		$row = mysqli_fetch_array($query);
		$current_stock = $row['current_stock_level'];
		$new_stock = $current_stock + $qty_to_add;
		
		$sql = mysqli_query($this->con, "UPDATE $this->tbName SET current_stock_level = '$new_stock' WHERE product_id = '$product_id' ");
		return $sql;
	}
	
	public function trans($tbName, $resRow, $rName, $r_id){
		$query = mysqli_query($this->con, "SELECT * FROM $tbName WHERE $rName = $r_id  ");
		$row = mysqli_fetch_array($query);
		
		return $row[$resRow];
	}
	 
	
	 
	
	
}


?>