# item_cart

if $ARGS[0] = 'shoppping_aisle':
	gs 'item_stock_db', 'cleanup'
    $curr_aisle = $ARGS[1]
	gs 'item_stock_db', $ARGS[1]
	'<center><table border=5 cellspacing=0 cellpadding=5 bgcolor='+$bcolor+'><TR><TH colspan=9><font size="+2"><<$shop_title>></font></TH></TR></table></center>'
	gs 'item_cart', 'header'
	*p '<center><table border=5 cellspacing=0 cellpadding=5 bgcolor='+$bcolor+'><TR><<$Headerstring>></TR><TR>'
	i = 1
	:loopshopping_cart
		!! if item is uninitialized (has blank name) - skip it
		if $item_curr_aisle['<<i>>'] = '': i += 1 & jump 'loopshopping_cart'
		$temp_bcolor = func('themes', 'alt_color', $temp_bcolor)
		$item_line = '<TR bgcolor='+$temp_bcolor+'>'
!!		uncomment if you want items with events to be marked with a * peepoShrug
!!		if $event_curr_aisle['<<i>>'] ! '':
!!			$item_line += '<TD align="left"><<$item_curr_aisle[''<<i>>'']>> *</TD>'
!!		else
			$item_line += '<TD align="left"><<$item_curr_aisle[''<<i>>'']>></TD>'
!!		end
		dynamic '$cart_tmp = $var_curr_aisle[''<<i>>'']'
		$item_line += '<TD><<mc_inventory[''<<$cart_tmp>>'']>></TD>'
		$item_line += '<TD><<quantity_curr_aisle[''<<i>>'']>></TD>'
		$item_line += '<TD><<cost_curr_aisle[''<<i>>'']>></TD>'
		if cart_curr_quantity['<<i>>'] < 10:
			$item_line += '<TD></TD>'
		else
			$item_line += '<TD><a href="exec:gt ''item_cart'', ''remove10'', <<i>>">remove</a></TD>'
		end
		if cart_curr_quantity['<<i>>'] < 1:
			$item_line += '<TD></TD>'
		else
			$item_line += '<TD><a href="exec:gt ''item_cart'', ''remove'', <<i>>">remove</a></TD>'
		end
		$item_line += '<TD><<cart_curr_quantity[''<<i>>'']>></TD>'
		if max_curr_aisle['<<i>>'] > cart_curr_quantity['<<i>>']+mc_inventory['<<$cart_tmp>>'] or max_curr_aisle['<<i>>'] = 0:
			$item_line += '<TD><a href="exec:gt ''item_cart'', ''add'', <<i>>">add</a></TD>'
		else
			$item_line += '<TD>Max</TD>'
		end
		if max_curr_aisle['<<i>>'] > cart_curr_quantity['<<i>>'] + 10 + mc_inventory['<<$cart_tmp>>'] or max_curr_aisle['<<i>>'] = 0:
			$item_line += '<TD><a href="exec:gt ''item_cart'', ''add10'', <<i>>">add</a></TD>'
		else
			$item_line += '<TD></TD>'
		end
		$item_line += '</TR>'
		*p $item_line
		i += 1
	if i <= item_idx:jump 'loopshopping_cart'
	'</table></center>'
	killvar '$temp_bcolor'
	gs 'item_cart', 'cart_total'
end

if $ARGS[0] = 'header':
	$Headerstring = '<TH>Item</TH><TH>You own</TH><TH>Units/pack</TH><TH>Cost in <b>₽</b></TH><TH>Remove 10</TH><TH>Remove 1 pack</TH><TH>In cart</TH><TH>Add one pack</TH><TH>Add 10</TH>'
end

if $ARGS[0] = 'remove':
	cart_tally -= cost_curr_aisle['<<ARGS[1]>>']
	cart_curr_quantity['<<ARGS[1]>>'] -= 1
	gt $loc_s, $args_s
end

if $ARGS[0] = 'remove10':
	cart_tally -= cost_curr_aisle['<<ARGS[1]>>']*10
	cart_curr_quantity['<<ARGS[1]>>'] -= 10
	gt $loc_s, $args_s
end

if $ARGS[0] = 'add':
	cart_tally += cost_curr_aisle['<<ARGS[1]>>']
	cart_curr_quantity['<<ARGS[1]>>'] += 1
	if $event_curr_aisle['<<ARGS[1]>>'] ! '': dynamic "<<$event_curr_aisle['<<ARGS[1]>>']>>"
	gt $loc_s, $args_s
end

if $ARGS[0] = 'add10':
	cart_tally += cost_curr_aisle['<<ARGS[1]>>']*10
	cart_curr_quantity['<<ARGS[1]>>'] += 10
	if $event_curr_aisle['<<ARGS[1]>>'] ! '': dynamic "<<$event_curr_aisle['<<ARGS[1]>>']>>"
	gt $loc_s, $args_s
end

if $ARGS[0] = 'simple_add':
	cart_tally += cost_curr_aisle['<<ARGS[1]>>']*max(1,ARGS[2])
	cart_curr_quantity['<<ARGS[1]>>'] += 1*max(1,ARGS[2])
end

if $ARGS[0] = 'cart_total':
	'<center><table border=5 cellspacing=0 cellpadding=5 bgcolor='+$bcolor+'><TD>&nbsp;<b>Total cost of items in cart: <<cart_tally>>₽</b>&nbsp;</TD></table></center>'

	*p '<center><table border=5 cellspacing=0 cellpadding=5 bgcolor='+$bcolor+'><TR>'
	$item_line[2] = ''
	if cart_tally > 0:
		if cart_tally > karta + bankDebtLimit and cart_tally > money:
			$item_line[2] = '<TD>&nbsp;You cannot afford this&nbsp;</TD>'
		else
			if cart_tally <= money:
				$item_line[2] = '<TD>&nbsp;<a href="exec:gt ''item_cart'', ''cash'', <<i>>">Buy (cash)</a>&nbsp;</TD>'
			else
				$item_line[2] = '<TD>&nbsp;Not enough cash&nbsp;</TD>'
			end
			if cart_tally <= karta + bankDebtLimit:
				$item_line[2] += '<TD>&nbsp;<a href="exec:gt ''item_cart'', ''card'', <<i>>">Buy (card)</a>&nbsp;</TD>'
			else
				$item_line[2] += '<TD>&nbsp;Not enough on card&nbsp;</TD>'
			end
		end
	end
	$item_line[2] += '<TD>&nbsp;<a href="exec:gt ''item_cart'', ''cancel''">cancel</a>&nbsp;</TD>'
	$item_line[2] += '</TR></table></center>'
	*p $item_line[2]
end
!!{
if $ARGS[0] = 'shoppping_cart':
    cart_idx = 0
	cart_shoppingCost = 0
    :cart_checkout
        if tmp_$var_$curr_aisle['cart_idx']['cart_idx'] > 0:
            basket total += tmp_$curr_aisle['cart_idx'] * cost_$curr_aisle['cart_idx']                           
        end
   if idx < arrsize($item_$curr_aisle): idx += 1 & jump 'cart_checkout'
	'<center>Total cost of shopping:' + cart_tally + '<b>₽</b></center>'
end
}

if $ARGS[0] = 'cash':
	i = 1
	:loopshopping_cash
		$cart_tmp1 = $var_curr_aisle['<<i>>']
		dynamic 'cart_quantity = quantity_curr_aisle[''<<i>>''] * cart_curr_quantity[''<<i>>'']'
		dynamic 'mc_inventory[''<<$cart_tmp1>>''] += cart_quantity'
		i += 1
	if i <= item_idx:jump 'loopshopping_cash'
	money -= cart_tally
	gs 'item_cart', 'shopping_var_clear'
	gt $loc_s, $args_s
end

if $ARGS[0] = 'card':
	i = 1
	:loopshopping_card
		$cart_tmp1 = $var_curr_aisle['<<i>>']
		dynamic 'cart_quantity = quantity_curr_aisle[''<<i>>''] * cart_curr_quantity[''<<i>>'']'
		dynamic 'mc_inventory[''<<$cart_tmp1>>''] += cart_quantity'
		i += 1
	if i <= item_idx:jump 'loopshopping_card'
	karta -= cart_tally
	gs 'item_cart', 'shopping_var_clear'
	gt $loc_s, $args_s
end

if $ARGS[0] = 'cancel':
	gs 'item_cart', 'shopping_var_clear'
	gt $loc_s, $args_s
end

if $ARGS[0] = 'shopping_var_clear':
    !! clean up all transient variables/arrays.
    killvar 'cart_tally'
    killvar 'cart_curr_quantity'
	killvar 'cart_quantity'
	killvar '$cart_tmp'
	killvar '$cart_tmp1'
	gs 'item_stock_db', 'cleanup'
end


--- item_cart ---------------------------------

