package hrm_package;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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 com.itextpdf.text.List;

import main_package.DataBaseConnection;
import main_package.DataBaseMethods;
import main_package.EmpInfo;

@ManagedBean(name="attendanceReport")
@ViewScoped
public class AttendanceReportBean implements Serializable
{
	
	private static final long serialVersionUID = 1L;
	ArrayList<SelectItem> empList;
	DataBaseMethods dbm=new DataBaseMethods();
	String empUserId;
	int branchId;
	Date startDate=new Date(), endDate=new Date();
	boolean show;
	ArrayList<EmpInfo> empAttendanceList;
	private ArrayList<EmpInfo> employees;
    private ArrayList<String> daysOfMonth;
    
    
	
	public AttendanceReportBean() 
	
	{
		
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	    branchId = (int) ss.getAttribute("branchId");
		Connection conn=DataBaseConnection.JAVAConnection();
		empList=dbm.employeeTypeList(branchId,conn);
		startDate.setDate(1);  
		
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Attendance();
	   
		
	}
	
	


    
    public void Attendance() {
    	
    	System.out.println("method hit 2");
        // For demo, let's assume current month
        YearMonth yearMonth = YearMonth.now();
        System.out.println("value of the yearMonth :"+yearMonth);
        int daysInMonth = yearMonth.lengthOfMonth();
        
        daysOfMonth = new ArrayList<>();
        for (int i = 1; i <= daysInMonth; i++) {
            daysOfMonth.add(String.valueOf(i));
        }
        

        
    }
    
    
 

//    public void Employee(String firstName, String lastName, LocalDate dob, int year, int month) {
//       
//    	public java.util.List<String> attendance = new ArrayList<>();
//
//        YearMonth ym = YearMonth.of(year, month);
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
//
//        for (int i = 1; i <= ym.lengthOfMonth(); i++) {
//            LocalDate date = LocalDate.of(year, month, i);
//            String key = date.format(formatter);  // e.g. 01-02-2025
//            
//        }
//    }
//   
    
                                     
	public void employeeDataList()
	{  
	 
		try(Connection conn = DataBaseConnection.JAVAConnection()){
			
		String query = "select * from employee where branch_id = ? ";
			
			try(PreparedStatement ps = conn.prepareStatement(query)){
				
				ps.setInt(1, branchId);
				
				ResultSet rs = ps.executeQuery();
				
				while(rs.next()) {
				
					EmpInfo ei = new EmpInfo();
					
					ei.setName(rs.getString("name"));
					ei.setDob(rs.getDate("dob"));
					ei.setDsaCode(rs.getString("dsacode"));
					ei.setDepartment(designationNameByDesignationId(rs.getString("designation")));
          
					
					
				}
				
				
			}catch(Exception e) {
				
				e.printStackTrace();
				
			}
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
			
		}
		
	}  
	
	public void attendanceMarking(){
		
		
		
		
	}
	
	
	public String designationNameByDesignationId(String designationId) {
		String name =" ";
		try(Connection conn = DataBaseConnection.JAVAConnection()){
		 
			String query = " select * from hrm_basic where id = ?";
			
			try(PreparedStatement ps = conn.prepareStatement(query)){
				
			   ps.setString(1, designationId);
			   
			   ResultSet rs = ps.executeQuery();
			   
			   if(rs.next()) {
				   
				   name = rs.getString("name");
				   
			   }
				
			}catch(Exception e) {
				
				e.printStackTrace();
				
			}
			
		}catch(Exception e) {
			
			e.printStackTrace();
			
		}
		
		return name;
		
	}
	
	

	
	
	
	///////////////////////////////////////////////
	
	public void searchData()
	{    
		//Attendance();
		HttpSession ss=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		int branchId = (int) ss.getAttribute("branchId");
		show=true;
		empAttendanceList=dbm.viewAttendanceByEmpUserId(empUserId, startDate, endDate,branchId);
		
	
	}

	public ArrayList<SelectItem> getEmpList() {
		return empList;
	}

	public void setEmpList(ArrayList<SelectItem> empList) {
		this.empList = empList;
	}

	public String getEmpUserId() {
		return empUserId;
	}

	public void setEmpUserId(String empUserId) {
		this.empUserId = empUserId;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public boolean isShow() {
		return show;
	}

	public void setShow(boolean show) {
		this.show = show;
	}

	public ArrayList<EmpInfo> getEmpAttendanceList() {
		return empAttendanceList;
	}

	public void setEmpAttendanceList(ArrayList<EmpInfo> empAttendanceList) {
		this.empAttendanceList = empAttendanceList;
	}








	public ArrayList<EmpInfo> getEmployees() {
		return employees;
	}








	public void setEmployees(ArrayList<EmpInfo> employees) {
		this.employees = employees;
	}








	public ArrayList<String> getDaysOfMonth() {
		return daysOfMonth;
	}








	public void setDaysOfMonth(ArrayList<String> daysOfMonth) {
		this.daysOfMonth = daysOfMonth;
	}
	
}