package json_package;

import java.io.IOException;
import java.io.Serializable;
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 main_package.InstallmentInfo;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import other_package.DataBaseMethodsOther;
import other_package.ReportInfo;

@ManagedBean(name="savingAccLedgerJson")
@ViewScoped

public class SavingAccLedgerJsonBean implements Serializable
{
	String json;
	Date startDate;
	Date endDate;
	public SavingAccLedgerJsonBean() throws ParseException 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
        
		//String shareId = params.get("userid");
		String accNo = params.get("accNo");
		String sDate=params.get("startDate");
		String eDate=params.get("endDate");
		//String type = params.get("loanType");
	//	String email=params.get("email");
		/*String mobile=params.get("mobile");
		String pwd=params.get("pwd");*/
		
		 if(accNo.contains("'") || sDate.contains("'") || eDate.contains("'"))
			{
		 accNo="";
				sDate="";
				eDate="";
			}
		 
		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);
		}
        
     ArrayList<ReportInfo> list=new ArrayList<>();
     list=new DataBaseMethodsOther().savingAccountReport(accNo,startDate,endDate);
     JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();

		     if(list.size()>0)
		     {
		    	 for(ReportInfo uu : list)
				   {
					   JSONObject obj = new JSONObject();
					   
					   obj.put("status",uu.getStatus());
					   obj.put("date",uu.getDateStr());
					   obj.put("debitAmount",uu.getCreditAmount());
					   obj.put("creditAmount",uu.getDebitAmount());
					   
					   arr.add(obj);
				   }
					
		     }
		    	 /*String status="";
		    	 if(i==1)
		    	 {
		    		 status="Registered Successfully";
		    	 }
		    	 else if(i==-1)
		    	 {
		    		 status="Please enter a unique email id.";
		    	 }
		    	 else
		    	 {
		    		status="Something went wrong.";
		    	 }*/
		    	
		    	  
			      

		    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;
	}
}
