package json_package;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
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.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.LoanInfo;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import report_package.DataBaseReportMethods;

@ManagedBean(name="bulkDepositDetailJson")
@ViewScoped
public class BulkDepositDetailJsonBean implements Serializable
{
	String json;
	Date payDate;
	int branchId;
	
	public BulkDepositDetailJsonBean() 
	{
		Map<String, String> params =FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap();
       
		String branchStr = params.get("branchId");
		branchId = Integer.valueOf(branchStr);
		String sDate = params.get("sDate");
		String searchBase = params.get("searchBase");
		String searchType = params.get("searchType");
		String dsaCode = params.get("dsaCode");
		

		 if(searchBase.contains("'") || sDate.contains("'") || searchType.contains("'"))
				{
			 searchBase="";
					sDate="";
				}

		   ArrayList<LoanInfo> allDDList = null;
		   DataBaseMethods dbm=new DataBaseMethods();
		   
		   
			DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			if(sDate!=null && !sDate.equals(""))
			{
				try {
					payDate = (Date)formatter.parse(sDate);
					allDDList=dbm.allDDList(dsaCode, searchType, payDate, "-1", searchBase, branchId);
					
					
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
	
        
        
  
     JSONObject mainobj = new JSONObject();
			JSONArray arr=new JSONArray();
			Connection conn=DataBaseConnection.JAVAConnection();
		     if(allDDList.size()>0)
		     {
		    	 for(LoanInfo uu : allDDList)
				   {
					   JSONObject obj = new JSONObject();
					   
					   obj.put("sno",uu.getSno());
					   obj.put("shareHolderNo", uu.getShareHolderNo());
					   obj.put("depositNo", uu.getUserId());
					   obj.put("name",uu.getApplicantName());
					   obj.put("totalAmount",uu.getTotalAmountToBePaid());
					   obj.put("totalPaid",uu.getAmountPaid());
					   obj.put("totalLeft",uu.getAmountLeftPaid());
					   obj.put("tillLeft",uu.getTillLeft());
					   obj.put("installAmount",uu.getInstallAmount());
					   obj.put("address",uu.getAddress());
					   obj.put("fatherHusbandName",uu.getFatherHusbandName());
					   obj.put("contactNo",uu.getContactNo());
					   
					   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);
		
		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;
	}
}
