<?php 

include_once './include/common.inc.php';
if($action !='add') showmessage($LANG['illegal_action'],$referer);

if(!$_userid)
{
	showmessage($LANG['login_website'], $MODULE['member']['linkurl'].'login.php?forward='.urlencode($PHP_URL));
}

$head['title'] = $_username.$LANG['someone_cart'];
$head['keywords'] = $_username.$LANG['someone_cart'];
$head['description'] = $_username.$LANG['someone_cart'];


switch ($action)
{
	case 'add':
		$productid = intval($productid);
		$productid  or showmessage($LANG['illegal_operation']);

		$mr = $db->get_one("SELECT money From ".TABLE_MEMBER." WHERE username='".$_username."' limit 0,1");
		$query = "SELECT pdt_name,pdt_No,price,itembind,itemcolor From ".TABLE_PRODUCT." WHERE productid=".$productid." limit 0,1";
		$r = $db->get_one($query);
		if($mr['money'] < $r['price'])
		{
		   showmessage('您的余额不足以购买此商品，请及时充值！','/pay/index.php');
		}else{
		$scode = md5(date('Y-m-d',$PHP_TIME));
    $scode = base64_encode($scode.$_username);
    
		$check = "SELECT cart_id FROM ".TABLE_PRODUCT_CART." WHERE user_id=$_userid AND odr_id=0 limit 0,1" ;
		$rc = $db->get_one($check);
		if($rc)
		{
		  $query = "UPDATE ".TABLE_PRODUCT_CART." SET odr_id=0,user_id=$_userid,productid=$productid,pdt_No='".$r['pdt_No']."',pdt_name='".$r['pdt_name']."',pdt_number=1,itembind='".$r['itembind']."',itemcolor='".$r['itemcolor']."' WHERE cart_id='".$rc['cart_id']."'";
		  $db->query($query);
		}else{
		$query = "INSERT INTO ".TABLE_PRODUCT_CART."(odr_id,user_id,productid,pdt_No,pdt_name,pdt_number,itembind,itemcolor)"
				." VALUES(0,$_userid,$productid,'".$r['pdt_No']."','".$r['pdt_name']."',1,'".$r['itembind']."','".$r['itemcolor']."')";
		$db->query($query);
		}
		header('location:'.$MOD['linkurl'].'payment.php?frameid='.$scode);	
		}	
}

?>