<%@page contentType="text/html; charset=utf-8"%>
<%@page import="java.util.*" %>
<%@page import="org.acts.common.*" %>
<%@page import="org.acts.common.html.*" %>
<%@page import="org.acts.attendance.*" %>
<%@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("請輸入會眾編號");
        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")
}

//set to refresh every 5 minutes, in order to avoid the session timeout in Tomcat when page get long time no use
setTimeout("location.reload();",300000);

</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,245,300,155,40,100});
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","",20,1000);
member_idTB.addAttribute("onkeydown='if(event.keyCode==13){insert()}'");
Button insertB=new Button("輸入","insert()");

String bgcolor="";
String fontcolor="";
if(msbean.getSData("insert_status").equals("true")){
    bgcolor="bgcolor=#55FF55";
	fontcolor="<font color=#000000>";
}else
if(msbean.getSData("insert_status").equals("false")){
    bgcolor="bgcolor=#FF0000";
	fontcolor="<font color=#FFFFFF>";
}

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,"data","member_seq","");
attendSM.addAttribute("style=\"width:295px;\"");
attendSM.addAttribute("style=\"width:295px;\"");
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,"額外出席人次");
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);
%>

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

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