package main_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;
import javax.servlet.http.HttpSession;

@ManagedBean(name="extraPermissionManage")
@ViewScoped
public class ExtraPermissionManageBean  implements Serializable
{
	DataBaseMethods obj1=new DataBaseMethods();
	String empType;

	ArrayList<SelectItem> empList= new ArrayList<>();
	ArrayList<UserInfo> pageList= new ArrayList<>();
	
public ExtraPermissionManageBean()
{
	HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	 int branchId = (int) ss.getAttribute("branchId");
   Connection con = DataBaseConnection.JAVAConnection();
   empList=obj1.employeeTypeList(branchId,con);
//   pageList = obj1.pageList("",con);
  
   try
   {
	con.close();
   } 
   catch (Exception e) 
   {
	// TODO: handle exception
   }
}
public void checkEmpAssign()
{
	
	  Connection con = DataBaseConnection.JAVAConnection();
	  pageList = obj1.pageList(empType,con);
	  try
	   {
		con.close();
	   } 
	   catch (Exception e) 
	   {
		// TODO: handle exception
	   }
}

public void submit()
{

	if(empType != null && !empType.isEmpty())
	{
		 insertPermission();
//		for(int i=0;i<pageList.size();i++)
//		{
//			
//		   if(pageList.get(i).getDeleteValue().equals("false") && pageList.get(i).getEditValue().equals("false"))
//		   {
//			   if(i==pageList.size()-1)
//			   {
//				   FacesContext fc=FacesContext.getCurrentInstance();
//			       fc.addMessage(null, new FacesMessage("Please Select Some Page Name"));	   
//			   }
//		   }
//		   else
//		   {
//			   insertPermission();
//			   break;
//		   }
//		}
	}
	else
	{
			   FacesContext fc=FacesContext.getCurrentInstance();
		       fc.addMessage(null, new FacesMessage("Employee Can Not Be Empty"));	
	}
}

public void insertPermission()
{
	Connection con= DataBaseConnection.JAVAConnection();
	int val= obj1.addExtraPermission(empType,pageList,con);
	if(val>0)
	{
		FacesContext fc=FacesContext.getCurrentInstance();
       fc.addMessage(null, new FacesMessage("Permission Added Successfully"));
	}
	else
	{
		FacesContext fc=FacesContext.getCurrentInstance();
	       fc.addMessage(null, new FacesMessage("something went wrong!"));
	}
	
	try {
		con.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
}

public String getEmpType() {
	return empType;
}


public void setEmpType(String empType) {
	this.empType = empType;
}


public ArrayList<SelectItem> getEmpList() {
	return empList;
}


public void setEmpList(ArrayList<SelectItem> empList) {
	this.empList = empList;
}


public ArrayList<UserInfo> getPageList() {
	return pageList;
}


public void setPageList(ArrayList<UserInfo> pageList) {
	this.pageList = pageList;
}

}
