#!/usr/bin/python
import cgi,os,sys,re
from utils import *
from cookie import *
from konst import *
from prefs import *

print 'Content-Type: text/html'

if not os.environ.has_key('HTTP_COOKIE'):
    Redirect(kLoginPage)

username=GetCookie('user')
if not username:
    Redirect(kLoginPage)

print '\n\n'

sys.stderr = sys.stdout

prefs = Prefs('%s%s/.prefs' %(kReportsDir, username))

print """<HTML><HEAD><TITLE> %s\'s status for the week
    </TITLE>
	  <LINK REL=STYLESHEET HREF="reports.css">
	  <LINK REL=icon HREF="/reports/alien.gif">
</HEAD>""" % username

PrintFile('header.inc')

print '<CENTER><H3>%s\'s weekly <A HREF="faq.shtml">status</A></H3>' % username

if TodayIs(int(prefs.submitday)):
    print '<H4><DIV STYLE="background:yellow;display:inline;">Reminder: Submit today!</DIV></H4>'
elif not LastSubmitted(username, 1):
    print "<H4><DIV STYLE=\"background:yellow;display:inline;\">You haven't submitted in a week! Reminder: Submit soon!</DIV></H4>"

PrintFile('motd.inc')

print '<FORM ACTION="/reports/submit.cgi" METHOD="POST"><TABLE WIDTH=75%>'
splitchar = ' '
if ',' in prefs.sectionorder:
    splitchar = ','

for k in string.split(prefs.sectionorder, splitchar):
    print """<TR><TD>%s</TD></TR>
<TR ALIGN=CENTER><TD><TEXTAREA NAME="%s" ROWS=%s COLS=80>""" % (prefs.sections[k], k, prefs.rows)
    PrintFile('%s%s/%s' % (kReportsDir, username, k), esc=1)
    print '</TEXTAREA></TD></TR>'

#if username=='gagan': # later on change to just pref
if prefs.needAutoStats():
    print '<TR><TD><b>Autostats</b></TD></TR>'
    from autostats import autostats
    print '<TR><TD>', autostats(username, prefs), '</TD></TR>'

print """<TR><TD>
<TABLE BORDER=0 COLS=3 WIDTH=90%%>
<TR ALIGN=CENTER>
<INPUT TYPE="HIDDEN" NAME="perm" VALUE="1">
<TD>
<INPUT TYPE="Button" VALUE="Submit!" onClick="javascript:if
(confirm('Are you sure you want to SUBMIT it today?')) {form.submit();};"></TD>
<TD><INPUT TYPE="Button" VALUE="Save and Preview"
onClick="form.perm.value=0; form.submit()"></TD> </TR>
</TABLE>
</FORM>
<HR NOSHADE>
<TABLE WIDTH=80% ALIGN=CENTER border=0><TR>
<TD ALIGN=CENTER><A HREF="faq.shtml">FAQ</A></TD>
<TD ALIGN=CENTER><A HREF="view.shtml">View</A></TD>
<TD ALIGN=CENTER><A HREF="prefs.cgi">Prefs</A></TD>
<TD ALIGN=CENTER><A HREF="logout.cgi">Logout</A></TD>
</TR></TABLE>
</TD></TR>
</TABLE>
</CENTER>"""
PrintFile('footer.inc')
