#!/usr/bin/python3
#####################################################################################
# Create and use a shopping cart (sample code)
# This shopping cart does not use cookies, session variables, nor database
# A cart is created using appName_IPaddr
#####################################################################################
import cgi
import cgitb
import shopcartFunc                     #import custom shopCart functions

cgitb.enable()
 
print('Content-type: text/html \n')

appName = 'XYZ'                         #enter a unique application name or leave it XYZ

#-- Display Product list ------------------------------------------------------------------------
print("""
    <html>
    <head>
    <title>Shopping Ecample</title>
    <style>
        #table1 td       {border:2px solid black; width:120; background-color:lightgray; cursor:pointer} 
        #table1 td:hover {border-color:FF0000;}
        #table1 a        {color:black; text-decoration:none} 
        #table1 img      {height:120; width:120} 
        button           {width:110px; color:white; background-color:green; height:30px}
    </style> 
    </head>
    <body bgcolor=lightyellow>
    <h2>Shop From the List of Products Below</h2>
    <br>
    <fieldset style='width:650px'>
""")

#-- Below data should come from a database table ------------------------------------------

print("""
    <table id=table1 >
    <tr>
    <td><a href=shopcart.py?item=Apples&price=1.49>  <img src=img/Apples.jpg> <br>Apples  $1.49</a></td> 
    <td><a href=shopcart.py?item=Oranges&price=1.25> <img src=img/Oranges.jpg><br>Oranges $1.25</a></td> 
    <td><a href=shopcart.py?item=Peaches&price=1.99> <img src=img/Peaches.jpg><br>Peaches $1.99</a></td> 
    <td><a href=shopcart.py?item=Bananas&price=0.99> <img src=img/Bananas.jpg><br>Bananas $0.99</a></td> 
    <td><a href=shopcart.py?item=Pears&price=1.79>   <img src=img/Pears.jpg>  <br>Pears   $1.79</a></td> 
    <tr>
    <td><a href=shopcart.py?item=Potatoes&price=0.99> <img src=img/Potatoes.jpg> <br>Potatoes  $0.99</a></td> 
    <td><a href=shopcart.py?item=Tomatos&price=1.49>  <img src=img/Tomatos.jpg>  <br>Tomatos   $1.49</a></td> 
    <td><a href=shopcart.py?item=Spinach&price=2.29>  <img src=img/Spinach.jpg>  <br>Spinach   $2.29</a></td> 
    <td><a href=shopcart.py?item=Carrots&price=1.19>  <img src=img/Carrots.jpg>  <br>Carrots   $1.19</a></td> 
    <td><a href=shopcart.py?item=Cucumbers&price=1.29><img src=img/Cucumbers.jpg><br>Cucumbers $1.29</a></td> 
    <tr>
    <td><a href=shopcart.py?item=Milk&price=3.49>   <img src=img/Milk.jpg>   <br>Milk    $3.49</a></td> 
    <td><a href=shopcart.py?item=Yogurt&price=1.49> <img src=img/Yogurt.jpg> <br>Yogurt  $1.49</a></td> 
    <td><a href=shopcart.py?item=Cheese&price=5.99> <img src=img/Cheese.jpg> <br>Cheese  $5.99</a></td> 
    <td><a href=shopcart.py?item=Juice&price=2.99>  <img src=img/Juice.jpg>  <br>Juice   $2.99</a></td> 
    <td><a href=shopcart.py?item=Cookies&price=6.99><img src=img/Cookies.jpg><br>Cookies $6.99</a></td> 
    </table>
    </fieldset>
    <button onclick="location.href='shopcheckout.py'"> Checkout </button>       <!-- redirect to another page -->
""")

#-- Display Shopping Cart ---------------------------------------------------------------------                                                     #if option is view 

shopcartFunc.accessCart()                                           #access shopping cart (if any)
cartId    = shopcartFunc.getCartId()                                #get the cart id
cartItems = shopcartFunc.getCartItems()                             #get all items as a dictionary

if cartItems:    
    print(F"""<h4><i>Cart Id:</i> {cartId} </h4> 
          <table border=2 bgcolor=tan>
          <tr bgcolor=cccccc><th><i><nobr>Item<th width=500><i>Price & Quantity<th><i>Delete</th>
    """)        
    for name in sorted(cartItems):                                  #loop thru the cart items
        print("<tr><td><b>",name,"<td width=500>",cartItems[name])  #print the item name & values
        print(F"<td><a href=shopcart.py?opt=del&item={name}>")      #anchor to current script
        print("<img src=img/delete.png height=20 width=35></a>")    #add delete button

    print("</table>")
else:    
    print("<h4>Your Shopping Cart is Empty</h4>")
    
    




#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================