package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.DataBaseMethodsSalary;
import main_package.EmpInfo;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@ViewScoped
@ManagedBean(name="empListJson")

public class EmpListJsonBean implements Serializable
{
	String json;
	Date startDate, endDate;
	
	ArrayList<EmpInfo> list;
	
	DataBaseMethods obj = new DataBaseMethods();
	DataBaseMethods dbm = new DataBaseMethods();
	
	
	
	public EmpListJsonBean() throws ParseException 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
       
		String branchStr = params.get("branchId");
		int branchId = Integer.valueOf(branchStr);
		ArrayList<SelectItem> employeeList=new DataBaseMethodsSalary().employeeListForSalary(branchId,"");
		
		JSONObject mainobj = new JSONObject();
		JSONArray arr=new JSONArray();
		
		if(employeeList.size()>0)
	     {
			Connection conn = DataBaseConnection.JAVAConnection();
			
	    	 for(SelectItem uu : employeeList)
	    	 {
	    		 JSONObject obj = new JSONObject();
				   
	    		 String dsaCode = dbm.dsaCodeByEmpUserId((String) uu.getValue(), conn);
				 
	    		 obj.put("empLabel",uu.getLabel());
	    		 obj.put("empValue", uu.getValue());
	    		 obj.put("dsaCode", dsaCode);
			   
	    		 arr.add(obj);
	    	 }
	    	 
	    	 try 
	    	 {
	    		 conn.close();
				
	    	 } 
	    	 catch (Exception e) 
	    	 {
	    		e.printStackTrace(); 
	    	 }
	     }

	    mainobj.put("track", arr);

	json=mainobj.toJSONString();
	
		//int i=new DatabaseMethods1().news(concern);
		  
	}
	
	
	
	public void renderJson() throws IOException {
 	    FacesContext facesContext = FacesContext.getCurrentInstance();
 	    ExternalContext externalContext = facesContext.getExternalContext();
 	    externalContext.setResponseContentType("application/json");
 	    externalContext.setResponseCharacterEncoding("UTF-8");
 	    externalContext.getResponseOutputWriter().write(json);
 	    facesContext.responseComplete();
 	    
 	    
 	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}
}