<!-- #include file=../../inc/BBSsetup.asp -->
<!-- #include file=../../inc/Board_popfun.asp -->
<!--#include file="wxzfunction.asp"-->
<%
dim CurItems,ItemName,rsvisual
dim SucFlag,CurItem,CartBag,SendMsg,CurType,SalePrice

WPD_html
WPD_Getconfig
WPD_ScriptSrc(WPD_pluspath&"visual/wxzmain.js")
WPD_checkplususer
WPD_isshowtop=0
%>
<!--#include file="z_Visual_const.asp"-->
<%
if not WPD_founduser or WPD_membername=""  then
	sendmsg=sendmsg+"<br>"+"<li>您还没有<a href=login.asp>登录论坛</a>，不能使用个人形象设计功能。如果您还没有<a href=reg.asp>注册</a>，请先<a href=reg.asp>注册</a>！"
	WPD_founderr=true
end if
CurItem=WPD_checkstr(request("itemid"))
if isnull(curitem) then
	sendmsg=sendmsg+"<br>"+"<li>没有指定商品编号！"
	WPD_founderr=true
else
	if not isnumeric(curitem) then
		sendmsg=sendmsg+"<br>"+"<li>提交的数据有错误！"
		WPD_founderr=true
	end if
end if

if WPD_founderr then
	WPD_stats="购买商品"
	WPD_header "虚拟形象","visual","index.asp"
	call send_msg()
else
	set rsvisual=server.createobject("ADODB.Recordset")
	sql="select top 1 itemid from visual_myitems where username='"&WPD_membername&"' and itemid="&CurItem
	rsvisual.open sql,conn,1,1
	if not (rsvisual.eof or rsvisual.bof) then
		sendmsg=sendmsg+"<br>"+"<li>已经拥有该商品，无须再次购买！"
		WPD_founderr=true
	else
		if request("fromuser")<>"" then
			rsvisual.close
			sql="select m.username,m.saleprice,v.name,v.type from visual_myitems m inner join visual_items v on m.itemid=v.id where m.itemid="&CurItem&" and m.username='"&WPD_checkstr(trim(request("fromuser")))&"'"
			rsvisual.open sql,conn,1,1
			ItemName=rsvisual("name")
			CurType=rsvisual("type") mod 100
			if CurType>=5 then CurType=4
			saleprice=rsvisual("salePrice")
			if saleprice>WPD_mymoney then
				sendmsg=sendmsg+"<br>"+"<li>购买此商品需要现金 "&saleprice&" 元，您的现金("&WPD_mymoney&" 元)不足，无法购买！"
				WPD_founderr=true
			end if
		else
			rsvisual.close
			sql="select * from visual_items where id="&CurItem
			rsvisual.open sql,conn,1,1
			ItemName=rsvisual("name")
			if rsvisual("vip") and v_myvip<>1 then
				sendmsg=sendmsg+"<br>"+"<li>您不是VIP，不能购买VIP专用商品！"
				WPD_founderr=true
			end if
			if rsvisual("quantity")<=0 then
				sendmsg=sendmsg+"<br>"+"<li>该商品库存不足，您无法购买！"
				WPD_founderr=true
			end if
			if rsvisual("flag")=0 then
				sendmsg=sendmsg+"<br>"+"<li>保留商品，您无法购买！"
				WPD_founderr=true
			end if
			if rsvisual("flag")=1 and WPD_membername="" then
				sendmsg=sendmsg+"<br>"+"<li>只有会员才能购买此商品，您无法购买！"
				WPD_founderr=true
			end if
			if rsvisual("flag")=2 and not WPD_master and not WPD_boardmaster and not WPD_superboardmaster then
				sendmsg=sendmsg+"<br>"+"<li>只有版主以上人员才能购买此商品，您无法购买！"
				WPD_founderr=true
			end if
			if rsvisual("flag")=3 and not WPD_master and not WPD_superboardmaster then
				sendmsg=sendmsg+"<br>"+"<li>只有超级版主和管理员才能购买此商品，您无法购买！"
				WPD_founderr=true
			end if
			if rsvisual("flag")=4 and not WPD_master then
				sendmsg=sendmsg+"<br>"+"<li>只有管理员才能购买此商品，您无法购买！"
				WPD_founderr=true
			end if
		end if
		if WPD_founderr then
			WPD_stats="购买商品"
			WPD_header "虚拟形象","visual","index.asp"
			call send_msg()
		else	
			if request("fromuser")<>"" then
				conn.execute("update visual_myitems set type="&curtype&",price=saleprice,saleprice=0,fromuser=username,username='"&WPD_membername&"' where itemid="&curitem&" and type=5 and username='"&WPD_checkstr(trim(request("fromuser")))&"'")
				conn.execute("update visual_events set username='"&WPD_membername&"' where itemid="&curitem&" and type=2 and fromuser='"&WPD_checkstr(trim(request("fromuser")))&"' and (username='' or isnull(username))")
				conn.execute("insert into visual_events (ItemID,UserName,FromUser,DateAndTime,Price,Type) values ("&CurItem&",'"&WPD_membername&"','"&WPD_checkstr(trim(request("fromuser")))&"','"&year(now())&"-"&month(now())&"-"&day(now())&"',"&saleprice&",0)")
				call mtmoney(saleprice,WPD_membername,bsmoney)
				call ktmoney(saleprice,WPD_checkstr(trim(request("fromuser"))),bsmoney)
				set rs=server.createobject("adodb.recordset")
				sql="select * from LeadBBS_InfoBox"
				rs.open sql,con,1,3      
				rs.addnew      
				rs("fromuser")=DEF_SiteNameString
				rs("touser")=trim(request("fromuser"))
				rs("title")="您出售的商品有人购买了！"
				rs("content")="您在二手市场出售的商品《"&itemName&"》已经被"&WPD_membername&"购买了！"
				rs("readflag")=0 
				rs("sendtime")=GetTimeValue(now)
				rs.update
				rs.close
				set rs=nothing
				Con.Execute("Update LeadBBS_User Set MessageFlag=1 where username='" & trim(request("fromuser"))&"'")
				sendmsg="<li>已经成功地购买了二手商品 <font color=blue>"&ItemName&"</font>！"
				WPD_stats="购买商品"
				WPD_header "虚拟形象","visual","index.asp"
				response.write "<script>"
				response.write "window.opener.document.execCommand('Refresh');"
				response.write "</script>"
				call send_msg()
			else
				CartBag=request.cookies("WxzEtBuy_"&WPD_userid)
				if isnull(CartBag) then CartBag=""
				if instr(1,"|"&CartBag&"|","|"&CurItem&"|")<=0 then
					if ubound(split(CartBag,"|"))<CartLength-1 then
						if CartBag<>"" then CartBag=CartBag&"|"
						CartBag=CartBag&CurItem
						response.cookies("WxzEtBuy_"&WPD_userid)=CartBag
						sendmsg="<li>已将商品 <font color=blue>"&ItemName&"</font> 放到您的购物袋中！"
					else
						sendmsg=sendmsg+"<br>"+"<li>您的购物袋已满，无法将此商品放入！"
						WPD_founderr=true
					end if
				else
					sendmsg=sendmsg+"<br>"+"<li>购物袋中已有此商品，无须再次购买！"
					WPD_founderr=true
				end if
				WPD_stats="购买商品"
				WPD_header "虚拟形象","visual","index.asp"
				call send_msg()
			end if
		end if
	end if
	rsvisual.close
	set rsvisual=nothing
end if
WPD_footer

sub send_msg()%>
<table width="97%" border=0 cellspacing=1 cellpadding=5 align=center bgcolor=<%=DEF_BBS_DarkColor%> class=TBone>
<tr bgcolor=<%=DEF_BBS_LightDarkColor%> align=center>
			<td class=TBHead width="100%" id=TableTitleLink>购买形象商品信息</td>
		</tr>
		<tr bgcolor=<%=DEF_BBS_LightestColor%> class=TBBG9>
			<td width="100%" ><b>操作结果：</b><br><%
				if WPD_founderr then
					response.write "<font color="&WPD_spcolor&">"
					response.write sendmsg
					response.write "</font>"
				else
					response.write sendmsg
				end if
			%></td>
		</tr>
		<tr bgcolor=<%=DEF_BBS_LightestColor%> class=TBBG9 align=center>
			<td width="100%" ><a href="javascript:window.close()">[关闭窗口]</a></td>
		</tr>
	</table>
<%end sub%>