#!/usr/bin/python3
#=========================================================================================
# Create a shopping cart 
# Compute total price and print
#=========================================================================================
print("Content-Type: text/html \n")          #required http response header (w/ extra line)
 
def print_cart(cart):

    for item in cart:                                   #iterate thru each cart item
        print(item, '<br>')                             #print each item (as a dictionary)
    print('<br>')

    print('<table border=1>')
    cart_total = 0

    headers = cart[0].keys()                            #from the first item, get the keys -> headers
    print('<tr bgcolor=cyan>', end='')
    for header in headers:                              #for each header
        HEADER = header.upper()                         #make it uppercase
        print(F'<th> {HEADER} </th>', end='')           #print the key/header
    print(F'<th> TOTAL </td>', end='')                  #create one more column header called TOTAL        
    print('</tr>')
        
    for article in cart:                                        #loop through all items
        print('<tr>', end='')
        for (key, value) in article.items():                    #loop through each item key/value pair
            print(F'<td align=center> {value} </td>', end='')   #print the value
        item_total  = article['qty'] * article['price']         #compute item_total = qty * price
        cart_total += item_total                                #add it to cart_total
        print(F'<th> ${item_total:.2f} </th>', end='')          #print item_total (with 2 decimal)         
        print('</tr>')

    print(F'<tr bgcolor=cyan><th colspan=4> Cart Total <th> ${cart_total} </tr>', end='')  #print final cart_total                    
    print('</table>')

### Main Code ############################################################################
    
shop_cart = [ {'item':'Apples',  'description':'Macintosh', 'qty':3, 'price':1.59},
              {'item':'Oranges', 'description':'Navel',     'qty':2, 'price':1.35},
              {'item':'Pears',   'description':'Barlett',   'qty':4, 'price':1.79} ]

print('<h3>Before Adding Items</h3>')   
print_cart(shop_cart)
print('<br>')   

item = {'item':'Grapes', 'description':'Concord', 'qty':5, 'price':0.99}    #create another item
shop_cart.append(item)                                                      #add item to the cart
item = {'item':'Cherries', 'description':'Bing',  'qty':1, 'price':2.49}
shop_cart.append(item)                                              

print('<h3>After Adding 2 Items</h3>')   
print_cart(shop_cart)
print('<br>')




#=== 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
#=================================================================================