package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;


@ManagedBean(name="products")
@ViewScoped
public class productBean  implements Serializable{
	
	public String brand,name,model;
	DbYashwant dby= new DbYashwant();
	ArrayList<productInfo>productList;
	
	productInfo selected;
	
	String categoryType;
	ArrayList<SelectItem> categoryTypeList;
	DataBaseMethodsRaviK dbrk = new DataBaseMethodsRaviK();
	
	
	public productBean() {
		Connection conn = DataBaseConnection.JAVAConnection();
		categoryTypeList = dbrk.categoryTypeListt(conn);
		selectData();
	}
	
	public void add()
	{
		Connection conn = DataBaseConnection.JAVAConnection();
		// int i=dby.addProduct(conn,brand,name,model);
		int i=dby.addProduct(conn,brand,name,model,categoryType);
		 
		 if(i>0)
		 {
			 reset();
			 FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("Product Add Sucessfully"));
		 }
		 selectData();
	}
	
	
	
	public void selectData()
	{
		
		Connection conn = DataBaseConnection.JAVAConnection();
		productList=dby.selectProduct(conn);
	}
	
	
	public void update()
	{   
		Connection conn =DataBaseConnection.JAVAConnection();
		
		int i=dby.editProduct(conn, selected);
		if(i>0)
		{
			selectData();
			 FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("Updated Sucessfully"));
		 }
		
	}
	
	
	public void delete()
	{
		
		Connection conn =DataBaseConnection.JAVAConnection();
		int i=dby.deleteProduct(conn, selected);
		
		if(i>0)
		{
			 FacesContext fc=FacesContext.getCurrentInstance();
				fc.addMessage(null, new FacesMessage("Product Deleted Sucessfully"));
				selectData();
		}
		
	}
	
	public void reset()
	{
		//brand=name=model="";
		brand=name=model=categoryType="";
	}
	


	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}



	public ArrayList<productInfo> getProductList() {
		return productList;
	}



	public void setProductList(ArrayList<productInfo> productList) {
		this.productList = productList;
	}

	public productInfo getSelected() {
		return selected;
	}

	public void setSelected(productInfo selected) {
		this.selected = selected;
	}

	public String getCategoryType() {
		return categoryType;
	}

	public void setCategoryType(String categoryType) {
		this.categoryType = categoryType;
	}

	public ArrayList<SelectItem> getCategoryTypeList() {
		return categoryTypeList;
	}

	public void setCategoryTypeList(ArrayList<SelectItem> categoryTypeList) {
		this.categoryTypeList = categoryTypeList;
	}
	
	

}
