package com.csc.library.dwr;

import com.csc.library.utilities.MyLog;
import java.rmi.RemoteException;
import java.text.DecimalFormat;
import java.util.Iterator;

import com.csc.library.database.MyHashMap;
import com.csc.library.session.DbInquiry;
import com.csc.library.session.InitialInquiry;
import com.csc.library.utilities.CscCalendar;
import com.csc.library.utilities.MyTreeMap;

public class Trequestshift_nstda_dwr extends DWR_base {

		MyHashMap hmTime = new MyHashMap(); //�� description �ͧ���ҧ��÷ӧҹ
		MyHashMap hmData = new MyHashMap(); //����������´��ҧ�
		DecimalFormat df = new DecimalFormat("0.00");
		
		public Trequestshift_nstda_dwr() { }
		
		public String getDWRData(String[] str) {
			this.setUProfile();
			String data="";
			data += this.getHeaderFrame();
			MyTreeMap tr = new MyTreeMap();
			
			String emp = this.chkEmptyData(str, 0);
			String timeCode = this.chkEmptyData(str, 1);
			String month = this.chkEmptyData(str, 2);
			String year = this.chkEmptyData(str, 3);
			
			tr.put("shift011", this.chkEmptyData(str, 4));
			tr.put("shift012", this.chkEmptyData(str, 5));	
			tr.put("shift013", this.chkEmptyData(str, 6));	
			tr.put("shift021", this.chkEmptyData(str, 7));
			tr.put("shift022", this.chkEmptyData(str, 8));
			tr.put("shift023", this.chkEmptyData(str, 9));
			tr.put("shift031", this.chkEmptyData(str, 10));
			tr.put("shift032", this.chkEmptyData(str, 11));
			tr.put("shift033", this.chkEmptyData(str, 12));
			tr.put("shift041", this.chkEmptyData(str, 13));
			tr.put("shift042", this.chkEmptyData(str, 14));
			tr.put("shift043", this.chkEmptyData(str, 15));
			tr.put("shift051", this.chkEmptyData(str, 16));
			tr.put("shift052", this.chkEmptyData(str, 17));
			tr.put("shift053", this.chkEmptyData(str, 18));
			tr.put("shift061", this.chkEmptyData(str, 19));
			tr.put("shift062", this.chkEmptyData(str, 20));
			tr.put("shift063", this.chkEmptyData(str, 21));
			tr.put("shift071", this.chkEmptyData(str, 22));
			tr.put("shift072", this.chkEmptyData(str, 23));
			tr.put("shift073", this.chkEmptyData(str, 24));
			tr.put("shift081", this.chkEmptyData(str, 25));
			tr.put("shift082", this.chkEmptyData(str, 26));
			tr.put("shift083", this.chkEmptyData(str, 27));
			tr.put("shift091", this.chkEmptyData(str, 28));
			tr.put("shift092", this.chkEmptyData(str, 29));
			tr.put("shift093", this.chkEmptyData(str, 30));
			tr.put("shift101", this.chkEmptyData(str, 31));
			tr.put("shift102", this.chkEmptyData(str, 32));
			tr.put("shift103", this.chkEmptyData(str, 33));
			
			tr.put("shift111", this.chkEmptyData(str, 34));
			tr.put("shift112", this.chkEmptyData(str, 35));
			tr.put("shift113", this.chkEmptyData(str, 36));
			tr.put("shift121", this.chkEmptyData(str, 37));
			tr.put("shift122", this.chkEmptyData(str, 38));
			tr.put("shift123", this.chkEmptyData(str, 39));
			tr.put("shift131", this.chkEmptyData(str, 40));
			tr.put("shift132", this.chkEmptyData(str, 41));
			tr.put("shift133", this.chkEmptyData(str, 42));
			tr.put("shift141", this.chkEmptyData(str, 43));
			tr.put("shift142", this.chkEmptyData(str, 44));
			tr.put("shift143", this.chkEmptyData(str, 45));
			tr.put("shift151", this.chkEmptyData(str, 46));
			tr.put("shift152", this.chkEmptyData(str, 47));
			tr.put("shift153", this.chkEmptyData(str, 48));
			tr.put("shift161", this.chkEmptyData(str, 49));
			tr.put("shift162", this.chkEmptyData(str, 50));
			tr.put("shift163", this.chkEmptyData(str, 51));
			tr.put("shift171", this.chkEmptyData(str, 52));
			tr.put("shift172", this.chkEmptyData(str, 53));
			tr.put("shift173", this.chkEmptyData(str, 54));
			tr.put("shift181", this.chkEmptyData(str, 55));
			tr.put("shift182", this.chkEmptyData(str, 56));
			tr.put("shift183", this.chkEmptyData(str, 57));
			tr.put("shift191", this.chkEmptyData(str, 58));
			tr.put("shift192", this.chkEmptyData(str, 59));
			tr.put("shift193", this.chkEmptyData(str, 60));
			tr.put("shift201", this.chkEmptyData(str, 61));
			tr.put("shift202", this.chkEmptyData(str, 62));
			tr.put("shift203", this.chkEmptyData(str, 63));
			
			tr.put("shift211", this.chkEmptyData(str, 64));
			tr.put("shift212", this.chkEmptyData(str, 65));
			tr.put("shift213", this.chkEmptyData(str, 66));
			tr.put("shift221", this.chkEmptyData(str, 67));
			tr.put("shift222", this.chkEmptyData(str, 68));
			tr.put("shift223", this.chkEmptyData(str, 69));
			tr.put("shift231", this.chkEmptyData(str, 70));
			tr.put("shift232", this.chkEmptyData(str, 71));
			tr.put("shift233", this.chkEmptyData(str, 72));
			tr.put("shift241", this.chkEmptyData(str, 73));
			tr.put("shift242", this.chkEmptyData(str, 74));
			tr.put("shift243", this.chkEmptyData(str, 75));
			tr.put("shift251", this.chkEmptyData(str, 76));
			tr.put("shift252", this.chkEmptyData(str, 77));
			tr.put("shift253", this.chkEmptyData(str, 78));
			tr.put("shift261", this.chkEmptyData(str, 79));
			tr.put("shift262", this.chkEmptyData(str, 80));
			tr.put("shift263", this.chkEmptyData(str, 81));
			tr.put("shift271", this.chkEmptyData(str, 82));
			tr.put("shift272", this.chkEmptyData(str, 83));
			tr.put("shift273", this.chkEmptyData(str, 84));
			tr.put("shift281", this.chkEmptyData(str, 85));
			tr.put("shift282", this.chkEmptyData(str, 86));
			tr.put("shift283", this.chkEmptyData(str, 87));
			tr.put("shift291", this.chkEmptyData(str, 88));
			tr.put("shift292", this.chkEmptyData(str, 89));
			tr.put("shift293", this.chkEmptyData(str, 90));
			tr.put("shift301", this.chkEmptyData(str, 91));
			tr.put("shift302", this.chkEmptyData(str, 92));
			tr.put("shift303", this.chkEmptyData(str, 93));
			tr.put("shift311", this.chkEmptyData(str, 94));
			tr.put("shift312", this.chkEmptyData(str, 95));
			tr.put("shift313", this.chkEmptyData(str, 96));
			
			try {				
				data += "<table cellspacing='1' cellpadding='0' width='100%' align='center' border='0'>";
				data += "<tr bgcolor='#0099CC'>";
                if(this.up.get("lang").equalsIgnoreCase("ENG")){
    				data += "<td colspan='5' align='center' class='txt_normal'><strong>&nbsp;:&nbsp;Working Information&nbsp;:&nbsp;</strong></td>";
    				//Working Information = �����š�÷ӧҹ
    				data += "</tr>";
    				data += "<tr align='center' bgcolor='#0099CC'>";
    				data += "<td class='txt_normal'><strong>Date&nbsp;</strong></td>";
    				data += "<td class='txt_normal'><strong>Time Code&nbsp;</strong></td>";
    				data += "<td class='txt_normal'><strong>Description&nbsp;</strong></td>";
    				data += "<td class='txt_normal'><strong>Time In&nbsp;</strong></td>";			
    				data += "<td class='txt_normal'><strong>Time Out&nbsp;</strong></td>";
                }else{
                    data += "<td colspan='5' align='center' class='txt_normal'><strong>&nbsp;:&nbsp;�����š�÷ӧҹ&nbsp;:&nbsp;</strong></td>";
                    //Working Information = �����š�÷ӧҹ
                    data += "</tr>";
                    data += "<tr align='center' bgcolor='#0099CC'>";
                    data += "<td class='txt_normal'><strong>�ѹ���&nbsp;</strong></td>";
                    data += "<td class='txt_normal'><strong>���ʡ�&nbsp;</strong></td>";
                    data += "<td class='txt_normal'><strong>��������´&nbsp;</strong></td>";
                    data += "<td class='txt_normal'><strong>�����ٴ���&nbsp;</strong></td>";           
                    data += "<td class='txt_normal'><strong>�����ٴ�͡&nbsp;</strong></td>";
                }
				data += "</tr>";
				
				int day=0;
				String key = "", date="", shiftCode= "";				
				Iterator it = tr.keySet().iterator();
				while (it.hasNext()) {
					key = (String) it.next();
					date = key.substring(5,7)+"-"+month+"-"+year;
					CscCalendar csc = new CscCalendar(date);
					shiftCode = (String) tr.get(key);
					if (!shiftCode.equals("")) {
						this.loadTimeData(shiftCode);
						if (this.getDayType(shiftCode, date)) {
							data += "<tr align='center'>";
							data += "<td class='desc'>"+csc.getDDMMYYYY()+"</td>";
							data += "<td class='desc'>"+shiftCode+"</td>";
							data += "<td class='desc'>"+this.getTimeDesc(shiftCode)+"</td>";
							data += "<td class='desc'>"+this.getTimeIn(shiftCode, date)+"</td>";
							data += "<td class='desc'>"+this.getTimeOut(shiftCode, date)+"</td>";
							data += "</tr>";	
							day++;
						}
					}
				}		
				data += "<tr>";
                /*if (((String)this.up.get("lang")).equalsIgnoreCase("ENG")) {
                    data += "<td class='desc' align='right'>Total&nbsp;:&nbsp;</td>";
                    data += "<td class='desc' align='center'>"+day+"&nbsp;</td>";
                    data += "<td class='desc'>Day</td>";
                    data += "<td class='desc'>&nbsp;</td>";
                    data += "<td class='desc'>&nbsp;</td>";
                }else{
                    data += "<td class='desc' align='right'>���&nbsp;:&nbsp;</td>";
                    data += "<td class='desc' algin='center'>"+day+"&nbsp;</td>";
                    data += "<td class='desc'>�ѹ</td>";
                    data += "<td class='desc'>&nbsp;</td>";
                    data += "<td class='desc'>&nbsp;</td>";               
                }*/
				data += "</tr>";
				data += "</table>";
			} catch (Exception e) {
				MyLog.error(this, e);

				
			}
			data += this.getBelowFrame();
			return data;
		}
		
		private void loadTimeData(String shiftCode) {
			try {
				if (!this.hmTime.containsKey(shiftCode)) {				
					DbInquiry inqChild  = new InitialInquiry(up).getDbInquiry();
					inqChild.initMyTable("Mtime1","","");
					inqChild.setColumn("*");
					
					DbInquiry inq = new InitialInquiry(up).getDbInquiry();
					inq.initMyTable("Mtime0","time0id='"+shiftCode+"'","");
					inq.setColumn("*");
					inq.putChild(inqChild);
					inq.refresh();			
					while (inq.next()) {
						MyHashMap hmDay = new MyHashMap();
						DbInquiry child = (DbInquiry) inq.getChild("mtime1");
						do {
							MyHashMap hm = new MyHashMap();
							hm.put("dayweekid", child.getString("dayweekid"));
							hm.put("stopday", child.getString("stopday"));
							hm.put("timein", child.getString("timein1"));
							hm.put("timeout", child.getString("timeout2"));		
							hmDay.put(child.getString("dayweekid"), hm);
						} while (child.next());
						this.hmData.put(inq.getString("time0id"), hmDay);
						this.hmTime.put(inq.getString("time0id"), inq.getString("tdesc"));
					}
				}
			} catch (RemoteException e) {
				MyLog.error(this, e);

				// TODO Auto-generated catch block
				
			}			
		}

		public String getTimeDesc(String shiftCode) {
			String result="";
			if (this.hmTime.containsKey(shiftCode)) {			
				result = this.hmTime.getString(shiftCode);
			}
			return result;
		}
		
		private String getTimeIn(String shiftCode, String date) {
			return this.getTime(shiftCode, date, 0);
		}
		
		private String getTimeOut(String shiftCode, String date) {
			return this.getTime(shiftCode, date, 1);
		}
		
		private boolean getDayType(String shiftCode, String date) {
			if (this.getTime(shiftCode, date, 2).equals("1")) { 
				return false;
			} else return true;
		}
		
		private String getTime(String shiftCode, String date, int type) {
			String result="";
			if (this.hmData.containsKey(shiftCode)) {
				CscCalendar cs = new CscCalendar(date);
				MyHashMap hm = (MyHashMap) this.hmData.get(shiftCode);
				if (hm.containsKey(String.valueOf(cs.getDOW()))) {					
					MyHashMap hmDay = (MyHashMap) hm.get(String.valueOf(cs.getDOW()));
					if (type==0) {
						result = hmDay.getString("timein");
					} else if (type==1) {
						result = hmDay.getString("timeout");
					} else if (type==3) {
						result = hmDay.getString("stopday");
					}
				}
			}
			if (!result.equals("")) {
				result = df.format(Double.parseDouble(result));
			}
			return result;
		}
}