#!/usr/local/bin/python3
#  ^^^ this is bad practice, DON'T do as I did!
import header
import footer
from htmlify import *
import cgi
import cgitb
import os
import traceback
import http.cookies as Cookie
import hashlib
import pickle
from assets import *

print("Content-Type: text/html;charset=utf-8\n")
cgitb.enable()


try:
	with open(".config/autosave.bin", "rb") as dataFile:
		locations = pickle.load(dataFile)
except (FileNotFoundError, PermissionError):
	locations = []
except:
	locations = []
	print("error")
	print(traceback.format_exc())
if 'HTTP_COOKIE' in os.environ:
	c = Cookie.SimpleCookie()
	c.load(os.environ.get('HTTP_COOKIE'))  # i want cookies!
	try:
		cookieLoginData = c['password'].value  # retrieve the value of the cookie
		loggedIn = authenticate(cookieLoginData)
	except KeyError:  # no such value in the cookie jar
		loggedIn = False
else:
	loggedIn = False
localData = cgi.FieldStorage()

header.showHeader(loggedIn=loggedIn)


locationName = localData.getvalue("location")
missCounter = 0
for l in locations:
	if l.name == locationName:
		location = l
	else:
		missCounter += 1
if missCounter >= len(locations):
	dispHTML("h3", contents="Error")
	dispHTML("p", contents="Location '" + str(locationName) + "' not found.")
	itemFound = False
else:
	missCounter = 0
	itemName = localData.getvalue("item")
	itemFound = False
	for i in location.items:
		if i.name == itemName:
			item = i
			itemFound = True
		else:
			missCounter += 1
	if missCounter >= len(location.items):
		dispHTML("h3", contents="Error")
		dispHTML("p", contents="Item '" + str(itemName) + "' not found.")

if loggedIn and itemFound:
	dispHTML("h3", contents=item.name)
	startTag("form", method="post")
	print("<b>Name: </b> " + "<input type=\"text\" name=\"setName\"" + "value=\"" + str(item.name) + "\"")
	print("<b>Quantity: </b> " + "<input type=\"text\" name=\"setQuant\"" + "value=\"" + str(item.quantity) + "\"")
	startTag("select", name="setLoc")
	dispHTML("option", value="", disabled="disabled", selected="selected", contents="Location")
	for loc in locations:
		dispHTML("option", value=loc.name, contents=loc.name)
	endTag("select")
	startTag("select", name="setOwner")
	dispHTML("option", value="", disabled="disabled", selected="selected", contents="Owner")
	users = []  # TEMPORARY
	for user in users:
		dispHTML("option", value=user.name, contents=user.name)
	endTag("select")
	startTag("select", name="setCurrentUser")
	dispHTML("option", value="", disabled="disabled", selected="selected", contents="Current User")
	for user in users:
		dispHTML("option", value=user.name, contents=user.name)
	endTag("select")
	dispHTML("input", type="submit", value="Save changes")
	endTag("form")
elif not loggedIn:
	dispHTML("h3", contents="Please login")
	dispHTML("p", contents="This area of the site requires you to authenticate.")
	dispHTML("p", contents="With cookies enabled, please go to the Login page, login, and navigate back here.")
	dispHTML("p", contents="If you don't want to find this page again, please copy the link now. \
	Once you have logged in you can paste it into your browser's address bar and, through the power of cookies, \
	you will be logged in.")


footer.showFooter()

@@ -1700,69 +1700,85 @@
 %22)%0A%09
-print(%22%3Cb%3EName: %3C/b%3E %22 + %22%3C
+dispHTML(%22b%22, contents=%22Name:%22)%0A%09dispHTML(%22
 input
+%22,
  type=
-%5C
 %22text
-%5C
 %22
+,
  name=
-%5C
 %22setName
 %5C%22%22 
@@ -1777,108 +1777,118 @@
 Name
-%5C%22%22 + %22value=%5C%22%22 + str(item.name) + %22%5C%22%22)%0A%09print(%22%3Cb%3EQuantity: %3C/b%3E %22 + %22%3C
+%22, value=item.name)%0A%09dispHTML(%22br%22)%0A%09dispHTML(%22b%22, contents=%22Quantity:%22)%0A%09dispHTML(%22
 input
+%22,
  type=
-%5C
 %22text
-%5C
 %22
+,
  name=
-%5C
 %22set
@@ -1896,51 +1896,87 @@
 uant
-%5C%22%22 + %22value=%5C%22%22 + str(item.quantity) + %22%5C%22
+%22, value=str(item.quantity))%0A%09dispHTML(%22br%22)%0A%09dispHTML(%22b%22, contents=%22Location:
 %22)%0A%09
@@ -2094,18 +2094,21 @@
 nts=
-%22L
+l
 ocation
-%22
+.name
 )%0A%09f
@@ -2194,32 +2194,82 @@
 ndTag(%22select%22)%0A
+%09dispHTML(%22br%22)%0A%09dispHTML(%22b%22, contents=%22Owner:%22)%0A
 %09startTag(%22selec
@@ -2374,15 +2374,23 @@
 nts=
-%22Owner%22
+item.owner.name
 )%0A%09u
@@ -2508,16 +2508,73 @@
 elect%22)%0A
+%09dispHTML(%22br%22)%0A%09dispHTML(%22b%22, contents=%22Current user:%22)%0A
 %09startTa
@@ -2693,22 +2693,29 @@
 nts=
-%22C
+item.c
 urrent
- 
 User
-%22
+.name
 )%0A%09f
@@ -2800,32 +2800,48 @@
 ndTag(%22select%22)%0A
+%09dispHTML(%22br%22)%0A
 %09dispHTML(%22input

Make editing item look nicer