<%@page contentType="text/html; charset=utf-8"%>
<%@page import="java.util.*" %>
<%@page import="org.acts.attendance.*" %>
<%@page import="org.acts.common.*" %>
<%@page import="org.acts.common.html.*" %>
<%@include file="../include/check_login.jsp" %>

<%@include file="../include/header.jsp" %>
<%msbean.setCur_menu_node(MenuItem.ATTENDANCE__TAKE);%>
<script type="text/javascript">
function js_clock(){
    var f=document.stdform;
	var time=new Date(parseInt(f.sys_time.value));
    var clock_div=document.getElementById('js_clock');
    clock_div.innerHTML=padZero(time.getHours())+":"+padZero(time.getMinutes())+":"+padZero(time.getSeconds());
    f.sys_time.value=parseInt(f.sys_time.value)+1000;
    setTimeout("js_clock()",1000);
}

function refocus(){
    var f=document.stdform;
	f.insert_member_id.focus();
	f.focus_tid.value=setTimeout("refocus()",500);
}

function delayFocus(){
    var f=document.stdform;
	clearTimeout(f.focus_tid.value);
	f.focus_tid.value=setTimeout("refocus()",4000); //for headcount select menu to have enough time to select
}

function insert(){
    if(document.stdform.member_id==""){
        alert("<%=Lang.set("請輸入會眾編號")%>");
        return;
    }
    opSubmit("attendance.AttendanceModuleAction.take.insert")
}
function deleteItem(){
    opSubmit("attendance.AttendanceModuleAction.take.delete")
}
function addHeadCount(hc_mg_seq){
	document.stdform.hc_mg_seq.value=hc_mg_seq;
	opSubmit("attendance.AttendanceModuleAction.take.addHeadCount")
}
function deleteHeadCount(hc_mg_seq){
	document.stdform.hc_mg_seq.value=hc_mg_seq;
	opSubmit("attendance.AttendanceModuleAction.take.deleteHeadCount")
}

function hideOverlayMessage(){
	var e1=document.getElementById("overlay");
	var e2=document.getElementById("overlayMessage");
	if(e1.style.display == "block"){
		e1.style.display = "none";
		e2.style.display = "none";
  	}
} 

//set to refresh every 5 minutes, in order to avoid the session timeout in Tomcat when page get long time no use
setTimeout("opSubmit('attendance.AttendanceModuleAction.take.load');",300000);

setTimeout("hideOverlayMessage();",3000);

</script>
<%@include file="../include/body.jsp" %>
<input type=hidden name=sys_time value="<%=TimeUtil.getCurrentTimestampFromDB().getTime()%>">
<input type=hidden name=hc_mg_seq value=-1>
<input type=hidden name=focus_tid value=-1>
<%
RecordList r=Attendance.getActivity(msbean.getIData("activity_seq"));
r.setCurrentRow(0);

HtmlTable t=new HtmlTable(new int[]{900});
t.addTitleCell(0,0,msbean.getCur_menu_node().getName());
t.toHtml(out);

t=new HtmlTable(new int[]{90,230,290,155,40,125});
t.addDescCell(0,0,"名稱");
t.addDescCell(0,1,"類別");
t.addDescCell(0,2,"地點");
t.addDescCell(0,3,"日期");
t.addDescCell(0,4,"系統時間");
t.addDescCell(0,5,"");
t.addDescCell(0,6,"會眾編號");
t.addDescCell(0,7,"系統訊息");

TextBox member_idTB=new TextBox("insert_member_id","",130,1000);
member_idTB.addAttribute("onkeydown='if(event.keyCode==13){insert()}'");
Button insertB=new Button("輸入","insert()");

String bgcolor="";
String fontcolor="";
String overlayStyle="style=\"display: none\"";
String overlayMessageStyle="style=\"display: none\"";
if(msbean.getSData("insert_status").equals("true")){
    bgcolor="bgcolor=#60A917";
	fontcolor="<font color=#FFFFFF size=5>";
	overlayStyle="style=\"display: block\"";
	overlayMessageStyle="style=\"display: block; background-color:#60A917\"";
}else
if(msbean.getSData("insert_status").equals("false")){
    bgcolor="bgcolor=#FF002A";
	fontcolor="<font color=#FFFFFF size=5>";
	overlayStyle="style=\"display: block\"";
	overlayMessageStyle="style=\"display: block; background-color:#FF002A\"";
}

t.addLCell(1,0,r.getS("name"));
t.addLCell(1,1,r.getS("type_name"));
t.addLCell(1,2,r.getS("venue"));
t.addLCell(1,3,TimeUtil.getStringByDate(r.getT("date")));
t.addLCell(1,4,"<div id='js_clock'></div>");
t.addLCell(1,5,"");
t.addLCell(1,6,member_idTB.toHtml()+insertB.toHtml());
t.addCell(1,7,2,1,msbean.getSData("sys_msg"),bgcolor,fontcolor+"<b>","</b></font>");

RecordList attends=Attendance.getAttendanceList(msbean.getIData("activity_seq"));
SelectMenu attendSM=new SelectMenu("attendance_list",attends,"list_item","attendance_seq","");
attendSM.addAttribute("style=\"width:285px;\"");
attendSM.addAttribute("style=\"width:285px;\"");
attendSM.addAttribute("onfocus=delayFocus()");
attendSM.setSize(10);
attendSM.setNilItemEnable(false);
attendSM.setDefaultSelectedPos(attends.size()-1);

Button deleteB=new Button("刪除","deleteItem()");

t.addLCell(2,0,1,6,attendSM);
t.addRCell(2,6,deleteB);

RecordList mgs=Attendance.getMemberGroups(msbean.getIData("activity_seq"));

t.addLCell(3,0,3,1,Lang.set("額外出席人次"));
for(int i=0;i<mgs.size();i++){
	mgs.setCurrentRow(i);
	RecordList hcArray=new RecordList(new String[]{"name","value"});
	for(int j=1;j<=10;j++){hcArray.addItem(new String[]{String.valueOf(j),String.valueOf(j)});}
	int mg_seq=mgs.getI("member_group_seq");
	SelectMenu hcSM=new SelectMenu("headcount"+mg_seq,hcArray,"name","value","1");
	hcSM.addAttribute("onfocus=delayFocus()");
	hcSM.setNilItemEnable(false);
	Button addHeadCountB=new Button("+","addHeadCount("+mg_seq+")");
	addHeadCountB.addAttribute("style=\"width:20px;height:20px\"");
	Button delHeadCountB=new Button("-","deleteHeadCount("+mg_seq+")");
	delHeadCountB.addAttribute("style=\"width:20px;height:20px\"");
	t.addLCell(3,i+1,mgs.getS("name"));
	t.addRCell(4,i+1,mgs.getI("headcount"));
	t.addLCell(5,i+1,delHeadCountB.toHtml()+"&nbsp;"+hcSM.toHtml()+"&nbsp;"+addHeadCountB.toHtml());
}
	
t.toHtml(out);
%>

<div id="overlay" class="Overlay" <%=overlayStyle%>></div>
<div id="overlayMessage" class="OverlayMessage" <%=overlayMessageStyle%>>
    <p>
	<%=msbean.getSData("overlay_msg")%>
	</p>
</div>

<script type="text/javascript">
	js_clock();
	refocus();
</script>

<%@include file="../include/footer.jsp" %>