package com.csc.library.timeattendance;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.csc.library.databean.Memployee_ta;
import com.csc.library.databean.Memployee_worktime1;
import com.csc.library.databean.Mtime0;
import com.csc.library.databean.Tabsent_nstda;
import com.csc.library.session.DbInquiry;
import com.csc.library.session.DbRecord;
import com.csc.library.session.InitialInquiry;
import com.csc.library.session.InitialRecord;
import com.csc.library.session.RecordCmp;
import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.MyTreeMap;
import com.csc.library.utilities.UProfile;

public class GenAbsentDoc {
	private String employee = null;
	private CscCalendar StDate = null;
	private CscCalendar EnDate = null;
	private HashMap hm_evant = new HashMap();
	private UProfile userPro = null;
	
	public GenAbsentDoc(UProfile upf, String empid,String stdate,String endate){
		this.userPro = upf;
		this.setEmployee(empid);
		this.setStDate(stdate);
		this.setEnDate(endate);
	}
	
	private void setEmployee(String Empid){
		this.employee = Empid;
	}
	
	public String getEmployee(){
		return this.employee;
	}
	
	private void setStDate(String stDate){
		this.StDate = new CscCalendar(stDate);
	}
	
	public CscCalendar getStDate(){
		return this.StDate;
	}
	
	private void setEnDate(String enDate){
		this.EnDate = new CscCalendar(enDate);
	}
	
	public CscCalendar getEnDate(){
		return this.EnDate;
	}
	
	public String generate(){		
		MyTreeMap trEmpSwipe = new MyTreeMap();
		StringBuffer result = new StringBuffer("true,");
		try {
			DbRecord dbEMP = new InitialRecord(this.userPro).getDbRecord("Memployee_worktime1");
	        dbEMP.setColumn("*");
	        dbEMP.setParam("date_start", this.getStDate().getYYYYMMDD());
	        dbEMP.setParam("date_end", this.getEnDate().getYYYYMMDD());
	        dbEMP.set("EMPLOYEEID", this.getEmployee());
	        if(dbEMP.search() == 1) { // search found
	        	dbEMP.set("dayshift","");
	        	
	        	WorkingShift pws=(WorkingShift)dbEMP.getParam().get("ws");
	        	for (Iterator IT = pws.keySet().iterator(); IT.hasNext(); ) {
					String key = (String) IT.next();
					DayShift dShift = (DayShift) pws.get(key);
					for (Iterator IT1 = dShift.keySet().iterator(); IT1.hasNext(); ) {
						String key1 = (String) IT1.next();
						Shift sh = (Shift) dShift.get(key1);
                    	if(!sh.getEventGroup().equals("T")){
                    		if(this.hm_evant.isEmpty()){
                    			this.loadEventgrp();
                    			result = new StringBuffer();
                    			result.append("false,");
                    		}
                    		if("tha".equalsIgnoreCase(this.userPro.get("lang")))
                    			result.append("�ѹ "+sh.getShiftDate().getYYYYMMDD()+" ���ա�� "+this.hm_evant.get(sh.getEventGroup()).toString()+" ���� ");
                    		else
                    			result.append("Date "+sh.getShiftDate().getYYYYMMDD()+" is used for "+this.hm_evant.get(sh.getEventGroup()).toString()+" ");
                    	}
	                }
	            }
			        
		        if(result.toString().indexOf("false")>-1){
		        	return result.toString().substring(0,result.toString().length()-5);
		        }else{
		        	return "true";
		        }
	        }
		} catch (Exception e) {
            MyLog.debug(this, e);
        }   
		return result.toString();
	}
	
	private void loadEventgrp(){
		try{
			this.hm_evant = new HashMap();
			DbInquiry inqEvent = new InitialInquiry(this.userPro).getDbInquiry("Meventgrp");
			inqEvent.setColumn("eventgrpid,tdesc,edesc");
			inqEvent.referLangOn();
			inqEvent.refresh();
			while(inqEvent.next()){
				this.hm_evant.put(inqEvent.getString("eventgrpid"),inqEvent.getString("tdesc"));
			}
		} catch (Exception e) {
	        MyLog.debug(this, e);
	    }   
	}
	
	public void genAbsent(){
		try{
			//this.hm_evant = new HashMap();
			DbRecord recAbsent = new InitialRecord(this.userPro).getDbRecord("Tabsent_nstda");
			recAbsent.setColumn("*");
			recAbsent.set("employeeid", this.getEmployee());
			recAbsent.set("type_absent", "Q");//�١�ѡ�ҹ
			recAbsent.set("start_date", this.getStDate().getYYYYMMDD());
			recAbsent.set("end_date", this.getStDate().getYYYYMMDD());
			recAbsent.set("leave_format", "0");
			recAbsent.set("leave_day", this.getEnDate().subForDay(this.getStDate())+1);
			recAbsent.set("wf_status", "6");
			
			int result = recAbsent.save();
			if(recAbsent.getParam().containsKey("error_msg")){
				MyLog.debug("/***************************************/");
				MyLog.debug(this, recAbsent.getParam("error_msg"));
				MyLog.debug("/***************************************/");
			}
		} catch (Exception e) {
	        MyLog.debug(this, e);
	    }   
	}
}