package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
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.CenterInfo;
import main_package.DataBaseConnection;
import main_package.DataBaseMethods;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

@ViewScoped
@ManagedBean(name="viewCenterDetailListJson")
public class ViewCenterDetailListJson implements Serializable
{
	String json;
	Date startDate, endDate;
	DataBaseMethods db=new DataBaseMethods();
	ArrayList<CenterInfo> center_info;
	
	public ViewCenterDetailListJson() throws ParseException 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
		String userId=params.get("userId");
		String branchId=params.get("branchId");
		
		if(userId.contains("'"))
		{
			userId="";
		}
		
		
		Connection conn=DataBaseConnection.JAVAConnection();
		
		JSONObject mainobj = new JSONObject();
		JSONArray arr=new JSONArray();
		
		String userType=db.userTypeByUserId(userId, conn);
		if(userType.equals("admin") || userType.equals("masterAdmin"))
		{
			center_info = db.allCenterInfo(Integer.parseInt(branchId),"all",conn);
		}
		else
		{
			String dsaCode=db.dsaCodeByEmpUserId(userId, conn);
			center_info = db.allCenterInfo(Integer.parseInt(branchId),dsaCode,conn);
		}
			
		if(center_info.size()>0)
	     {
			int totalLoan=0, totalCenter=0;
			for(CenterInfo cc: center_info)
			{
				totalLoan+=cc.getTotalLoan();
			}
			totalCenter=center_info.size();
			
			for(CenterInfo cc : center_info)
			   {
				   JSONObject obj = new JSONObject();
				   
				 
				   obj.put("centerCode",cc.getCenterCode());
				   obj.put("centerName", cc.getCenterName());
				   obj.put("description",cc.getDescription());
				   obj.put("groupNo", cc.getGroupNo());
				   obj.put("dsaCode", cc.getDsacode() + " / " + cc.getDsaName());
				   obj.put("activeLoan", cc.getTotalLoan());
				   
				   obj.put("totalLoan", totalLoan);
				   obj.put("totalCenter", totalCenter);
				   
				   
				   arr.add(obj);
			   }
	     }
		
		

	    mainobj.put("track", arr);

	json=mainobj.toJSONString();
	
		//int i=new DatabaseMethods1().news(concern);
	try {
		conn.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	
	
	
	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;
	}
	
}	
