package report_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import other_package.DataBaseMethodsOther;
import other_package.ReportInfo;
import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.DataBaseMethodsHemant;
import main_package.LoanInfo;

@ViewScoped
@ManagedBean(name="loanSpecialReport")

public class LoanSpecialReportBean implements Serializable
{
	String loanType,terms;
	LoanInfo selectedLoan;
	boolean showData;
	ArrayList<LoanInfo> list;
	ArrayList<ReportInfo> dailyPayList;
	ArrayList<ColumnModel> columns;
	ArrayList<SelectItem> emiTermsList, loanTermsList;
	DataBaseMethods db=new DataBaseMethods();
	double  penaltyAmount,totalChqCharge,paidChqCharge,penaltyPaid, penaltyDue, penaltyDiscount;
	int branchId;
	String branchUserId;
	List<SelectItem> branchList = new ArrayList<SelectItem>();
	DataBaseMethodsHemant dbmh = new DataBaseMethodsHemant();

	boolean disAll;
	String userType;
	
		
		
		
	    public LoanSpecialReportBean() 
	    {
	    	Connection con = DataBaseConnection.JAVAConnection();
			HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
			branchUserId = (String) ss.getAttribute("branchUserId");
			branchList = new DataBaseMethods().branchList(branchUserId,con);
			
			String cc=(String) ss.getAttribute("companyName");

			userType= (String) ss.getAttribute("type");
	        
			
		    disAll= dbmh.disAllByCompanyNameUserType(userType,cc);

			try {
				con.close();
			} catch (Exception e) {
				// TODO: handle exception
			}
	    	
	    }
	    

	public void findLoanTerms()
	{
		Connection conn=DataBaseConnection.JAVAConnection();
		loanTermsList=db.termsSelectItemListFormLoanTable(loanType,conn);
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void searchReport()
	{
		
		if(branchId != 0)
		{
			Connection conn=DataBaseConnection.JAVAConnection();
			showData=true;
			
			list=db.loanListTermsWise(branchId,loanType, terms, conn);
			emiTermsList=new ArrayList<>();
			
			double term=Double.valueOf(terms);
			for(int i=1;i<=(int)term; i++)
			{
				SelectItem ss=new SelectItem();
				
				ss.setLabel(String.valueOf(i));
				ss.setValue(i);
				
				emiTermsList.add(ss);
			}
			
			//System.out.println(itemList.size()+"========="+list.size());
			columns = new ArrayList<ColumnModel>(); 
			
			db.calculateLoanEMiRecord(emiTermsList, list, conn);
			
			for(SelectItem tt: emiTermsList)
			{
				ColumnModel cc=new ColumnModel();
				
				//System.out.println("Label: "+tt.getLabel());
				cc.setEmiNo(tt.getLabel());
				cc.setId(String.valueOf(tt.getValue()));
				
				columns.add(cc);
			}
			
			
			
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	else
		{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Select Branch"));
		}
	}
	
	
	public void printDailyPay()
	{
		dailyPayList= new ArrayList<>();
		dailyPayList= new DataBaseMethodsOther().dailyloanReport(selectedLoan.getLoanNo());
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		ss.setAttribute("penaltyAmount", penaltyAmount);
		ss.setAttribute("penaltyPaid", penaltyPaid);
		ss.setAttribute("penaltyDue", penaltyDue);
		ss.setAttribute("totalChqCharge", totalChqCharge);
		ss.setAttribute("paidChqCharge", paidChqCharge);
		ss.setAttribute("dailyPayList", dailyPayList);
		ss.setAttribute("selectedVal",selectedLoan);
		ss.setAttribute("operationVal", "loan");
		ss.setAttribute("showPen",true);
		RequestContext.getCurrentInstance().execute("window.open('printPerticularDatePayment.xhtml')");
		
	}

	static public class ColumnModel implements Serializable 
	{
		String emiNo;
        String id;
		
       
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		
		public String getEmiNo() {
			return emiNo;
		}
		public void setEmiNo(String emiNo) {
			this.emiNo = emiNo;
		}
		
		
	}

	public String getLoanType() {
		return loanType;
	}

	public void setLoanType(String loanType) {
		this.loanType = loanType;
	}

	public boolean isShowData() {
		return showData;
	}

	public void setShowData(boolean showData) {
		this.showData = showData;
	}

	public ArrayList<LoanInfo> getList() {
		return list;
	}

	public void setList(ArrayList<LoanInfo> list) {
		this.list = list;
	}

	public ArrayList<ColumnModel> getColumns() {
		return columns;
	}

	public void setColumns(ArrayList<ColumnModel> columns) {
		this.columns = columns;
	}

	

	public String getTerms() {
		return terms;
	}

	public void setTerms(String terms) {
		this.terms = terms;
	}

	public ArrayList<SelectItem> getLoanTermsList() {
		return loanTermsList;
	}

	public void setLoanTermsList(ArrayList<SelectItem> loanTermsList) {
		this.loanTermsList = loanTermsList;
	}

	public ArrayList<SelectItem> getEmiTermsList() {
		return emiTermsList;
	}

	public void setEmiTermsList(ArrayList<SelectItem> emiTermsList) {
		this.emiTermsList = emiTermsList;
	}

	public LoanInfo getSelectedLoan() {
		return selectedLoan;
	}

	public void setSelectedLoan(LoanInfo selectedLoan) {
		this.selectedLoan = selectedLoan;
	}
	public int getBranchId() {
		return branchId;
	}

	public void setBranchId(int branchId) {
		this.branchId = branchId;
	}

	public List<SelectItem> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<SelectItem> branchList) {
		this.branchList = branchList;
	}


	public boolean isDisAll() {
		return disAll;
	}


	public void setDisAll(boolean disAll) {
		this.disAll = disAll;
	}


	public String getUserType() {
		return userType;
	}


	public void setUserType(String userType) {
		this.userType = userType;
	}
    


}
