#!/usr/local/bin/python3.3
import cgi
import cgitb
import pymysql
import datetime
import DAPRfunctions as dapr

#dapr.loadConfiguration()

cookie = dapr.get_cookies()
db = dapr.DBConnection()

cgitb.enable()

if cookie:
	print(cookie)
	fname = cookie["fname"].value
	lname = cookie["lname"].value
	name = " ".join((fname, lname))
	admin = cookie["admin"].value
	uid = cookie["uid"].value
	uname = cookie["uname"].value
	
	
	form = cgi.FieldStorage()
	
	new = form.getvalue("wetname")
	des1 = form.getvalue("description")
	wmid = form.getvalue("wmid")	
	
	if new == None:
		new = ""
	if des1 == None:
		des1 = ""
	if wmid == None:
		wmid = ""
	
	
	def addwetMethod(name, des1):
		query = """INSERT INTO WetlabMethod (name, description)
					VALUES ("%s", "%s");"""%(name, des1)
		db.runInsert(query)
	
	def delwetMethod(wmid):
		query = """delete from WetlabMethod where wmid = ("%s");""" %(wmid)
		db.runInsert(query)
	
	if len(new) != 0:
		addwetMethod(new, des1)
		print("Location: showWetMethod.py")
	elif len(wmid) != 0:
		delwetMethod(wmid)
		print("Location: showWetMethod.py")
	
	print("Content-type: text/html\n")
	dapr.phead(name, admin)
	print("""<div class="container">""")
	
	print('<p class="lead">All Wetlab Method for user <b>%s</b>:</p>'%name)
	q1="""
	select wmid, name, description
	from WetlabMethod;
	""" 
	
	res1 = db.runQuery(q1)
	
	if len(res1) != 0 :
		print("""
		<table id="example" class="display" cellspacing="0" width="100%">
		  <thead>
				 <tr>
				 	<th></th>
					 <th>wmid</th>
					 <th>Wetlab Method</th>
					 <th>Description</th>
				   </tr></thead>"""
				   )
		n1 = len(res1)
		print("<tbody>")
		for i in range(n1):
			print('<tr>')
			print("""<td><input name ="input" class="input" type="checkbox"/></td>""")
			if str(res1[i][1])== None:
				print("""<td>%s</td><td>%s</td><td>%s</td>""" % (str(res1[i][0]),str(res1[i][1])," "))
			else:
				print("""<td>%s</td><td>%s</td><td>%s</td>""" % (str(res1[i][0]),str(res1[i][1]),str(res1[i][2])))
			print("</tr>")
		print("</tbody></table></br></br>")
	
	print("""
	<p class="lead">Add a new Wetlab Method:</p>
	(Remember to type in the Method name!)
	<br/>
	<br/>
	<form class="form-horizontal" method="post" action="./showWetMethod.py" enctype="multipart/form-data">
		<div class="form-group">
		<div class="col-xs-8"> 
		      <label for="Method-name" class="col-sm-2 control-label">Method name:</label>		  
			   <div class="col-sm-10">
				<input class="form-control" type="text" placeholder="Please enter method name" name="wetname" size="55" maxlength="255" title="Enter Method Name">
        </div>
        </div>
      </div>
        </br>
        <div class="form-group">
        <div class="col-xs-8">
			<label for="Description" class="col-sm-2 control-label">Description:</label>
			<div class="col-sm-10">
			<input class="form-control" type="text" name="description" size="55" maxlength="255" title="Enter Description"> 
				
			  </div>
			  </div>
			  </div>
			</br>
			  <p><input type="submit" value="Add" class="btn btn-lg btn-default"> </p>
																	
		  </form>""")
	
	print("""
	<p class="lead">Remove Wetlab Method:</p>
	(Remember to type in the Wetlab wmid!)
	<br/>
	<br/>
	<form class="form-horizontal" method="post" action="./showWetMethod.py" enctype="multipart/form-data">
        <div class="form-group">
		<div class="col-xs-8"> 
			  <label for="Wetlab-wmid" class="col-sm-2 control-label">Wetlab wmid:</label>
				  	<div class="col-sm-10">														  
				<input class="form-control" type="text" placeholder="Please enter wmid" name="wmid" size="55" maxlength="255" title="Enter Wetlab wmid">
                                </div>
                                </div>
                                </div>
			
			  
			  <p><input type="submit" value="Remove" class="btn btn-lg btn-default"></p>
		  </form>
		  </div>""")
	
	dapr.ptail()
	
else:
	print("Location: login.py")
	print("Content-type: text/html\n")
