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.DataBaseConnection;
import main_package.UserInfo;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import report_package.DataBaseReportMethods;

@ManagedBean(name="dueEmiJson")
@ViewScoped

public class DueEmiJsonBean implements Serializable
{
	String json;
	Date startDate=new Date();
	Date endDate=new Date();
	public DueEmiJsonBean() 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 mobile=params.get("mobile");
		String pwd=params.get("pwd");*/
		String[] selectedCenter = null; ArrayList<SelectItem> centerList = null;
		String branchStr = params.get("branchId");
		System.out.println("br str"+branchStr);
		int branchId = Integer.valueOf(branchStr);
	//	System.out.println(sDate+"================"+category+"----------"+eDate);
		
		if(category.contains("'"))
		{
			category="";
		}
		
		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);
		
		startDate.setYear(115);
		Connection conn=DataBaseConnection.JAVAConnection();
		list=new DataBaseReportMethods().allDueEMI(branchId,category, "all", "all",conn);
		//System.out.println("upper"+list.size());
		
     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());
					   
					 //  System.out.println(category);
					   arr.add(obj);
				   }
					try {
						conn.close();
					} catch (Exception e) {
						// TODO: handle exception
					}
		     }
		     
					
		    	 /*String status="";
		    	 if(i==1)
		    	 {
		    		 status="Registered Successfully";
		    	 }
		    	 else if(i==-1)
		    	 {
		    		 status="Please enter a unique email id.";
		    	 }
		    	 else
		    	 {
		    		status="Something went wrong.";
		    	 }*/
		    	
		    	  
			     // System.out.println("size: "+arr.size());

		    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;
	}
	
}
