package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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.DataBaseMethods;
import main_package.DataBaseConnection;
import main_package.UserInfo;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import report_package.DataBaseReportMethods;

@ManagedBean(name="upcomingEmiJson")
@ViewScoped
public class UpcomingEmiJsonBean implements Serializable
{
	String json;
	Date startDate;
	Date endDate;
	public UpcomingEmiJsonBean() throws ParseException 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
        
		String sDate=params.get("startDate");
		String eDate=params.get("endDate");
		String category=params.get("category");
		String branchStr = params.get("branchId");
		int branchId = Integer.valueOf(branchStr);
		
		if(category.contains("'") || sDate.contains("'") || eDate.contains("'"))
		{
			category="";
			sDate="";
			eDate="";
		}
		/*String mobile=params.get("mobile");
		String pwd=params.get("pwd");*/
		String[] selectedCenter = null; ArrayList<SelectItem> centerList = null;
		
		System.out.println(sDate+"================"+category+"----------"+eDate);
		
		ArrayList<UserInfo> list=new ArrayList<>();
		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		/*if(sDate!=null && eDate!=null && !sDate.equals("") && !eDate.equals(""))
		{
			startDate = (Date)formatter.parse(sDate);
			endDate = (Date)formatter.parse(eDate);
			
		}*/
		startDate = (Date)formatter.parse(sDate);
		endDate = (Date)formatter.parse(eDate);
		Connection conn=DataBaseConnection.JAVAConnection();
		list=new DataBaseReportMethods().allUpcomingEMI(branchId,startDate,endDate,category,conn,selectedCenter,centerList, false, false);
		System.out.println("list:   "+list.size());
		if(category==null)
		{
			
		}
		else
		{
			
		}
		
     JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();
				
			if(list.size()>0)
		     {
		    	 for(UserInfo uu : list)
				   {
					   JSONObject obj = new JSONObject();
					   
					   obj.put("sno",uu.getSno());
					   obj.put("shareHolderNo",uu.getShareHolderNo());
					   obj.put("applicantName",uu.getFirst_applicantName());
					   obj.put("contactNo",uu.getContactNo());
					   obj.put("type",uu.getChq_ddNo());
					   obj.put("loanOrRdNo",uu.getLoanNo());
					   obj.put("date",uu.getEntryDateStr());
					   obj.put("amount",uu.getAmount());
					   
					   arr.add(obj);
				   }
					try {
						conn.close();
					} catch (Exception e) {
						// TODO: handle exception
					}
		     }

		    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;
	}
}
